Skip to content

Commit b836426

Browse files
committed
Allow building from local tarballs
This is useful for the case we find ourselves in now — where we want to build from a local copy of the tarball built from a repo with cherry-picked changes.
1 parent 310e0a5 commit b836426

File tree

1 file changed

+59
-4
lines changed

1 file changed

+59
-4
lines changed

update.py

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import re
88
import shutil
99
import subprocess
10+
import sys
1011
import tarfile
1112
import tempfile
1213
import textwrap
@@ -60,6 +61,37 @@ def download_tzdb_tarballs(
6061
return download_locations
6162

6263

64+
def retrieve_local_tarballs(
65+
version: str, source_dir: pathlib.Path, working_dir: pathlib.Path = WORKING_DIR
66+
) -> typing.List[pathlib.Path]:
67+
"""Retrieve the tzdata and tzcode tarballs from a folder.
68+
69+
This is useful when building against a local, patched version of tzdb.
70+
"""
71+
tzdata_file = f"tzdata{version}.tar.gz"
72+
tzcode_file = f"tzcode{version}.tar.gz"
73+
74+
target_dir = working_dir / version / "download"
75+
76+
# mkdir -p target_dir
77+
target_dir.mkdir(parents=True, exist_ok=True)
78+
79+
dest_locations = []
80+
81+
for filename in [tzdata_file, tzcode_file]:
82+
source_location = source_dir / filename
83+
dest_location = target_dir / filename
84+
85+
if dest_location.exists():
86+
logging.info("File %s exists, overwriting", dest_location)
87+
88+
shutil.copy(source_location, dest_location)
89+
90+
dest_locations.append(dest_location)
91+
92+
return dest_locations
93+
94+
6395
def unpack_tzdb_tarballs(download_locations: typing.List[pathlib.Path]) -> pathlib.Path:
6496
assert len(download_locations) == 2
6597
assert download_locations[0].parent == download_locations[1].parent
@@ -381,17 +413,40 @@ def update_news(news_entry: NewsEntry):
381413
@click.option(
382414
"--version", "-v", default=None, help="The version of the tzdata file to download"
383415
)
416+
@click.option(
417+
"--source-dir",
418+
"-s",
419+
default=None,
420+
help="A local source directory containing tarballs (must be used together with --version)",
421+
type=click.Path(
422+
exists=True, file_okay=False, dir_okay=True, path_type=pathlib.Path
423+
), # type: ignore
424+
)
384425
@click.option(
385426
"--news-only/--no-news-only",
386427
help="Flag to disable data updates and only update the news entry",
387428
)
388-
def main(version: str, news_only: bool):
429+
def main(
430+
version: typing.Optional[str],
431+
news_only: bool,
432+
source_dir: typing.Optional[pathlib.Path],
433+
):
389434
logging.basicConfig(level=logging.INFO)
390435

391-
if version is None:
392-
version = find_latest_version()
436+
if source_dir is not None:
437+
if version is None:
438+
logging.error(
439+
"--source-dir specified without --version: "
440+
"If using --source-dir, --version must also be used."
441+
)
442+
sys.exit(-1)
443+
download_locations = retrieve_local_tarballs(version, source_dir)
444+
else:
445+
if version is None:
446+
version = find_latest_version()
447+
448+
download_locations = download_tzdb_tarballs(version)
393449

394-
download_locations = download_tzdb_tarballs(version)
395450
tzdb_location = unpack_tzdb_tarballs(download_locations)
396451

397452
# Update the news entry

0 commit comments

Comments
 (0)