Extract version logic from URL in RPM Build Script
[integration/packaging.git] / rpm / build.py
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