From 7e75b82444bd097b1562b27087c5aef4f7b82f96 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 19 Aug 2019 20:34:09 +0000 Subject: [PATCH 1/3] Add cmd-koji-upload to pysources And fix a line length issue (and also use `stat()` directly rather than via a subprocess). Prep for *also* adding Python2 checking to this file. --- Makefile | 2 +- src/cmd-koji-upload | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index a80b23db37..14c45bb64d 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ DESTDIR ?= # E722 do not use bare 'except' PYIGNORE ?= E402,E722 -pysources = src/cosalib src/oscontainer.py src/cmd-kola +pysources = src/cosalib src/oscontainer.py src/cmd-kola src/cmd-koji-upload .PHONY: all check flake8 unittest clean mantle install diff --git a/src/cmd-koji-upload b/src/cmd-koji-upload index 4944c5ce01..18fa908910 100755 --- a/src/cmd-koji-upload +++ b/src/cmd-koji-upload @@ -279,7 +279,7 @@ class Build(_Build): continue # os.path.getsize uses 1kB instead of 1KB. So we use stat instead. - fsize = subprocess.check_output(["stat", "--dereference", "--format", '%s', lpath]) + fsize = '{}'.format(os.stat(lpath).st_size) log.debug(" * calculating checksum") self._found_files[lpath] = { "local_path": lpath, From 735b59b7d0aa0c4fe18345efdb2597117d0d64c6 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 19 Aug 2019 20:44:43 +0000 Subject: [PATCH 2/3] cmdlib.py: Make dual python2/3 compatible Python3 was such a bad idea...but anyways we are being dragged back into it by Koji. Change our `cmdlib.py` to at least "compile" (syntax check) with Python2. --- Makefile | 8 ++++++-- src/cmdlib.py | 20 ++++++++++---------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 14c45bb64d..7bcc005dbd 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,8 @@ DESTDIR ?= # E722 do not use bare 'except' PYIGNORE ?= E402,E722 -pysources = src/cosalib src/oscontainer.py src/cmd-kola src/cmd-koji-upload +pysources = src/cosalib src/oscontainer.py src/cmd-kola $(py2sources) +py2sources = src/cmdlib.py src/cmd-koji-upload .PHONY: all check flake8 unittest clean mantle install @@ -20,7 +21,7 @@ cwd_checked:=$(patsubst ./%,.%.shellchecked,${cwd}) .%.shellchecked: % ./tests/check_one.sh $< $@ -check: ${src_checked} ${tests_checked} ${cwd_checked} flake8 +check: ${src_checked} ${tests_checked} ${cwd_checked} flake8 py2syntax echo OK flake8: @@ -30,6 +31,9 @@ flake8: # grep -r "^\#\!/usr/bin/py" src/ | cut -d : -f 1 | xargs flake8 --ignore=$(PYIGNORE) # find src -maxdepth 1 -name "*.py" | xargs flake8 --ignore=$(PYIGNORE) +py2syntax: + python -m py_compile $(py2sources) + unittest: PYTHONPATH=`pwd`/src python3 -m pytest tests/ diff --git a/src/cmdlib.py b/src/cmdlib.py index c3927997db..5e08aeacb2 100755 --- a/src/cmdlib.py +++ b/src/cmdlib.py @@ -101,7 +101,7 @@ def fatal(msg): :type msg: str :raises: SystemExit """ - print('fatal: {}'.format(msg), file=sys.stderr) + sys.stderr.write('fatal: {}\n'.format(msg)) raise SystemExit(1) @@ -112,7 +112,7 @@ def info(msg): :param msg: The message to show to output :type msg: str """ - print('info: {}'.format(msg), file=sys.stderr) + print('info: {}'.format(msg)) def rfc3339_time(t=None): @@ -166,14 +166,14 @@ def import_ostree_commit(repo, commit, tarfile): # in the common case where we're operating on a recent build, the OSTree # commit should already be in the tmprepo - commitpartial = os.path.join(repo, f'state/{commit}.commitpartial') - if (subprocess.call(['ostree', 'show', '--repo', repo, commit], - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL) == 0 - and not os.path.isfile(commitpartial)): + commitpartial = os.path.join(repo, 'state/{}.commitpartial'.format(commit)) + have_commit = subprocess.call(['ostree', 'show', '--repo', repo, commit], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL) == 0 + if have_commit and not os.path.isfile(commitpartial): return - with tempfile.TemporaryDirectory(dir=f'{repo}/tmp') as d: + with tempfile.TemporaryDirectory(dir='{}/tmp'.format(repo)) as d: subprocess.check_call(['tar', '-C', d, '-xf', tarfile]) subprocess.check_call(['ostree', 'pull-local', '--repo', repo, d, commit]) @@ -242,11 +242,11 @@ def get_build_dir(self, build_id, basearch=None): if build_id == 'latest': build_id = self.get_latest() if self._legacy: - return self._path(f"builds/{build_id}") + return self._path("builds/" + build_id) if not basearch: # just assume caller wants build dir for current arch basearch = get_basearch() - return self._path(f"builds/{build_id}/{basearch}") + return self._path("builds/{}/{}".format(build_id, basearch)) def insert_build(self, build_id, basearch=None): if self._legacy: From f6be5ac5a3333737108cd70e86904eec46bc167b Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 19 Aug 2019 20:50:46 +0000 Subject: [PATCH 3/3] cmdlib: Import non-stdlib modules on demand More work to deal with koji being Python 2 only. --- src/cmdlib.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/cmdlib.py b/src/cmdlib.py index 5e08aeacb2..10bf8b920a 100755 --- a/src/cmdlib.py +++ b/src/cmdlib.py @@ -9,11 +9,6 @@ import subprocess import sys import tempfile -import gi -import semver - -gi.require_version("RpmOstree", "1.0") -from gi.repository import RpmOstree from datetime import datetime @@ -183,6 +178,9 @@ def get_basearch(): try: return get_basearch.saved except AttributeError: + import gi + gi.require_version("RpmOstree", "1.0") + from gi.repository import RpmOstree get_basearch.saved = RpmOstree.get_basearch() return get_basearch.saved @@ -190,6 +188,7 @@ def get_basearch(): # FIXME: Add tests class Builds: # pragma: nocover def __init__(self, workdir=None): + import semver self._workdir = workdir self._fn = self._path("builds/builds.json") if not os.path.isdir(self._path("builds")):