Skip to content

Commit 2117781

Browse files
committed
Add auto version detection to update script
1 parent d352844 commit 2117781

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

templates/__init__.py.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
IANA_VERSION = %%IANA_VERSION%%
2+
3+
__version__ = IANA_VERSION

update.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
import io
12
import logging
23
import os
34
import pathlib
5+
import re
46
import shutil
57
import subprocess
8+
import tarfile
69
import tempfile
710
import typing
811

912
import click
1013
import requests
1114

15+
IANA_LATEST_LOCATION = "https://www.iana.org/time-zones/repository/tzdata-latest.tar.gz"
1216
SOURCE = "https://data.iana.org/time-zones/releases"
1317
WORKING_DIR = pathlib.Path("tmp")
1418
REPO_ROOT = pathlib.Path(__file__).parent
@@ -112,13 +116,34 @@ def create_package(version: str, zoneinfo_dir: pathlib.Path):
112116
init_file.touch()
113117

114118

119+
def find_latest_version() -> str:
120+
r = requests.get(IANA_LATEST_LOCATION)
121+
fobj = io.BytesIO(r.content)
122+
with tarfile.open(fileobj=fobj, mode="r:gz") as tf:
123+
vfile = tf.extractfile("version")
124+
version = vfile.read().decode("utf-8").strip()
125+
126+
assert re.match("\d{4}[a-z]$", version), version
127+
128+
target_dir = WORKING_DIR / version / "download"
129+
130+
fobj.seek(0)
131+
with open(f"tzdata{version}.tar.gz", "wb") as f:
132+
f.write(fobj.read())
133+
134+
return version
135+
136+
115137
@click.command()
116138
@click.option(
117139
"--version", "-v", default=None, help="The version of the tzdata file to download"
118140
)
119141
def main(version: str):
120142
logging.basicConfig(level=logging.INFO)
121143

144+
if version is None:
145+
version = find_latest_version()
146+
122147
download_locations = download_tzdb_tarballs(version)
123148
tzdb_location = unpack_tzdb_tarballs(download_locations)
124149

0 commit comments

Comments
 (0)