Bug 1754496 - [taskgraph] Use 'util/time.py' from vendored taskgraph, r=taskgraph-reviewers,aki

Differential Revision: https://phabricator.services.mozilla.com/D138459
This commit is contained in:
Andrew Halberstadt 2022-03-02 16:43:08 +00:00
Родитель 8dd0df41aa
Коммит 8f7d6eaa66
7 изменённых файлов: 6 добавлений и 177 удалений

Просмотреть файл

@ -11,9 +11,9 @@ import logging
from slugid import nice as slugid
from taskgraph.util.taskcluster import get_session, CONCURRENCY
from taskgraph.util.time import current_json_time
from gecko_taskgraph.util.parameterization import resolve_timestamps
from gecko_taskgraph.util.time import current_json_time
logger = logging.getLogger(__name__)

Просмотреть файл

@ -29,6 +29,5 @@ subsuite = taskgraph
[test_util_runnable_jobs.py]
[test_util_schema.py]
[test_util_templates.py]
[test_util_time.py]
[test_util_treeherder.py]
[test_util_verify.py]

Просмотреть файл

@ -9,8 +9,8 @@ import slugid
import unittest
from mozunit import main
from taskgraph.util.yaml import load_yaml
from taskgraph.util.time import current_json_time
from gecko_taskgraph.util.time import current_json_time
from gecko_taskgraph import GECKO

Просмотреть файл

@ -1,54 +0,0 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import unittest
import mozunit
from datetime import datetime
from gecko_taskgraph.util.time import (
InvalidString,
UnknownTimeMeasurement,
value_of,
json_time_from_now,
)
class FromNowTest(unittest.TestCase):
def test_invalid_str(self):
with self.assertRaises(InvalidString):
value_of("wtfs")
def test_missing_unit(self):
with self.assertRaises(InvalidString):
value_of("1")
def test_missing_unknown_unit(self):
with self.assertRaises(UnknownTimeMeasurement):
value_of("1z")
def test_value_of(self):
self.assertEqual(value_of("1s").total_seconds(), 1)
self.assertEqual(value_of("1 second").total_seconds(), 1)
self.assertEqual(value_of("1min").total_seconds(), 60)
self.assertEqual(value_of("1h").total_seconds(), 3600)
self.assertEqual(value_of("1d").total_seconds(), 86400)
self.assertEqual(value_of("1mo").total_seconds(), 2592000)
self.assertEqual(value_of("1 month").total_seconds(), 2592000)
self.assertEqual(value_of("1y").total_seconds(), 31536000)
with self.assertRaises(UnknownTimeMeasurement):
value_of("1m").total_seconds() # ambiguous between minute and month
def test_json_from_now_utc_now(self):
# Just here to ensure we don't raise.
json_time_from_now("1 years")
def test_json_from_now(self):
now = datetime(2014, 1, 1)
self.assertEqual(json_time_from_now("1 years", now), "2015-01-01T00:00:00.000Z")
self.assertEqual(json_time_from_now("6 days", now), "2014-01-07T00:00:00.000Z")
if __name__ == "__main__":
mozunit.main()

Просмотреть файл

@ -16,8 +16,10 @@ import time
from copy import deepcopy
import attr
from mozbuild.util import memoize
from taskgraph.util.time import value_of
from voluptuous import Any, Required, Optional, Extra, Match, All, NotIn
from gecko_taskgraph.util.attributes import TRUNK_PROJECTS, is_try, release_level
from gecko_taskgraph.util.hash import hash_path
from gecko_taskgraph.util.treeherder import split_symbol
@ -37,9 +39,7 @@ from gecko_taskgraph.util.scriptworker import (
get_release_config,
)
from gecko_taskgraph.util.signed_artifacts import get_signed_artifacts
from gecko_taskgraph.util.time import value_of
from gecko_taskgraph.util.workertypes import worker_type_implementation
from voluptuous import Any, Required, Optional, Extra, Match, All, NotIn
from gecko_taskgraph import GECKO, MAX_DEPENDENCIES
from ..util import docker as dockerutil
from ..util.workertypes import get_worker_type

Просмотреть файл

@ -6,8 +6,7 @@
import re
from taskgraph.util.taskcluster import get_artifact_url
from gecko_taskgraph.util.time import json_time_from_now
from taskgraph.util.time import json_time_from_now
TASK_REFERENCE_PATTERN = re.compile("<([^>]+)>")
ARTIFACT_REFERENCE_PATTERN = re.compile("<([^/]+)/([^>]+)>")

Просмотреть файл

@ -1,115 +0,0 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# Python port of the ms.js node module this is not a direct port some things are
# more complicated or less precise and we lean on time delta here.
import re
import datetime
PATTERN = re.compile(r"((?:\d+)?\.?\d+) *([a-z]+)")
def seconds(value):
return datetime.timedelta(seconds=int(value))
def minutes(value):
return datetime.timedelta(minutes=int(value))
def hours(value):
return datetime.timedelta(hours=int(value))
def days(value):
return datetime.timedelta(days=int(value))
def months(value):
# See warning in years(), below
return datetime.timedelta(days=int(value) * 30)
def years(value):
# Warning here "years" are vague don't use this for really sensitive date
# computation the idea is to give you a absolute amount of time in the
# future which is not the same thing as "precisely on this date next year"
return datetime.timedelta(days=int(value) * 365)
ALIASES = {}
ALIASES["seconds"] = ALIASES["second"] = ALIASES["s"] = seconds
ALIASES["minutes"] = ALIASES["minute"] = ALIASES["min"] = minutes
ALIASES["hours"] = ALIASES["hour"] = ALIASES["h"] = hours
ALIASES["days"] = ALIASES["day"] = ALIASES["d"] = days
ALIASES["months"] = ALIASES["month"] = ALIASES["mo"] = months
ALIASES["years"] = ALIASES["year"] = ALIASES["y"] = years
class InvalidString(Exception):
pass
class UnknownTimeMeasurement(Exception):
pass
def value_of(input_str):
"""
Convert a string to a json date in the future
:param str input_str: (ex: 1d, 2d, 6years, 2 seconds)
:returns: Unit given in seconds
"""
matches = PATTERN.search(input_str)
if matches is None or len(matches.groups()) < 2:
raise InvalidString(f"'{input_str}' is invalid string")
value, unit = matches.groups()
if unit not in ALIASES:
raise UnknownTimeMeasurement(
"{} is not a valid time measure use one of {}".format(
unit, sorted(ALIASES.keys())
)
)
return ALIASES[unit](value)
def json_time_from_now(input_str, now=None, datetime_format=False):
"""
:param str input_str: Input string (see value of)
:param datetime now: Optionally set the definition of `now`
:param boolean datetime_format: Set `True` to get a `datetime` output
:returns: JSON string representation of time in future.
"""
if now is None:
now = datetime.datetime.utcnow()
time = now + value_of(input_str)
if datetime_format is True:
return time
else:
# Sorta a big hack but the json schema validator for date does not like the
# ISO dates until 'Z' (for timezone) is added...
# Microseconds are excluded (see bug 1381801)
return time.isoformat(timespec="milliseconds") + "Z"
def current_json_time(datetime_format=False):
"""
:param boolean datetime_format: Set `True` to get a `datetime` output
:returns: JSON string representation of the current time.
"""
if datetime_format is True:
return datetime.datetime.utcnow()
else:
# Microseconds are excluded (see bug 1381801)
return datetime.datetime.utcnow().isoformat(timespec="milliseconds") + "Z"