3 ##############################################################################
4 # Copyright (c) 2016 Daniel Farrell and Others. All rights reserved.
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 ##############################################################################
11 """Build OpenDaylight's RPMs using build description and Jinja2 templates."""
16 from string import Template
19 import specs.build_specs as build_specs
21 # This file is assumed to be in the root of the RPM build logic's dir structure
22 rpm_root = os.path.dirname(os.path.abspath(__file__))
24 # FIXME: Surely there is a better way to do this
25 pkg_lib = imp.load_source("", os.path.join(rpm_root, os.pardir, "lib.py"))
27 # Common paths used in this script
28 rpmbuild_dir = os.path.join(os.path.expanduser("~"), "rpmbuild")
29 src_in_dir = os.path.join(rpmbuild_dir, "SOURCES")
30 spec_in_dir = os.path.join(rpmbuild_dir, "SPECS")
32 # Templates that can be specialized per-build
33 # NB: Templates can't be concatenated with other Templates or strings, or
34 # cast to strings for concatenation. If they could, we would do elegant
35 # refactoring like concatenating paths to templates here and only calling
36 # Template.substitute in the build_rpm function.
37 rpm_template = Template("opendaylight-$version_major.$version_minor."
38 "$version_patch-$pkg_version.el7.noarch.rpm")
39 srpm_template = Template("opendaylight-$version_major.$version_minor."
40 "$version_patch-$pkg_version.el7.src.rpm")
41 spec_template = Template("opendaylight-$version_major.$version_minor."
42 "$version_patch-$pkg_version.spec")
46 """Build the RPMs described by the given build description
48 :param build: Description of an RPM build
52 # Specialize templates for the given build
53 distro_tar_path = os.path.join(
55 pkg_lib.distro_template.substitute(build)) + ".tar.gz"
56 rpm_out_path = os.path.join(rpmbuild_dir, "RPMS", "noarch",
57 rpm_template.substitute(build))
58 spec_in_path = os.path.join(rpmbuild_dir, "SPECS",
59 spec_template.substitute(build))
60 spec_path = os.path.join(rpm_root, "specs",
61 spec_template.substitute(build))
62 srpm_out_path = os.path.join(rpmbuild_dir, "SRPMS",
63 srpm_template.substitute(build))
64 unitfile_tar_path = os.path.join(
66 pkg_lib.unitfile_template.substitute(build)) + ".tar.gz"
68 # Call helper script to build the required RPM .spec files
69 build_specs.build_spec(build)
71 # Clean up old rpmbuild dir structure if it exists
72 if os.path.isdir(rpmbuild_dir):
73 shutil.rmtree(rpmbuild_dir)
75 # Create rpmbuild dir structure
76 subprocess.call("rpmdev-setuptree")
78 # Cache ODL distro and systemd unit file to package
79 distro_tar_path = pkg_lib.cache_distro(build)
80 unitfile_tar_path = pkg_lib.cache_sysd(build)["unitfile_tar_path"]
82 # Move unit file, tarball and specfile to correct rpmbuild dirs
83 shutil.copy(distro_tar_path, src_in_dir)
84 shutil.copy(unitfile_tar_path, src_in_dir)
85 shutil.copy(spec_path, spec_in_dir)
87 # Call rpmbuild, build both SRPMs/RPMs
88 subprocess.call(["rpmbuild", "-ba", spec_in_path])
90 # Copy the RPMs/SRPMs from their output dir to the cache dir
91 shutil.copy(rpm_out_path, pkg_lib.cache_dir)
92 shutil.copy(srpm_out_path, pkg_lib.cache_dir)