Adds the feature to build ODL's .deb package from local tarball 41/45541/2 release/boron
authorAkshita Jha <zenith158@gmail.com>
Tue, 13 Sep 2016 15:07:59 +0000 (20:37 +0530)
committerDaniel Farrell <dfarrell@redhat.com>
Tue, 13 Sep 2016 17:08:32 +0000 (13:08 -0400)
The file `cache/cache.py` caches the tarball required for the given
debian build. `build.py` now uses a local tar.gz file, if it exists;
otherwise `cache/cache.py` downloads the appropriate version of
OpenDaylight's release tarball.

This commit further removes all old debian files created during
previous debian packaging.

It also removes executable permission from `build_vars.yaml`.

Change-Id: Ida69d19b34751bd7f221b8756348b9a77c3decd0
Signed-off-by: Akshita Jha <zenith158@gmail.com>
Signed-off-by: Daniel Farrell <dfarrell@redhat.com>
deb/build.py
deb/build_vars.yaml [changed mode: 0755->0644]
deb/cache/__init__.py [new file with mode: 0644]
deb/cache/cache.py [new file with mode: 0755]
deb/templates/build_debianfiles.py [changed mode: 0644->0755]

index fbfd7cacbcd2aad11b057a085951bd0b8bde1036..ca103ca4dc70140c1452ad5c5e9ee003e5fc6abf 100755 (executable)
@@ -3,6 +3,7 @@
 
 import os
 import sys
+import shutil
 import argparse
 import subprocess
 from string import Template
@@ -14,18 +15,24 @@ except ImportError:
     sys.stderr.write("Else, install the Python libs it installs.\n")
     raise
 
+import cache.cache as cache
 import templates.build_debianfiles as build_debfiles
 
 # Common paths used in this script
 # This file is assumed to be in the root of the .deb build logic's dir structure
 project_root = os.path.dirname(os.path.abspath(__file__))
 
+# Cache directory for OpenDaylight's release tarball
+cache_dir = os.path.join(project_root, "cache")
+
 # Debian templates directory
 templates_dir = os.path.join(project_root, "templates")
 
 # Specialized opendaylight directory for each build
 odl_dir_template = Template("opendaylight/opendaylight-$version_major.$version_minor."
                             "$version_patch-$pkg_version/")
+odl_deb_template = Template("opendaylight/opendaylight_$version_major.$version_minor."
+                            "$version_patch-${pkg_version}_all.deb")
 
 
 def build_deb(build):
@@ -35,12 +42,20 @@ def build_deb(build):
     :type build: dict
 
     """
+    # Specialize a series of name templates for the given build
     odl_dir_name = odl_dir_template.substitute(build)
     odl_dir_path = os.path.join(templates_dir, os.pardir, odl_dir_name)
+    odl_deb = odl_deb_template.substitute(build)
 
     # Call helper script to build the required debian files
     build_debfiles.build_debfiles(build)
 
+    # Call a helper function to cache the artifacts required for each build
+    odl_tarball_path = cache.cache_build(build)
+
+    # Move ODL's tarball to the specialized OpenDaylight directory
+    shutil.copy(odl_tarball_path, odl_dir_path)
+
     # Build debian package
     os.chdir(odl_dir_path)
     subprocess.call(["dpkg-buildpackage", "-us -uc -b", odl_dir_path], shell=True)
@@ -51,6 +66,9 @@ def build_deb(build):
 
     os.chdir(project_root)
 
+    # Copy the .debs from their output dir to the cache dir
+    shutil.copy(odl_deb, cache_dir)
+
 
 # When run as a script, accept a set of builds and execute them
 if __name__ == "__main__":
old mode 100755 (executable)
new mode 100644 (file)
diff --git a/deb/cache/__init__.py b/deb/cache/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/deb/cache/cache.py b/deb/cache/cache.py
new file mode 100755 (executable)
index 0000000..63606dc
--- /dev/null
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+"""Read YAML description of ODL builds and cache the required artifacts."""
+
+import sys
+import os
+import urllib
+
+try:
+    import yaml
+except ImportError:
+    sys.stderr.write("We recommend using our included Vagrant env.\n")
+    sys.stderr.write("Else, install the Python libs it installs.\n")
+    raise
+
+# Path to the directory that contains this file is assumed to be the cache dir
+cache_dir = os.path.dirname(os.path.abspath(__file__))
+
+
+def cache_build(build):
+    """Cache the artifacts required for the given debian build.
+
+    :param build: Description of an ODL build, typically from build_vars.yaml
+    :type build: dict
+
+    """
+    # OpenDaylight's tarball release name for the given build
+    odl_tarball = build["download_url"].split('/')[-1]
+
+    # After getting the string, build the tarball's full path
+    odl_tarball_path = os.path.join(cache_dir, odl_tarball)
+
+    # Cache appropriate version of OpenDaylight's release tarball
+    if not os.path.isfile(odl_tarball_path):
+        print("Downloading: {}".format(build["download_url"]))
+        urllib.urlretrieve(build["download_url"], odl_tarball_path)
+        print("Cached: {}".format(odl_tarball))
+    else:
+        print("Already cached: {}".format(odl_tarball))
+
+    return odl_tarball_path
+
+
+# If run as a script, cache artifacts required for all builds
+if __name__ == "__main__":
+    # Load debian build variables from a YAML config file
+    with open(os.path.join(cache_dir, os.pardir, "build_vars.yaml")) as var_fd:
+        build_vars = yaml.load(var_fd)
+    for build in build_vars["builds"]:
+        odl_tarball_path = cache_build(build)
old mode 100644 (file)
new mode 100755 (executable)
index bd0331d..187dc97
@@ -2,6 +2,7 @@
 """Build debian files from YAML debian config and Jinja2 debian templates."""
 
 import os
+import re
 import sys
 import shutil
 import urllib
@@ -31,8 +32,8 @@ odl_dir_template = Template("opendaylight/opendaylight-$version_major.$version_m
                             "$version_patch-$pkg_version/")
 odl_deb_template = Template("opendaylight/opendaylight_$version_major.$version_minor."
                             "$version_patch-${pkg_version}_all.deb")
-odl_changes_template = Template("opendaylight/opendaylight_$version_major.$version_minor."
-                                "$version_patch-${pkg_version}_all.changes")
+odl_files_template = Template("opendaylight_$version_major.$version_minor."
+                              "$version_patch-${pkg_version}*")
 
 
 def build_debfiles(build):
@@ -46,6 +47,7 @@ def build_debfiles(build):
     :type build: dict
 
     """
+    # Specialize a series of name templates for the given build
     odl_dir_name = odl_dir_template.substitute(build)
     odl_dir_path = os.path.join(templates_dir, os.pardir, odl_dir_name)
 
@@ -59,11 +61,13 @@ def build_debfiles(build):
     if os.path.isfile(odl_deb_path):
         os.remove(odl_deb_path)
 
-    # Delete old .changes file if it exists
-    odl_changes_name = odl_changes_template.substitute(build)
-    odl_changes_path = os.path.join(templates_dir, os.pardir, odl_changes_name)
-    if os.path.isfile(odl_changes_path):
-        os.remove(odl_changes_path)
+    # Delete old .changes, .dsc, .tar.gz files if they exist
+    odl_files_regex = odl_files_template.substitute(build)
+    odl_par_dir = os.path.join(templates_dir, os.pardir, "opendaylight")
+    if os.path.isdir(odl_par_dir):
+        for f in os.listdir(odl_par_dir):
+            if re.search(odl_files_regex, f):
+                os.remove(os.path.join(odl_par_dir, f))
 
     # Create debian directory
     debian_dir_path = os.path.join(odl_dir_path, "debian")
@@ -98,7 +102,7 @@ def build_debfiles(build):
         urllib.urlretrieve(unitfile_url, unitfile_path)
 
 
-# If run as a script, build .spec files for all builds
+# If run as a script, build debian files for all builds
 if __name__ == "__main__":
     # Load debian build variables from a YAML config file
     with open(os.path.join(templates_dir, os.pardir, "build_vars.yaml")) as var_fd: