Skip to content

Commit 3864f6f

Browse files
committed
Collect all builds in bump-timestamp
1 parent fef96f9 commit 3864f6f

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

src/bump-timestamp

+57
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,70 @@
55
'''
66

77
import argparse
8+
import collections
9+
import os
10+
import sys
811

912
from cosalib.builds import Builds
13+
from cosalib.cmdlib import get_timestamp
14+
15+
Build = collections.namedtuple('Build', ['id', 'timestamp', 'basearches'])
1016

1117
parser = argparse.ArgumentParser()
1218
parser.add_argument("--workdir", default='.', help="Path to workdir")
1319
args = parser.parse_args()
1420

1521
builds = Builds(args.workdir)
22+
23+
scanned_builds = []
24+
builds_dir = os.path.join(args.workdir, "builds")
25+
26+
# first, pick up all the builds from the dir itself
27+
with os.scandir(builds_dir) as it:
28+
for entry in it:
29+
# ignore non-dirs
30+
if not entry.is_dir(follow_symlinks=False):
31+
# those are really the only two non-dir things we expect there
32+
if entry.name not in ['builds.json', 'latest']:
33+
print(f"Ignoring non-directory {entry.path}")
34+
continue
35+
36+
# scan all per-arch builds, pick up the most recent build of those as
37+
# the overall "build" timestamp for pruning purposes
38+
with os.scandir(entry.path) as basearch_it:
39+
multiarch_build = None
40+
for basearch_entry in basearch_it:
41+
# ignore non-dirs
42+
if not basearch_entry.is_dir(follow_symlinks=False):
43+
print(f"Ignoring non-directory {basearch_entry.path}")
44+
continue
45+
ts = get_timestamp(basearch_entry)
46+
if not ts:
47+
continue
48+
if not multiarch_build:
49+
multiarch_build = Build(id=entry.name, timestamp=ts,
50+
basearches=[basearch_entry.name])
51+
else:
52+
multiarch_build.basearches += [basearch_entry.name]
53+
multiarch_build.timestamp = max(
54+
multiarch_build.timestamp, ts)
55+
if multiarch_build:
56+
scanned_builds.append(multiarch_build)
57+
58+
# just get the trivial case out of the way
59+
if len(scanned_builds) == 0:
60+
print("No builds found!")
61+
sys.exit(0)
62+
63+
# sort by timestamp, newest first
64+
scanned_builds = sorted(scanned_builds,
65+
key=lambda x: x.timestamp,
66+
reverse=True)
67+
68+
builds.raw()['builds'] = []
69+
for build in reversed(scanned_builds):
70+
for basearch in build.basearches:
71+
builds.insert_build(build.id, basearch)
72+
1673
builds.bump_timestamp()
1774
print("Build timestamp was updated")

src/cosalib/cmdlib.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
gi.require_version("RpmOstree", "1.0")
2424
from gi.repository import RpmOstree
2525

26-
from datetime import datetime
26+
from datetime import datetime, timezone
2727

2828
retry_stop = (stop_after_delay(10) | stop_after_attempt(5))
2929
retry_boto_exception = (retry_if_exception_type(ConnectionClosedError) |
@@ -204,3 +204,33 @@ def get_basearch():
204204
except AttributeError:
205205
get_basearch.saved = RpmOstree.get_basearch()
206206
return get_basearch.saved
207+
208+
209+
def parse_date_string(date_string):
210+
"""
211+
Parses the date strings expected from the build system. Returned
212+
datetime instances will be in utc.
213+
:param date_string: string to turn into date. Format: %Y-%m-%dT%H:%M:%SZ
214+
:type date_string: str
215+
:returns: datetime instance from the date string
216+
:rtype: datetime.datetime
217+
:raises: ValueError, TypeError
218+
"""
219+
dt = datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%SZ')
220+
return dt.replace(tzinfo=timezone.utc)
221+
222+
223+
def get_timestamp(entry):
224+
225+
# ignore dirs missing meta.json
226+
meta_file = os.path.join(entry.path, 'meta.json')
227+
if not os.path.isfile(meta_file):
228+
print(f"Ignoring directory {entry.name}")
229+
return None
230+
231+
# collect dirs and timestamps
232+
with open(meta_file) as f:
233+
j = json.load(f)
234+
# Older versions only had ostree-timestamp
235+
ts = j.get('coreos-assembler.build-timestamp') or j['ostree-timestamp']
236+
return parse_date_string(ts)

0 commit comments

Comments
 (0)