|
15 | 15 | # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
16 | 16 | # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
17 | 17 | # |
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 $ |
19 | 19 |
|
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 |
21 | 27 |
|
22 | 28 |
|
23 | 29 | def checkDigest(filename): |
@@ -72,43 +78,76 @@ def close(self): |
72 | 78 | file, ext = os.path.splitext(self.filename) |
73 | 79 |
|
74 | 80 | # ".filter", ".index", ".item" are roundup-specific |
75 | | - if ext in [".xml", ".ent", ".html", ".filter", ".index", ".item"]: |
| 81 | + if ext in sgml_file_types: |
76 | 82 | self.file.write("<!-- SHA: %s -->\n" % (self.digest.hexdigest(),)) |
77 | | - elif ext in [".py", ".sh", ".conf", ".cgi", '']: |
| 83 | + elif ext in hash_file_types: |
78 | 84 | self.file.write("#SHA: %s\n" % (self.digest.hexdigest(),)) |
79 | | - elif ext in [".css"]: |
| 85 | + elif ext in slast_file_types: |
80 | 86 | self.file.write("/* SHA: %s */\n" % (self.digest.hexdigest(),)) |
81 | 87 |
|
82 | 88 | self.file.close() |
83 | 89 |
|
84 | 90 |
|
| 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 | + |
85 | 119 | def test(): |
86 | 120 | import sys |
87 | 121 |
|
88 | 122 | testdata = open(sys.argv[0], 'r').read() |
89 | | - testfile = "digest_test.py" |
90 | 123 |
|
91 | | - out = DigestFile(testfile) |
92 | | - out.write(testdata) |
93 | | - out.close() |
| 124 | + for ext in digested_file_types: |
| 125 | + testfile = "__digest_test" + ext |
94 | 126 |
|
95 | | - assert checkDigest(testfile), "digest ok w/o modification" |
| 127 | + out = DigestFile(testfile) |
| 128 | + out.write(testdata) |
| 129 | + out.close() |
96 | 130 |
|
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" |
101 | 132 |
|
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() |
103 | 137 |
|
104 | | - os.remove(testfile) |
| 138 | + assert not checkDigest(testfile), "digest fails after modification" |
| 139 | + |
| 140 | + os.remove(testfile) |
105 | 141 |
|
106 | 142 |
|
107 | 143 | if __name__ == '__main__': |
108 | 144 | test() |
109 | 145 |
|
110 | 146 | # |
111 | 147 | # $Log: not supported by cvs2svn $ |
| 148 | +# Revision 1.3 2001/11/12 22:38:48 richard |
| 149 | +# bleah typo |
| 150 | +# |
112 | 151 | # Revision 1.2 2001/11/12 22:37:13 richard |
113 | 152 | # Handle all the various file formats in roundup |
114 | 153 | # |
|
0 commit comments