Skip to content

Commit 7ffeecb

Browse files
committed
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.
1 parent c6faa0b commit 7ffeecb

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

roundup/install_util.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import os, shutil
2424
from hashlib import sha1
2525

26+
from roundup.anypy.strings import s2b
27+
2628
sgml_file_types = [".xml", ".ent", ".html"]
2729
hash_file_types = [".py", ".sh", ".conf", ".cgi"]
2830
slast_file_types = [".css"]
@@ -31,25 +33,25 @@
3133

3234
def extractFingerprint(lines):
3335
# get fingerprint from last line
34-
if lines[-1].startswith("#SHA: "):
36+
if lines[-1].startswith(b"#SHA: "):
3537
# handle .py/.sh comment
3638
return lines[-1][6:].strip()
37-
elif lines[-1].startswith("<!-- SHA: "):
39+
elif lines[-1].startswith(b"<!-- SHA: "):
3840
# handle xml/html files
3941
fingerprint = lines[-1][10:]
40-
fingerprint = fingerprint.replace('-->', '')
42+
fingerprint = fingerprint.replace(b'-->', b'')
4143
return fingerprint.strip()
42-
elif lines[-1].startswith("/* SHA: "):
44+
elif lines[-1].startswith(b"/* SHA: "):
4345
# handle css files
4446
fingerprint = lines[-1][8:]
45-
fingerprint = fingerprint.replace('*/', '')
47+
fingerprint = fingerprint.replace(b'*/', b'')
4648
return fingerprint.strip()
4749
return None
4850

4951
def checkDigest(filename):
5052
"""Read file, check for valid fingerprint, return TRUE if ok"""
5153
# open and read file
52-
inp = open(filename, "r")
54+
inp = open(filename, "rb")
5355
lines = inp.readlines()
5456
inp.close()
5557

@@ -64,7 +66,7 @@ def checkDigest(filename):
6466
digest.update(line)
6567

6668
# compare current to stored digest
67-
return fingerprint == digest.hexdigest()
69+
return fingerprint == s2b(digest.hexdigest())
6870

6971

7072
class DigestFile:
@@ -75,27 +77,27 @@ class DigestFile:
7577
def __init__(self, filename):
7678
self.filename = filename
7779
self.digest = sha1()
78-
self.file = open(self.filename, "w")
80+
self.file = open(self.filename, "wb")
7981

8082
def write(self, data):
8183
lines = data.splitlines()
8284
# if the file is coming from an installed tracker being used as a
8385
# template, then we will want to re-calculate the SHA
8486
fingerprint = extractFingerprint(lines)
8587
if fingerprint is not None:
86-
data = '\n'.join(lines[:-1]) + '\n'
88+
data = b'\n'.join(lines[:-1]) + b'\n'
8789
self.file.write(data)
8890
self.digest.update(data)
8991

9092
def close(self):
9193
file, ext = os.path.splitext(self.filename)
9294

9395
if ext in sgml_file_types:
94-
self.file.write("<!-- SHA: %s -->\n" % (self.digest.hexdigest(),))
96+
self.file.write(s2b("<!-- SHA: %s -->\n" % (self.digest.hexdigest(),)))
9597
elif ext in hash_file_types:
96-
self.file.write("#SHA: %s\n" % (self.digest.hexdigest(),))
98+
self.file.write(s2b("#SHA: %s\n" % (self.digest.hexdigest(),)))
9799
elif ext in slast_file_types:
98-
self.file.write("/* SHA: %s */\n" % (self.digest.hexdigest(),))
100+
self.file.write(s2b("/* SHA: %s */\n" % (self.digest.hexdigest(),)))
99101

100102
self.file.close()
101103

@@ -118,7 +120,7 @@ def copyDigestedFile(src, dst, copystat=1):
118120
fsrc = None
119121
fdst = None
120122
try:
121-
fsrc = open(src, 'r')
123+
fsrc = open(src, 'rb')
122124
fdst = DigestFile(dst)
123125
shutil.copyfileobj(fsrc, fdst)
124126
finally:
@@ -131,7 +133,7 @@ def copyDigestedFile(src, dst, copystat=1):
131133
def test():
132134
import sys
133135

134-
testdata = open(sys.argv[0], 'r').read()
136+
testdata = open(sys.argv[0], 'rb').read()
135137

136138
for ext in digested_file_types:
137139
testfile = "__digest_test" + ext
@@ -142,7 +144,7 @@ def test():
142144

143145
assert checkDigest(testfile), "digest ok w/o modification"
144146

145-
mod = open(testfile, 'r+')
147+
mod = open(testfile, 'r+b')
146148
mod.seek(0)
147149
mod.write('# changed!')
148150
mod.close()

0 commit comments

Comments
 (0)