Code

bleah typo
[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.3 2001-11-12 22:38:48 richard Exp $
20 import os, sha
23 def checkDigest(filename):
24     """Read file, check for valid fingerprint, return TRUE if ok"""
25     # open and read file
26     inp = open(filename, "r")
27     lines = inp.readlines()
28     inp.close()
30     # get fingerprint from last line
31     if lines[-1][:6] == "#SHA: ":
32         # handle .py/.sh comment
33         fingerprint = lines[-1][6:].strip()
34     elif lines[-1][:10] == "<!-- SHA: ":
35         # handle xml/html files
36         fingerprint = lines[-1][10:]
37         fingerprint = fingerprint.replace('-->', '')
38         fingerprint = fingerprint.strip()
39     elif lines[-1][:8] == "/* SHA: ":
40         # handle css files
41         fingerprint = lines[-1][8:]
42         fingerprint = fingerprint.replace('*/', '')
43         fingerprint = fingerprint.strip()
44     else:
45         return 0
46     del lines[-1]
48     # calculate current digest
49     digest = sha.new()
50     for line in lines:
51         digest.update(line)
53     # compare current to stored digest
54     return fingerprint == digest.hexdigest()
57 class DigestFile:
58     """ A class that you can use like open() and that calculates
59         and writes a SHA digest to the target file.
60     """
62     def __init__(self, filename):
63         self.filename = filename
64         self.digest = sha.new()
65         self.file = open(self.filename, "w")
67     def write(self, data):
68         self.file.write(data)
69         self.digest.update(data)
71     def close(self):
72         file, ext = os.path.splitext(self.filename)
74         # ".filter", ".index", ".item" are roundup-specific
75         if ext in [".xml", ".ent", ".html", ".filter", ".index", ".item"]:
76             self.file.write("<!-- SHA: %s -->\n" % (self.digest.hexdigest(),))
77         elif ext in [".py", ".sh", ".conf", ".cgi", '']:
78             self.file.write("#SHA: %s\n" % (self.digest.hexdigest(),))
79         elif ext in [".css"]:
80             self.file.write("/* SHA: %s */\n" % (self.digest.hexdigest(),))
82         self.file.close()
85 def test():
86     import sys
88     testdata = open(sys.argv[0], 'r').read()
89     testfile = "digest_test.py"
91     out = DigestFile(testfile)
92     out.write(testdata)
93     out.close()
95     assert checkDigest(testfile), "digest ok w/o modification"
97     mod = open(testfile, 'r+')
98     mod.seek(0)
99     mod.write('# changed!')
100     mod.close()
102     assert not checkDigest(testfile), "digest fails after modification"
104     os.remove(testfile)
107 if __name__ == '__main__':
108     test()
111 # $Log: not supported by cvs2svn $
112 # Revision 1.2  2001/11/12 22:37:13  richard
113 # Handle all the various file formats in roundup
115 # Revision 1.1  2001/11/12 22:26:32  jhermann
116 # Added install utils (digest calculation)