Do a 3-way version check (upstream, debian, specfile)
authorRémi Cardona <remi.cardona@logilab.fr>
Mon, 03 Mar 2014 10:11:00 +0100
changeset 1181 4e611b5e2508
parent 1180 f90937f86e89
child 1182 4812f27d65bb
Do a 3-way version check (upstream, debian, specfile) - check versions of rpm and debian packages independently (if they exist) - fix error message when more than one spec file is found Closes #223076.
lgp/build.py
lgp/setupinfo.py
lgp/utils.py
--- a/lgp/build.py	Fri Feb 28 15:03:18 2014 +0100
+++ b/lgp/build.py	Mon Mar 03 10:11:00 2014 +0100
@@ -228,9 +228,20 @@
             http://www.debian.org/doc/debian-policy/ch-source.html
         """
         has_debian_dir = osp.isdir('debian')
+        # Ugly code, does the job for the moment
+        specfile = self.config.specfile
+        if specfile is None:
+            specfiles = glob('*.spec')
+            try:
+                specfile, = specfiles
+            except ValueError:
+                pass
         if has_debian_dir:
-            # _check_version_mismatch() is 100% Debian-specific
-            self._check_version_mismatch()
+            self._check_debian_version_mismatch()
+        if specfile is not None:
+            self._check_rpm_version_mismatch(specfile)
+
+        if has_debian_dir:
             is_initial_debian_revision = self.is_initial_debian_revision()
         else:
             # always rebuild the source/orig tarball if debian/ is missing,
@@ -319,7 +330,7 @@
                     self.logger.error("unable to find the '.spec' file")
                 else:
                     self.logger.error("more than one spec file found")
-                self.logger.error("please use the '--specfile' option")
+                self.logger.error("please use the '--spec' option")
                 raise LGPException("cannot build source distribution")
         specfile = osp.abspath(specfile)
 
--- a/lgp/setupinfo.py	Fri Feb 28 15:03:18 2014 +0100
+++ b/lgp/setupinfo.py	Mon Mar 03 10:11:00 2014 +0100
@@ -21,6 +21,7 @@
 import sys
 import os
 import stat
+from glob import glob
 import os.path as osp
 import logging
 from string import Template
@@ -379,9 +380,8 @@
         versions = self.get_debian_version().rsplit('-', 1)
         return versions
 
-    def _check_version_mismatch(self):
+    def _check_debian_version_mismatch(self):
         upstream_version = self.get_upstream_version()
-        #debian_upstream_version = self.get_versions()[0]
         debian_upstream_version = self.get_debian_version().rsplit('-', 1)[0]
         assert debian_upstream_version == self.get_versions()[0], "get_versions() failed"
         if upstream_version != debian_upstream_version:
@@ -389,6 +389,16 @@
             msg %= (upstream_version, debian_upstream_version)
             raise LGPException(msg)
 
+    def _check_rpm_version_mismatch(self, specfile):
+        upstream_version = self.get_upstream_version()
+        rpm_upstream_version = utils.parse_rpm_version(specfile)
+        self.logger.debug('retrieve rpm version from %s: %s' %
+                          (specfile, rpm_upstream_version))
+        if upstream_version != rpm_upstream_version:
+            msg = "version mismatch: upstream says '%s' and spec file says '%s'"
+            msg %= (upstream_version, rpm_upstream_version)
+            raise LGPException(msg)
+
     def get_basetgz(self, distrib, arch, check=True):
         basetgz = osp.join(self.config.basetgz, "%s-%s.tgz" % (distrib, arch))
         if check and not osp.exists(basetgz):
--- a/lgp/utils.py	Fri Feb 28 15:03:18 2014 +0100
+++ b/lgp/utils.py	Mon Mar 03 10:11:00 2014 +0100
@@ -29,6 +29,11 @@
 from debian.deb822 import Deb822
 from debian.changelog import Changelog, ChangelogParseError
 
+try:
+    import rpm
+except ImportError:
+    rpm = None
+
 from logilab.devtools.lgp import LGP_SUITES
 from logilab.devtools.lgp.exceptions import (ArchitectureException,
                                              DistributionException,
@@ -233,6 +238,16 @@
     except ChangelogParseError:
         raise LGPException("Malformed Debian changelog '%s'" % changelog)
 
+def parse_rpm_version(specfile):
+    if rpm is None:
+        raise LGPException('rpm unsupported: missing rpm python module')
+    try:
+        spec = rpm.spec(specfile)
+        return spec.sourceHeader['version']
+    except rpm.error:
+        raise LGPException('python-rpm specfile error: '
+                           'cannot determine version')
+
 @contextmanager
 def tempdir(keep_tmpdir=False):
     """create new build temporary context