From 68b8f1c22eea7ce51974f6e910fdb46e1b594ddd Mon Sep 17 00:00:00 2001 From: Chenxiong Qi Date: Aug 16 2018 04:01:09 +0000 Subject: New option --buildrootdir This new option is for command local, prep, install and verify-files to allow not to assemble the rpm in ~/rpmbuild/BUILDROOT/. Resolves: rhbz#1583822 Signed-off-by: Chenxiong Qi --- diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py index 8692635..c094e8a 100644 --- a/pyrpkg/__init__.py +++ b/pyrpkg/__init__.py @@ -2303,13 +2303,23 @@ 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): - """Run rpm -bi + def install(self, arch=None, short=False, builddir=None, nocheck=False, + buildrootdir=None): + """Run ``rpmbuild -bi`` optionally for a specific arch, short-circuit it, or define an alternative builddir Logs the output and returns nothing + + :param str arch: specify a specific arch. + :param bool short: short-circuit it. + :param str builddir: alternate builddir. + :param bool nocheck: do not check. + :param str buildrootdir: alternate buildrootdir. + + .. versionadded: 1.56 + Parameter buildrootdir. """ # setup the rpm command @@ -2326,6 +2336,9 @@ class Commands(object): cmd.append('--nocheck') if self.quiet: cmd.append('--quiet') + if buildrootdir: + cmd.append("--define '_buildrootdir {0}'".format( + os.path.abspath(buildrootdir))) cmd.extend(['-bi', os.path.join(self.path, self.spec)]) # Run the command self._run_command(cmd, shell=True) @@ -2374,7 +2387,8 @@ class Commands(object): # Run the command self._run_command(cmd, shell=True) - def local(self, localargs, arch=None, hashtype=None, builddir=None): + def local(self, localargs, arch=None, hashtype=None, builddir=None, + buildrootdir=None): """rpmbuild locally for given arch. Takes localargs (passed to rpmbuild), arch to build for, and hashtype @@ -2388,7 +2402,11 @@ class Commands(object): :param str hashtype: an alternative algorithm used for payload file digests. :param str builddir: an alternative builddir. + :param str buildrootdir: an alternative buildrootdir. :raises rpkgError: if underlying `rpmbuild` fails. + + .. versionadded: 1.56 + Parameter buildrootdir. """ # This could really use a list of arches to build for and loop over @@ -2413,6 +2431,9 @@ class Commands(object): cmd.extend(['--target', arch]) if self.quiet: cmd.append('--quiet') + if buildrootdir: + cmd.append("--define '_buildrootdir {0}'".format( + os.path.abspath(buildrootdir))) cmd.extend(['-ba', os.path.join(self.path, self.spec)]) logfile = '.build-%s-%s.log' % (self.ver, self.rel) @@ -2654,12 +2675,16 @@ class Commands(object): self.repo.index.add(['sources', '.gitignore']) - def prep(self, arch=None, builddir=None): - """Run `rpmbuild -bp` + def prep(self, arch=None, builddir=None, buildrootdir=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 str builddir: an alternative builddir. + :param str buildrootdir: an alternative buildrootdir. + + .. versionadded: 1.56 + Parameter buildrootdir. """ # setup the rpm command @@ -2672,6 +2697,9 @@ class Commands(object): cmd.extend(['--target', arch]) if self.quiet: cmd.append('--quiet') + if buildrootdir: + cmd.append("--define '_buildrootdir {0}'".format( + os.path.abspath(buildrootdir))) cmd.extend(['--nodeps', '-bp', os.path.join(self.path, self.spec)]) # Run the command self._run_command(cmd, shell=True) @@ -2767,10 +2795,14 @@ class Commands(object): line_num += 1 return [line_num, offset - offset_inc + 1] - def verify_files(self, builddir=None): + def verify_files(self, builddir=None, buildrootdir=None): """Run `rpmbuild -bl` to verify the `%files` section :param str builddir: optionally define an alternate builddir. + :param str buildrootdir: optionally define an alternate buildrootdir. + + .. versionadded:: 1.56 + Parameter buildrootdir. """ # setup the rpm command @@ -2779,6 +2811,9 @@ class Commands(object): 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 self.quiet: cmd.append('--quiet') cmd.extend(['-bl', os.path.join(self.path, self.spec)]) diff --git a/pyrpkg/cli.py b/pyrpkg/cli.py index 28fd6da..abd7c6a 100644 --- a/pyrpkg/cli.py +++ b/pyrpkg/cli.py @@ -513,6 +513,9 @@ class cliClient(object): self.rpm_parser_common.add_argument( '--builddir', default=None, help='Define an alternate builddir') self.rpm_parser_common.add_argument( + '--buildrootdir', default=None, + help='Define an alternate buildrootdir') + self.rpm_parser_common.add_argument( '--arch', help='Prep for a specific arch') def register_build(self): @@ -1849,7 +1852,8 @@ see API KEY section of copr-cli(1) man page. self.cmd.install(arch=self.args.arch, short=self.args.short_circuit, builddir=self.args.builddir, - nocheck=self.args.nocheck) + nocheck=self.args.nocheck, + buildrootdir=self.args.buildrootdir) def lint(self): self.cmd.lint(self.args.info, self.args.rpmlintconf) @@ -1867,8 +1871,11 @@ see API KEY section of copr-cli(1) man page. for arg in self.args.bcond_without: localargs.extend(['--without', arg]) - self.cmd.local(localargs, arch=self.args.arch, hashtype=self.args.hash, - builddir=self.args.builddir) + self.cmd.local(localargs, + arch=self.args.arch, + hashtype=self.args.hash, + builddir=self.args.builddir, + buildrootdir=self.args.buildrootdir) def mockbuild(self): try: @@ -2150,7 +2157,9 @@ see API KEY section of copr-cli(1) man page. def prep(self): self.sources() - self.cmd.prep(arch=self.args.arch, builddir=self.args.builddir) + self.cmd.prep(arch=self.args.arch, + builddir=self.args.builddir, + buildrootdir=self.args.buildrootdir) def pull(self): self.cmd.pull(rebase=self.args.rebase, @@ -2224,7 +2233,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(builddir=self.args.builddir, + buildrootdir=self.args.buildrootdir) def verrel(self): print('%s-%s-%s' % (self.cmd.repo_name, self.cmd.ver, diff --git a/tests/test_cli.py b/tests/test_cli.py index 8a25a77..a95736b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -545,18 +545,24 @@ class TestPrep(CliTestCase): @patch('pyrpkg.Commands._run_command') def test_prep_with_options(self, _run_command): builddir = os.path.join(self.cloned_repo_path, 'builddir') + buildrootdir = os.path.join(self.cloned_repo_path, 'buildrootdir') - cli_cmd = ['rpkg', '--path', self.cloned_repo_path, '--release', 'rhel-6', '-q', - 'compile', '--arch', 'i686', '--builddir', builddir] + cli_cmd = [ + 'rpkg', '--path', self.cloned_repo_path, '--release', 'rhel-6', + '-q', 'compile', '--arch', 'i686', '--builddir', builddir, + '--buildrootdir', buildrootdir + ] with patch('sys.argv', new=cli_cmd): cli = self.new_cli() cli.prep() spec = os.path.join(cli.cmd.path, cli.cmd.spec) - rpmbuild = ['rpmbuild'] + cli.cmd.rpmdefines + \ - ["--define '_builddir %s'" % builddir, '--target', 'i686', '--quiet', '--nodeps', - '-bp', spec] + rpmbuild = ['rpmbuild'] + cli.cmd.rpmdefines + [ + "--define '_builddir %s'" % builddir, '--target', 'i686', + '--quiet', "--define '_buildrootdir %s'" % buildrootdir, + '--nodeps', '-bp', spec + ] _run_command.assert_called_once_with(rpmbuild, shell=True) @@ -580,18 +586,25 @@ class TestInstall(CliTestCase): @patch('pyrpkg.Commands._run_command') def test_install_with_options(self, _run_command): builddir = os.path.join(self.cloned_repo_path, 'builddir') + buildrootdir = os.path.join(self.cloned_repo_path, 'buildrootdir') - cli_cmd = ['rpkg', '--path', self.cloned_repo_path, '--release', 'rhel-6', '-q', - 'install', '--nocheck', '--arch', 'i686', '--builddir', builddir] + cli_cmd = [ + 'rpkg', '--path', self.cloned_repo_path, '--release', 'rhel-6', + '-q', 'install', '--nocheck', '--arch', 'i686', + '--builddir', builddir, '--buildrootdir', buildrootdir + ] with patch('sys.argv', new=cli_cmd): cli = self.new_cli() cli.install() spec = os.path.join(cli.cmd.path, cli.cmd.spec) - rpmbuild = ['rpmbuild'] + cli.cmd.rpmdefines + \ - ["--define '_builddir %s'" % builddir, '--target', 'i686', '--nocheck', '--quiet', - '-bi', spec] + rpmbuild = ['rpmbuild'] + cli.cmd.rpmdefines + [ + "--define '_builddir %s'" % builddir, '--target', 'i686', + '--nocheck', '--quiet', + "--define '_buildrootdir %s'" % buildrootdir, + '-bi', spec + ] _run_command.assert_called_once_with(rpmbuild, shell=True) @@ -619,18 +632,25 @@ class TestLocal(CliTestCase): @patch('pyrpkg.subprocess.check_call') def test_local_with_options(self, check_call): builddir = os.path.join(self.cloned_repo_path, 'this-builddir') + buildrootdir = os.path.join(self.cloned_repo_path, 'this-buildrootdir') - cli_cmd = ['rpkg', '--path', self.cloned_repo_path, '--release', 'rhel-6', '-q', 'local', - '--builddir', builddir, '--arch', 'i686', '--with', 'a', '--without', 'b'] + cli_cmd = [ + 'rpkg', '--path', self.cloned_repo_path, '--release', 'rhel-6', + '-q', 'local', '--builddir', builddir, '--arch', 'i686', + '--with', 'a', '--without', 'b', '--buildrootdir', buildrootdir] with patch('sys.argv', new=cli_cmd): cli = self.new_cli() cli.local() spec = os.path.join(cli.cmd.path, cli.cmd.spec) - rpmbuild = ['rpmbuild'] + cli.cmd.rpmdefines + \ - ['--with', 'a', '--without', 'b', "--define '_builddir %s'" % builddir, - '--target', 'i686', '--quiet', '-ba', spec] + rpmbuild = ['rpmbuild'] + cli.cmd.rpmdefines + [ + '--with', 'a', '--without', 'b', + "--define '_builddir %s'" % builddir, + '--target', 'i686', '--quiet', + "--define '_buildrootdir %s'" % buildrootdir, + '-ba', spec + ] tee = ['tee', '.build-%s-%s.log' % (cli.cmd.ver, cli.cmd.rel)] cmd = '%s | %s; exit "${PIPESTATUS[0]} ${pipestatus[1]}"' % ( @@ -659,17 +679,24 @@ class TestVerifyFiles(CliTestCase): @patch('pyrpkg.Commands._run_command') def test_verify_files_with_options(self, _run_command): builddir = os.path.join(self.cloned_repo_path, 'this-builddir') + buildrootdir = os.path.join(self.cloned_repo_path, 'this-buildrootdir') - cli_cmd = ['rpkg', '--path', self.cloned_repo_path, '--release', 'rhel-6', '-q', - 'verify-files', '--builddir', builddir] + cli_cmd = [ + 'rpkg', '--path', self.cloned_repo_path, '--release', 'rhel-6', + '-q', 'verify-files', '--builddir', builddir, + '--buildrootdir', buildrootdir + ] with patch('sys.argv', new=cli_cmd): cli = self.new_cli() cli.verify_files() spec = os.path.join(cli.cmd.path, cli.cmd.spec) - rpmbuild = ['rpmbuild'] + cli.cmd.rpmdefines + \ - ["--define '_builddir %s'" % builddir, '--quiet', '-bl', spec] + rpmbuild = ['rpmbuild'] + cli.cmd.rpmdefines + [ + "--define '_builddir %s'" % builddir, + "--define '_buildrootdir %s'" % buildrootdir, + '--quiet', '-bl', spec + ] _run_command.assert_called_once_with(rpmbuild, shell=True)