3 import sys, argparse, subprocess, json
6 if not args.rsync or args.rsync == '-':
7 ver_out = sys.stdin.read().strip()
9 ver_out = subprocess.check_output([args.rsync, '--version', '--version'], encoding='utf-8').strip()
10 if ver_out.startswith('{'):
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'):
24 proto = proto.replace('.PR', '.')
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:
35 for x in line.strip(' ,').split(', '):
37 val, var = x.split(' ', 1)
40 elif val.endswith('-bit'):
41 var = var[:-1] + '_bits'
42 val = int(val.split('-')[0])
43 if var == 'protect-args':
48 var = var.replace(' ', '_').replace('-', '_')
49 info[sect_name][var] = val
51 info[sect_name] += [ x for x in line.split() if not x.startswith('(') ]
55 sect_name = line.strip(' :').replace(' ', '_').lower()
58 for chk in 'capabilities optimizations'.split():
61 for chk in 'checksum_list compress_list daemon_auth_list'.split():
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()