blob: ef847de71a8ec448afb2bfd9450b51c8a312e217 [file] [edit]
#!/usr/bin/env python3
# Copyright 2022 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import argparse
import datetime
import json
import os
import urllib.request
_COMMITS_URL = ('https://api.github.com/repos/catapult-project/catapult/commits'
'?per_page=1&until={}')
_ARCHIVE_URL = 'https://github.com/catapult-project/catapult/archive/{}.tar.gz'
def get_last_commit_of_the_week():
"""Returns data of last commit until the end of last week."""
# Get a datetime object representing today ignoring time of day.
# Use UTC to avoid potential date difference due to timezones.
utc_now = datetime.datetime.now(datetime.timezone.utc)
utc_today = datetime.datetime.combine(utc_now.date(), datetime.time.min)
# Find number of days since last monday.
days_since_week_start = utc_today.weekday()
# Get time at the start of this week (start of Monday of this week).
end_of_last_week = utc_today - datetime.timedelta(days=days_since_week_start)
url = _COMMITS_URL.format(end_of_last_week.isoformat())
# Search for the last commit until the start of this week.
return json.load(urllib.request.urlopen(url))[0]
def do_latest():
print(get_last_commit_of_the_week()['sha'])
def get_download_url(sha):
partial_manifest = {
'url': [_ARCHIVE_URL.format(sha)],
'ext': '.tar.gz',
}
print(json.dumps(partial_manifest))
def main():
ap = argparse.ArgumentParser()
sub = ap.add_subparsers(dest='action', required=True)
latest = sub.add_parser("latest")
latest.set_defaults(func=do_latest)
download = sub.add_parser("get_url")
download.set_defaults(
func=lambda: get_download_url(os.environ['_3PP_VERSION']))
opts = ap.parse_args()
opts.func()
if __name__ == '__main__':
main()