]>
Commit | Line | Data |
---|---|---|
94bd9ad3 | 1 | #!/usr/bin/python3 |
e9a8101d XL |
2 | # Copyright: 2015-2017 The Debian Project |
3 | # License: MIT or Apache-2.0 | |
4 | # | |
5 | # Helper to remove removed-files from .cargo-checksum | |
6 | # TODO: rewrite to perl and add to dh-cargo, maybe? | |
7 | ||
8 | from collections import OrderedDict | |
9 | import argparse | |
10 | import json | |
11 | import os | |
12 | import sys | |
13 | ||
14 | def prune_keep(cfile): | |
15 | with open(cfile) as fp: | |
16 | sums = json.load(fp, object_pairs_hook=OrderedDict) | |
17 | ||
18 | oldfiles = sums["files"] | |
19 | newfiles = OrderedDict([entry for entry in oldfiles.items() if os.path.exists(entry[0])]) | |
20 | sums["files"] = newfiles | |
21 | ||
22 | if len(oldfiles) == len(newfiles): | |
23 | return | |
24 | ||
25 | with open(cfile, "w") as fp: | |
26 | json.dump(sums, fp, separators=(',', ':')) | |
27 | ||
28 | def prune(cfile): | |
29 | with open(cfile, "r+") as fp: | |
30 | sums = json.load(fp, object_pairs_hook=OrderedDict) | |
31 | sums["files"] = {} | |
32 | fp.seek(0) | |
33 | json.dump(sums, fp, separators=(',', ':')) | |
34 | fp.truncate() | |
35 | ||
36 | if __name__ == "__main__": | |
37 | parser = argparse.ArgumentParser() | |
38 | parser.add_argument("-k", "--keep", action="store_true", help="keep " | |
39 | "checksums of files that still exist, and assume they haven't changed.") | |
40 | parser.add_argument('crates', nargs=argparse.REMAINDER, | |
41 | help="crates whose checksums to prune. (default: ./)") | |
42 | args = parser.parse_args(sys.argv[1:]) | |
43 | crates = args.crates or ["."] | |
44 | f = prune_keep if args.keep else prune | |
45 | for c in crates: | |
46 | cfile = os.path.join(c, ".cargo-checksum.json") if os.path.isdir(c) else c | |
47 | f(cfile) |