Skip to content

Commit 771d890

Browse files
author
Jürgen Hermann
committed
Copy function, and proper handling of unknown file types
1 parent b5c552f commit 771d890

File tree

1 file changed

+55
-16
lines changed

1 file changed

+55
-16
lines changed

roundup/install_util.py

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: install_util.py,v 1.3 2001-11-12 22:38:48 richard Exp $
18+
# $Id: install_util.py,v 1.4 2001-11-12 23:14:40 jhermann Exp $
1919

20-
import os, sha
20+
import os, sha, shutil
21+
22+
sgml_file_types = [".xml", ".ent", ".html", ".filter", ".index", ".item"]
23+
hash_file_types = [".py", ".sh", ".conf", ".cgi", '']
24+
slast_file_types = [".css"]
25+
26+
digested_file_types = sgml_file_types + hash_file_types + slast_file_types
2127

2228

2329
def checkDigest(filename):
@@ -72,43 +78,76 @@ def close(self):
7278
file, ext = os.path.splitext(self.filename)
7379

7480
# ".filter", ".index", ".item" are roundup-specific
75-
if ext in [".xml", ".ent", ".html", ".filter", ".index", ".item"]:
81+
if ext in sgml_file_types:
7682
self.file.write("<!-- SHA: %s -->\n" % (self.digest.hexdigest(),))
77-
elif ext in [".py", ".sh", ".conf", ".cgi", '']:
83+
elif ext in hash_file_types:
7884
self.file.write("#SHA: %s\n" % (self.digest.hexdigest(),))
79-
elif ext in [".css"]:
85+
elif ext in slast_file_types:
8086
self.file.write("/* SHA: %s */\n" % (self.digest.hexdigest(),))
8187

8288
self.file.close()
8389

8490

91+
def copyDigestedFile(src, dst, copystat=1):
92+
""" Copy data from `src` to `dst`, adding a fingerprint to `dst`.
93+
If `copystat` is true, the file status is copied, too
94+
(like shutil.copy2).
95+
"""
96+
if os.path.isdir(dst):
97+
dst = os.path.join(dst, os.path.basename(src))
98+
99+
dummy, ext = os.path.splitext(src)
100+
if ext not in digested_file_types:
101+
if copystat:
102+
return shutil.copy2(srcname, dstname)
103+
else:
104+
return shutil.copyfile(srcname, dstname)
105+
106+
fsrc = None
107+
fdst = None
108+
try:
109+
fsrc = open(src, 'r')
110+
fdst = DigestFile(dst)
111+
shutil.copyfileobj(fsrc, fdst)
112+
finally:
113+
if fdst: fdst.close()
114+
if fsrc: fsrc.close()
115+
116+
if copystat: shutil.copystat(src, dst)
117+
118+
85119
def test():
86120
import sys
87121

88122
testdata = open(sys.argv[0], 'r').read()
89-
testfile = "digest_test.py"
90123

91-
out = DigestFile(testfile)
92-
out.write(testdata)
93-
out.close()
124+
for ext in digested_file_types:
125+
testfile = "__digest_test" + ext
94126

95-
assert checkDigest(testfile), "digest ok w/o modification"
127+
out = DigestFile(testfile)
128+
out.write(testdata)
129+
out.close()
96130

97-
mod = open(testfile, 'r+')
98-
mod.seek(0)
99-
mod.write('# changed!')
100-
mod.close()
131+
assert checkDigest(testfile), "digest ok w/o modification"
101132

102-
assert not checkDigest(testfile), "digest fails after modification"
133+
mod = open(testfile, 'r+')
134+
mod.seek(0)
135+
mod.write('# changed!')
136+
mod.close()
103137

104-
os.remove(testfile)
138+
assert not checkDigest(testfile), "digest fails after modification"
139+
140+
os.remove(testfile)
105141

106142

107143
if __name__ == '__main__':
108144
test()
109145

110146
#
111147
# $Log: not supported by cvs2svn $
148+
# Revision 1.3 2001/11/12 22:38:48 richard
149+
# bleah typo
150+
#
112151
# Revision 1.2 2001/11/12 22:37:13 richard
113152
# Handle all the various file formats in roundup
114153
#

0 commit comments

Comments
 (0)