#411 Permit setting arbitrary rpm macros during build
Merged 4 years ago by onosek. Opened 4 years ago by jcpunk.
Unknown source define  into  master

file modified
+47 -11
@@ -2372,11 +2372,11 @@

          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 @@

              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 @@

          # 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 @@

              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 @@

          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 @@

          `.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 @@

                          % 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 @@

  

          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 @@

              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 @@

          # 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 @@

  

          :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 @@

          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 @@

                  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 @@

          # 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))

file modified
+44 -5
@@ -529,6 +529,9 @@

              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 @@

  

          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 @@

          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 @@

  

      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 @@

  

          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 @@

  

      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 @@

  

      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 @@

          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):

file modified
+18 -2
@@ -534,6 +534,20 @@

              ['--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 @@

          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 @@

  

          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)

This should resolve https://pagure.io/rpkg/issue/379 along with the related fedpkg feature request.

rebased onto 4a522f7ffadba45e076c0c0adbb524691d9a0f7b

4 years ago

rebased onto b03edcef9ed9697ed17fac46efd803c519d5554c

4 years ago

rebased onto aaa85ae34c8b4760102578e6d0556c1abb6e4127

4 years ago

It's safer to add new arguments to the end. There could very well be a user of this method that calls it as compile("abc", False, "/my/builddir"). Such call would stop working.

This would add also --arch option to srpm command. It doesn't make much sense there, but I guess it does not hurt either.

I'll get those fixed up shortly. I'll probably add the other default rpm args (builddir/buildrootdir) to srpm as well.

rebased onto 497b75c6a4d8bcc79b1d0ebb849e965d33172f92

4 years ago

rebased onto 7517e9ef8c5b4ed4683f41f16698cc3fdf341d9e

4 years ago

rebased onto 9a485325d94c24664bc96f55c35987c0a59ea8b8

4 years ago

I'm not sure how to resolve the last jenkins failure.

pretty please pagure-ci rebuild

4 years ago

rebased onto 1895d90

4 years ago

Tests are OK now, merging.
Thanks for your contribution.

Pull-Request has been merged by onosek

4 years ago