• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2import os
3import hashlib
4import json
5import io
6
7from six.moves.urllib import request
8from six.moves.urllib import error as urllib_error
9
10from dateutil.zoneinfo import rebuild
11
12METADATA_FILE = "zonefile_metadata.json"
13
14
15def main():
16    with io.open(METADATA_FILE, 'r') as f:
17        metadata = json.load(f)
18
19    releases_urls = metadata['releases_url']
20    if metadata['metadata_version'] < 2.0:
21        # In later versions the releases URL is a mirror URL
22        releases_urls = [releases_urls]
23
24    if not os.path.isfile(metadata['tzdata_file']):
25
26        for ii, releases_url in enumerate(releases_urls):
27            print("Downloading tz file from mirror {ii}".format(ii=ii))
28            try:
29                request.urlretrieve(os.path.join(releases_url,
30                                                 metadata['tzdata_file']),
31                                    metadata['tzdata_file'])
32            except urllib_error.URLError as e:
33                print("Download failed, trying next mirror.")
34                last_error = e
35                continue
36
37            last_error = None
38            break
39
40        if last_error is not None:
41            raise last_error
42
43    with open(metadata['tzdata_file'], 'rb') as tzfile:
44        sha_hasher = hashlib.sha512()
45        sha_hasher.update(tzfile.read())
46        sha_512_file = sha_hasher.hexdigest()
47        assert metadata['tzdata_file_sha512'] == sha_512_file, "SHA failed for"
48    print("Updating timezone information...")
49    rebuild.rebuild(metadata['tzdata_file'], zonegroups=metadata['zonegroups'],
50            metadata=metadata)
51    print("Done.")
52
53
54if __name__ == "__main__":
55    main()
56