Relocate `extract_version()` & `get_sysd_commit()`
[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 rpm import build as build_rpm
16 import vars
17
18 if __name__ == "__main__":
19     # Accept the version(s) of the build(s) to perform as args
20     # TODO: More docs on ArgParser and argument
21     parser = argparse.ArgumentParser(conflict_handler='resolve')
22
23     new_build_group = parser.add_argument_group("New build")
24     new_build_group.add_argument(
25         "--download_url", help="Tarball to repackage into RPM")
26     new_build_group.add_argument(
27         "--sysd_commit", help="Version of ODL unitfile to package")
28     new_build_group.add_argument(
29         "--changelog_date", help="Date this RPM was defined")
30     new_build_group.add_argument(
31         "--changelog_name", help="Name of person who defined RPM")
32     new_build_group.add_argument(
33         "--changelog_email", help="Email of person who defined RPM")
34
35     # Arguments needed to build RPM from latest snapshot
36     # given a stable major branch
37     latest_snap_group = parser.add_argument_group("Latest snapshot build")
38     latest_snap_group.add_argument("--build-latest-snap", action='store_true',
39                                    help="Build RPM from the latest snpashot")
40     latest_snap_group.add_argument("--major", help="Stable branch from which "
41                                    "to build the snapshot")
42     latest_snap_group.add_argument("--minor", help="Minor version of the "
43                                    "stable branch to build the snapshot")
44     latest_snap_group.add_argument("--sysd_commit",
45                                    help="Version of ODL unitfile to package")
46     latest_snap_group.add_argument("--changelog_name",
47                                    help="Name of person who defined RPM")
48     latest_snap_group.add_argument("--changelog_email",
49                                    help="Email of person who defined RPM")
50     # Print help if no arguments are given
51     if len(sys.argv) == 1:
52         parser.print_help()
53         sys.exit(1)
54
55     # Parse the given args
56     args = parser.parse_args()
57
58     # A dictionary containing essential build variables
59     build = {}
60     # Check if `changelog_date` has been passed as an arg
61     # The current datetime should be the default date for RPM changelog date
62     # but can still accept optional `changelog_date` param
63     # `changelog_date` is in the format: 'Sat Dec 10 2016'
64     # Docs:
65     #   https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
66     if not args.changelog_date:
67         args.changelog_date = datetime.date.today().strftime("%a %b %d %Y")
68
69     # Check if `sysd_commit` has been passed as an arg
70     # Use latest Int/Pack repo commit hash as sysd_commit var
71     # unless passed by param
72     if not args.sysd_commit:
73         args.sysd_commit = vars.get_sysd_commit()
74
75     # If download_url is given, update version info
76     if args.download_url:
77         build.update({"download_url": args.download_url})
78         version = vars.extract_version(args.download_url)
79         build.update(version)
80
81     # Common parameters for all new and snapshot builds
82     build.update({"sysd_commit": args.sysd_commit,
83                   "changelog_name": args.changelog_name,
84                   "changelog_email": args.changelog_email,
85                   "changelog_date": args.changelog_date,
86                   })
87
88     # If the flag `--build-latest-snap` is true, extract information
89     # from the snapshot URL using major version and minor version(optional)
90     # info, else proceed directly to build the RPM
91     if args.build_latest_snap:
92         if args.major:
93             build.update({'version_major': args.major})
94             if args.minor:
95                 build.update({'version_minor': args.minor})
96             build_rpm.build_snapshot_rpm(build)
97     else:
98         build_rpm.build_rpm(build)