Update Neon SR3 repo links
[integration/packaging.git] / packages / rpm / lib.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 imp
14 import os
15 import shutil
16 from string import Template
17 import subprocess
18
19 import specs.build_specs as build_specs
20
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__))
23
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"))
26
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")
31
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")
43
44
45 def build_rpm(build):
46     """Build the RPMs described by the given build description
47
48     :param build: Description of an RPM build
49     :type build: dict
50
51     """
52     # Specialize templates for the given build
53     distro_tar_path = os.path.join(
54         pkg_lib.cache_dir,
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(
65         pkg_lib.cache_dir,
66         pkg_lib.unitfile_template.substitute(build)) + ".tar.gz"
67
68     # Call helper script to build the required RPM .spec files
69     build_specs.build_spec(build)
70
71     # Clean up old rpmbuild dir structure if it exists
72     if os.path.isdir(rpmbuild_dir):
73         shutil.rmtree(rpmbuild_dir)
74
75     # Create rpmbuild dir structure
76     subprocess.call("rpmdev-setuptree")
77
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"]
81
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)
86
87     # Call rpmbuild, build both SRPMs/RPMs
88     subprocess.call(["rpmbuild", "-ba", spec_in_path])
89
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)