From 4949d34ae01b70e73c3bcf50e1cb4f95a1247944 Mon Sep 17 00:00:00 2001 From: Paul Ganssle Date: Mon, 23 Sep 2024 14:32:54 -0700 Subject: [PATCH] Don't update the repo unless we need to --- update.py | 56 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/update.py b/update.py index 2558607..aa760f3 100644 --- a/update.py +++ b/update.py @@ -186,6 +186,15 @@ def create_package(version: str, zonenames: Sequence[str], zoneinfo_dir: pathlib init_file.touch() +def get_current_package_version() -> str: + with open(PKG_BASE / "tzdata/__init__.py", "rt") as f: + for line in f: + if line.startswith("IANA_VERSION"): + return line.split("=", 1)[1].strip(' "\n') + + raise ValueError("IANA version not found!") + + def find_latest_version() -> str: r = requests.get(IANA_LATEST_LOCATION) fobj = io.BytesIO(r.content) @@ -426,27 +435,54 @@ def update_news(news_entry: NewsEntry): "--news-only/--no-news-only", help="Flag to disable data updates and only update the news entry", ) +@click.option( + "--skip-existing/--no-skip-existing", + default=True, + help="Whether to skip the update if we're already at the current value.", +) def main( version: str | None, news_only: bool, + skip_existing: bool, source_dir: pathlib.Path | None, ): logging.basicConfig(level=logging.INFO) - if source_dir is not None: - if version is None: - logging.error( - "--source-dir specified without --version: " - "If using --source-dir, --version must also be used." + if skip_existing: + existing_version: str | None = get_current_package_version() + else: + existing_version = None + + if version is None or version != existing_version: + if source_dir is not None: + if version is None: + logging.error( + "--source-dir specified without --version: " + "If using --source-dir, --version must also be used." + ) + sys.exit(-1) + download_locations: Sequence[pathlib.Path] | None = retrieve_local_tarballs( + version, source_dir ) - sys.exit(-1) - download_locations = retrieve_local_tarballs(version, source_dir) + else: + if version is None: + version = find_latest_version() + + if version != existing_version or not skip_existing: + download_locations = download_tzdb_tarballs(version) + else: + download_locations = None else: - if version is None: - version = find_latest_version() + download_locations = None - download_locations = download_tzdb_tarballs(version) + if skip_existing and version == existing_version: + logging.info( + f"Selected version {version} is identical " + f"to existing version {existing_version}; nothing to do!" + ) + sys.exit(0) + assert download_locations is not None tzdb_location = unpack_tzdb_tarballs(download_locations) # Update the news entry