source: diffdist.py@ 301:cb7f4de9b2a2

Last change on this file since 301:cb7f4de9b2a2 was 193:ef5f3f8cf79e, checked in by István Váradi <ivaradi@…>, 12 years ago

Added changing the mode of the manifest file to be readable by everybody

File size: 2.7 KB
Line 
1# Program to create a diff of two distribution directories
2
3#--------------------------------------------------------------------------
4
5from mlx.update import manifestName, Manifest
6
7import tarfile
8import sys
9import os
10import tempfile
11
12#--------------------------------------------------------------------------
13
14tarName = "diffdist.tar.bz2"
15
16#--------------------------------------------------------------------------
17
18def usage():
19 """Print a usage message."""
20 print "Usage: %s <old dist dir> <new dist dir>" % (sys.argv[0],)
21
22#--------------------------------------------------------------------------
23
24if __name__ == "__main__":
25 if len(sys.argv)!=3:
26 usage()
27 sys.exit(1)
28
29 oldDirectory = sys.argv[1]
30 newDirectory = sys.argv[2]
31
32 oldManifest = Manifest()
33 newManifest = Manifest()
34
35 with open(os.path.join(oldDirectory, manifestName), "rt") as f:
36 oldManifest.readFrom(f)
37
38 with open(os.path.join(newDirectory, manifestName), "rt") as f:
39 newManifest.readFrom(f)
40
41 finalManifest = newManifest.copy()
42
43 (modifiedAndNew, removed) = oldManifest.compare(newManifest)
44 #print removed
45 #print modifiedAndNew
46
47 tarFile = tarfile.open(tarName, mode="w:bz2")
48
49 for (path, newSize, newSum) in modifiedAndNew:
50 copyOld = False
51 if path in oldManifest:
52 (oldSize, oldSum) = oldManifest[path]
53 if path.endswith(".pyc") and newSize<1024 and newSize==oldSize:
54 with open(os.path.join(oldDirectory, path), "rb") as f:
55 oldData = f.read()
56 with open(os.path.join(newDirectory, path), "rb") as f:
57 newData = f.read()
58
59 numDiffs = 0
60 for i in range(0, newSize):
61 if oldData[i]!=newData[i]: numDiffs += 1
62
63 if numDiffs<=3:
64 print "File %s is considered to be the same in both versions with %d changes" % \
65 (path, numDiffs)
66 finalManifest.addFile(path, oldSize, oldSum)
67 copyOld = True
68
69
70 if not copyOld:
71 print ">>> File %s is being copied" % (path,)
72 tarFile.add(os.path.join(newDirectory, path), arcname = path)
73
74 (fd, path) = tempfile.mkstemp()
75 with os.fdopen(fd, "wt") as f:
76 finalManifest.writeInto(f)
77 os.chmod(path, 0644)
78 tarFile.add(path, arcname = manifestName)
79 tarFile.close()
80
81 os.remove(path)
82
83 print
84 print "%s created" % (tarName,)
85 if removed:
86 print
87 print "Files to remove:"
88 print
89 for path in removed:
90 print " ", path
Note: See TracBrowser for help on using the repository browser.