1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17 #
18 # $Id: install_util.py,v 1.4 2001-11-12 23:14:40 jhermann Exp $
20 import os, sha, shutil
22 sgml_file_types = [".xml", ".ent", ".html", ".filter", ".index", ".item"]
23 hash_file_types = [".py", ".sh", ".conf", ".cgi", '']
24 slast_file_types = [".css"]
26 digested_file_types = sgml_file_types + hash_file_types + slast_file_types
29 def checkDigest(filename):
30 """Read file, check for valid fingerprint, return TRUE if ok"""
31 # open and read file
32 inp = open(filename, "r")
33 lines = inp.readlines()
34 inp.close()
36 # get fingerprint from last line
37 if lines[-1][:6] == "#SHA: ":
38 # handle .py/.sh comment
39 fingerprint = lines[-1][6:].strip()
40 elif lines[-1][:10] == "<!-- SHA: ":
41 # handle xml/html files
42 fingerprint = lines[-1][10:]
43 fingerprint = fingerprint.replace('-->', '')
44 fingerprint = fingerprint.strip()
45 elif lines[-1][:8] == "/* SHA: ":
46 # handle css files
47 fingerprint = lines[-1][8:]
48 fingerprint = fingerprint.replace('*/', '')
49 fingerprint = fingerprint.strip()
50 else:
51 return 0
52 del lines[-1]
54 # calculate current digest
55 digest = sha.new()
56 for line in lines:
57 digest.update(line)
59 # compare current to stored digest
60 return fingerprint == digest.hexdigest()
63 class DigestFile:
64 """ A class that you can use like open() and that calculates
65 and writes a SHA digest to the target file.
66 """
68 def __init__(self, filename):
69 self.filename = filename
70 self.digest = sha.new()
71 self.file = open(self.filename, "w")
73 def write(self, data):
74 self.file.write(data)
75 self.digest.update(data)
77 def close(self):
78 file, ext = os.path.splitext(self.filename)
80 # ".filter", ".index", ".item" are roundup-specific
81 if ext in sgml_file_types:
82 self.file.write("<!-- SHA: %s -->\n" % (self.digest.hexdigest(),))
83 elif ext in hash_file_types:
84 self.file.write("#SHA: %s\n" % (self.digest.hexdigest(),))
85 elif ext in slast_file_types:
86 self.file.write("/* SHA: %s */\n" % (self.digest.hexdigest(),))
88 self.file.close()
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))
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)
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()
116 if copystat: shutil.copystat(src, dst)
119 def test():
120 import sys
122 testdata = open(sys.argv[0], 'r').read()
124 for ext in digested_file_types:
125 testfile = "__digest_test" + ext
127 out = DigestFile(testfile)
128 out.write(testdata)
129 out.close()
131 assert checkDigest(testfile), "digest ok w/o modification"
133 mod = open(testfile, 'r+')
134 mod.seek(0)
135 mod.write('# changed!')
136 mod.close()
138 assert not checkDigest(testfile), "digest fails after modification"
140 os.remove(testfile)
143 if __name__ == '__main__':
144 test()
146 #
147 # $Log: not supported by cvs2svn $
148 # Revision 1.3 2001/11/12 22:38:48 richard
149 # bleah typo
150 #
151 # Revision 1.2 2001/11/12 22:37:13 richard
152 # Handle all the various file formats in roundup
153 #
154 # Revision 1.1 2001/11/12 22:26:32 jhermann
155 # Added install utils (digest calculation)
156 #