2018-09-17 00:27:00 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import shutil
|
|
|
|
import json
|
|
|
|
from subprocess import check_call
|
|
|
|
|
2018-10-31 16:08:29 +00:00
|
|
|
from dateutil.zoneinfo import tar_open, METADATA_FN, ZONEFILENAME
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def rebuild(filename, tag=None, format="gz", zonegroups=[], metadata=None):
|
|
|
|
"""Rebuild the internal timezone info in dateutil/zoneinfo/zoneinfo*tar*
|
|
|
|
|
2018-10-31 16:08:29 +00:00
|
|
|
filename is the timezone tarball from ftp.iana.org/tz.
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
tmpdir = tempfile.mkdtemp()
|
|
|
|
zonedir = os.path.join(tmpdir, "zoneinfo")
|
|
|
|
moduledir = os.path.dirname(__file__)
|
|
|
|
try:
|
2018-10-31 16:08:29 +00:00
|
|
|
with tar_open(filename) as tf:
|
2018-09-17 00:27:00 +00:00
|
|
|
for name in zonegroups:
|
|
|
|
tf.extract(name, tmpdir)
|
|
|
|
filepaths = [os.path.join(tmpdir, n) for n in zonegroups]
|
|
|
|
try:
|
|
|
|
check_call(["zic", "-d", zonedir] + filepaths)
|
|
|
|
except OSError as e:
|
|
|
|
_print_on_nosuchfile(e)
|
|
|
|
raise
|
|
|
|
# write metadata file
|
|
|
|
with open(os.path.join(zonedir, METADATA_FN), 'w') as f:
|
|
|
|
json.dump(metadata, f, indent=4, sort_keys=True)
|
|
|
|
target = os.path.join(moduledir, ZONEFILENAME)
|
2018-10-31 16:08:29 +00:00
|
|
|
with tar_open(target, "w:%s" % format) as tf:
|
2018-09-17 00:27:00 +00:00
|
|
|
for entry in os.listdir(zonedir):
|
|
|
|
entrypath = os.path.join(zonedir, entry)
|
|
|
|
tf.add(entrypath, entry)
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(tmpdir)
|
|
|
|
|
|
|
|
def _print_on_nosuchfile(e):
|
|
|
|
"""Print helpful troubleshooting message
|
|
|
|
|
|
|
|
e is an exception raised by subprocess.check_call()
|
|
|
|
|
|
|
|
"""
|
|
|
|
if e.errno == 2:
|
|
|
|
logging.error(
|
|
|
|
"Could not find zic. Perhaps you need to install "
|
|
|
|
"libc-bin or some other package that provides it, "
|
|
|
|
"or it's not in your PATH?")
|