Skip to content

Commit

Permalink
backend, rpmbuild: move package NEVR parsing to builder
Browse files Browse the repository at this point in the history
  • Loading branch information
FrostyX committed Jul 1, 2023
1 parent 0b462e7 commit 1e46817
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 65 deletions.
2 changes: 0 additions & 2 deletions backend/copr-backend.spec
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ BuildRequires: python3-retask
BuildRequires: python3-setproctitle
BuildRequires: python3-sphinx
BuildRequires: python3-tabulate
BuildRequires: python3-specfile
BuildRequires: modulemd-tools >= 0.6
BuildRequires: prunerepo >= %prunerepo_version
BuildRequires: dnf
Expand Down Expand Up @@ -95,7 +94,6 @@ Requires: python3-retask
Requires: python3-setproctitle
Requires: python3-tabulate
Requires: python3-boto3
Requires: python3-specfile
Requires: redis
Requires: rpm-sign
Requires: rsync
Expand Down
17 changes: 11 additions & 6 deletions backend/copr_backend/background_worker_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
FrontendClientException,
)
from copr_backend.helpers import (
call_copr_repo, pkg_name_evr, run_cmd, register_build_result, find_spec_file,
call_copr_repo, run_cmd, register_build_result, format_evr,
)
from copr_backend.job import BuildJob
from copr_backend.msgbus import MessageSender
Expand Down Expand Up @@ -628,15 +628,20 @@ def _do_createrepo(self):

def _get_srpm_build_details(self, job):
build_details = {'srpm_url': ''}
self.log.info("Retrieving srpm URL from %s", job.results_dir)
self.log.info("Retrieving SRPM info from %s", job.results_dir)

results_path = os.path.join(job.results_dir, "results.json")
with open(results_path, "r") as fp:
results = json.load(fp)

build_details["pkg_name"] = results["name"]
build_details["pkg_version"] = format_evr(
results["epoch"], results["version"], results["release"])

pattern = os.path.join(job.results_dir, '*.src.rpm')
srpm_file = glob.glob(pattern)[0]
srpm_name = os.path.basename(srpm_file)
srpm_url = os.path.join(job.results_dir_url, srpm_name)
spec_file_path = find_spec_file(job.results_dir, self.log)

build_details['pkg_name'], build_details['pkg_version'] = \
pkg_name_evr(spec_file_path, srpm_file, self.log)
build_details['srpm_url'] = srpm_url
self.log.info("SRPM URL: %s", srpm_url)
return build_details
Expand Down
56 changes: 0 additions & 56 deletions backend/copr_backend/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import munch
from munch import Munch
from specfile import Specfile

from copr_common.redis_helpers import get_redis_connection
from copr.v3 import Client
Expand Down Expand Up @@ -633,22 +632,6 @@ def get_chroot_arch(chroot):
return chroot.rsplit("-", 2)[2]


def find_spec_file(folder, logger):
"""
Returns spec file path or None
"""
spec_pattern = os.path.join(folder, "*.spec")
result = glob.glob(spec_pattern)
if not result:
return None

if len(result) > 1:
logger.error("More than one spec file found! Using the first %s from %s",
str(result[0]), str(result))

return result[0]


def format_evr(epoch, version, release):
"""
Return evr in format (epoch:)version-release
Expand All @@ -659,45 +642,6 @@ def format_evr(epoch, version, release):
return f"{version}-{release}"


def pkg_name_evr(spec_file_path, srpm_path, logger):
"""
Queries a SRPM or spec file for its name and evr (epoch:version-release)
"""
try:
spec = Specfile(spec_file_path)
return spec.expanded_name, format_evr(spec.expanded_epoch,
spec.expanded_version,
spec.expanded_release)
except Exception: # pylint: disable=broad-exception-caught
# Specfile library raises too many exception to name the in except block
logger.exception("Exception appeared during handling spec file: %s", spec_file_path)
return pkg_name_evr_from_srpm(srpm_path, logger)



def pkg_name_evr_from_srpm(srpm_path, logger):
"""
Queries a package for its name and evr (epoch:version-release)
"""
cmd = ['rpm', '-qp', '--nosignature', '--qf',
'%{NAME} %{EPOCH} %{VERSION} %{RELEASE}', srpm_path]

try:
result = run_cmd(cmd, logger=logger)
except OSError as e:
raise CoprBackendSrpmError(str(e)) from e

if result.returncode != 0:
raise CoprBackendSrpmError('Error querying SRPM')

try:
name, epoch, version, release = result.stdout.split(" ")
except ValueError as e:
raise CoprBackendSrpmError(str(e)) from e

return name, format_evr(epoch, version, release)


def format_filename(name, version, release, epoch, arch, zero_epoch=False):
if not epoch.isdigit() and zero_epoch:
epoch = "0"
Expand Down
3 changes: 2 additions & 1 deletion rpmbuild/copr_rpmbuild/automation/srpm_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ def get_package_info(self):
"""
spec_path = locate_spec(self.resultdir)
spec = Spec(spec_path)
keys = ["name", "exclusivearch", "excludearch"]
keys = ["name", "epoch", "version", "release",
"exclusivearch", "excludearch"]
return {key: getattr(spec, key) for key in keys}

0 comments on commit 1e46817

Please sign in to comment.