rpm: add version parsing functions to pkg policy
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Mon, 23 Apr 2012 09:51:08 +0000 (12:51 +0300)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Fri, 14 Nov 2014 12:45:07 +0000 (14:45 +0200)
Adds functions for version string generation and parsing. These are
intended for parsing version strings taken e.g. from rpm filename or
changelogs.

Also, take these new version parsing functions into use in the rpm gbp
scripts.

Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
Signed-off-by: Ed Bartosh <eduard.bartosh@intel.com>
gbp/rpm/policy.py
gbp/scripts/buildpackage_rpm.py
gbp/scripts/import_orig_rpm.py
gbp/scripts/pq_rpm.py

index f8cb863..5818fcd 100644 (file)
@@ -70,3 +70,73 @@ class RpmPkgPolicy(PkgPolicy):
             return True
         return False
 
+    @classmethod
+    def split_full_version(cls, version):
+        """
+        Parse full version string and split it into individual "version
+        components", i.e. upstreamversion, epoch and release
+
+        @param version: full version of a package
+        @type version: C{str}
+        @return: individual version components
+        @rtype: C{dict}
+
+        >>> RpmPkgPolicy.split_full_version("1")
+        {'release': None, 'epoch': None, 'upstreamversion': '1'}
+        >>> RpmPkgPolicy.split_full_version("1.2.3-5.3")
+        {'release': '5.3', 'epoch': None, 'upstreamversion': '1.2.3'}
+        >>> RpmPkgPolicy.split_full_version("3:1.2.3")
+        {'release': None, 'epoch': '3', 'upstreamversion': '1.2.3'}
+        >>> RpmPkgPolicy.split_full_version("3:1-0")
+        {'release': '0', 'epoch': '3', 'upstreamversion': '1'}
+        """
+        epoch = None
+        upstreamversion = None
+        release = None
+
+        e_vr = version.split(":", 1)
+        if len(e_vr) == 1:
+            v_r = e_vr[0].split("-", 1)
+        else:
+            epoch = e_vr[0]
+            v_r = e_vr[1].split("-", 1)
+        upstreamversion = v_r[0]
+        if len(v_r) > 1:
+            release = v_r[1]
+
+        return {'epoch': epoch,
+                'upstreamversion': upstreamversion,
+                'release': release}
+
+    @classmethod
+    def compose_full_version(cls, evr):
+        """
+        Compose a full version string from individual "version components",
+        i.e. epoch, version and release
+
+        @param evr: dict of version components
+        @type evr: C{dict} of C{str}
+        @return: full version
+        @rtype: C{str}
+
+        >>> RpmPkgPolicy.compose_full_version({'epoch': '', 'upstreamversion': '1.0'})
+        '1.0'
+        >>> RpmPkgPolicy.compose_full_version({'epoch': '2', 'upstreamversion': '1.0', 'release': None})
+        '2:1.0'
+        >>> RpmPkgPolicy.compose_full_version({'epoch': None, 'upstreamversion': '1', 'release': '0'})
+        '1-0'
+        >>> RpmPkgPolicy.compose_full_version({'epoch': '2', 'upstreamversion': '1.0', 'release': '2.3'})
+        '2:1.0-2.3'
+        >>> RpmPkgPolicy.compose_full_version({'epoch': '2', 'upstreamversion': '', 'release': '2.3'})
+        """
+        if 'upstreamversion' in evr and evr['upstreamversion']:
+            version = ""
+            if 'epoch' in evr and evr['epoch']:
+                version += "%s:" % evr['epoch']
+            version += evr['upstreamversion']
+            if 'release' in evr and evr['release']:
+                version += "-%s" % evr['release']
+            if version:
+                return version
+        return None
+
index 724922c..028f612 100755 (executable)
@@ -147,7 +147,7 @@ def pristine_tar_build_orig(repo, orig_file, output_dir, options):
 def get_upstream_tree(repo, spec, options):
     """Determine the upstream tree from the given options"""
     if options.upstream_tree.upper() == 'TAG':
-        upstream_tree = repo.version_to_tag(options.upstream_tag, spec.version, vendor="Upstream")
+        upstream_tree = repo.version_to_tag(options.upstream_tag, dict(upstreamversion=spec.version), "Upstream")
     elif options.upstream_tree.upper() == 'BRANCH':
         if not repo.has_branch(options.upstream_branch):
             raise GbpError("%s is not a valid branch" % options.upstream_branch)
@@ -470,7 +470,7 @@ def main(argv):
         # Tag (note: tags the exported version)
         if options.tag or options.tag_only:
             gbp.log.info("Tagging %s" % spec.version)
-            tag = repo.version_to_tag(options.packaging_tag, spec.version, vendor=options.vendor)
+            tag = repo.version_to_tag(options.packaging_tag, dict(upstreamversion=spec.version), options.vendor)
             if options.retag and repo.has_tag(tag):
                 repo.delete_tag(tag)
             repo.create_tag(name=tag, msg="%s release %s" % (options.vendor, spec.version),
@@ -503,8 +503,9 @@ def main(argv):
     if not options.tag_only:
         if spec and options.notify:
             summary = "Gbp-rpm %s" % ["failed", "successful"][not retval]
+            pkg_evr = {'upstreamversion': spec.version}
             message = ("Build of %s %s %s" % (spec.name,
-                            spec.version,
+                            RpmPkgPolicy.compose_full_version(pkg_evr),
                             ["failed", "succeeded"][not retval]))
             if not gbp.notifications.notify(summary, message, options.notify):
                 gbp.log.err("Failed to send notification")
index 3978eff..e6306fd 100755 (executable)
@@ -266,7 +266,7 @@ def main(argv):
                 gbp.log.info("Pristine-tar: commiting %s" % pristine_orig)
                 repo.pristine_tar.commit(pristine_orig, options.upstream_branch)
 
-            tag = repo.version_to_tag(options.upstream_tag, version, vendor="Upstream")
+            tag = repo.version_to_tag(options.upstream_tag, dict(upstreamversion=version), "Upstream")
             repo.create_tag(name=tag,
                             msg="Upstream version %s" % version,
                             commit=commit,
index b1a10ca..0e5ac6d 100755 (executable)
@@ -143,7 +143,7 @@ def export_patches(repo, branch, options):
         raise GbpError, "Can't parse spec"
 
     # Find upstream version
-    upstream_commit = repo.find_version(options.upstream_tag, spec.version, vendor="Upstream")
+    upstream_commit = repo.find_version(options.upstream_tag, dict(upstreamversion=spec.version), "Upstream")
     if not upstream_commit:
         raise GbpError, ("Couldn't find upstream version %s. Don't know on what base to import." % spec.version)
 
@@ -234,7 +234,7 @@ def import_spec_patches(repo, branch, options):
         raise GbpError, "Can't parse spec"
 
     # Find upstream version
-    commit = repo.find_version(options.upstream_tag, spec.version, vendor="Upstream")
+    commit = repo.find_version(options.upstream_tag, dict(upstreamversion=spec.version), "Upstream")
     if commit:
         commits=[commit]
     else:
@@ -296,7 +296,7 @@ def rebase_pq(repo, branch, options):
         raise GbpError, "Can't parse spec"
 
     # Find upstream version
-    upstream_commit = repo.find_version(options.upstream_tag, spec.version, vendor="Upstream")
+    upstream_commit = repo.find_version(options.upstream_tag, dict(upstreamversion=spec.version), "Upstream")
     if not upstream_commit:
         raise GbpError, ("Couldn't find upstream version %s. Don't know on what base to import." % spec.version)