From fe534d5229de5e83dccdfa3e4ba3c1b29bd38a71 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Feb 2021 08:34:39 +0100 Subject: Adding upstream version 2020.1+dfsg. Signed-off-by: Daniel Baumann --- pytzdata/commands/__init__.py | 2 + pytzdata/commands/app.py | 15 ++++ pytzdata/commands/make.py | 164 ++++++++++++++++++++++++++++++++++++++++++ pytzdata/commands/zones.py | 36 ++++++++++ 4 files changed, 217 insertions(+) create mode 100644 pytzdata/commands/__init__.py create mode 100644 pytzdata/commands/app.py create mode 100644 pytzdata/commands/make.py create mode 100644 pytzdata/commands/zones.py (limited to 'pytzdata/commands') diff --git a/pytzdata/commands/__init__.py b/pytzdata/commands/__init__.py new file mode 100644 index 0000000..633f866 --- /dev/null +++ b/pytzdata/commands/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf-8 -*- + diff --git a/pytzdata/commands/app.py b/pytzdata/commands/app.py new file mode 100644 index 0000000..0d09a43 --- /dev/null +++ b/pytzdata/commands/app.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- + +from cleo import Application + +from .make import MakeCommand +from .zones import ZonesCommand + +app = Application('pytzdata') + +app.add(MakeCommand()) +app.add(ZonesCommand()) + + +if __name__ == '__main__': + app.run() diff --git a/pytzdata/commands/make.py b/pytzdata/commands/make.py new file mode 100644 index 0000000..e0a7b4b --- /dev/null +++ b/pytzdata/commands/make.py @@ -0,0 +1,164 @@ +# -*- coding: utf-8 -*- + +import os +import errno +import shutil +import tarfile +import subprocess + +from urllib.request import urlopen +from contextlib import closing +from distutils.dir_util import copy_tree, remove_tree +from cleo import Command + + +class MakeCommand(Command): + """ + Make tzdata files. + + make + {version? : The version to make.} + {--path= : The destination directory.} + """ + + FILES = ["tzcode-latest.tar.gz", "tzdata-latest.tar.gz"] + + BASE_URL = "http://www.iana.org/time-zones/repository" + + def __init__(self): + super(MakeCommand, self).__init__() + + self.path = None + + def handle(self): + self.path = self.option("path") or self.get_build_dir() + + self.mkdir(self.path) + + self.download() + self.line("") + self.uncompress() + self.line("") + self.build() + self.line("") + self.copy() + self.line("") + self.clean() + self.line("") + self.call("zones:dump") + + def download(self): + if not self.argument("version"): + files = self.FILES + base_url = self.BASE_URL + else: + files = [ + "tzcode{}.tar.gz".format(self.argument("version")), + "tzdata{}.tar.gz".format(self.argument("version")), + ] + base_url = "https://data.iana.org/time-zones/releases" + + self.line("[Downloading archives]") + for filename in files: + url = os.path.join(base_url, filename) + self.write("Downloading {}".format(filename)) + dest = os.path.join(self.path, filename) + with closing(urlopen(url)) as r: + with open(dest, "wb") as f: + shutil.copyfileobj(r, f) + + self.overwrite("Downloaded {} ".format(filename)) + self.line("") + + def uncompress(self): + self.line("[Uncompressing archives]") + dest_path = os.path.join(self.path, "tz") + self.mkdir(dest_path) + + if not self.argument("version"): + files = self.FILES + else: + files = [ + "tzcode{}.tar.gz".format(self.argument("version")), + "tzdata{}.tar.gz".format(self.argument("version")), + ] + + for filename in files: + filepath = os.path.join(self.path, filename) + self.write("Uncompressing {}".format(filename)) + with closing(tarfile.open(filepath)) as f: + f.extractall(dest_path) + + self.overwrite("Uncompressed {} ".format(filename)) + self.line("") + + def build(self): + self.line("[Building tzdata]") + dest_path = os.path.join(self.path, "tz") + + # Getting VERSION + with open(os.path.join(dest_path, "version")) as f: + version = f.read().strip() + + self.write("Building version {}".format(version)) + os.chdir(dest_path) + + with open(os.devnull, "w") as temp: + subprocess.call( + ["make", "TOPDIR={}".format(dest_path), "install"], + stdout=temp, + stderr=temp, + ) + + self.overwrite("Built version {}".format(version)) + self.line("") + + def copy(self): + self.line("[Copying tzdata]") + tzdata_dir = os.path.realpath( + os.path.join(self.path, "tz", "usr", "share", "zoneinfo") + ) + local_dir = os.path.realpath( + os.path.join(os.path.dirname(__file__), "..", "zoneinfo") + ) + self.line( + "Copying {} to {}".format( + tzdata_dir, local_dir + ) + ) + remove_tree(local_dir) + copy_tree(tzdata_dir, local_dir) + + def clean(self): + self.line("[Cleaning up]") + if not self.argument("version"): + files = self.FILES + else: + files = [ + "tzcode{}.tar.gz".format(self.argument("version")), + "tzdata{}.tar.gz".format(self.argument("version")), + ] + + for filename in files: + filepath = os.path.join(self.path, filename) + self.write("Removing {}".format(filename)) + os.remove(filepath) + + self.overwrite("Removed {} ".format(filename)) + self.line("") + + self.write("Removing tz/*") + shutil.rmtree(os.path.join(self.path, "tz")) + self.overwrite("Removed tz/*") + + def get_build_dir(self): + return os.path.join(os.path.dirname(__file__), "..", "..", "_build") + + def mkdir(self, path, mode=0o777): + try: + os.makedirs(path, mode) + except OSError as exc: + if exc.errno == errno.EEXIST and os.path.isdir(path): + pass + else: + raise diff --git a/pytzdata/commands/zones.py b/pytzdata/commands/zones.py new file mode 100644 index 0000000..ef3e33a --- /dev/null +++ b/pytzdata/commands/zones.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- + +import os +import pprint +import pytzdata + +from cleo import Command + + +class ZonesCommand(Command): + """ + Dumps available timezones to the _timezone.py file. + + zones:dump + """ + + TEMPLATE = """# -*- coding: utf-8 -*- + +timezones = {} +""" + + def handle(self): + zones = pytzdata.get_timezones() + + tz_file = os.path.join( + os.path.dirname(__file__), + '..', + '_timezones.py' + ) + + with open(tz_file, 'w') as fd: + fd.write( + self.TEMPLATE.format(pprint.pformat(zones)) + ) + + self.info('Dumped {} timezones'.format(len(zones))) -- cgit v1.2.3