-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.py
executable file
·128 lines (105 loc) · 4.94 KB
/
build.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python2
# tz.js - Library for working with timezones in JavaScript
# Written in 2011 by L. David Baron <dbaron@dbaron.org>
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication
# along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
import os
import os.path
import subprocess
import gzip
import imp
import re
import shutil
import tempfile
import json
INPUT_DIR = os.path.dirname(os.path.abspath(__file__))
OUTPUT_DIR = os.path.join(INPUT_DIR, "output")
if not os.path.exists(OUTPUT_DIR):
os.mkdir(OUTPUT_DIR)
version_proc = subprocess.Popen(["git", "log", "--format=oneline", "HEAD"],
cwd=INPUT_DIR,
stdout=subprocess.PIPE)
version = version_proc.stdout.read(40)
version_proc.stdout.close()
version_proc = subprocess.Popen(["git", "describe", "--tags", "--exact-match"],
cwd=INPUT_DIR,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(describe_stdout, describe_stderr) = version_proc.communicate()
if version_proc.returncode == 0:
# Use the tag as primary description and the hash in parentheses.
version = "{0} ({1})".format(describe_stdout.rstrip("\n"), version)
# If this looks like a github repository, compute the HTTP URL from the
# git URL and include that as part of the version.
version_proc = subprocess.Popen(["git", "remote", "show", "-n", "origin"],
cwd=INPUT_DIR,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(describe_stdout, describe_stderr) = version_proc.communicate()
if version_proc.returncode == 0:
fetch_re = re.compile("^ Fetch URL: git@github.com:(.*)\.git$",
re.MULTILINE)
m = fetch_re.search(describe_stdout)
if m is not None:
url = "https://github.com/" + m.groups()[0]
version = "{0} {1}".format(url, version)
get_tz_module = imp.load_source("get_tz",
os.path.join(INPUT_DIR,
"get-latest-tz-release.py"))
tzversions = get_tz_module.get_latest_tz_release()
tztempdir = tempfile.mkdtemp()
build_tz_module = imp.load_source("build_tz",
os.path.join(INPUT_DIR,
"build-tz.py"))
build_tz_module.build_tz(tztempdir,
"tzcode" + tzversions["tzcode"] + ".tar.gz",
"tzdata" + tzversions["tzdata"] + ".tar.gz")
zoneinfo_dir = os.path.join(tztempdir, "output", "usr", "share", "zoneinfo")
zdump_command = os.path.join(tztempdir, "output", "usr", "bin", "zdump")
tz_json_module = imp.load_source("compiled_to_json",
os.path.join(INPUT_DIR,
"compiled-to-json.py"))
tz_json = tz_json_module.json_zones(zoneinfo_dir)
source_dir = os.path.join(tztempdir, "source")
link_json_module = imp.load_source("links_to_json",
os.path.join(INPUT_DIR,
"links-to-json.py"))
link_json = link_json_module.json_zones(source_dir)
tz_js_in = open(os.path.join(INPUT_DIR, "tz.js.in"), "rb")
tz_js_source = tz_js_in.read()
tz_js_in.close()
# Most Links are included in zone.tab (though some are not). So let's
# delete them from the JSON. (It saves about 10% uncompressed size of
# tz.js, although only 5% compressed.)
tzs = json.loads(tz_json)
for zone in json.loads(link_json):
if zone in tzs:
del tzs[zone]
tz_json = json.dumps(tzs, sort_keys=True)
tz_js_source = tz_js_source.replace("@@VERSION@@", version)
tz_js_source = tz_js_source.replace("@@TZDATA_VERSION@@", tzversions["tzdata"])
tz_js_source = tz_js_source.replace("@@LINKS@@", link_json)
tz_js_source = tz_js_source.replace("@@ZONES@@", tz_json)
tz_js_gz = gzip.open(os.path.join(OUTPUT_DIR, "tz.js.gz"), "wb")
tz_js_gz.write(tz_js_source)
tz_js_gz.close()
tz_js = open(os.path.join(OUTPUT_DIR, "tz.js"), "wb")
tz_js.write(tz_js_source)
tz_js.close()
test_input_file = os.path.join(INPUT_DIR, "build-tests.py")
test_output_file = os.path.join(OUTPUT_DIR, "test-tz.js")
if (not os.path.exists(test_output_file)) or \
os.stat(test_input_file).st_mtime > os.stat(test_output_file).st_mtime:
tz_tests_module = imp.load_source("tz_test_generator", test_input_file)
tests_io = open(test_output_file, "wb")
tz_tests_module.output_tests(zoneinfo_dir, zdump_command, tests_io)
tests_io.close()
shutil.copy(os.path.join(INPUT_DIR, "test-tz-browser.html"), OUTPUT_DIR)
shutil.copy(os.path.join(INPUT_DIR, "test-tz-node.js"), OUTPUT_DIR)
shutil.rmtree(tztempdir)