]> Frank Brehm's Git Trees - profitbricks/jenkins-build-scripts.git/commitdiff
debian_build.py: Increment or decrement version for develop and experemental build...
authorBenjamin Drung <benjamin.drung@profitbricks.com>
Fri, 4 Jul 2014 09:56:10 +0000 (11:56 +0200)
committerBenjamin Drung <benjamin.drung@profitbricks.com>
Fri, 4 Jul 2014 09:56:10 +0000 (11:56 +0200)
debian_build.py

index 4f0024f0d03500370a447b1f3b6da3920daaad30..548e5beb8c42b49edcb4c56b4266359597a2bbe9 100755 (executable)
@@ -15,6 +15,7 @@ import sys
 from pprint import pformat
 
 # import 3rd parties
+import debian.debian_support
 from debian import changelog
 
 # import local modules
@@ -143,6 +144,65 @@ def upload_to_apt_repository(apt_target, apt_dist, changes_file, logger):
     return success
 
 
+def is_valid_debian_version(version):
+    """Return True if the given version is a valid Debian version string."""
+    is_valid = True
+    try:
+        debian.debian_support.Version(version)
+    except ValueError:
+        is_valid = False
+    return is_valid
+
+
+def version_substitution(logger, pattern, git_commit=None):
+    """Return a version string with the pattern substituted.
+
+    Following substitution variables are supported:
+    {debupstream}   Replaced by the upstream portion of the version number
+                    taken from debian/changelog. For example: if the version
+                    is 1.0-1, this would evaluate to 1.0.
+    {debversion}    Replaced with the version in the changelog
+    {git-commit}    Replaced with the last 7 characters of the git commit that
+                    was built
+    {time}          Replaced by the date and time (UTC) when the package was
+                    built. You can specify a format like {time:%Y%m%d+%H%M}
+    """
+    subst = dict()
+    if git_commit:
+        subst['git-commit'] = git_commit.hexsha[0:7]
+    subst['time'] = datetime.datetime.utcnow()
+    if os.path.isfile('debian/changelog'):
+        changelog_file = open('debian/changelog')
+        changelog = debian.changelog.Changelog(changelog_file, max_blocks=1)
+        subst['debupstream'] = changelog.upstream_version
+        subst['debversion'] = changelog.full_version
+    try:
+        version = pattern.format(**subst)  # pylint: disable=W0142
+    except KeyError as error:
+        key = error.args[0]
+        allowed_keys = ["{debupstream}", "{debversion}", "{git-commit}",
+                        "{time}"]
+        if key in ('debupstream', 'debversion'):
+            msg = ("Cannot substitude {{{key}}} in '{pattern}', because "
+                   "debian/changelog is missing. Please add a debian/changelog"
+                   " file or avoid {{{key}}} in the substitution pattern.")
+        else:
+            msg = ("Key {{{key}}} from pattern '{pattern}' is not a valid "
+                   "substitution. Please use one of the following allowed "
+                   "substitutions: {allowed_keys}")
+        logger.error(msg.format(key=key, pattern=pattern,
+                                allowed_keys=", ".join(allowed_keys)))
+        sys.exit(1)
+    if not is_valid_debian_version(version):
+        logger.error("The generated version '{version}' from the pattern "
+                     "'{pattern}' is not a valid Debian version. The Debian "
+                     "version may contain only alphanumerics and the "
+                     "characters . + - : ~ (full stop, plus, hyphen, colon, "
+                     "tilde).".format(version=version, pattern=pattern))
+        sys.exit(1)
+    return version
+
+
 if __name__ == '__main__':
     logger.debug('Start-up environment:\n\n{env}\n'.format(env=pformat(ENV)))
 
@@ -283,13 +343,12 @@ if __name__ == '__main__':
             ))
             exit_error()
         new_dist = 'unstable'
-        new_version = '{version}~develop{date}+{build}+{commit}{suffix}'.format(
-            version=curr_version,
-            date=daily_date,
-            build=ENV['BUILD_NUMBER'],
-            commit=curr_commit_hexsha[0:7],
-            suffix=options.version_suffix,
-        )
+        if cl.distributions == "UNRELEASED":
+            pattern = "{debversion}~develop{time:%Y%m%d+%H%M}+{build}+{git-commit}"
+        else:
+            pattern = "{debversion}+develop{time:%Y%m%d+%H%M}+{build}+{git-commit}"
+        pattern = pattern.format(build=ENV['BUILD_NUMBER']) + options.version_suffix
+        new_version = version_substitution(logger, pattern, curr_commit)
         pb_suite = 'develop-{dist}'.format(dist=curr_dist)
         reprepro_base = '/srv/pb-{dist}'.format(dist=curr_dist)
         apt_dist = curr_dist + '-dev'
@@ -312,13 +371,12 @@ if __name__ == '__main__':
         else:
             new_dist = 'dev-{0}'.format(local_branch.replace("/", "-"))
             pb_suite = new_dist
-        new_version = '{version}~experimental{date}+{build}+{commit}{suffix}'.format(
-            version=curr_version,
-            date=daily_date,
-            build=ENV['BUILD_NUMBER'],
-            commit=curr_commit_hexsha[0:7],
-            suffix=options.version_suffix,
-        )
+        if cl.distributions == "UNRELEASED":
+            pattern = "{debversion}~experimental{time:%Y%m%d+%H%M}+{build}+{git-commit}"
+        else:
+            pattern = "{debversion}+experimental{time:%Y%m%d+%H%M}+{build}+{git-commit}"
+        pattern = pattern.format(build=ENV['BUILD_NUMBER']) + options.version_suffix
+        new_version = version_substitution(logger, pattern, curr_commit)
         reprepro_base = '/srv/dev-{dist}'.format(dist=curr_dist)
         apt_dist = curr_dist + "-" + local_branch.replace("/", "-")
         apt_target = "apt01-debian-dev"