Skip to content

Commit

Permalink
add support for spec file templates into builddep subcommand
Browse files Browse the repository at this point in the history
* uses preproc-rpmspec to render the spec file
* usage of --spec switch is required for spec.rpkg files
* the spec file is only preprocessed if preprocessing is
  enabled by rpkg configuration within the spec file's context
  (i.e. git-toplevel's rpkg.conf and spec's directory rpkg.conf
  are read if present), otherwise the original text is simply
  returned. This is the effect of --check-context option of
  preproc-rpmspec.
  • Loading branch information
clime committed Dec 25, 2021
1 parent 4c31b66 commit 1cafff4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
5 changes: 5 additions & 0 deletions dnf-plugins-core.spec
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ Source0: %{url}/archive/%{version}/%{name}-%{version}.tar.gz
BuildArch: noarch
BuildRequires: cmake
BuildRequires: gettext

%if 0%{?rhel} > 7 || 0%{?fedora}
Recommends: preproc-rpmspec >= 1.2
%endif

# Documentation
%if %{with python3}
BuildRequires: %{_bindir}/sphinx-build-3
Expand Down
32 changes: 31 additions & 1 deletion plugins/builddep.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import rpm
import shutil
import tempfile
import subprocess


@dnf.plugin.register_command
Expand Down Expand Up @@ -204,13 +205,42 @@ def _src_deps(self, src_fn):
err = _("Not all dependencies satisfied")
raise dnf.exceptions.Error(err)

def _preprocess_spec(self, input_path):
# run preproc-rpmspec to get preprocessed spec file text
#
# --check-context enables preprocessing only if it is explicitly
# allowed by rpkg configuration in the spec file's directory
# and git repo context, ! otherwise original text is output !
# --no-outdir disables dynamic source generation
cmd = ['preproc-rpmspec', '--check-context', '--no-outdir', input_path]

process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

(out, err) = process.communicate()

if process.returncode != 0:
raise dnf.exceptions.Error("Could not preprocess spec file at {} with error:\n{}"
.format(input_path, err.decode('utf-8')))

return out.decode('utf-8')

def _spec_deps(self, spec_fn):
temp = tempfile.NamedTemporaryFile("w", delete=False)
preprocessed_text = self._preprocess_spec(spec_fn)
temp.write(preprocessed_text)
temp.close()
spec_file = temp.name

try:
spec = rpm.spec(spec_fn)
spec = rpm.spec(spec_file)
except ValueError as ex:
msg = _("Failed to open: '%s', not a valid spec file: %s") % (
spec_fn, ex)
raise dnf.exceptions.Error(msg)
finally:
os.remove(temp.name)

done = True
for dep in rpm.ds(spec.sourceHeader, 'requires'):
reldep_str = self._rpm_dep2reldep_str(dep)
Expand Down

0 comments on commit 1cafff4

Please sign in to comment.