From 1895d90d58df31e970ae2fd42b80d30a3b5586c7 Mon Sep 17 00:00:00 2001 From: Pat Riehecky Date: Feb 25 2019 12:12:12 +0000 Subject: Permit setting arbitrary rpm macros during build Signed-off-by: Pat Riehecky --- diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py index 3872dab..a70f775 100644 --- a/pyrpkg/__init__.py +++ b/pyrpkg/__init__.py @@ -2372,11 +2372,11 @@ class Commands(object): with open(os.path.join(self.path, 'clog'), 'w') as clog: clog.writelines(clog_lines) - def compile(self, arch=None, short=False, builddir=None, nocheck=False): + def compile(self, arch=None, short=False, builddir=None, nocheck=False, define=None): """Run rpmbuild -bc - optionally for a specific arch, or short-circuit it, or - define an alternate builddir + optionally for a specific arch, or short-circuit it, + define an alternate builddir, or define some macro Logs the output and returns nothing """ @@ -2389,6 +2389,9 @@ class Commands(object): cmd.append("--define '_builddir %s'" % os.path.abspath(builddir)) if arch: cmd.extend(['--target', arch]) + if define: + for entry in define: + cmd.extend(['--define', entry]) if short: cmd.append('--short-circuit') if nocheck: @@ -2425,16 +2428,17 @@ class Commands(object): # This should have a try and catch koji errors self.kojisession.uploadWrapper(file, path, callback=callback) - def install(self, arch=None, short=False, builddir=None, nocheck=False, - buildrootdir=None): + def install(self, arch=None, short=False, builddir=None, + nocheck=False, buildrootdir=None, define=None): """Run ``rpmbuild -bi`` - optionally for a specific arch, short-circuit it, or - define an alternative builddir + optionally for a specific arch, short-circuit it, + define an alternative builddir, or define rpmbuild macros Logs the output and returns nothing :param str arch: specify a specific arch. + :param list define: specify a list of rpmbuild macros. :param bool short: short-circuit it. :param str builddir: alternate builddir. :param bool nocheck: do not check. @@ -2452,6 +2456,9 @@ class Commands(object): cmd.append("--define '_builddir %s'" % os.path.abspath(builddir)) if arch: cmd.extend(['--target', arch]) + if define: + for entry in define: + cmd.extend(['--define', entry]) if short: cmd.append('--short-circuit') if nocheck: @@ -2510,7 +2517,7 @@ class Commands(object): self._run_command(cmd, shell=True) def local(self, localargs, arch=None, hashtype=None, builddir=None, - buildrootdir=None): + buildrootdir=None, define=None): """rpmbuild locally for given arch. Takes localargs (passed to rpmbuild), arch to build for, and hashtype @@ -2521,6 +2528,7 @@ class Commands(object): `.build-{version}-{release}.log`. :param str arch: to optionally build for a specific arch. + :param list define: optional list of rpmbuild macros. :param str hashtype: an alternative algorithm used for payload file digests. :param str builddir: an alternative builddir. @@ -2551,6 +2559,9 @@ class Commands(object): % hashtype]) if arch: cmd.extend(['--target', arch]) + if define: + for entry in define: + cmd.extend(['--define', entry]) if self.quiet: cmd.append('--quiet') if buildrootdir: @@ -2807,11 +2818,12 @@ class Commands(object): self.repo.index.add(['sources', '.gitignore']) - def prep(self, arch=None, builddir=None, buildrootdir=None): + def prep(self, arch=None, builddir=None, buildrootdir=None, define=None): """Run ``rpmbuild -bp`` :param str arch: optional to run prep section for a specific arch. By default, local system arch will be used. + :param list define: an optional list of rpmbuild macros. :param str builddir: an alternative builddir. :param str buildrootdir: an alternative buildrootdir. @@ -2827,6 +2839,9 @@ class Commands(object): cmd.append("--define '_builddir %s'" % os.path.abspath(builddir)) if arch: cmd.extend(['--target', arch]) + if define: + for entry in define: + cmd.extend(['--define', entry]) if self.quiet: cmd.append('--quiet') if buildrootdir: @@ -2836,7 +2851,7 @@ class Commands(object): # Run the command self._run_command(cmd, shell=True) - def srpm(self, hashtype=None): + def srpm(self, hashtype=None, define=None, builddir=None, buildrootdir=None, arch=None): """Create an srpm using hashtype from content Requires sources already downloaded. The generated SRPM file will be @@ -2844,6 +2859,11 @@ class Commands(object): :param str hashtype: an alternative algorithm used for payload file digests. + :param list define: an optional list of rpmbuild macros. + :param str builddir: optionally define an alternate builddir. + :param str buildrootdir: optionally define an alternate buildrootdir. + :param str arch: optional to run prep section for a specific arch. By + default, local system arch will be used. """ self.srpmname = os.path.join(self.path, @@ -2858,6 +2878,18 @@ class Commands(object): cmd.extend(self.rpmdefines) if self.quiet: cmd.append('--quiet') + if define: + for entry in define: + cmd.extend(['--define', entry]) + if builddir: + # Tack on a new builddir to the end of the defines + cmd.append("--define '_builddir %s'" % os.path.abspath(builddir)) + if buildrootdir: + cmd.append("--define '_buildrootdir {0}'".format( + os.path.abspath(buildrootdir))) + if arch: + cmd.extend(['--target', arch]) + # Figure out which hashtype to use, if not provided one if not hashtype: # Try to determine the dist @@ -2927,9 +2959,10 @@ class Commands(object): line_num += 1 return [line_num, offset - offset_inc + 1] - def verify_files(self, builddir=None, buildrootdir=None): + def verify_files(self, builddir=None, buildrootdir=None, define=None): """Run `rpmbuild -bl` to verify the `%files` section + :param list define: an optional list of rpmbuild macros. :param str builddir: optionally define an alternate builddir. :param str buildrootdir: optionally define an alternate buildrootdir. @@ -2940,6 +2973,9 @@ class Commands(object): # setup the rpm command cmd = ['rpmbuild'] cmd.extend(self.rpmdefines) + if define: + for entry in define: + cmd.extend(['--define', entry]) if builddir: # Tack on a new builddir to the end of the defines cmd.append("--define '_builddir %s'" % os.path.abspath(builddir)) diff --git a/pyrpkg/cli.py b/pyrpkg/cli.py index 4aae289..cef1ebd 100644 --- a/pyrpkg/cli.py +++ b/pyrpkg/cli.py @@ -529,6 +529,9 @@ class cliClient(object): help='Define an alternate buildrootdir') self.rpm_parser_common.add_argument( '--arch', help='Prep for a specific arch') + self.rpm_parser_common.add_argument( + '--define', help='Pass custom macros to rpmbuild, may specify multiple times', + action='append') def register_build(self): """Register the build target""" @@ -1276,6 +1279,7 @@ defined, packages will be built sequentially.""" % {'name': self.name}) srpm_parser = self.subparsers.add_parser( 'srpm', help='Create a source rpm', + parents=[self.rpm_parser_common], description='Create a source rpm') # optionally define old style hashsums srpm_parser.add_argument( @@ -1792,15 +1796,18 @@ see API KEY section of copr-cli(1) man page. self.sources() arch = None + define = None short = False nocheck = False if self.args.arch: arch = self.args.arch + if self.args.define: + define = self.args.define if self.args.short_circuit: short = True if self.args.nocheck: nocheck = True - self.cmd.compile(arch=arch, short=short, + self.cmd.compile(arch=arch, define=define, short=short, builddir=self.args.builddir, nocheck=nocheck) def container_build_koji(self): @@ -1912,7 +1919,7 @@ see API KEY section of copr-cli(1) man page. def install(self): self.sources() - self.cmd.install(arch=self.args.arch, + self.cmd.install(arch=self.args.arch, define=self.args.define, short=self.args.short_circuit, builddir=self.args.builddir, nocheck=self.args.nocheck, @@ -1936,6 +1943,7 @@ see API KEY section of copr-cli(1) man page. self.cmd.local(localargs, arch=self.args.arch, + define=self.args.define, hashtype=self.args.hash, builddir=self.args.builddir, buildrootdir=self.args.buildrootdir) @@ -2267,7 +2275,7 @@ see API KEY section of copr-cli(1) man page. def prep(self): self.sources() - self.cmd.prep(arch=self.args.arch, + self.cmd.prep(arch=self.args.arch, define=self.args.define, builddir=self.args.builddir, buildrootdir=self.args.buildrootdir) @@ -2305,7 +2313,37 @@ see API KEY section of copr-cli(1) man page. def srpm(self): self.sources() - self.cmd.srpm(hashtype=self.args.hash) + + if hasattr(self.args, 'define'): + defines = self.args.define + else: + # koji does not allow defines argparse won't set them + # for koji style builds + defines = None + + if hasattr(self.args, 'builddir'): + builddir = self.args.builddir + else: + # koji does not allow custom builddir argparse won't set them + # for koji style builds + builddir = None + + if hasattr(self.args, 'buildroot'): + buildrootdir = self.args.buildroot + else: + # koji does not allow custom buildroot argparse won't set them + # for koji style builds + buildrootdir = None + + if hasattr(self.args, 'arch'): + arch = self.args.arch + else: + # normal koji builds are for all arches and not set via argparse + arch = None + + self.cmd.srpm(builddir=builddir, define=defines, + buildrootdir=buildrootdir, + arch=arch, hashtype=self.args.hash) def switch_branch(self): if self.args.branch: @@ -2343,7 +2381,8 @@ see API KEY section of copr-cli(1) man page. print('\n'.join(unused)) def verify_files(self): - self.cmd.verify_files(builddir=self.args.builddir, + self.cmd.verify_files(define=self.args.define, + builddir=self.args.builddir, buildrootdir=self.args.buildrootdir) def verrel(self): diff --git a/tests/test_cli.py b/tests/test_cli.py index 3dfa9e8..c667f2c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -534,6 +534,20 @@ class TestSrpm(CliTestCase): ['--nodeps', '-bs', os.path.join(cli.cmd.path, cli.cmd.spec)] _run_command.assert_called_once_with(expected_cmd, shell=True) + @patch('pyrpkg.Commands._run_command') + def test_srpm_with_options(self, _run_command): + cli_cmd = ['rpkg', '--path', self.cloned_repo_path, '--release', 'rhel-6', 'srpm', + '--define', 'macro1 meansthis'] + + with patch('sys.argv', new=cli_cmd): + cli = self.new_cli() + cli.srpm() + + expected_cmd = ['rpmbuild'] + cli.cmd.rpmdefines + \ + ['--define', 'macro1 meansthis', '--nodeps', '-bs', + os.path.join(cli.cmd.path, cli.cmd.spec)] + _run_command.assert_called_once_with(expected_cmd, shell=True) + class TestCompile(CliTestCase): @@ -557,7 +571,8 @@ class TestCompile(CliTestCase): builddir = os.path.join(self.cloned_repo_path, 'builddir') cli_cmd = ['rpkg', '--path', self.cloned_repo_path, '--release', 'rhel-6', '-q', 'compile', - '--builddir', builddir, '--short-circuit', '--arch', 'i686', '--nocheck'] + '--builddir', builddir, '--short-circuit', '--arch', 'i686', '--nocheck', + '--define', 'macro1 meansthis'] with patch('sys.argv', new=cli_cmd): cli = self.new_cli() @@ -565,7 +580,8 @@ class TestCompile(CliTestCase): spec = os.path.join(cli.cmd.path, cli.cmd.spec) rpmbuild = ['rpmbuild'] + cli.cmd.rpmdefines + \ - ["--define '_builddir %s'" % builddir, '--target', 'i686', '--short-circuit', + ["--define '_builddir %s'" % builddir, '--target', 'i686', + '--define', 'macro1 meansthis', '--short-circuit', '--nocheck', '--quiet', '-bc', spec] _run_command.assert_called_once_with(rpmbuild, shell=True)