Fix Coala Jinja linting, errors
[integration/packaging.git] / packages / rpm / 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 """Build OpenDaylight's RPMs using build description and Jinja2 templates."""
12
13 import os
14 import shutil
15 from string import Template
16 import subprocess
17
18 import cache.cache as cache
19 import specs.build_specs as build_specs
20
21 # Common paths used in this script
22 # This file is assumed to be in the root of the RPM build logic's dir structure
23 project_root = os.path.dirname(os.path.abspath(__file__))
24 cache_dir = os.path.join(project_root, "cache")
25 specs_dir = os.path.join(project_root, "specs")
26 rpmbuild_dir = os.path.join(os.path.expanduser("~"), "rpmbuild")
27 src_in_dir = os.path.join(rpmbuild_dir, "SOURCES")
28 spec_in_dir = os.path.join(rpmbuild_dir, "SPECS")
29 srpm_out_dir = os.path.join(rpmbuild_dir, "SRPMS")
30 rpm_out_dir = os.path.join(rpmbuild_dir, "RPMS", "noarch")
31
32 # Templates that can be specialized into common artifact names per-build
33 odl_template = Template("opendaylight-$version_major.$version_minor."
34                         "$version_patch-$pkg_version.tar.gz")
35 specfile_template = Template("opendaylight-$version_major.$version_minor."
36                              "$version_patch-$pkg_version.spec")
37 unitfile_tb_template = Template("opendaylight-$sysd_commit.service.tar.gz")
38 rpm_template = Template("opendaylight-$version_major.$version_minor."
39                         "$version_patch-$pkg_version.el7.noarch.rpm")
40 srpm_template = Template("opendaylight-$version_major.$version_minor."
41                          "$version_patch-$pkg_version.el7.src.rpm")
42
43
44 def build_rpm(build):
45     """Build the RPMs described by the given build description
46
47     :param build: Description of an RPM build
48     :type build: dict
49
50     """
51     # Specialize a series of name templates for the given build
52     odl_tarball = odl_template.substitute(build)
53     odl_rpm = rpm_template.substitute(build)
54     odl_srpm = srpm_template.substitute(build)
55     odl_specfile = specfile_template.substitute(build)
56     unitfile_tarball = unitfile_tb_template.substitute(build)
57
58     # After building strings from the name templates, build their full path
59     odl_tarball_path = os.path.join(cache_dir, odl_tarball)
60     unitfile_tarball_path = os.path.join(cache_dir, unitfile_tarball)
61     specfile_path = os.path.join(specs_dir, odl_specfile)
62     spec_in_path = os.path.join(spec_in_dir, odl_specfile)
63     rpm_out_path = os.path.join(rpm_out_dir, odl_rpm)
64     srpm_out_path = os.path.join(srpm_out_dir, odl_srpm)
65
66     # Call a helper function to cache the artifacts required for each build
67     cache.cache_build(build)
68
69     # Call helper script to build the required RPM .spec files
70     build_specs.build_spec(build)
71
72     # Clean up old rpmbuild dir structure if it exists
73     if os.path.isdir(rpmbuild_dir):
74         shutil.rmtree(rpmbuild_dir)
75
76     # Create rpmbuild dir structure
77     subprocess.call("rpmdev-setuptree")
78
79     # Move unitfile, tarball and specfile to correct rpmbuild dirs
80     shutil.copy(odl_tarball_path, src_in_dir)
81     shutil.copy(unitfile_tarball_path, src_in_dir)
82     shutil.copy(specfile_path, spec_in_dir)
83
84     # Call rpmbuild, build both SRPMs/RPMs
85     subprocess.call(["rpmbuild", "-ba", spec_in_path])
86
87     # Copy the RPMs/SRPMs from their output dir to the cache dir
88     shutil.copy(rpm_out_path, cache_dir)
89     shutil.copy(srpm_out_path, cache_dir)