comparison roundup/install_util.py @ 387:c066d3c91728

Copy function, and proper handling of unknown file types
author Jürgen Hermann <jhermann@users.sourceforge.net>
date Mon, 12 Nov 2001 23:14:40 +0000
parents 66e18fd9f5a2
children 2d92f93072f9
comparison
equal deleted inserted replaced
386:59ed29a879f6 387:c066d3c91728
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 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" 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, 15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17 # 17 #
18 # $Id: install_util.py,v 1.3 2001-11-12 22:38:48 richard Exp $ 18 # $Id: install_util.py,v 1.4 2001-11-12 23:14:40 jhermann Exp $
19 19
20 import os, sha 20 import os, sha, shutil
21
22 sgml_file_types = [".xml", ".ent", ".html", ".filter", ".index", ".item"]
23 hash_file_types = [".py", ".sh", ".conf", ".cgi", '']
24 slast_file_types = [".css"]
25
26 digested_file_types = sgml_file_types + hash_file_types + slast_file_types
21 27
22 28
23 def checkDigest(filename): 29 def checkDigest(filename):
24 """Read file, check for valid fingerprint, return TRUE if ok""" 30 """Read file, check for valid fingerprint, return TRUE if ok"""
25 # open and read file 31 # open and read file
70 76
71 def close(self): 77 def close(self):
72 file, ext = os.path.splitext(self.filename) 78 file, ext = os.path.splitext(self.filename)
73 79
74 # ".filter", ".index", ".item" are roundup-specific 80 # ".filter", ".index", ".item" are roundup-specific
75 if ext in [".xml", ".ent", ".html", ".filter", ".index", ".item"]: 81 if ext in sgml_file_types:
76 self.file.write("<!-- SHA: %s -->\n" % (self.digest.hexdigest(),)) 82 self.file.write("<!-- SHA: %s -->\n" % (self.digest.hexdigest(),))
77 elif ext in [".py", ".sh", ".conf", ".cgi", '']: 83 elif ext in hash_file_types:
78 self.file.write("#SHA: %s\n" % (self.digest.hexdigest(),)) 84 self.file.write("#SHA: %s\n" % (self.digest.hexdigest(),))
79 elif ext in [".css"]: 85 elif ext in slast_file_types:
80 self.file.write("/* SHA: %s */\n" % (self.digest.hexdigest(),)) 86 self.file.write("/* SHA: %s */\n" % (self.digest.hexdigest(),))
81 87
82 self.file.close() 88 self.file.close()
89
90
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))
98
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)
105
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()
115
116 if copystat: shutil.copystat(src, dst)
83 117
84 118
85 def test(): 119 def test():
86 import sys 120 import sys
87 121
88 testdata = open(sys.argv[0], 'r').read() 122 testdata = open(sys.argv[0], 'r').read()
89 testfile = "digest_test.py"
90 123
91 out = DigestFile(testfile) 124 for ext in digested_file_types:
92 out.write(testdata) 125 testfile = "__digest_test" + ext
93 out.close()
94 126
95 assert checkDigest(testfile), "digest ok w/o modification" 127 out = DigestFile(testfile)
128 out.write(testdata)
129 out.close()
96 130
97 mod = open(testfile, 'r+') 131 assert checkDigest(testfile), "digest ok w/o modification"
98 mod.seek(0)
99 mod.write('# changed!')
100 mod.close()
101 132
102 assert not checkDigest(testfile), "digest fails after modification" 133 mod = open(testfile, 'r+')
134 mod.seek(0)
135 mod.write('# changed!')
136 mod.close()
103 137
104 os.remove(testfile) 138 assert not checkDigest(testfile), "digest fails after modification"
139
140 os.remove(testfile)
105 141
106 142
107 if __name__ == '__main__': 143 if __name__ == '__main__':
108 test() 144 test()
109 145
110 # 146 #
111 # $Log: not supported by cvs2svn $ 147 # $Log: not supported by cvs2svn $
148 # Revision 1.3 2001/11/12 22:38:48 richard
149 # bleah typo
150 #
112 # Revision 1.2 2001/11/12 22:37:13 richard 151 # Revision 1.2 2001/11/12 22:37:13 richard
113 # Handle all the various file formats in roundup 152 # Handle all the various file formats in roundup
114 # 153 #
115 # Revision 1.1 2001/11/12 22:26:32 jhermann 154 # Revision 1.1 2001/11/12 22:26:32 jhermann
116 # Added install utils (digest calculation) 155 # Added install utils (digest calculation)

Roundup Issue Tracker: http://roundup-tracker.org/