Add pyproject.toml to set line length for black

... and rerun black.

Signed-off-by: Nils Philippsen <nils@redhat.com>
This commit is contained in:
Nils Philippsen 2020-02-21 18:21:19 +01:00
parent 59f651a397
commit 6803b2b8c7
4 changed files with 42 additions and 68 deletions

View file

@ -21,9 +21,7 @@ def run_command(command, cwd=None):
try: try:
output = subprocess.check_output(command, cwd=cwd, stderr=subprocess.PIPE) output = subprocess.check_output(command, cwd=cwd, stderr=subprocess.PIPE)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
_log.error( _log.error("Command `{}` return code: `{}`".format(" ".join(command), e.returncode))
"Command `{}` return code: `{}`".format(" ".join(command), e.returncode)
)
_log.error("stdout:\n-------\n{}".format(e.stdout)) _log.error("stdout:\n-------\n{}".format(e.stdout))
_log.error("stderr:\n-------\n{}".format(e.stderr)) _log.error("stderr:\n-------\n{}".format(e.stderr))
raise Exception("Command failed to run") raise Exception("Command failed to run")
@ -53,8 +51,7 @@ def main(args):
break break
repo_obj.checkout_tree( repo_obj.checkout_tree(
commit, commit, strategy=pygit2.GIT_CHECKOUT_FORCE | pygit2.GIT_CHECKOUT_RECREATE_MISSING,
strategy=pygit2.GIT_CHECKOUT_FORCE | pygit2.GIT_CHECKOUT_RECREATE_MISSING,
) )
if os.path.exists(os.path.join(args[0], f"{name}.spec")): if os.path.exists(os.path.join(args[0], f"{name}.spec")):
try: try:
@ -71,11 +68,7 @@ def main(args):
except Exception: except Exception:
continue continue
output = tuple( output = tuple(
output.decode("utf-8") output.decode("utf-8").strip().split("\n")[0].rsplit(".", 1)[0].split(" ")
.strip()
.split("\n")[0]
.rsplit(".", 1)[0]
.split(" ")
) )
nvr = "-".join(output) nvr = "-".join(output)

View file

@ -23,9 +23,7 @@ _evr_re = re.compile(r"^(?:(?P<epoch>\d+):)?(?P<version>[^-:]+)(?:-(?P<release>[
def get_cli_arguments(args): def get_cli_arguments(args):
""" Set the CLI argument parser and return the argument parsed. """ Set the CLI argument parser and return the argument parsed.
""" """
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(description="Script to determine the next NVR of a build")
description="Script to determine the next NVR of a build"
)
parser.add_argument( parser.add_argument(
"--koji-url", "--koji-url",
help="The base URL of the Koji hub", help="The base URL of the Koji hub",
@ -41,10 +39,7 @@ def get_cli_arguments(args):
parser.add_argument("package", help="The name of the package of interest") parser.add_argument("package", help="The name of the package of interest")
parser.add_argument("dist", help="The dist-tag of interest") parser.add_argument("dist", help="The dist-tag of interest")
parser.add_argument( parser.add_argument(
"evr", "evr", help="The [epoch:]version[-release] of the package", nargs="?", type=parse_evr,
help="The [epoch:]version[-release] of the package",
nargs="?",
type=parse_evr,
) )
return parser.parse_args(args) return parser.parse_args(args)
@ -56,10 +51,10 @@ def parse_evr(evr_str):
if not match: if not match:
raise ValueError(str) raise ValueError(str)
epoch = match.group('epoch') or 0 epoch = match.group("epoch") or 0
epoch = int(epoch) epoch = int(epoch)
return epoch, match.group('version'), match.group('release') return epoch, match.group("version"), match.group("release")
def parse_release_tag(tag): def parse_release_tag(tag):
@ -96,21 +91,19 @@ def main_sequential_builds_algo(args, client, pkgid):
n_builds = max([pkgrel + 1, n_builds]) n_builds = max([pkgrel + 1, n_builds])
except TypeError: except TypeError:
pass pass
print( print(f"Next build: {last_build['name']}-{last_build['version']}-{n_builds}.{args.dist}")
f"Next build: {last_build['name']}-{last_build['version']}-{n_builds}.{args.dist}"
)
_rpmvercmp_key = cmp_to_key(lambda b1, b2: rpm.labelCompare( _rpmvercmp_key = cmp_to_key(
lambda b1, b2: rpm.labelCompare(
(str(b1["epoch"]), b1["version"], b1["release"]), (str(b1["epoch"]), b1["version"], b1["release"]),
(str(b2["epoch"]), b2["version"], b2["release"]), (str(b2["epoch"]), b2["version"], b2["release"]),
)) )
)
def holistic_heuristic_calculate_release( def holistic_heuristic_calculate_release(
args: argparse.Namespace, args: argparse.Namespace, lower_bound: dict, higher_bound: typing.Optional[dict],
lower_bound: dict,
higher_bound: typing.Optional[dict],
): ):
dist = args.dist dist = args.dist
@ -119,29 +112,29 @@ def holistic_heuristic_calculate_release(
epoch, version, release = args.evr epoch, version, release = args.evr
except TypeError: except TypeError:
epoch, version, release = ( epoch, version, release = (
lower_bound['epoch'], lower_bound["epoch"],
lower_bound['version'], lower_bound["version"],
lower_bound['release'], lower_bound["release"],
) )
new_evr = {'epoch': epoch, 'version': version, 'release': release} new_evr = {"epoch": epoch, "version": version, "release": release}
if _rpmvercmp_key(new_evr) > _rpmvercmp_key(lower_bound): if _rpmvercmp_key(new_evr) > _rpmvercmp_key(lower_bound):
lower_bound = new_evr lower_bound = new_evr
if not release: if not release:
lower_bound['release'] = f"1.{dist}" lower_bound["release"] = f"1.{dist}"
lpkgrel, _, lminorbump = parse_release_tag(lower_bound['release']) lpkgrel, _, lminorbump = parse_release_tag(lower_bound["release"])
# If the higher bound has the same version, bump its release to give it enough wiggle-room for # If the higher bound has the same version, bump its release to give it enough wiggle-room for
# parallel builds of the (almost) same EVRs against several Fedora versions. # parallel builds of the (almost) same EVRs against several Fedora versions.
if higher_bound and higher_bound['version'] == version: if higher_bound and higher_bound["version"] == version:
higher_bound = higher_bound.copy() higher_bound = higher_bound.copy()
hbpkgrel, hbmiddle, _ = parse_release_tag(higher_bound['release']) hbpkgrel, hbmiddle, _ = parse_release_tag(higher_bound["release"])
higher_bound['release'] = f"{hbpkgrel + 1}{hbmiddle}" higher_bound["release"] = f"{hbpkgrel + 1}{hbmiddle}"
# Bump the left-most release number and check that it doesn't violate the higher bound, if it # Bump the left-most release number and check that it doesn't violate the higher bound, if it
# exists. # exists.
new_evr['release'] = f"{lpkgrel + 1}.{dist}" new_evr["release"] = f"{lpkgrel + 1}.{dist}"
if not higher_bound or _rpmvercmp_key(new_evr) < _rpmvercmp_key(higher_bound): if not higher_bound or _rpmvercmp_key(new_evr) < _rpmvercmp_key(higher_bound):
# No (satisfiable) higher bound exists or it has a higher epoch-version-release. # No (satisfiable) higher bound exists or it has a higher epoch-version-release.
@ -152,19 +145,19 @@ def holistic_heuristic_calculate_release(
else: else:
nminorbump = 1 nminorbump = 1
new_evr['release'] = rel_bak = f"{lpkgrel}.{dist}.{nminorbump}" new_evr["release"] = rel_bak = f"{lpkgrel}.{dist}.{nminorbump}"
if _rpmvercmp_key(new_evr) < _rpmvercmp_key(higher_bound): if _rpmvercmp_key(new_evr) < _rpmvercmp_key(higher_bound):
return new_evr return new_evr
# Oops. Attempt appending '.1' to the minor bump, ... # Oops. Attempt appending '.1' to the minor bump, ...
new_evr['release'] += ".1" new_evr["release"] += ".1"
if _rpmvercmp_key(new_evr) < _rpmvercmp_key(higher_bound): if _rpmvercmp_key(new_evr) < _rpmvercmp_key(higher_bound):
return new_evr return new_evr
# ... otherwise don't bother. # ... otherwise don't bother.
new_evr['release'] = rel_bak new_evr["release"] = rel_bak
return new_evr return new_evr
@ -183,22 +176,20 @@ def main_holistic_heuristic_algo(args, client, pkgid):
dtag_re = re.compile(fr"\.{distcode}(?P<distver>\d+)") dtag_re = re.compile(fr"\.{distcode}(?P<distver>\d+)")
builds = [ builds = [
build build for build in client.listBuilds(pkgid, type="rpm") if dtag_re.search(build["release"])
for build in client.listBuilds(pkgid, type="rpm")
if dtag_re.search(build['release'])
] ]
# builds by distro release # builds by distro release
builds_per_distver = defaultdict(list) builds_per_distver = defaultdict(list)
for build in client.listBuilds(pkgid, type="rpm"): for build in client.listBuilds(pkgid, type="rpm"):
match = dtag_re.search(build['release']) match = dtag_re.search(build["release"])
if not match: if not match:
# ignore builds for other distro types (e.g. Fedora vs. EPEL), or modular builds # ignore builds for other distro types (e.g. Fedora vs. EPEL), or modular builds
continue continue
distver = int(match.group('distver')) distver = int(match.group("distver"))
builds_per_distver[distver].append(build) builds_per_distver[distver].append(build)
if not builds_per_distver: if not builds_per_distver:
@ -221,7 +212,7 @@ def main_holistic_heuristic_algo(args, client, pkgid):
# Lower bound: the RPM-wise "highest" build which this release has to exceed. # Lower bound: the RPM-wise "highest" build which this release has to exceed.
lower_bound = lower_bound_builds[0] lower_bound = lower_bound_builds[0]
lower_bound_nvr = lower_bound['nvr'] lower_bound_nvr = lower_bound["nvr"]
# All builds that should be 'higher' than what we are targetting, i.e. the highest build of each # All builds that should be 'higher' than what we are targetting, i.e. the highest build of each
# newer release. We aim at a new release which is lower than every one of them, but if this # newer release. We aim at a new release which is lower than every one of them, but if this
@ -230,7 +221,7 @@ def main_holistic_heuristic_algo(args, client, pkgid):
(builds[0] for dver, builds in builds_per_distver.items() if dver > pkgdistver), (builds[0] for dver, builds in builds_per_distver.items() if dver > pkgdistver),
key=_rpmvercmp_key, key=_rpmvercmp_key,
) )
higher_bound_builds_nvr = [b['nvr'] for b in higher_bound_builds] higher_bound_builds_nvr = [b["nvr"] for b in higher_bound_builds]
print(f"Highest build of lower or current distro versions: {lower_bound_nvr}") print(f"Highest build of lower or current distro versions: {lower_bound_nvr}")
print(f"Highest builds of higher distro versions: {', '.join(higher_bound_builds_nvr)}") print(f"Highest builds of higher distro versions: {', '.join(higher_bound_builds_nvr)}")
@ -247,14 +238,14 @@ def main_holistic_heuristic_algo(args, client, pkgid):
if satisfiable_higher_bound_builds: if satisfiable_higher_bound_builds:
# Find the higher bound which we can stay below. # Find the higher bound which we can stay below.
higher_bound = satisfiable_higher_bound_builds[0] higher_bound = satisfiable_higher_bound_builds[0]
higher_bound_nvr = higher_bound['nvr'] higher_bound_nvr = higher_bound["nvr"]
else: else:
higher_bound = higher_bound_nvr = None higher_bound = higher_bound_nvr = None
print(f"Lowest satisfiable higher build in higher distro version: {higher_bound_nvr}") print(f"Lowest satisfiable higher build in higher distro version: {higher_bound_nvr}")
new_evr = holistic_heuristic_calculate_release(args, lower_bound, higher_bound) new_evr = holistic_heuristic_calculate_release(args, lower_bound, higher_bound)
if new_evr['epoch']: if new_evr["epoch"]:
new_evr_str = f"{new_evr['epoch']}:{new_evr['version']}-{new_evr['release']}" new_evr_str = f"{new_evr['epoch']}:{new_evr['version']}-{new_evr['release']}"
else: else:
new_evr_str = f"{new_evr['version']}-{new_evr['release']}" new_evr_str = f"{new_evr['version']}-{new_evr['release']}"

2
pyproject.toml Normal file
View file

@ -0,0 +1,2 @@
[tool.black]
line_length = 100

View file

@ -14,16 +14,8 @@ test_data = [
"package": "gimp", "package": "gimp",
"expected_results": [ "expected_results": [
# 5 existing builds -> 6 # 5 existing builds -> 6
{ {"dist": "fc32", "last": "gimp-2.10.14-4.fc32.1", "next": "gimp-2.10.14-6.fc32",},
"dist": "fc32", {"dist": "fc31", "last": "gimp-2.10.14-3.fc31", "next": "gimp-2.10.14-4.fc31",},
"last": "gimp-2.10.14-4.fc32.1",
"next": "gimp-2.10.14-6.fc32",
},
{
"dist": "fc31",
"last": "gimp-2.10.14-3.fc31",
"next": "gimp-2.10.14-4.fc31",
},
], ],
}, },
] ]
@ -45,9 +37,7 @@ class TestNextBuild:
@pytest.mark.parametrize("test_data", data_as_test_parameters(test_data)) @pytest.mark.parametrize("test_data", data_as_test_parameters(test_data))
def test_main(self, test_data, capsys): def test_main(self, test_data, capsys):
with open( with open(
os.path.join( os.path.join(__here__, "koji-output", "list-builds", test_data["package"] + ".json"),
__here__, "koji-output", "list-builds", test_data["package"] + ".json"
),
"rb", "rb",
) as f: ) as f:
koji_list_builds_output = json.load(f) koji_list_builds_output = json.load(f)
@ -63,9 +53,7 @@ class TestNextBuild:
mock_client.getPackageID.assert_called_once() mock_client.getPackageID.assert_called_once()
mock_client.listBuilds.assert_called_once() mock_client.listBuilds.assert_called_once()
expected_output = ( expected_output = f"Last build: {test_data['last']}\n" f"Next build: {test_data['next']}\n"
f"Last build: {test_data['last']}\n" f"Next build: {test_data['next']}\n"
)
captured_output = capsys.readouterr() captured_output = capsys.readouterr()
assert captured_output.out == expected_output assert captured_output.out == expected_output