summaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2019-11-22 20:59:58 +0100
committerSébastien Helleu <flashcode@flashtux.org>2019-11-22 20:59:58 +0100
commit73b3ff149190db1bdf331d3aab2f91200349e4a9 (patch)
tree2951ec5b7a36524a758de2299acc2125f15602db /doc
parent178b3eb8a6c89c06e9a775c58e0bb6784987eb28 (diff)
downloadweechat-73b3ff149190db1bdf331d3aab2f91200349e4a9.zip
doc: simplify function to compute SHA256 checksum, move function outside class AutogenDoc (documentation generator)
Diffstat (limited to 'doc')
-rw-r--r--doc/docgen.py32
1 files changed, 13 insertions, 19 deletions
diff --git a/doc/docgen.py b/doc/docgen.py
index 544aafe53..1ec6c8090 100644
--- a/doc/docgen.py
+++ b/doc/docgen.py
@@ -57,7 +57,6 @@ try:
import hashlib
import os
import re
- import sys
from collections import defaultdict
from operator import itemgetter
except ImportError as message:
@@ -141,6 +140,16 @@ IGNORE_COMPLETIONS_ITEMS = (
)
+def sha256_file(filename, default=None):
+ """Return SHA256 checksum of a file."""
+ try:
+ with open(filename, 'rb') as _file:
+ checksum = hashlib.sha256(_file.read()).hexdigest()
+ except IOError:
+ checksum = default
+ return checksum
+
+
class AutogenDoc(object):
"""A class to write auto-generated doc files."""
@@ -158,29 +167,14 @@ class AutogenDoc(object):
"""Write a line in auto-generated doc file."""
self._file.write(string)
- @staticmethod
- def sha256_file(filename, default):
- """
- Return SHA256 checksum of a file, "default" if file is not found.
- """
- try:
- with open(filename, 'r') as _file:
- content = _file.read()
- if sys.version_info >= (3, ):
- content = content.encode('utf-8')
- checksum = hashlib.sha256(content).hexdigest()
- except IOError:
- checksum = default
- return checksum
-
def update(self, obj_name, num_files, num_files_updated):
"""Update doc file if needed (if content has changed)."""
# close temp file
self._file.close()
- shaold = AutogenDoc.sha256_file(self.filename, 'old')
- shanew = AutogenDoc.sha256_file(self.filename_tmp, 'new')
+ sha_old = sha256_file(self.filename, 'old')
+ sha_new = sha256_file(self.filename_tmp, 'new')
# compare checksums
- if shaold != shanew:
+ if sha_old != sha_new:
# update doc file
if os.path.exists(self.filename):
os.unlink(self.filename)