|
7 | 7 | import re |
8 | 8 | import shutil |
9 | 9 | import subprocess |
| 10 | +import sys |
10 | 11 | import tarfile |
11 | 12 | import tempfile |
12 | 13 | import textwrap |
@@ -60,6 +61,37 @@ def download_tzdb_tarballs( |
60 | 61 | return download_locations |
61 | 62 |
|
62 | 63 |
|
| 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 | + |
63 | 95 | def unpack_tzdb_tarballs(download_locations: typing.List[pathlib.Path]) -> pathlib.Path: |
64 | 96 | assert len(download_locations) == 2 |
65 | 97 | assert download_locations[0].parent == download_locations[1].parent |
@@ -381,17 +413,40 @@ def update_news(news_entry: NewsEntry): |
381 | 413 | @click.option( |
382 | 414 | "--version", "-v", default=None, help="The version of the tzdata file to download" |
383 | 415 | ) |
| 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 | +) |
384 | 425 | @click.option( |
385 | 426 | "--news-only/--no-news-only", |
386 | 427 | help="Flag to disable data updates and only update the news entry", |
387 | 428 | ) |
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 | +): |
389 | 434 | logging.basicConfig(level=logging.INFO) |
390 | 435 |
|
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) |
393 | 449 |
|
394 | | - download_locations = download_tzdb_tarballs(version) |
395 | 450 | tzdb_location = unpack_tzdb_tarballs(download_locations) |
396 | 451 |
|
397 | 452 | # Update the news entry |
|
0 commit comments