File: prune-checksums

package info (click to toggle)
rustc 1.48.0+dfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, experimental, sid
  • size: 275,960 kB
  • sloc: xml: 147,652; ansic: 16,929; sh: 16,839; javascript: 6,817; python: 6,021; cpp: 4,663; makefile: 3,284; asm: 1,437; ruby: 68; awk: 12
file content (47 lines) | stat: -rwxr-xr-x 1,507 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python3
# Copyright: 2015-2017 The Debian Project
# License: MIT or Apache-2.0
#
# Helper to remove removed-files from .cargo-checksum
# TODO: rewrite to perl and add to dh-cargo, maybe?

from collections import OrderedDict
import argparse
import json
import os
import sys

def prune_keep(cfile):
    with open(cfile) as fp:
        sums = json.load(fp, object_pairs_hook=OrderedDict)

    oldfiles = sums["files"]
    newfiles = OrderedDict([entry for entry in oldfiles.items() if os.path.exists(entry[0])])
    sums["files"] = newfiles

    if len(oldfiles) == len(newfiles):
        return

    with open(cfile, "w") as fp:
        json.dump(sums, fp, separators=(',', ':'))

def prune(cfile):
    with open(cfile, "r+") as fp:
        sums = json.load(fp, object_pairs_hook=OrderedDict)
        sums["files"] = {}
        fp.seek(0)
        json.dump(sums, fp, separators=(',', ':'))
        fp.truncate()

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-k", "--keep", action="store_true", help="keep "
        "checksums of files that still exist, and assume they haven't changed.")
    parser.add_argument('crates', nargs=argparse.REMAINDER,
        help="crates whose checksums to prune. (default: ./)")
    args = parser.parse_args(sys.argv[1:])
    crates = args.crates or ["."]
    f = prune_keep if args.keep else prune
    for c in crates:
        cfile = os.path.join(c, ".cargo-checksum.json") if os.path.isdir(c) else c
        f(cfile)