Code

Added module docstrings to all modules.
[roundup.git] / roundup / install_util.py
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.6 2001-11-22 15:46:42 jhermann Exp $
20 __doc__ = """
21 Support module to generate and check fingerprints of installed files.
22 """
24 import os, sha, shutil
26 sgml_file_types = [".xml", ".ent", ".html", ".filter", ".index", ".item"]
27 hash_file_types = [".py", ".sh", ".conf", ".cgi", '']
28 slast_file_types = [".css"]
30 digested_file_types = sgml_file_types + hash_file_types + slast_file_types
33 def checkDigest(filename):
34     """Read file, check for valid fingerprint, return TRUE if ok"""
35     # open and read file
36     inp = open(filename, "r")
37     lines = inp.readlines()
38     inp.close()
40     # get fingerprint from last line
41     if lines[-1][:6] == "#SHA: ":
42         # handle .py/.sh comment
43         fingerprint = lines[-1][6:].strip()
44     elif lines[-1][:10] == "<!-- SHA: ":
45         # handle xml/html files
46         fingerprint = lines[-1][10:]
47         fingerprint = fingerprint.replace('-->', '')
48         fingerprint = fingerprint.strip()
49     elif lines[-1][:8] == "/* SHA: ":
50         # handle css files
51         fingerprint = lines[-1][8:]
52         fingerprint = fingerprint.replace('*/', '')
53         fingerprint = fingerprint.strip()
54     else:
55         return 0
56     del lines[-1]
58     # calculate current digest
59     digest = sha.new()
60     for line in lines:
61         digest.update(line)
63     # compare current to stored digest
64     return fingerprint == digest.hexdigest()
67 class DigestFile:
68     """ A class that you can use like open() and that calculates
69         and writes a SHA digest to the target file.
70     """
72     def __init__(self, filename):
73         self.filename = filename
74         self.digest = sha.new()
75         self.file = open(self.filename, "w")
77     def write(self, data):
78         self.file.write(data)
79         self.digest.update(data)
81     def close(self):
82         file, ext = os.path.splitext(self.filename)
84         # ".filter", ".index", ".item" are roundup-specific
85         if ext in sgml_file_types:
86             self.file.write("<!-- SHA: %s -->\n" % (self.digest.hexdigest(),))
87         elif ext in hash_file_types:
88             self.file.write("#SHA: %s\n" % (self.digest.hexdigest(),))
89         elif ext in slast_file_types:
90             self.file.write("/* SHA: %s */\n" % (self.digest.hexdigest(),))
92         self.file.close()
95 def copyDigestedFile(src, dst, copystat=1):
96     """ Copy data from `src` to `dst`, adding a fingerprint to `dst`.
97         If `copystat` is true, the file status is copied, too
98         (like shutil.copy2).
99     """
100     if os.path.isdir(dst):
101         dst = os.path.join(dst, os.path.basename(src))
103     dummy, ext = os.path.splitext(src)
104     if ext not in digested_file_types:
105         if copystat:
106             return shutil.copy2(src, dst)
107         else:
108             return shutil.copyfile(src, dst)
110     fsrc = None
111     fdst = None
112     try:
113         fsrc = open(src, 'r')
114         fdst = DigestFile(dst)
115         shutil.copyfileobj(fsrc, fdst)
116     finally:
117         if fdst: fdst.close()
118         if fsrc: fsrc.close()
120     if copystat: shutil.copystat(src, dst)
123 def test():
124     import sys
126     testdata = open(sys.argv[0], 'r').read()
128     for ext in digested_file_types:
129         testfile = "__digest_test" + ext
131         out = DigestFile(testfile)
132         out.write(testdata)
133         out.close()
135         assert checkDigest(testfile), "digest ok w/o modification"
137         mod = open(testfile, 'r+')
138         mod.seek(0)
139         mod.write('# changed!')
140         mod.close()
142         assert not checkDigest(testfile), "digest fails after modification"
144         os.remove(testfile)
147 if __name__ == '__main__':
148     test()
151 # $Log: not supported by cvs2svn $
152 # Revision 1.5  2001/11/12 23:17:38  jhermann
153 # Code using copyDigestedFile() that passes unit tests
155 # Revision 1.4  2001/11/12 23:14:40  jhermann
156 # Copy function, and proper handling of unknown file types
158 # Revision 1.3  2001/11/12 22:38:48  richard
159 # bleah typo
161 # Revision 1.2  2001/11/12 22:37:13  richard
162 # Handle all the various file formats in roundup
164 # Revision 1.1  2001/11/12 22:26:32  jhermann
165 # Added install utils (digest calculation)