1#!/usr/bin/env python3
2
3import hashlib
4
5ZONEFILE_METADATA_TEMPLATE = """{{
6    "metadata_version": 2.0,
7    "releases_url": [],
8    "tzdata_file": "{tzdata_file}",
9    "tzdata_file_sha512": "{tzdata_sha512}",
10    "tzversion": "{tzdata_version}",
11    "zonegroups": [
12        "africa",
13        "antarctica",
14        "asia",
15        "australasia",
16        "europe",
17        "northamerica",
18        "southamerica",
19        "pacificnew",
20        "etcetera",
21        "systemv",
22        "factory",
23        "backzone",
24        "backward"
25    ]
26}}
27"""
28
29
30def calculate_sha512(fpath):
31    with open(fpath, 'rb') as f:
32        sha_hasher = hashlib.sha512()
33        sha_hasher.update(f.read())
34        return sha_hasher.hexdigest()
35
36
37if __name__ == "__main__":
38    import argparse
39    parser = argparse.ArgumentParser()
40
41    parser.add_argument('tzdata', metavar='TZDATA',
42                        help='The name tzdata tarball file')
43    parser.add_argument('version', metavar='VERSION',
44                        help='The version of the tzdata tarball')
45    parser.add_argument('out', metavar='OUT', nargs='?',
46                        default='zonefile_metadata.json',
47                        help='Where to write the file')
48
49    args = parser.parse_args()
50
51    tzdata = args.tzdata
52    version = args.version
53    sha512 = calculate_sha512(tzdata)
54
55    metadata_file_text = ZONEFILE_METADATA_TEMPLATE.format(
56        tzdata_file=tzdata,
57        tzdata_version=version,
58        tzdata_sha512=sha512,
59    )
60
61    with open(args.out, 'w') as f:
62        f.write(metadata_file_text)
63
64