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 <gbeaty@google.com>
Reviewed-by: Gavin Mak <gavinmak@google.com>
changes/25/5246425/5
Garrett Beaty 2 years ago committed by LUCI CQ
parent b808b1bcdd
commit a95979fcc3

@ -52,6 +52,13 @@ wheel: <
version: "version:1.0.9" 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: # Used by:
# my_activity.py # my_activity.py
wheel: < wheel: <

@ -3,9 +3,11 @@
# found in the LICENSE file. # found in the LICENSE file.
"""Generic presubmit checks that can be reused by other presubmit checks.""" """Generic presubmit checks that can be reused by other presubmit checks."""
import datetime
import io as _io import io as _io
import os as _os import os as _os
import time import time
import zoneinfo
import metadata.discover import metadata.discover
import metadata.validate import metadata.validate
@ -890,14 +892,9 @@ def CheckChromiumDependencyMetadata(input_api, output_api, file_filter=None):
_IGNORE_FREEZE_FOOTER = 'Ignore-Freeze' _IGNORE_FREEZE_FOOTER = 'Ignore-Freeze'
# The time module's handling of timezones is abysmal, so the boundaries are _FREEZE_TZ = zoneinfo.ZoneInfo("America/Los_Angeles")
# precomputed in UNIX time _FREEZE_START = datetime.datetime(2023, 12, 15, 0, 0, tzinfo=_FREEZE_TZ)
# TODO: crbug.com/1521396 - Specify the timestamps using datetime and zoneinfo _FREEZE_END = datetime.datetime(2024, 1, 2, 0, 0, tzinfo=_FREEZE_TZ)
# 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
def CheckInfraFreeze(input_api, def CheckInfraFreeze(input_api,
output_api, output_api,
@ -923,7 +920,7 @@ def CheckInfraFreeze(input_api,
will be excluded. will be excluded.
""" """
# Not in the freeze time range # 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: if now < _FREEZE_START or now >= _FREEZE_END:
input_api.logging.info('No freeze is in effect') input_api.logging.info('No freeze is in effect')
return [] return []
@ -951,12 +948,6 @@ def CheckInfraFreeze(input_api,
input_api.logging.info('No affected files are covered by freeze') input_api.logging.info('No affected files are covered by freeze')
return [] 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 # Don't report errors when on the presubmit --all bot or when testing with
# presubmit --files. # presubmit --files.
if input_api.no_diffs: if input_api.no_diffs:
@ -966,8 +957,7 @@ def CheckInfraFreeze(input_api,
return [ return [
report_type('There is a prod infra freeze in effect from {} until {},' report_type('There is a prod infra freeze in effect from {} until {},'
'the following files cannot be modified:\n {}'.format( 'the following files cannot be modified:\n {}'.format(
convert(_FREEZE_START), convert(_FREEZE_END), _FREEZE_START, _FREEZE_END, '\n '.join(files)))
'\n '.join(files)))
] ]

Loading…
Cancel
Save