Mention smart-make in a comment.
[rsync.git] / support / json-rsync-version
blobbf5684b74c2ce261a1ea36d5f48b0b812b2b6dd4
1 #!/usr/bin/python3
3 import sys, argparse, subprocess, json
5 def main():
6     if not args.rsync or args.rsync == '-':
7         ver_out = sys.stdin.read().strip()
8     else:
9         ver_out = subprocess.check_output([args.rsync, '--version', '--version'], encoding='utf-8').strip()
10     if ver_out.startswith('{'):
11         print(ver_out)
12         return
13     info = { }
14     for line in ver_out.splitlines():
15         if line.startswith('rsync '):
16             prog, vstr, ver, pstr, vstr2, proto = line.split()
17             info['program'] = prog
18             if ver.startswith('v'):
19                 ver = ver[1:]
20             info[vstr] = ver
21             if '.' not in proto:
22                 proto += '.0'
23             else:
24                 proto = proto.replace('.PR', '.')
25             info[pstr] = proto
26         elif line.startswith('Copyright '):
27             info['copyright'] = line[10:]
28         elif line.startswith('Web site: '):
29             info['url'] = line[10:]
30         elif line.startswith('  '):
31             if not saw_comma and ',' in line:
32                 saw_comma = True
33                 info[sect_name] = { }
34             if saw_comma:
35                 for x in line.strip(' ,').split(', '):
36                     if ' ' in x:
37                         val, var = x.split(' ', 1)
38                         if val == 'no':
39                             val = False
40                         elif val.endswith('-bit'):
41                             var = var[:-1] + '_bits'
42                             val = int(val.split('-')[0])
43                         if var == 'protect-args':
44                             var = 'secluded-args'
45                     else:
46                         var = x
47                         val = True
48                     var = var.replace(' ', '_').replace('-', '_')
49                     info[sect_name][var] = val
50             else:
51                 info[sect_name] += [ x for x in line.split() if not x.startswith('(') ]
52         elif line == '':
53             break
54         else:
55             sect_name = line.strip(' :').replace(' ', '_').lower()
56             info[sect_name] = [ ]
57             saw_comma = False
58     for chk in 'capabilities optimizations'.split():
59         if chk not in info:
60             info[chk] = { }
61     for chk in 'checksum_list compress_list daemon_auth_list'.split():
62         if chk not in info:
63             info[chk] = [ ]
64     info['license'] = 'GPL3'
65     info['caveat'] = 'rsync comes with ABSOLUTELY NO WARRANTY'
66     print(json.dumps(info))
69 if __name__ == '__main__':
70     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)
71     parser.add_argument('rsync', nargs='?', help="Specify an rsync command to run. Otherwise stdin is consumed.")
72     parser.add_argument("--help", "-h", action="help", help="Output this help message and exit.")
73     args = parser.parse_args()
74     main()
76 # vim: sw=4 et