-
Notifications
You must be signed in to change notification settings - Fork 87
python project configuration for koji scripts #733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
glehmann
wants to merge
3
commits into
master
Choose a base branch
from
gln/koji-build-script-rvln
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+880
−267
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
__pycache__ | ||
|
||
# Distribution / packaging | ||
dist/ | ||
*.egg-info/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.11.11 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,4 +35,3 @@ def main(): | |
|
||
if __name__ == "__main__": | ||
main() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,251 +1,9 @@ | ||
#!/usr/bin/env python3 | ||
import argparse | ||
import logging | ||
import os | ||
import re | ||
import subprocess | ||
from contextlib import contextmanager | ||
from datetime import datetime, timedelta | ||
from pathlib import Path | ||
|
||
try: | ||
from specfile import Specfile | ||
except ImportError: | ||
print("error: specfile module can't be imported. Please install it with 'pip install --user specfile'.") | ||
exit(1) | ||
import sys | ||
|
||
TIME_FORMAT = '%Y-%m-%d-%H-%M-%S' | ||
from koji_utils.koji_build import main | ||
ydirson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# target -> required branch | ||
PROTECTED_TARGETS = { | ||
"v8.2-ci": "8.2", | ||
"v8.2-fasttrack": "8.2", | ||
"v8.2-incoming": "8.2", | ||
"v8.3-ci": "master", | ||
"v8.3-fasttrack": "master", | ||
"v8.3-incoming": "master", | ||
} | ||
|
||
@contextmanager | ||
def cd(dir): | ||
"""Change to a directory temporarily. To be used in a with statement.""" | ||
prevdir = os.getcwd() | ||
os.chdir(dir) | ||
try: | ||
yield os.path.realpath(dir) | ||
finally: | ||
os.chdir(prevdir) | ||
|
||
def check_dir(dirpath): | ||
if not os.path.isdir(dirpath): | ||
raise Exception("Directory %s doesn't exist" % dirpath) | ||
return dirpath | ||
|
||
def check_git_repo(dirpath): | ||
"""check that the working copy is a working directory and is clean.""" | ||
with cd(dirpath): | ||
return subprocess.run(['git', 'diff-index', '--quiet', 'HEAD', '--']).returncode == 0 | ||
|
||
def check_commit_is_available_remotely(dirpath, hash, target, warn): | ||
with cd(dirpath): | ||
if not subprocess.check_output(['git', 'branch', '-r', '--contains', hash]): | ||
raise Exception("The current commit is not available in the remote repository") | ||
try: | ||
expected_branch = PROTECTED_TARGETS.get(target) | ||
if ( | ||
expected_branch is not None | ||
and not is_remote_branch_commit(dirpath, hash, expected_branch) | ||
): | ||
raise Exception(f"The current commit is not the last commit in the remote branch {expected_branch}.\n" | ||
f"This is required when using the protected target {target}.\n") | ||
except Exception as e: | ||
if warn: | ||
print(f"warning: {e}", flush=True) | ||
else: | ||
raise e | ||
|
||
def get_repo_and_commit_info(dirpath): | ||
with cd(dirpath): | ||
remote = subprocess.check_output(['git', 'config', '--get', 'remote.origin.url']).decode().strip() | ||
# We want the exact hash for accurate build history | ||
hash = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode().strip() | ||
return remote, hash | ||
|
||
def koji_url(remote, hash): | ||
if remote.startswith('git@'): | ||
remote = re.sub(r'git@(.+):', r'git+https://\1/', remote) | ||
elif remote.startswith('https://'): | ||
remote = 'git+' + remote | ||
else: | ||
raise Exception("Unrecognized remote URL") | ||
return remote + "?#" + hash | ||
|
||
@contextmanager | ||
def local_branch(branch): | ||
prev_branch = subprocess.check_output(['git', 'branch', '--show-current']).strip() | ||
commit = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode().strip() | ||
subprocess.check_call(['git', 'checkout', '--quiet', commit]) | ||
try: | ||
yield branch | ||
finally: | ||
subprocess.check_call(['git', 'checkout', prev_branch]) | ||
|
||
def is_old_branch(b): | ||
branch_time = datetime.strptime(b.split('/')[-1], TIME_FORMAT) | ||
return branch_time < datetime.now() - timedelta(hours=3) | ||
|
||
def clean_old_branches(git_repo): | ||
with cd(git_repo): | ||
remote_branches = [ | ||
line.split()[-1] for line in subprocess.check_output(['git', 'ls-remote']).decode().splitlines() | ||
] | ||
remote_branches = [b for b in remote_branches if b.startswith('refs/heads/koji/test/')] | ||
old_branches = [b for b in remote_branches if is_old_branch(b)] | ||
if old_branches: | ||
print("removing outdated remote branch(es)", flush=True) | ||
subprocess.check_call(['git', 'push', '--delete', 'origin'] + old_branches) | ||
|
||
def xcpng_version(target): | ||
xcpng_version_match = re.match(r'^v(\d+\.\d+)-u-\S+$', target) | ||
if xcpng_version_match is None: | ||
raise Exception(f"Can't find XCP-ng version in {target}") | ||
return xcpng_version_match.group(1) | ||
|
||
def find_next_release(package, spec, target, test_build_id, pre_build_id): | ||
assert test_build_id is not None or pre_build_id is not None | ||
builds = subprocess.check_output(['koji', 'list-builds', '--quiet', '--package', package]).decode().splitlines() | ||
if test_build_id: | ||
base_nvr = f'{package}-{spec.version}-{spec.release}.0.{test_build_id}.' | ||
else: | ||
base_nvr = f'{package}-{spec.version}-{spec.release}~{pre_build_id}.' | ||
# use a regex to match %{macro} without actually expanding the macros | ||
base_nvr_re = ( | ||
re.escape(re.sub('%{.+}', "@@@", base_nvr)).replace('@@@', '.*') | ||
+ r'(\d+)' | ||
+ re.escape(f'.xcpng{xcpng_version(target)}') | ||
) | ||
build_matches = [re.match(base_nvr_re, b) for b in builds] | ||
build_nbs = [int(m.group(1)) for m in build_matches if m] | ||
build_nb = sorted(build_nbs)[-1] + 1 if build_nbs else 1 | ||
if test_build_id: | ||
return f'{spec.release}.0.{test_build_id}.{build_nb}' | ||
else: | ||
return f'{spec.release}~{pre_build_id}.{build_nb}' | ||
|
||
def push_bumped_release(git_repo, target, test_build_id, pre_build_id): | ||
t = datetime.now().strftime(TIME_FORMAT) | ||
branch = f'koji/test/{test_build_id or pre_build_id}/{t}' | ||
with cd(git_repo), local_branch(branch): | ||
spec_paths = subprocess.check_output(['git', 'ls-files', 'SPECS/*.spec']).decode().splitlines() | ||
assert len(spec_paths) == 1 | ||
spec_path = spec_paths[0] | ||
with Specfile(spec_path) as spec: | ||
# find the next build number | ||
package = Path(spec_path).stem | ||
spec.release = find_next_release(package, spec, target, test_build_id, pre_build_id) | ||
subprocess.check_call(['git', 'commit', '--quiet', '-m', "bump release for test build", spec_path]) | ||
subprocess.check_call(['git', 'push', 'origin', f'HEAD:refs/heads/{branch}']) | ||
commit = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode().strip() | ||
return commit | ||
|
||
def is_remote_branch_commit(git_repo, sha, branch): | ||
with cd(git_repo): | ||
remote_sha = ( | ||
subprocess.check_output(['git', 'ls-remote', 'origin', f'refs/heads/{branch}']).decode().strip().split()[0] | ||
) | ||
return sha == remote_sha | ||
|
||
def build_id_of(name, candidate): | ||
if candidate is None: | ||
return None | ||
|
||
length = len(candidate) | ||
if length > 16: | ||
logging.error(f"The {name} build id must be at most 16 characters long, it's {length} characters long") | ||
exit(1) | ||
|
||
invalid_chars = any(re.match(r'[a-zA-Z0-9]', char) is None for char in candidate) | ||
|
||
if invalid_chars: | ||
pp_invalid = ''.join("^" if re.match(r'[a-zA-Z0-9]', char) is None else " " for char in candidate) | ||
logging.error(f"The {name} build id must only contain letters and digits:") | ||
logging.error(f" {candidate}") | ||
logging.error(f" {pp_invalid}") | ||
exit(1) | ||
|
||
return candidate | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
description='Build a package or chain-build several from local git repos for RPM sources' | ||
) | ||
parser.add_argument('target', help='Koji target for the build') | ||
parser.add_argument('git_repos', nargs='+', | ||
help='local path to one or more git repositories. If several are provided, ' | ||
'a chained build will be started in the order of the arguments') | ||
parser.add_argument('--scratch', action="store_true", help='Perform scratch build') | ||
parser.add_argument('--nowait', action="store_true", help='Do not wait for the build to end') | ||
parser.add_argument('--force', action="store_true", help='Bypass sanity checks') | ||
parser.add_argument( | ||
'--test-build', | ||
metavar="ID", | ||
help='Run a test build. The provided ID will be used to build a unique release tag.', | ||
) | ||
parser.add_argument( | ||
'--pre-build', | ||
metavar="ID", | ||
help='Run a pre build. The provided ID will be used to build a unique release tag.', | ||
) | ||
args = parser.parse_args() | ||
|
||
target = args.target | ||
git_repos = [os.path.abspath(check_dir(d)) for d in args.git_repos] | ||
is_scratch = args.scratch | ||
is_nowait = args.nowait | ||
|
||
test_build = build_id_of("test", args.test_build) | ||
pre_build = build_id_of("pre", args.pre_build) | ||
|
||
if test_build and pre_build: | ||
logging.error("--pre-build and --test-build can't be used together") | ||
exit(1) | ||
|
||
if len(git_repos) > 1 and is_scratch: | ||
parser.error("--scratch is not compatible with chained builds.") | ||
|
||
for d in git_repos: | ||
if not check_git_repo(d): | ||
parser.error("%s is not in a clean state (or is not a git repository)." % d) | ||
|
||
if len(git_repos) == 1: | ||
clean_old_branches(git_repos[0]) | ||
remote, hash = get_repo_and_commit_info(git_repos[0]) | ||
if test_build or pre_build: | ||
hash = push_bumped_release(git_repos[0], target, test_build, pre_build) | ||
else: | ||
check_commit_is_available_remotely(git_repos[0], hash, None if is_scratch else target, args.force) | ||
url = koji_url(remote, hash) | ||
command = ( | ||
['koji', 'build'] | ||
+ (['--scratch'] if is_scratch else []) | ||
+ [target, url] | ||
+ (['--nowait'] if is_nowait else []) | ||
) | ||
print(' '.join(command), flush=True) | ||
subprocess.check_call(command) | ||
else: | ||
urls = [] | ||
for d in git_repos: | ||
clean_old_branches(d) | ||
remote, hash = get_repo_and_commit_info(d) | ||
if test_build or pre_build: | ||
hash = push_bumped_release(d, target, test_build, pre_build) | ||
else: | ||
check_commit_is_available_remotely(d, hash, None if is_scratch else target, args.force) | ||
urls.append(koji_url(remote, hash)) | ||
command = ['koji', 'chain-build', target] + (' : '.join(urls)).split(' ') + (['--nowait'] if is_nowait else []) | ||
print(' '.join(command), flush=True) | ||
subprocess.check_call(command) | ||
|
||
if __name__ == "__main__": | ||
main() | ||
print("\033[33mwarning: koji_build.py as moved to koji_utils/koji_build.py. " | ||
"Please update your configuration to use that file.\033[0m", file=sys.stderr) | ||
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the point of this file? This version can't be enforced (my system doesn't provide it)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uv
uses it to pin the python versionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uv
should install that exact version for you. That's not the case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I install tools using uv tool, I've never seen it installing python version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels strange, as https://docs.astral.sh/uv/guides/tools/ says
uv tool run
is the same asuvx
, andThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the system python matches the version allowed in pyproject.toml, I don't see why it would be strange to use the system interpreter