Skip to content

Commit

Permalink
cmdlib: Add builds/ dir helpers
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jlebon committed Jul 3, 2019
1 parent f583a0d commit 8834902
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
49 changes: 49 additions & 0 deletions src/cmdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
19 changes: 15 additions & 4 deletions src/cmdlib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 8834902

Please sign in to comment.