Skip to content

Commit 3b721cd

Browse files
committed
Don't update the repo unless we need to
1 parent d913020 commit 3b721cd

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

Diff for: update.py

+46-10
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ def create_package(version: str, zonenames: Sequence[str], zoneinfo_dir: pathlib
186186
init_file.touch()
187187

188188

189+
def get_current_package_version() -> str:
190+
with open(PKG_BASE / "tzdata/__init__.py", "rt") as f:
191+
for line in f:
192+
if line.startswith("IANA_VERSION"):
193+
return line.split("=", 1)[1].strip(' "\n')
194+
195+
raise ValueError("IANA version not found!")
196+
197+
189198
def find_latest_version() -> str:
190199
r = requests.get(IANA_LATEST_LOCATION)
191200
fobj = io.BytesIO(r.content)
@@ -426,27 +435,54 @@ def update_news(news_entry: NewsEntry):
426435
"--news-only/--no-news-only",
427436
help="Flag to disable data updates and only update the news entry",
428437
)
438+
@click.option(
439+
"--skip-existing/--no-skip-existing",
440+
default=True,
441+
help="Whether to skip the update if we're already at the current value.",
442+
)
429443
def main(
430444
version: str | None,
431445
news_only: bool,
446+
skip_existing: bool,
432447
source_dir: pathlib.Path | None,
433448
):
434449
logging.basicConfig(level=logging.INFO)
435450

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."
451+
if skip_existing:
452+
existing_version: str | None = get_current_package_version()
453+
else:
454+
existing_version = None
455+
456+
if version is None or version != existing_version:
457+
if source_dir is not None:
458+
if version is None:
459+
logging.error(
460+
"--source-dir specified without --version: "
461+
"If using --source-dir, --version must also be used."
462+
)
463+
sys.exit(-1)
464+
download_locations: Sequence[pathlib.Path] | None = retrieve_local_tarballs(
465+
version, source_dir
441466
)
442-
sys.exit(-1)
443-
download_locations = retrieve_local_tarballs(version, source_dir)
467+
else:
468+
if version is None:
469+
version = find_latest_version()
470+
471+
if version != existing_version or not skip_existing:
472+
download_locations = download_tzdb_tarballs(version)
473+
else:
474+
download_locations = None
444475
else:
445-
if version is None:
446-
version = find_latest_version()
476+
download_locations = None
447477

448-
download_locations = download_tzdb_tarballs(version)
478+
if skip_existing and version == existing_version:
479+
logging.info(
480+
f"Selected version {version} is identical "
481+
f"to existing version {existing_version}; nothing to do!"
482+
)
483+
sys.exit(0)
449484

485+
assert download_locations is not None
450486
tzdb_location = unpack_tzdb_tarballs(download_locations)
451487

452488
# Update the news entry

0 commit comments

Comments
 (0)