Support pkg build for Managed Rel URLs, add tests
[integration/packaging.git] / packages / lib.py
index e3617a34e5f98de9b1fe12affb1fb831c396a26e..cfbf0bd2346029be229955693331692f0dfd495d 100644 (file)
@@ -42,7 +42,7 @@ distro_template = Template("opendaylight-$version_major.$version_minor."
 unitfile_template = Template("opendaylight-$sysd_commit.service")
 unitfile_url_template = Template("https://git.opendaylight.org/gerrit/"
                                  "gitweb?p=integration/packaging.git;a="
-                                 "blob_plain;f=packages/rpm/unitfiles/"
+                                 "blob_plain;f=packages/unitfiles/"
                                  "opendaylight.service;hb=$sysd_commit")
 
 
@@ -61,7 +61,7 @@ def extract_version(url):
     #  distribution-karaf-0.3.4-Lithium-SR4.tar.gz
     # major_version = 3
     # minor_version = 4
-    re_out = re.search(r'\d\.(\d)\.(\d)', url)
+    re_out = re.search(r'\d\.(\d+)\.(\d)', url)
     version["version_major"] = re_out.group(1)
     version["version_minor"] = re_out.group(2)
 
@@ -198,8 +198,9 @@ def get_snap_url(version_major):
     parent_dir_html = urlopen(parent_dir_url).read().decode('utf-8')
 
     # Get most recent minor version of the given major version
-    version_minor = max(re.findall(r'>\d\.{}\.(\d)-SNAPSHOT\/'.format(version_major),
-                                   parent_dir_html))
+    version_minor = max(re.findall(
+                        r'>\d\.{}\.(\d)-SNAPSHOT\/'.format(version_major),
+                        parent_dir_html))
 
     # Dir that contains snapshot builds for the given major version
     snapshot_dir_url = parent_dir_url + "0.{}.{}-SNAPSHOT/".format(
@@ -265,19 +266,34 @@ def get_changelog_date(pkg_type):
         raise ValueError("Unknown package type: {}".format(pkg_type))
 
 
-def get_distro_name_prefix(version_major):
-    """Return Karaf 3 or 4-style distro name prefix based on ODL major version
+def get_distro_name_prefix(version_major, download_url=""):
+    """Return distro name prefix based on ODL major version or distro URL.
 
-    :arg str major_version: OpenDaylight major version umber
-    :return str distro_name_style: Karaf 3 or 4-style distro name prefix
+    :arg str version_major: OpenDaylight major version number
+    :arg str download_url: URL to ODL distribution
+    :return str distro_prefix: MR, Karaf 3 or 4-style distro name prefix
 
     """
+    mrel_prefix = "opendaylight"
+    k3_prefix = "distribution-karaf"
+    k4_prefix = "karaf"
+    mrel_url_base = "https://nexus.opendaylight.org/content/repositories/public/org/opendaylight/integration/opendaylight/"
+    k3_url_base = "https://nexus.opendaylight.org/content/repositories/public/org/opendaylight/integration/distribution-karaf/"
+    k4_url_base = "https://nexus.opendaylight.org/content/repositories/public/org/opendaylight/integration/karaf/"
+
+    if mrel_url_base in download_url:
+        return mrel_prefix
+    elif k3_url_base in download_url:
+        return k3_prefix
+    elif k4_url_base in download_url:
+        return k4_prefix
+
     if int(version_major) < 7:
         # ODL versions before Nitrogen use Karaf 3, distribution-karaf- names
-        return "distribution-karaf"
+        return k3_prefix
     else:
         # ODL versions Nitrogen and after use Karaf 4, karaf- names
-        return "karaf"
+        return k4_prefix
 
 
 def cache_distro(build):
@@ -325,13 +341,13 @@ def cache_distro(build):
             # Get the most recent file in cache dir, hopefully unzipped archive
             unzipped_distro_path = max(cache_dir_ls, key=os.path.getctime)
             print("Extracted: {}".format(unzipped_distro_path))
-            # Remove path from unzipped distro filename, as will cd to dir below
+            # Remove path from 'unzipped_distro_path', as will cd to dir below
             unzipped_distro = os.path.basename(unzipped_distro_path)
-            # Using the full paths here creates those paths in the tarball, which
-            # breaks the build. There's a way to change the working dir during a
-            # single tar command using the system tar binary, but I don't see a
-            # way to do that with Python.
-            # TODO: Is there a good way to do this without changing directories?
+            # Using the full paths here creates those paths in the tarball,
+            # which breaks the build. There's a way to change the working dir
+            # during a single tar command using the system tar binary, but I
+            # don't see a way to do that with Python.
+            # TODO: Can this be done without changing directories?
             # TODO: Try https://goo.gl/XMx5gb
             cwd = os.getcwd()
             os.chdir(cache_dir)
@@ -365,11 +381,15 @@ def cache_sysd(build):
     unitfile_path = os.path.join(cache_dir, unitfile)
     unitfile_tar_path = os.path.join(cache_dir, unitfile_tar)
 
-    # Cache appropriate version of ODL's systemd unit file as a tarball
-    if not os.path.isfile(unitfile_tar_path):
-        # Download ODL's systemd unit file
+    # Download ODL's systemd unit file
+    if not os.path.isfile(unitfile_path):
         urllib.urlretrieve(unitfile_url, unitfile_path)
+        print("Cached: {}".format(unitfile))
+    else:
+        print("Already cached: {}".format(unitfile_path))
 
+    # Cache ODL's systemd unit file as a tarball
+    if not os.path.isfile(unitfile_tar_path):
         # Using the full paths here creates those paths in the tarball, which
         # breaks the build. There's a way to change the working dir during a
         # single tar command using the system tar binary, but I don't see a
@@ -383,11 +403,9 @@ def cache_sysd(build):
             tb.add(unitfile)
         os.chdir(cwd)
 
-        # Remove the now-archived unitfile
-        os.remove(unitfile_path)
         print("Cached: {}".format(unitfile_tar))
     else:
-        print("Already cached: {}".format(unitfile_tar))
+        print("Already cached: {}".format(unitfile_tar_path))
 
     return {"unitfile_tar_path": unitfile_tar_path,
             "unitfile_path": unitfile_path}