source: diffdist.py@ 1002:5e741be6b47c

python3
Last change on this file since 1002:5e741be6b47c was 919:2ce8ca39525b, checked in by István Váradi <ivaradi@…>, 6 years ago

Ran 2to3

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<=4:
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 if not copyOld:
70 print(">>> File %s is being copied" % (path,))
71 tarFile.add(os.path.join(newDirectory, path), arcname = path)
72
73 (fd, path) = tempfile.mkstemp()
74 with os.fdopen(fd, "wt") as f:
75 finalManifest.writeInto(f)
76 os.chmod(path, 0o644)
77 tarFile.add(path, arcname = manifestName)
78 tarFile.close()
79
80 os.remove(path)
81
82 print()
83 print("%s created" % (tarName,))
84 if removed:
85 print()
86 print("Files to remove:")
87 print()
88 for path in removed:
89 print(" ", path)
Note: See TracBrowser for help on using the repository browser.