From a95979fcc3a8e7c42086b8dd804bd143ce666c9c Mon Sep 17 00:00:00 2001 From: Garrett Beaty Date: Tue, 30 Jan 2024 18:43:43 +0000 Subject: [PATCH] Update the CheckInfraFreeze canned check to use datetime & zoneinfo. The current implementation of CheckInfraFreeze relies on timestamps being preconverted to UNIX timestamps because zoneinfo doesn't work by default on Windows. The necessary tzdata package has been added so that zoneinfo will work for Windows, so switching to datetime and zoneinfo is now possible and makes it easier to update the time range for freezes. Bug: 1521396 Change-Id: I8db111f05db2e79f4fffd9da3829b9071d12163f Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5246425 Commit-Queue: Garrett Beaty Reviewed-by: Gavin Mak --- .vpython3 | 7 +++++++ presubmit_canned_checks.py | 24 +++++++----------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/.vpython3 b/.vpython3 index ee587c6ea..c1bd7f630 100644 --- a/.vpython3 +++ b/.vpython3 @@ -52,6 +52,13 @@ wheel: < version: "version:1.0.9" > +# Used by: +# presubmit_canned_checks.py (via zoneinfo) +wheel: < + name: "infra/python/wheels/tzdata-py2_py3" + version: "version:2023.4" +> + # Used by: # my_activity.py wheel: < diff --git a/presubmit_canned_checks.py b/presubmit_canned_checks.py index 5460adde0..f3cc6f6ab 100644 --- a/presubmit_canned_checks.py +++ b/presubmit_canned_checks.py @@ -3,9 +3,11 @@ # found in the LICENSE file. """Generic presubmit checks that can be reused by other presubmit checks.""" +import datetime import io as _io import os as _os import time +import zoneinfo import metadata.discover import metadata.validate @@ -890,14 +892,9 @@ def CheckChromiumDependencyMetadata(input_api, output_api, file_filter=None): _IGNORE_FREEZE_FOOTER = 'Ignore-Freeze' -# The time module's handling of timezones is abysmal, so the boundaries are -# precomputed in UNIX time -# TODO: crbug.com/1521396 - Specify the timestamps using datetime and zoneinfo -# once the tzdata package is available as a dependency for Windows (Windows -# doesn't have a timezone database by default, so zoneinfo doesn't work) -_FREEZE_START = 1702627200 # 2023/12/15 00:00 -0800 -_FREEZE_END = 1704182400 # 2024/01/02 00:00 -0800 - +_FREEZE_TZ = zoneinfo.ZoneInfo("America/Los_Angeles") +_FREEZE_START = datetime.datetime(2023, 12, 15, 0, 0, tzinfo=_FREEZE_TZ) +_FREEZE_END = datetime.datetime(2024, 1, 2, 0, 0, tzinfo=_FREEZE_TZ) def CheckInfraFreeze(input_api, output_api, @@ -923,7 +920,7 @@ def CheckInfraFreeze(input_api, will be excluded. """ # Not in the freeze time range - now = input_api.time.time() + now = datetime.datetime.now(_FREEZE_TZ) if now < _FREEZE_START or now >= _FREEZE_END: input_api.logging.info('No freeze is in effect') return [] @@ -951,12 +948,6 @@ def CheckInfraFreeze(input_api, input_api.logging.info('No affected files are covered by freeze') return [] - # TODO: crbug.com/1521396 - When _FREEZE_START and _FREEZE_END are datetime - # objects, they can be printed directly and this function can be removed - def convert(t): - ts = input_api.time.localtime(t) - return input_api.time.strftime('%Y/%m/%d %H:%M %z', ts) - # Don't report errors when on the presubmit --all bot or when testing with # presubmit --files. if input_api.no_diffs: @@ -966,8 +957,7 @@ def CheckInfraFreeze(input_api, return [ report_type('There is a prod infra freeze in effect from {} until {},' 'the following files cannot be modified:\n {}'.format( - convert(_FREEZE_START), convert(_FREEZE_END), - '\n '.join(files))) + _FREEZE_START, _FREEZE_END, '\n '.join(files))) ]