Mercurial > p > roundup > code
changeset 5427:88cf5614e0f4
Python 3 preparation: use byte strings and binary I/O in roundup/install_util.py.
This code is hashing files it copies and inserting comments with the
hash value. The hash interfaces require bytes objects in Python 3 and
it seems reasonable to use such objects throughout this file.
| author | Joseph Myers <jsm@polyomino.org.uk> |
|---|---|
| date | Wed, 25 Jul 2018 09:54:08 +0000 |
| parents | 5dc27422f3ec |
| children | 1f1899658115 |
| files | roundup/install_util.py |
| diffstat | 1 files changed, 17 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/roundup/install_util.py Wed Jul 25 09:53:21 2018 +0000 +++ b/roundup/install_util.py Wed Jul 25 09:54:08 2018 +0000 @@ -23,6 +23,8 @@ import os, shutil from hashlib import sha1 +from roundup.anypy.strings import s2b + sgml_file_types = [".xml", ".ent", ".html"] hash_file_types = [".py", ".sh", ".conf", ".cgi"] slast_file_types = [".css"] @@ -31,25 +33,25 @@ def extractFingerprint(lines): # get fingerprint from last line - if lines[-1].startswith("#SHA: "): + if lines[-1].startswith(b"#SHA: "): # handle .py/.sh comment return lines[-1][6:].strip() - elif lines[-1].startswith("<!-- SHA: "): + elif lines[-1].startswith(b"<!-- SHA: "): # handle xml/html files fingerprint = lines[-1][10:] - fingerprint = fingerprint.replace('-->', '') + fingerprint = fingerprint.replace(b'-->', b'') return fingerprint.strip() - elif lines[-1].startswith("/* SHA: "): + elif lines[-1].startswith(b"/* SHA: "): # handle css files fingerprint = lines[-1][8:] - fingerprint = fingerprint.replace('*/', '') + fingerprint = fingerprint.replace(b'*/', b'') return fingerprint.strip() return None def checkDigest(filename): """Read file, check for valid fingerprint, return TRUE if ok""" # open and read file - inp = open(filename, "r") + inp = open(filename, "rb") lines = inp.readlines() inp.close() @@ -64,7 +66,7 @@ digest.update(line) # compare current to stored digest - return fingerprint == digest.hexdigest() + return fingerprint == s2b(digest.hexdigest()) class DigestFile: @@ -75,7 +77,7 @@ def __init__(self, filename): self.filename = filename self.digest = sha1() - self.file = open(self.filename, "w") + self.file = open(self.filename, "wb") def write(self, data): lines = data.splitlines() @@ -83,7 +85,7 @@ # template, then we will want to re-calculate the SHA fingerprint = extractFingerprint(lines) if fingerprint is not None: - data = '\n'.join(lines[:-1]) + '\n' + data = b'\n'.join(lines[:-1]) + b'\n' self.file.write(data) self.digest.update(data) @@ -91,11 +93,11 @@ file, ext = os.path.splitext(self.filename) if ext in sgml_file_types: - self.file.write("<!-- SHA: %s -->\n" % (self.digest.hexdigest(),)) + self.file.write(s2b("<!-- SHA: %s -->\n" % (self.digest.hexdigest(),))) elif ext in hash_file_types: - self.file.write("#SHA: %s\n" % (self.digest.hexdigest(),)) + self.file.write(s2b("#SHA: %s\n" % (self.digest.hexdigest(),))) elif ext in slast_file_types: - self.file.write("/* SHA: %s */\n" % (self.digest.hexdigest(),)) + self.file.write(s2b("/* SHA: %s */\n" % (self.digest.hexdigest(),))) self.file.close() @@ -118,7 +120,7 @@ fsrc = None fdst = None try: - fsrc = open(src, 'r') + fsrc = open(src, 'rb') fdst = DigestFile(dst) shutil.copyfileobj(fsrc, fdst) finally: @@ -131,7 +133,7 @@ def test(): import sys - testdata = open(sys.argv[0], 'r').read() + testdata = open(sys.argv[0], 'rb').read() for ext in digested_file_types: testfile = "__digest_test" + ext @@ -142,7 +144,7 @@ assert checkDigest(testfile), "digest ok w/o modification" - mod = open(testfile, 'r+') + mod = open(testfile, 'r+b') mod.seek(0) mod.write('# changed!') mod.close()
