Project

Profile

Help

Issue #3462 » tag-orphan-remove.py

Used by tag-orphan-test - amacdona@redhat.com, 04/10/2018 08:09 PM

 
1
"""
2
A silly hack on top of jq to show whether a docker tag is present."
3

4
Command-line tool to validate and pretty-print JSON
5

6
Usage::
7

8
    $ echo '{"json":"obj"}' | python -m json.tool
9
    {
10
        "json": "obj"
11
    }
12
    $ echo '{ 1.2:3.4}' | python -m json.tool
13
    Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
14

15
"""
16
import sys
17
import json
18

    
19
def test_obj(obj):
20
    """
21
    asmacdo's stupid hack
22
    """
23
    try:
24
        tag_1 = obj['manifest_list_amd64_tags']['1']
25
    except KeyError:
26
        return {"    Gone: (1 tag)": None}
27
    return {"    Present (1 tag)": tag_1}
28

    
29
def main():
30
    if len(sys.argv) == 1:
31
        infile = sys.stdin
32
        outfile = sys.stdout
33
    elif len(sys.argv) == 2:
34
        infile = open(sys.argv[1], 'rb')
35
        outfile = sys.stdout
36
    elif len(sys.argv) == 3:
37
        infile = open(sys.argv[1], 'rb')
38
        outfile = open(sys.argv[2], 'wb')
39
    else:
40
        raise SystemExit(sys.argv[0] + " [infile [outfile]]")
41
    with infile:
42
        try:
43
            obj = json.load(infile)
44
        except ValueError, e:
45
            raise SystemExit(e)
46
    with outfile:
47
        # call asmacdo's stupid hack
48
        obj = test_obj(obj)
49
        json.dump(obj, outfile, sort_keys=True,
50
                  indent=4, separators=(',', ': '))
51
        outfile.write('\n')
52

    
53

    
54
if __name__ == '__main__':
55
    main()
56

    
(1-1/2)