doc: simplify function to compute SHA256 checksum, move function outside class AutogenDoc (documentation generator)
This commit is contained in:
parent
178b3eb8a6
commit
73b3ff1491
@ -57,7 +57,6 @@ try:
|
|||||||
import hashlib
|
import hashlib
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
except ImportError as message:
|
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):
|
class AutogenDoc(object):
|
||||||
"""A class to write auto-generated doc files."""
|
"""A class to write auto-generated doc files."""
|
||||||
|
|
||||||
@ -158,29 +167,14 @@ class AutogenDoc(object):
|
|||||||
"""Write a line in auto-generated doc file."""
|
"""Write a line in auto-generated doc file."""
|
||||||
self._file.write(string)
|
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):
|
def update(self, obj_name, num_files, num_files_updated):
|
||||||
"""Update doc file if needed (if content has changed)."""
|
"""Update doc file if needed (if content has changed)."""
|
||||||
# close temp file
|
# close temp file
|
||||||
self._file.close()
|
self._file.close()
|
||||||
shaold = AutogenDoc.sha256_file(self.filename, 'old')
|
sha_old = sha256_file(self.filename, 'old')
|
||||||
shanew = AutogenDoc.sha256_file(self.filename_tmp, 'new')
|
sha_new = sha256_file(self.filename_tmp, 'new')
|
||||||
# compare checksums
|
# compare checksums
|
||||||
if shaold != shanew:
|
if sha_old != sha_new:
|
||||||
# update doc file
|
# update doc file
|
||||||
if os.path.exists(self.filename):
|
if os.path.exists(self.filename):
|
||||||
os.unlink(self.filename)
|
os.unlink(self.filename)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user