Extract version logic from URL in RPM Build Script 06/48106/30
authorGirish Sukhatankar <girish.sukhatankar@colorado.edu>
Tue, 8 Nov 2016 06:38:28 +0000 (23:38 -0700)
committerAlok Anand <alok4nand@gmail.com>
Tue, 27 Dec 2016 14:56:15 +0000 (20:26 +0530)
Add extract_url() function to return ODL version parameters.

Add unit tests for version extraction logic, doc.

Add license headers.

Change-Id: Id765c2cca9ad10c88efb08c6ac2bb98b1b0ef4c8
Signed-off-by: Girish Sukhatankar <girish.sukhatankar@colorado.edu>
Signed-off-by: Daniel Farrell <dfarrell@redhat.com>
Signed-off-by: Alok Anand <alok4nand@gmail.com>
rpm/README.markdown
rpm/build.py [changed mode: 0755->0644]
rpm/test_build.py [new file with mode: 0644]

index f939617067beefb3903ee530555210f73dfd1076..783990471388b14ff3b5a7f6cabd199306bfd46e 100644 (file)
@@ -128,6 +128,16 @@ The changelog entry must follow a specific format. It's best to follow the
 examples provided by existing build definitions closely. The `rpmbuild` tool
 will fail and complain fairly clearly if the format isn't correct.
 
+### Testing Build Logic
+
+Some `build.py` logic is covered by Python unit tests.
+
+To execute them:
+
+```
+$ python -m unittest test_build
+```
+
 ## Building SRPMs/RPMs
 
 The `build.py` helper script is used for building OpenDaylight SRPMs/RPMs.
old mode 100755 (executable)
new mode 100644 (file)
index 88daec7..2ad8bfe
@@ -1,22 +1,31 @@
 #!/usr/bin/env python
+
+##############################################################################
+# Copyright (c) 2016 Daniel Farrell and Others.  All rights reserved.
+#
+# This program and the accompanying materials are made available under the
+# terms of the Eclipse Public License v1.0 which accompanies this distribution,
+# and is available at http://www.eclipse.org/legal/epl-v10.html
+##############################################################################
+
 """Build OpenDaylight's RPMs using YAML build configs and Jinja2 templates."""
 
-import os
-import sys
 import argparse
-import shutil
-import subprocess
 import datetime
+import os
 import re
+import shutil
+import subprocess
+import sys
 
 from string import Template
 from urllib2 import urlopen
-import requests
-from requests.exceptions import HTTPError
 
 try:
-    import yaml
     from bs4 import BeautifulSoup
+    import requests
+    from requests.exceptions import HTTPError
+    import yaml
 except ImportError:
     sys.stderr.write("We recommend using our included Vagrant env.\n")
     sys.stderr.write("Else, do `pip install -r requirements.txt` in a venv.\n")
@@ -48,8 +57,69 @@ srpm_template = Template("opendaylight-$version_major.$version_minor."
                          "$version_patch-$rpm_release.el7.src.rpm")
 
 
+def extract_version(url):
+    """Determine ODL version information from the ODL tarball build URL
+
+    :arg str url: URL of the ODL tarball build for building RPMs
+
+    """
+    # Substitute the part of the build URL not required with empty string
+    date_url = re.sub('distribution-karaf-.*\.tar\.gz$', '', url)
+    # Set date_url as an environment variable for it to be used in a subprocess
+    os.environ["date_url"] = date_url
+    # Extract ODL artifact's date by scraping data from the build URL
+    odl_date = subprocess.Popen(
+        "curl -s $date_url | grep tar.gz -A1 | tail -n1 | sed \"s/<td>//g\""
+        "| sed \"s/\\n//g\" | awk '{print $3,$2,$6}' ",
+        shell=True, stdout=subprocess.PIPE,
+        stdin=subprocess.PIPE).stdout.read().rstrip().strip("</td>")
+    date = datetime.datetime.strptime(odl_date, "%d %b %Y").strftime('%Y%m%d')
+
+    if "autorelease" in url:
+        # Search the ODL autorelease build URL to match the Build ID that
+        # follows "autorelease-". eg:
+        # https://nexus.opendaylight.org/content/repositories/autorelease-1533/
+        #  org/opendaylight/integration/distribution-karaf/0.4.4-Beryllium-SR4/
+        # build_id = 1533
+        build_id = re.search(r'\/(autorelease)-([0-9]+)\/', url).group(2)
+        rpm_release = "0.1." + date + "rel" + build_id
+    elif "snapshot" in url:
+        # Search the ODL snapshot build URL to match the date and the Build ID
+        # that are between "distribution-karaf" and ".tar.gz".
+        # eg: https://nexus.opendaylight.org/content/repositories/
+        #      opendaylight.snapshot/org/opendaylight/integration/
+        #      distribution-karaf/0.6.0-SNAPSHOT/
+        #      distribution-karaf-0.6.0-20161201.031047-2242.tar.gz
+        # build_id = 2242
+        # date = 20161201
+        odl_rpm = re.search(
+            r'\/(distribution-karaf)-'
+            r'([0-9]\.[0-9]\.[0-9])-([0-9]+)\.([0-9]+)-([0-9]+)\.(tar\.gz)',
+            url)
+        rpm_release = "0.1." + odl_rpm.group(3) + "snap" + odl_rpm.group(5)
+    elif "public" or "opendaylight.release" in url:
+        rpm_release = "1"
+    else:
+        raise ValueError("Unrecognized URL {}".format(url))
+
+    version = {}
+    # Search the ODL build URL to match 0.major.minor-codename-SR and extarct
+    # version information. eg: release:
+    # https://nexus.opendaylight.org/content/repositories/public/org/
+    #  opendaylight/integration/distribution-karaf/0.3.3-Lithium-SR3/
+    #  distribution-karaf-0.3.3-Lithium-SR3.tar.gz
+    #     match: 0.3.3-Lithium-SR3
+    odl_version = re.search(r'\/(\d)\.(\d)\.(\d).(.*)\/', url)
+    version["version_major"] = odl_version.group(2)
+    version["version_minor"] = odl_version.group(3)
+    version["version_patch"] = "0"
+    version["rpm_release"] = rpm_release
+    version["codename"] = odl_version.group(4)
+    return version
+
+
 def build_rpm(build):
-    """Build the RPMs described by the given build description.
+    """Build the RPMs described by the given build description
 
     :param build: Description of an RPM build, typically from build_vars.yaml
     :type build: dict
diff --git a/rpm/test_build.py b/rpm/test_build.py
new file mode 100644 (file)
index 0000000..60ac18e
--- /dev/null
@@ -0,0 +1,93 @@
+
+##############################################################################
+# Copyright (c) 2016 Daniel Farrell.  All rights reserved.
+#
+# This program and the accompanying materials are made available under the
+# terms of the Eclipse Public License v1.0 which accompanies this distribution,
+# and is available at http://www.eclipse.org/legal/epl-v10.html
+##############################################################################
+
+"""Tests for package build logic."""
+
+import unittest
+
+import build
+
+
+class TestExtractVersion(unittest.TestCase):
+
+    """Test logic to extract ODL versions from artifact URLs."""
+
+    nexus_url = "https://nexus.opendaylight.org/content/repositories"
+
+    def test_beryllium_release_url(self):
+        """Test URL of ODL Beryllium release."""
+        url = "%s/public/org/opendaylight/integration/distribution-karaf/0.4.0-Beryllium/distribution-karaf-0.4.0-Beryllium.tar.gz" %self.nexus_url  # noqa
+        version = build.extract_version(url)
+        self.assertEqual(version["version_major"], "4")
+        self.assertEqual(version["version_minor"], "0")
+        self.assertEqual(version["version_patch"], "0")
+        self.assertEqual(version["rpm_release"], "1")
+
+    def test_beryllium_sr4_release_url(self):
+        """Test URL of ODL Beryllium SR4 release."""
+        url = "%s/opendaylight.release/org/opendaylight/integration/distribution-karaf/0.4.4-Beryllium-SR4/distribution-karaf-0.4.4-Beryllium-SR4.tar.gz" %self.nexus_url  # noqa
+        version = build.extract_version(url)
+        self.assertEqual(version["version_major"], "4")
+        self.assertEqual(version["version_minor"], "4")
+        self.assertEqual(version["version_patch"], "0")
+        self.assertEqual(version["rpm_release"], "1")
+
+    def test_boron_release_url(self):
+        """Test URL of ODL Boron release."""
+        url = "%s/opendaylight.release/org/opendaylight/integration/distribution-karaf/0.5.0-Boron/distribution-karaf-0.5.0-Boron.tar.gz" %self.nexus_url  # noqa
+        version = build.extract_version(url)
+        self.assertEqual(version["version_major"], "5")
+        self.assertEqual(version["version_minor"], "0")
+        self.assertEqual(version["version_patch"], "0")
+        self.assertEqual(version["rpm_release"], "1")
+
+    def test_boron_sr1_release_url(self):
+        """Test URL of ODL Boron SR1 release."""
+        url = "%s/opendaylight.release/org/opendaylight/integration/distribution-karaf/0.5.1-Boron-SR1/distribution-karaf-0.5.1-Boron-SR1.tar.gz" %self.nexus_url  # noqa
+        version = build.extract_version(url)
+        self.assertEqual(version["version_major"], "5")
+        self.assertEqual(version["version_minor"], "1")
+        self.assertEqual(version["version_patch"], "0")
+        self.assertEqual(version["rpm_release"], "1")
+
+    def test_boron_sr2_autorelease_url(self):
+        """Test URL of ODL Boron SR2 autorelease."""
+        url = "%s/autorelease-1599/org/opendaylight/integration/distribution-karaf/0.5.2-Boron-SR2/distribution-karaf-0.5.2-Boron-SR2.tar.gz" %self.nexus_url  # noqa
+        version = build.extract_version(url)
+        self.assertEqual(version["version_major"], "5")
+        self.assertEqual(version["version_minor"], "2")
+        self.assertEqual(version["version_patch"], "0")
+        self.assertEqual(version["rpm_release"], "0.1.20161211rel1599")
+
+    def test_carbon_autorelease_url(self):
+        """Test URL of ODL Carbon autorelease."""
+        url = "%s/autorelease-1582/org/opendaylight/integration/distribution-karaf/0.6.0-Carbon/distribution-karaf-0.6.0-Carbon.tar.gz" %self.nexus_url  # noqa
+        version = build.extract_version(url)
+        self.assertEqual(version["version_major"], "6")
+        self.assertEqual(version["version_minor"], "0")
+        self.assertEqual(version["version_patch"], "0")
+        self.assertEqual(version["rpm_release"], "0.1.20161117rel1582")
+
+    def test_boron_snapshot_url(self):
+        """Test URL of ODL Boron SR2 snapshot."""
+        url = "%s/opendaylight.snapshot/org/opendaylight/integration/distribution-karaf/0.5.2-SNAPSHOT/distribution-karaf-0.5.2-20161212.010649-530.tar.gz" %self.nexus_url  # noqa
+        version = build.extract_version(url)
+        self.assertEqual(version["version_major"], "5")
+        self.assertEqual(version["version_minor"], "2")
+        self.assertEqual(version["version_patch"], "0")
+        self.assertEqual(version["rpm_release"], "0.1.20161212snap530")
+
+    def test_carbon_snapshot_url(self):
+        """Test URL of ODL Carbon snapshot."""
+        url = "%s/opendaylight.snapshot/org/opendaylight/integration/distribution-karaf/0.6.0-SNAPSHOT/distribution-karaf-0.6.0-20161212.173815-2486.tar.gz" %self.nexus_url  # noqa
+        version = build.extract_version(url)
+        self.assertEqual(version["version_major"], "6")
+        self.assertEqual(version["version_minor"], "0")
+        self.assertEqual(version["version_patch"], "0")
+        self.assertEqual(version["rpm_release"], "0.1.20161212snap2486")