Build deb's using common build.py
[integration/packaging.git] / build.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2016 Daniel Farrell and Others.  All rights reserved.
5 #
6 # This program and the accompanying materials are made available under the
7 # terms of the Eclipse Public License v1.0 which accompanies this distribution,
8 # and is available at http://www.eclipse.org/legal/epl-v10.html
9 ##############################################################################
10
11 import argparse
12 import datetime
13 import sys
14
15 from deb import build as build_deb
16 from rpm import build as build_rpm
17 import vars
18
19 try:
20     from tzlocal import get_localzone
21 except ImportError:
22     sys.stderr.write("we recommend using our included Vagrant env.\n")
23     sys.stderr.write("Else, do `pip install -r requirements.txt` in a venv")
24
25 if __name__ == "__main__":
26     # Accept the version(s) of the build(s) to perform as args
27     # TODO: More docs on ArgParser and argument
28     parser = argparse.ArgumentParser(add_help=False,
29                                      conflict_handler='resolve')
30     parser._optionals.title = "Required Arguments"
31
32     package_build_group = parser.add_mutually_exclusive_group(required=True)
33     package_build_group.add_argument("--rpm", action="store_true",
34                                      help="Builds RPM package")
35     package_build_group.add_argument("--deb", action="store_true",
36                                      help="Builds DEB package")
37
38     new_build_group = parser.add_argument_group("New build")
39     new_build_group.add_argument(
40         "--download_url", help="Tarball to repackage into package")
41     new_build_group.add_argument(
42         "--sysd_commit", help="Version of ODL unitfile to package")
43     new_build_group.add_argument(
44         "--changelog_date", help="Date this package was defined")
45     new_build_group.add_argument(
46         "--changelog_name", help="Name of person who defined package")
47     new_build_group.add_argument(
48         "--changelog_email", help="Email of person who defined package")
49
50     # Arguments needed to build RPM from latest snapshot
51     # given a stable major branch
52     latest_snap_group = parser.add_argument_group("Latest snapshot build")
53     latest_snap_group.add_argument("--build-latest-snap", action='store_true',
54                                    help="Build package from the latest snpashot")
55     latest_snap_group.add_argument("--major", help="Stable branch from which "
56                                    "to build the snapshot")
57     latest_snap_group.add_argument("--minor", help="Minor version of the "
58                                    "stable branch to build the snapshot")
59     latest_snap_group.add_argument("--sysd_commit",
60                                    help="Version of ODL unitfile to package")
61     latest_snap_group.add_argument("--changelog_name",
62                                    help="Name of person who defined package")
63     latest_snap_group.add_argument("--changelog_email",
64                                    help="Email of person who defined package")
65     # Print help if no arguments are given
66     if len(sys.argv) == 1:
67         parser.print_help()
68         sys.exit(1)
69
70     # Parse the given args
71     args = parser.parse_args()
72
73     # A dictionary containing essential build variables
74     build = {}
75
76     # Check if the package to be created is rpm or deb and initialize timestamp
77     # details with current time for packages accordingly. For details on
78     # strftime please refer : https://docs.python.org/2/library/datetime.html#
79     # strftime-and-strptime-behavior. For details on get_localzone refer :
80     # https://pypi.python.org/pypi/tzlocal
81     if args.rpm:
82         # Building RPM only requires `changelog_date` in the format
83         # "Day Month Date Year" For ex - Mon Jun 21 2017
84         if not args.changelog_date:
85             args.changelog_date = datetime.date.today().strftime("%a %b %d %Y")
86     if args.deb:
87         if not args.changelog_date:
88             # Building Deb requires `changelog_date` and 'changelog_time' in
89             # the format "Day, Date Month Year" For ex - Mon, 21 Jun 2017 and
90             # time along with Time Zone information as UTC offset in format
91             # HH:MM:SS +HHMM". For ex - 15:01:16 +0530
92             args.changelog_date = datetime.date.today().\
93                                                 strftime("%a, %d %b %Y")
94             local_tz = get_localzone()
95             args.changelog_time = datetime.datetime.now(local_tz).\
96                 strftime("%H:%M:%S %z")
97             # Add comment
98             build.update({"changelog_time": args.changelog_time})
99
100     # Check if `sysd_commit` has been passed as an arg
101     # Use latest Int/Pack repo commit hash as sysd_commit var
102     # unless passed by param
103     if not args.sysd_commit:
104         args.sysd_commit = vars.get_sysd_commit()
105
106     # If the flag `--build-latest-snap` is true, extract information
107     # from the snapshot URL using major version and minor version(optional)
108     if args.build_latest_snap:
109         if args.major:
110             build.update({'version_major': args.major})
111             if args.minor:
112                 build.update({'version_minor': args.minor})
113             args.download_url = vars.get_snap_url(args.major, args.minor)
114
115     # If download_url is given, update version info
116     if args.download_url:
117         build.update({"download_url": args.download_url})
118         version = vars.extract_version(args.download_url)
119         build.update(version)
120
121     java_version_required = vars.get_java_version(build['version_major'])
122
123     # Common parameters for all new and snapshot builds
124     build.update({"download_url": args.download_url,
125                   "sysd_commit": args.sysd_commit,
126                   "java_version": java_version_required,
127                   "changelog_name": args.changelog_name,
128                   "changelog_email": args.changelog_email,
129                   "changelog_date": args.changelog_date,
130                   })
131     if args.rpm:
132         build_rpm.build_rpm(build)
133     elif args.deb:
134         build_deb.build_deb(build)
135     else:
136         raise ValueError("Unknown package type")