From 8834902c60dea6732b50894d9144236e47954a58 Mon Sep 17 00:00:00 2001 From: Jonathan Lebon Date: Tue, 2 Jul 2019 15:51:59 -0400 Subject: [PATCH] cmdlib: Add builds/ dir helpers Prep for introducing a new `builds/` dir layout. This will allow us to adapt the whole codebase to a new layout at once by enhancing the `Builds` class. The next commit will switch over all the commands to use these helpers. I kept them separate for easier review. --- src/cmdlib.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/cmdlib.sh | 19 +++++++++++++++---- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/cmdlib.py b/src/cmdlib.py index 8e625fe26c..aed528e36b 100755 --- a/src/cmdlib.py +++ b/src/cmdlib.py @@ -170,3 +170,52 @@ def get_basearch(): except AttributeError: get_basearch.saved = RpmOstree.get_basearch() return get_basearch.saved + + +class Builds: + def __init__(self, workdir=None): + self._workdir = workdir + self._fn = self._path("builds/builds.json") + if os.path.isfile(self._fn): + self._data = load_json(self._fn) + elif not os.path.isdir(self._path("builds")): + raise Exception("No builds/ dir found!") + else: + # must be a new workdir + self._data = { + 'builds': [] + } + self.flush() + + def _path(self, path): + if not self._workdir: + return path + return os.path.join(self._workdir, path) + + def has(self, build_id): + return build_id in self._data['builds'] + + def is_empty(self): + return len(self._data['builds']) == 0 + + def get_latest(self): + # just let throw if there are none + return self._data['builds'][0] + + def get_build_dir(self, build_id): + if build_id == 'latest': + build_id = self.get_latest() + return self._path(f"builds/{build_id}") + + def insert_build(self, build_id): + self._data['builds'].insert(0, build_id) + + def bump_timestamp(self): + self._data['timestamp'] = rfc3339_time() + self.flush() + + def raw(self): + return self._data + + def flush(self): + write_json(self._fn, self._data) diff --git a/src/cmdlib.sh b/src/cmdlib.sh index 5906ea533a..6f6997c287 100755 --- a/src/cmdlib.sh +++ b/src/cmdlib.sh @@ -591,16 +591,27 @@ run_virtinstall() { } get_latest_build() { - if [ -L builds/latest ]; then - readlink builds/latest + if [ -L "${workdir:-$(pwd)}/builds/latest" ]; then + readlink "${workdir:-$(pwd)}/builds/latest" fi } +get_build_dir() { + local buildid=$1; shift + # yup, this is happening + (python3 -c " +import sys +sys.path.insert(0, '${DIR}') +from cmdlib import Builds +print(Builds('${workdir:-$(pwd)}').get_build_dir('${buildid}'))") +} + get_latest_qemu() { - local latest + local latest builddir latest=$(get_latest_build) + builddir=$(get_build_dir "$latest") if [ -n "$latest" ]; then # shellcheck disable=SC2086 - ls builds/${latest}/*-qemu.qcow2 + ls ${builddir}/*-qemu.qcow2* fi }