[189] | 1 | # Program to create a diff of two distribution directories
|
---|
| 2 |
|
---|
| 3 | #--------------------------------------------------------------------------
|
---|
| 4 |
|
---|
| 5 | from mlx.update import manifestName, Manifest
|
---|
| 6 |
|
---|
| 7 | import tarfile
|
---|
| 8 | import sys
|
---|
| 9 | import os
|
---|
| 10 | import tempfile
|
---|
| 11 |
|
---|
| 12 | #--------------------------------------------------------------------------
|
---|
| 13 |
|
---|
| 14 | tarName = "diffdist.tar.bz2"
|
---|
| 15 |
|
---|
| 16 | #--------------------------------------------------------------------------
|
---|
| 17 |
|
---|
| 18 | def usage():
|
---|
| 19 | """Print a usage message."""
|
---|
[919] | 20 | print("Usage: %s <old dist dir> <new dist dir>" % (sys.argv[0],))
|
---|
[189] | 21 |
|
---|
| 22 | #--------------------------------------------------------------------------
|
---|
| 23 |
|
---|
| 24 | if __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 |
|
---|
[310] | 63 | if numDiffs<=4:
|
---|
[919] | 64 | print("File %s is considered to be the same in both versions with %d changes" % \
|
---|
| 65 | (path, numDiffs))
|
---|
[189] | 66 | finalManifest.addFile(path, oldSize, oldSum)
|
---|
| 67 | copyOld = True
|
---|
| 68 |
|
---|
| 69 | if not copyOld:
|
---|
[919] | 70 | print(">>> File %s is being copied" % (path,))
|
---|
[189] | 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)
|
---|
[919] | 76 | os.chmod(path, 0o644)
|
---|
[189] | 77 | tarFile.add(path, arcname = manifestName)
|
---|
| 78 | tarFile.close()
|
---|
| 79 |
|
---|
| 80 | os.remove(path)
|
---|
| 81 |
|
---|
[919] | 82 | print()
|
---|
| 83 | print("%s created" % (tarName,))
|
---|
[189] | 84 | if removed:
|
---|
[919] | 85 | print()
|
---|
| 86 | print("Files to remove:")
|
---|
| 87 | print()
|
---|
[189] | 88 | for path in removed:
|
---|
[919] | 89 | print(" ", path)
|
---|