Build deb's using common build.py 27/61427/5
authorAlok Anand <alok4nand@gmail.com>
Wed, 9 Aug 2017 14:23:51 +0000 (19:53 +0530)
committerDaniel Farrell <dfarrell@redhat.com>
Mon, 21 Aug 2017 21:57:03 +0000 (21:57 +0000)
Refactoring aims at creating a common entry point for both rpms and debs
building logic. Using a common build.py, debain will enjoy the same
advancements made earlier in rpm's build logic.

Change-Id: I038e40c7486cc8243feec9a7bb1dc407018f9b41
Signed-off-by: Alok Anand <alok4nand@gmail.com>
build.py
deb/__init__.py [new file with mode: 0644]
deb/build.py
requirements.txt

index 7fd5d3782e5b302beb97fe1532d79ee8076fcb9b..ac7513281ba6f03fd21c7face47974018112ac84 100755 (executable)
--- a/build.py
+++ b/build.py
@@ -12,31 +12,46 @@ import argparse
 import datetime
 import sys
 
+from deb import build as build_deb
 from rpm import build as build_rpm
 import vars
 
+try:
+    from tzlocal import get_localzone
+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")
+
 if __name__ == "__main__":
     # Accept the version(s) of the build(s) to perform as args
     # TODO: More docs on ArgParser and argument
-    parser = argparse.ArgumentParser(conflict_handler='resolve')
+    parser = argparse.ArgumentParser(add_help=False,
+                                     conflict_handler='resolve')
+    parser._optionals.title = "Required Arguments"
+
+    package_build_group = parser.add_mutually_exclusive_group(required=True)
+    package_build_group.add_argument("--rpm", action="store_true",
+                                     help="Builds RPM package")
+    package_build_group.add_argument("--deb", action="store_true",
+                                     help="Builds DEB package")
 
     new_build_group = parser.add_argument_group("New build")
     new_build_group.add_argument(
-        "--download_url", help="Tarball to repackage into RPM")
+        "--download_url", help="Tarball to repackage into package")
     new_build_group.add_argument(
         "--sysd_commit", help="Version of ODL unitfile to package")
     new_build_group.add_argument(
-        "--changelog_date", help="Date this RPM was defined")
+        "--changelog_date", help="Date this package was defined")
     new_build_group.add_argument(
-        "--changelog_name", help="Name of person who defined RPM")
+        "--changelog_name", help="Name of person who defined package")
     new_build_group.add_argument(
-        "--changelog_email", help="Email of person who defined RPM")
+        "--changelog_email", help="Email of person who defined package")
 
     # Arguments needed to build RPM from latest snapshot
     # given a stable major branch
     latest_snap_group = parser.add_argument_group("Latest snapshot build")
     latest_snap_group.add_argument("--build-latest-snap", action='store_true',
-                                   help="Build RPM from the latest snpashot")
+                                   help="Build package from the latest snpashot")
     latest_snap_group.add_argument("--major", help="Stable branch from which "
                                    "to build the snapshot")
     latest_snap_group.add_argument("--minor", help="Minor version of the "
@@ -44,9 +59,9 @@ if __name__ == "__main__":
     latest_snap_group.add_argument("--sysd_commit",
                                    help="Version of ODL unitfile to package")
     latest_snap_group.add_argument("--changelog_name",
-                                   help="Name of person who defined RPM")
+                                   help="Name of person who defined package")
     latest_snap_group.add_argument("--changelog_email",
-                                   help="Email of person who defined RPM")
+                                   help="Email of person who defined package")
     # Print help if no arguments are given
     if len(sys.argv) == 1:
         parser.print_help()
@@ -57,14 +72,30 @@ if __name__ == "__main__":
 
     # A dictionary containing essential build variables
     build = {}
-    # Check if `changelog_date` has been passed as an arg
-    # The current datetime should be the default date for RPM changelog date
-    # but can still accept optional `changelog_date` param
-    # `changelog_date` is in the format: 'Sat Dec 10 2016'
-    # Docs:
-    #   https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
-    if not args.changelog_date:
-        args.changelog_date = datetime.date.today().strftime("%a %b %d %Y")
+
+    # Check if the package to be created is rpm or deb and initialize timestamp
+    # details with current time for packages accordingly. For details on
+    # strftime please refer : https://docs.python.org/2/library/datetime.html#
+    # strftime-and-strptime-behavior. For details on get_localzone refer :
+    # https://pypi.python.org/pypi/tzlocal
+    if args.rpm:
+        # Building RPM only requires `changelog_date` in the format
+        # "Day Month Date Year" For ex - Mon Jun 21 2017
+        if not args.changelog_date:
+            args.changelog_date = datetime.date.today().strftime("%a %b %d %Y")
+    if args.deb:
+        if not args.changelog_date:
+            # Building Deb requires `changelog_date` and 'changelog_time' in
+            # the format "Day, Date Month Year" For ex - Mon, 21 Jun 2017 and
+            # time along with Time Zone information as UTC offset in format
+            # HH:MM:SS +HHMM". For ex - 15:01:16 +0530
+            args.changelog_date = datetime.date.today().\
+                                                strftime("%a, %d %b %Y")
+            local_tz = get_localzone()
+            args.changelog_time = datetime.datetime.now(local_tz).\
+                strftime("%H:%M:%S %z")
+            # Add comment
+            build.update({"changelog_time": args.changelog_time})
 
     # Check if `sysd_commit` has been passed as an arg
     # Use latest Int/Pack repo commit hash as sysd_commit var
@@ -97,5 +128,9 @@ if __name__ == "__main__":
                   "changelog_email": args.changelog_email,
                   "changelog_date": args.changelog_date,
                   })
-
-    build_rpm.build_rpm(build)
+    if args.rpm:
+        build_rpm.build_rpm(build)
+    elif args.deb:
+        build_deb.build_deb(build)
+    else:
+        raise ValueError("Unknown package type")
diff --git a/deb/__init__.py b/deb/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
index 669dbf020b853374dd957d4c7e5280712e345f8a..90567344134db0afe0ad3b8c5e6401b2eb567574 100755 (executable)
@@ -1,24 +1,14 @@
 #!/usr/bin/env python
-"""Build OpenDaylight's .debs using YAML build configs and Jinja2 templates."""
+"""Build OpenDaylight's .debs using build description and Jinja2 templates."""
 
-import argparse
 import os
 import shutil
 from string import Template
 import subprocess
-import sys
 
 import cache.cache as cache
 import templates.build_debianfiles as build_debfiles
 
-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
-
-
 # Common paths used in this script
 # This file is assumed to be in the root of the .deb build logic's dir
 # structure
@@ -40,7 +30,7 @@ odl_deb_template = Template("opendaylight/opendaylight_$version_major.$version_m
 def build_deb(build):
     """Build the .deb described by the given build description.
 
-    :param build: Description of a debian build, typically from build_vars.yaml
+    :param build: Description of a debian build
     :type build: dict
 
     """
@@ -71,88 +61,3 @@ def build_deb(build):
 
     # 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__":
-    # Load .deb build variables from a YAML config file
-    build_vars_path = os.path.join(project_root, "build_vars.yaml")
-    with open(build_vars_path) as deb_var_file:
-        build_vars = yaml.load(deb_var_file)
-
-    # Accept the version(s) of the build(s) to perform as args
-    parser = argparse.ArgumentParser()
-    existing_build_group = parser.add_argument_group("Existing build")
-    existing_build_group.add_argument(
-        "-v", "--version", action="append", metavar="major minor patch deb",
-        nargs="*", help="Deb version(s) to build"
-    )
-    new_build_group = parser.add_argument_group("New build")
-    new_build_group.add_argument(
-        "--major", help="Major (element) version to build")
-    new_build_group.add_argument("--minor", help="Minor (SR) version to build")
-    new_build_group.add_argument("--patch", help="Patch version to build")
-    new_build_group.add_argument("--deb",   help="Deb version to build")
-    new_build_group.add_argument(
-        "--sysd_commit", help="Version of ODL unitfile to package")
-    new_build_group.add_argument("--codename", help="Codename for ODL version")
-    new_build_group.add_argument(
-        "--download_url", help="Tarball to repackage into .deb")
-    new_build_group.add_argument(
-        "--java_version", help="Java dependency for the ODL release")
-    new_build_group.add_argument(
-        "--changelog_date", help="Date this .deb was defined")
-    new_build_group.add_argument(
-        "--changelog_time", help="Time this .deb was defined")
-    new_build_group.add_argument(
-        "--changelog_name", help="Name of person who defined .deb")
-    new_build_group.add_argument(
-        "--changelog_email", help="Email of person who defined .deb")
-
-    # Print help if no arguments are given
-    if len(sys.argv) == 1:
-        parser.print_help()
-        sys.exit(1)
-
-    # Parse the given args
-    args = parser.parse_args()
-
-    # Build list of .deb builds to perform
-    builds = []
-    if args.version:
-        # Build a list of requested versions as dicts of version components
-        versions = []
-        version_keys = ["version_major", "version_minor", "version_patch",
-                        "pkg_version"]
-        # For each version arg, match all version components to build_vars name
-        for version in args.version:
-            versions.append(dict(zip(version_keys, version)))
-
-        # Find every .deb build that matches any version argument
-        # A passed version "matches" a build when the provided version
-        # components are a subset of the version components of a build. Any
-        # version components that aren't passed are simply not checked, so
-        # they can't fail the match, effectively wild-carding them.
-        for build in build_vars["builds"]:
-            for version in versions:
-                # Converts both dicts' key:value pairs to lists of tuples and
-                # checks that each tuple in the version list is present in the
-                # build list.
-                if all(item in build.items() for item in version.items()):
-                    builds.append(build)
-    else:
-        builds.append({"version_major": args.major,
-                       "version_minor": args.minor,
-                       "version_patch": args.patch,
-                       "pkg_version": args.deb,
-                       "sysd_commit": args.sysd_commit,
-                       "codename": args.codename,
-                       "download_url": args.download_url,
-                       "java_version": args.java_version,
-                       "changelog_date": args.changelog_date,
-                       "changelog_time": args.changelog_time,
-                       "changelog_name": args.changelog_name,
-                       "changelog_email": args.changelog_email})
-
-    for build in builds:
-        build_deb(build)
index 36ee732c52bd0286a6551d56744cbd104bafd89b..7a85332a3e51a87e25b599610ce33693ff76c8c6 100644 (file)
@@ -2,3 +2,4 @@ pyyaml
 jinja2
 bs4
 requests
+tzlocal