osmo-ci/scripts/obs/update_obs_project.py

216 lines
6.7 KiB
Python
Raw Normal View History

scripts/obs: rewrite pushing source pkgs to OBS Harald requested that the OBS scripts should not stop if building one specific source package fails, instead it should keep going and report at the end a non-success exit code. Given that the shell script code has historically grown and became hard to maintain, I decided to rewrite the scripts for implementing this feature. This rewrite solves additional problems: * No full checkout of an OBS project like network:osmocom:latest anymore, with lots of packages that won't get updated (e.g. the uhd package has a uhd-images_3.14.1.1.tar.xz file that is 108 MB). With the old code, developers had to wait minutes during the checkout before the script reaches code that is currently being developed. Now only single packages get checked out right before they get updated. * No need to clone git repositories over and over. With the new code, git repos only get cloned if needed (for latest it is not needed if the remote git tag is the same as the version in OBS). During development, the cloned git repositories are cached. * Output from commands like "git tag -l" is not written to the log unless they failed. This makes the log more readable, which is especially important when a package fails to build, we keep going and need to spot the build error in the middle of the log later on. * No more duplicated code for nightly and latest scripts that worked similar but had slight differences. Also the list of packages is not duplicated for nightly and latest anymore; nightly uses all packages and latest uses packages that have at least one git tag. * Building source packages is decoupled from uploading them. A separate script build_srcpkg.py can be used to just build the deb + rpm spec source packages, without interacting with the OBS server. * The scripts can optionally run in docker with a command-line switch, and this is used by jenkins. This way we don't need to install more dependencies on the host such as rebar3 which is now needed for erlang/osmo_dia2gsup. * Add erlang/osmo_dia2gsup and run its generate_build_dep.sh (SYS#6006) I have done the new implementation in python to make use of argparse and to be able to use try/except and print a trace when building one package fails. Example output: * https://jenkins.osmocom.org/jenkins/job/Osmocom_OBS_nightly_obs.osmocom.org/48/console * https://jenkins.osmocom.org/jenkins/job/Osmocom_OBS_latest_obs.osmocom.org/46/console Change-Id: I45a555d05a9da808c0fe0145aae665f583cb80d9
2022-07-13 10:50:21 +00:00
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright 2022 sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
import argparse
import os
import traceback
import lib
import lib.config
import lib.docker
import lib.git
import lib.metapkg
import lib.osc
import lib.srcpkg
srcpkgs_built = {} # dict of pkgname: version
srcpkgs_skipped = [] # list of pkgnames
srcpkgs_failed_build = [] # list of pkgnames
srcpkgs_failed_upload = [] # list of pkgnames
srcpkgs_updated = [] # list of pkgnames
def parse_packages(packages_arg):
if packages_arg:
for package in packages_arg:
lib.check_package(package)
return packages_arg
# Default to all
ret = []
ret += lib.config.projects_osmocom
ret += lib.config.projects_other
return ret
def build_srcpkg(feed, package, conflict_version, fetch, is_meta_pkg):
global srcpkgs_built
global srcpkgs_failed_build
version = None
try:
if is_meta_pkg:
version = lib.metapkg.build(feed, conflict_version)
else:
version = lib.srcpkg.build(package, feed, conflict_version, fetch)
srcpkgs_built[package] = version
except Exception as ex:
traceback.print_exception(type(ex), ex, ex.__traceback__)
print()
print(f"{package}: build failed")
srcpkgs_failed_build += [package]
def is_up_to_date(obs_version, git_latest_version):
if obs_version == git_latest_version:
return True
# e.g. open5gs has "v" infront of version in git tag
if f"v{obs_version}" == git_latest_version:
return True
return False
def build_srcpkg_if_needed(proj, feed, pkgs_remote, package, conflict_version,
fetch, is_meta_pkg, skip_up_to_date):
global srcpkgs_skipped
if feed != "latest":
print(f"{package}: building source package (feed is {feed})")
else:
if is_meta_pkg:
latest_version = conflict_version if conflict_version else "1.0.0"
else:
latest_version = lib.git.get_latest_tag_remote(package)
if latest_version is None:
print(f"{package}: skipping (no git tag found)")
srcpkgs_skipped += [package]
return
if os.path.basename(package) not in pkgs_remote:
print(f"{package}: building source package (not in OBS)")
else:
obs_version = lib.osc.get_package_version(proj, package)
if is_up_to_date(obs_version, latest_version):
if skip_up_to_date:
print(f"{package}: skipping ({obs_version} is up-to-date)")
srcpkgs_skipped += [package]
return
else:
print(f"{package}: building source package"
f" ({obs_version} is up-to-date, but"
" --no-skip-up-to-date is set)")
else:
print(f"{package}: building source package (outdated:"
f" {latest_version} <=> {obs_version} in OBS)")
build_srcpkg(feed, package, conflict_version, fetch, is_meta_pkg)
def upload_srcpkg(proj, feed, pkgs_remote, package, version):
if os.path.basename(package) not in pkgs_remote:
lib.osc.create_package(proj, package)
lib.osc.update_package(proj, package, version)
def build_srcpkgs(proj, feed, pkgs_remote, packages, conflict_version, fetch,
meta, skip_up_to_date):
print()
print("### Building source packages ###")
print()
if meta:
build_srcpkg_if_needed(proj, feed, pkgs_remote, f"osmocom-{feed}",
conflict_version, fetch, True, skip_up_to_date)
for package in packages:
build_srcpkg_if_needed(proj, feed, pkgs_remote, package,
conflict_version, fetch, False, skip_up_to_date)
def upload_srcpkgs(proj, feed, pkgs_remote):
global srcpkgs_built
global srcpkgs_failed_upload
global srcpkgs_updated
srcpkgs_failed_upload = []
srcpkgs_updated = []
if not srcpkgs_built:
return
print()
print("### Uploading built packages ###")
print()
for package, version in srcpkgs_built.items():
try:
upload_srcpkg(proj, feed, pkgs_remote, package, version)
srcpkgs_updated += [package]
except Exception as ex:
traceback.print_exception(type(ex), ex, ex.__traceback__)
print()
print(f"{package}: upload failed")
srcpkgs_failed_upload += [package]
def exit_with_summary():
global srcpkgs_updated
global srcpkgs_skipped
global srcpkgs_failed_build
global srcpkgs_failed_upload
print()
print("### Summary ###")
print()
print(f"Updated: {len(srcpkgs_updated)}")
print(f"Skipped: {len(srcpkgs_skipped)}")
print(f"Failed (srcpkg build): {len(srcpkgs_failed_build)}")
print(f"Failed (srcpkg upload): {len(srcpkgs_failed_upload)}")
if not srcpkgs_failed_build and not srcpkgs_failed_upload:
exit(0)
print()
print("List of failed packages:")
for package in srcpkgs_failed_build:
print(f"* {package} (srcpkg build)")
for package in srcpkgs_failed_upload:
print(f"* {package} (srcpkg upload)")
exit(1)
def main():
parser = argparse.ArgumentParser(
description="Generate source packages and upload them to OBS.")
lib.add_shared_arguments(parser)
parser.add_argument("-A", "--apiurl", help="OBS API URL or .oscrc alias"
" (e.g. https://obs.osmocom.org)")
parser.add_argument("-n", "--no-skip-up-to-date",
dest="skip_up_to_date", action="store_false",
help="for latest feed, build and upload packages even"
" if the version did not change")
parser.add_argument("obs_project",
help="OBS project, e.g. home:osmith:nightly")
parser.add_argument("package", nargs="*",
help="package name, e.g. libosmocore or open5gs,"
" default is all packages")
args = parser.parse_args()
proj = args.obs_project
feed = args.feed
packages = parse_packages(args.package)
lib.set_cmds_verbose(args.verbose)
if args.docker:
lib.docker.run_in_docker_and_exit(__file__, args, True)
lib.osc.check_proj(proj)
lib.osc.set_apiurl(args.apiurl)
lib.check_required_programs()
lib.remove_temp()
pkgs_remote = lib.osc.get_remote_pkgs(proj)
build_srcpkgs(proj, feed, pkgs_remote, packages, args.conflict_version,
args.git_fetch, args.meta, args.skip_up_to_date)
upload_srcpkgs(proj, feed, pkgs_remote)
exit_with_summary()
if __name__ == "__main__":
main()