From 753774c5e91d35ad6f1b20d6a0c5fa33a054c5eb Mon Sep 17 00:00:00 2001 From: "Martin A. Brown" Date: Fri, 1 Apr 2016 22:19:25 -0700 Subject: [PATCH] compute and generate an MD5SUMS file for each source document --- tldp/doctypes/common.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/tldp/doctypes/common.py b/tldp/doctypes/common.py index 73d46e2..bb6fbb1 100644 --- a/tldp/doctypes/common.py +++ b/tldp/doctypes/common.py @@ -7,6 +7,7 @@ from __future__ import unicode_literals import os import sys import stat +import time import errno import codecs import shutil @@ -140,11 +141,39 @@ class BaseDoctype(object): s = ''' # - - - - - {source.stem} - - - - - - - cd -- "{output.dirname}"''' +cd -- "{output.dirname}"''' return self.shellscript(s, **kwargs) os.chdir(self.output.dirname) return True + def generate_md5sums(self, **kwargs): + logger.debug("%s generating MD5SUMS in %s.", + self.output.stem, self.output.dirname) + md5file = os.path.join(self.output.dirname, 'MD5SUMS') + fileset = sorted(self.source.md5sums.items()) + if self.config.script: + l = list() + for fname, hashval in fileset: + l.append('# {} {}'.format(hashval, fname)) + md5s = '\n'.join(l) + s = '''# -- calculating MD5SUMS file +# +# md5sum > {} -- {} +# +# -- These are the values for sources at timestamp {} +# +{} +#''' + s = s.format(md5file, + ' '.join(self.source.statinfo.keys()), + time.strftime('%F-%T', time.gmtime()), + md5s) + return self.shellscript(s, **kwargs) + with codecs.open(md5file, 'w', encoding='utf-8') as f: + for fname, hashval in fileset: + print(hashval + ' ' + fname, file=f) + return True + def copy_static_resources(self, **kwargs): logger.debug("%s copy resources %s.", self.output.stem, self.output.dirname) @@ -233,6 +262,7 @@ class BaseDoctype(object): 'clear_output', 'mkdir_output', 'chdir_output', + 'generate_md5sums', 'copy_static_resources', ]