Code

svn repository setup
[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.11 2006-01-25 03:11:43 richard Exp $
20 """Support module to generate and check fingerprints of installed files.
21 """
22 __docformat__ = 'restructuredtext'
24 import os, sha, shutil
26 sgml_file_types = [".xml", ".ent", ".html"]
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
32 def extractFingerprint(lines):
33     # get fingerprint from last line
34     if lines[-1].startswith("#SHA: "):
35         # handle .py/.sh comment
36         return lines[-1][6:].strip()
37     elif lines[-1].startswith("<!-- SHA: "):
38         # handle xml/html files
39         fingerprint = lines[-1][10:]
40         fingerprint = fingerprint.replace('-->', '')
41         return fingerprint.strip()
42     elif lines[-1].startswith("/* SHA: "):
43         # handle css files
44         fingerprint = lines[-1][8:]
45         fingerprint = fingerprint.replace('*/', '')
46         return fingerprint.strip()
47     return None
49 def checkDigest(filename):
50     """Read file, check for valid fingerprint, return TRUE if ok"""
51     # open and read file
52     inp = open(filename, "r")
53     lines = inp.readlines()
54     inp.close()
56     fingerprint = extractFingerprint(lines)
57     if fingerprint is None:
58         return 0
59     del lines[-1]
61     # calculate current digest
62     digest = sha.new()
63     for line in lines:
64         digest.update(line)
66     # compare current to stored digest
67     return fingerprint == digest.hexdigest()
70 class DigestFile:
71     """ A class that you can use like open() and that calculates
72         and writes a SHA digest to the target file.
73     """
75     def __init__(self, filename):
76         self.filename = filename
77         self.digest = sha.new()
78         self.file = open(self.filename, "w")
80     def write(self, data):
81         lines = data.splitlines()
82         # if the file is coming from an installed tracker being used as a
83         # template, then we will want to re-calculate the SHA
84         fingerprint = extractFingerprint(lines)
85         if fingerprint is not None:
86             data = '\n'.join(lines[:-1]) + '\n'
87         self.file.write(data)
88         self.digest.update(data)
90     def close(self):
91         file, ext = os.path.splitext(self.filename)
93         if ext in sgml_file_types:
94             self.file.write("<!-- SHA: %s -->\n" % (self.digest.hexdigest(),))
95         elif ext in hash_file_types:
96             self.file.write("#SHA: %s\n" % (self.digest.hexdigest(),))
97         elif ext in slast_file_types:
98             self.file.write("/* SHA: %s */\n" % (self.digest.hexdigest(),))
100         self.file.close()
103 def copyDigestedFile(src, dst, copystat=1):
104     """ Copy data from `src` to `dst`, adding a fingerprint to `dst`.
105         If `copystat` is true, the file status is copied, too
106         (like shutil.copy2).
107     """
108     if os.path.isdir(dst):
109         dst = os.path.join(dst, os.path.basename(src))
111     dummy, ext = os.path.splitext(src)
112     if ext not in digested_file_types:
113         if copystat:
114             return shutil.copy2(src, dst)
115         else:
116             return shutil.copyfile(src, dst)
118     fsrc = None
119     fdst = None
120     try:
121         fsrc = open(src, 'r')
122         fdst = DigestFile(dst)
123         shutil.copyfileobj(fsrc, fdst)
124     finally:
125         if fdst: fdst.close()
126         if fsrc: fsrc.close()
128     if copystat: shutil.copystat(src, dst)
131 def test():
132     import sys
134     testdata = open(sys.argv[0], 'r').read()
136     for ext in digested_file_types:
137         testfile = "__digest_test" + ext
139         out = DigestFile(testfile)
140         out.write(testdata)
141         out.close()
143         assert checkDigest(testfile), "digest ok w/o modification"
145         mod = open(testfile, 'r+')
146         mod.seek(0)
147         mod.write('# changed!')
148         mod.close()
150         assert not checkDigest(testfile), "digest fails after modification"
152         os.remove(testfile)
155 if __name__ == '__main__':
156     test()
158 # vim: set filetype=python ts=4 sw=4 et si