Mercurial > p > roundup > code
comparison roundup/install_util.py @ 383:a213e2a90364
Added install utils (digest calculation)
| author | Jürgen Hermann <jhermann@users.sourceforge.net> |
|---|---|
| date | Mon, 12 Nov 2001 22:26:32 +0000 |
| parents | |
| children | a72ad731b2cb |
comparison
equal
deleted
inserted
replaced
| 382:be28f4c4feb6 | 383:a213e2a90364 |
|---|---|
| 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.1 2001-11-12 22:26:32 jhermann Exp $ | |
| 19 | |
| 20 import os, sha | |
| 21 | |
| 22 | |
| 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() | |
| 29 | |
| 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 files | |
| 36 fingerprint = lines[-1][10:] | |
| 37 fingerprint = fingerprint.replace('-->', '') | |
| 38 fingerprint = fingerprint.strip() | |
| 39 else: | |
| 40 return 0 | |
| 41 del lines[-1] | |
| 42 | |
| 43 # calculate current digest | |
| 44 digest = sha.new() | |
| 45 for line in lines: | |
| 46 digest.update(line) | |
| 47 | |
| 48 # compare current to stored digest | |
| 49 return fingerprint == digest.hexdigest() | |
| 50 | |
| 51 | |
| 52 class DigestFile: | |
| 53 """ A class that you can use like open() and that calculates | |
| 54 and writes a SHA digest to the target file. | |
| 55 """ | |
| 56 | |
| 57 def __init__(self, filename): | |
| 58 self.filename = filename | |
| 59 self.digest = sha.new() | |
| 60 self.file = open(self.filename, "w") | |
| 61 | |
| 62 def write(self, data): | |
| 63 self.file.write(data) | |
| 64 self.digest.update(data) | |
| 65 | |
| 66 def close(self): | |
| 67 file, ext = os.path.splitext(self.filename) | |
| 68 | |
| 69 if ext in [".xml", ".ent"]: | |
| 70 self.file.write("<!-- SHA: %s -->\n" % (self.digest.hexdigest(),)) | |
| 71 elif ext in [".py", ".sh", ".conf", '']: | |
| 72 self.file.write("#SHA: %s\n" % (self.digest.hexdigest(),)) | |
| 73 | |
| 74 self.file.close() | |
| 75 | |
| 76 | |
| 77 def test(): | |
| 78 import sys | |
| 79 | |
| 80 testdata = open(sys.argv[0], 'r').read() | |
| 81 testfile = "digest_test.py" | |
| 82 | |
| 83 out = DigestFile(testfile) | |
| 84 out.write(testdata) | |
| 85 out.close() | |
| 86 | |
| 87 assert checkDigest(testfile), "digest ok w/o modification" | |
| 88 | |
| 89 mod = open(testfile, 'r+') | |
| 90 mod.seek(0) | |
| 91 mod.write('# changed!') | |
| 92 mod.close() | |
| 93 | |
| 94 assert not checkDigest(testfile), "digest fails after modification" | |
| 95 | |
| 96 os.remove(testfile) | |
| 97 | |
| 98 | |
| 99 if __name__ == '__main__': | |
| 100 test() | |
| 101 | |
| 102 # | |
| 103 # $Log: not supported by cvs2svn $ | |
| 104 |
