summaryrefslogtreecommitdiffstats
path: root/support/json-rsync-version
blob: b01e1e49560203943c575966f07e1515e6f5bad9 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3

import sys, argparse, subprocess, json

TWEAK_NAME = {
        'asm': 'asm_roll',
        'ASM': 'asm_roll',
        'hardlink_special': 'hardlink_specials',
        'protect_args': 'secluded_args',
        'protected_args': 'secluded_args',
        'SIMD': 'SIMD_roll',
    }

MOVE_OPTIM = set('asm_roll SIMD_roll'.split())

def main():
    if not args.rsync or args.rsync == '-':
        ver_out = sys.stdin.read().strip()
    else:
        ver_out = subprocess.check_output([args.rsync, '--version', '--version'], encoding='utf-8').strip()
    if ver_out.startswith('{'):
        print(ver_out)
        return
    info = { }
    misplaced_optims = { }
    for line in ver_out.splitlines():
        if line.startswith('rsync '):
            prog, vstr, ver, pstr, vstr2, proto = line.split()
            info['program'] = prog
            if ver.startswith('v'):
                ver = ver[1:]
            info[vstr] = ver
            if '.' not in proto:
                proto += '.0'
            else:
                proto = proto.replace('.PR', '.')
            info[pstr] = proto
        elif line.startswith('Copyright '):
            info['copyright'] = line[10:]
        elif line.startswith('Web site: '):
            info['url'] = line[10:]
        elif line.startswith('  '):
            if not saw_comma and ',' in line:
                saw_comma = True
                info[sect_name] = { }
            if saw_comma:
                for x in line.strip(' ,').split(', '):
                    if ' ' in x:
                        val, var = x.split(' ', 1)
                        if val == 'no':
                            val = False
                        elif val.endswith('-bit'):
                            var = var[:-1] + '_bits'
                            val = int(val.split('-')[0])
                    else:
                        var = x
                        val = True
                    var = var.replace(' ', '_').replace('-', '_')
                    if var in TWEAK_NAME:
                        var = TWEAK_NAME[var]
                    if sect_name[0] != 'o' and var in MOVE_OPTIM:
                        misplaced_optims[var] = val
                    else:
                        info[sect_name][var] = val
            else:
                info[sect_name] += [ x for x in line.split() if not x.startswith('(') ]
        elif line == '':
            break
        else:
            sect_name = line.strip(' :').replace(' ', '_').lower()
            info[sect_name] = [ ]
            saw_comma = False
    for chk in 'capabilities optimizations'.split():
        if chk not in info:
            info[chk] = { }
    if misplaced_optims:
        info['optimizations'].update(misplaced_optims)
    for chk in 'checksum_list compress_list daemon_auth_list'.split():
        if chk not in info:
            info[chk] = [ ]
    info['license'] = 'GPLv3' if ver[0] == '3' else 'GPLv2'
    info['caveat'] = 'rsync comes with ABSOLUTELY NO WARRANTY'
    print(json.dumps(info))


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Output rsync's version data in JSON format, even if the rsync doesn't support a native json-output method.", add_help=False)
    parser.add_argument('rsync', nargs='?', help="Specify an rsync command to run. Otherwise stdin is consumed.")
    parser.add_argument("--help", "-h", action="help", help="Output this help message and exit.")
    args = parser.parse_args()
    main()

# vim: sw=4 et