summaryrefslogtreecommitdiffstats
path: root/pytzdata/commands/make.py
blob: e0a7b4b876ee767a50dbcb69e660ed0197e9ad65 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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("[<comment>Downloading archives</>]")
        for filename in files:
            url = os.path.join(base_url, filename)
            self.write("<comment>Downloading</> <fg=cyan>{}</>".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("<info>Downloaded</> <fg=cyan>{}</> ".format(filename))
            self.line("")

    def uncompress(self):
        self.line("[<comment>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("<comment>Uncompressing</> <fg=cyan>{}</>".format(filename))
            with closing(tarfile.open(filepath)) as f:
                f.extractall(dest_path)

            self.overwrite("<info>Uncompressed</> <fg=cyan>{}</> ".format(filename))
            self.line("")

    def build(self):
        self.line("[<comment>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("<comment>Building</> version <fg=cyan>{}</>".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("<info>Built</> version <fg=cyan>{}</>".format(version))
        self.line("")

    def copy(self):
        self.line("[<comment>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(
            "<comment>Copying <fg=cyan>{}</> to <fg=cyan>{}</></>".format(
                tzdata_dir, local_dir
            )
        )
        remove_tree(local_dir)
        copy_tree(tzdata_dir, local_dir)

    def clean(self):
        self.line("[<comment>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("<comment>Removing</> <fg=cyan>{}</>".format(filename))
            os.remove(filepath)

            self.overwrite("<info>Removed</> <fg=cyan>{}</> ".format(filename))
            self.line("")

        self.write("<comment>Removing</> <fg=cyan>tz/*</>")
        shutil.rmtree(os.path.join(self.path, "tz"))
        self.overwrite("<info>Removed</> <fg=cyan>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