From 73b3ff149190db1bdf331d3aab2f91200349e4a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Fri, 22 Nov 2019 20:59:58 +0100 Subject: doc: simplify function to compute SHA256 checksum, move function outside class AutogenDoc (documentation generator) --- doc/docgen.py | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) (limited to 'doc') 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) -- cgit v1.2.3