Fix issues in deb pipeline.
[integration/packaging.git] / packages / deb / lib.py
1 #!/usr/bin/env python
2 """Build OpenDaylight's .debs using build description and Jinja2 templates."""
3
4 import imp
5 import os
6 import shutil
7 from string import Template
8 import subprocess
9
10 import templates.build_debianfiles as build_debfiles
11
12 # This file is assumed to be in the root of the Deb build logic's dir structure
13 deb_root = os.path.dirname(os.path.abspath(__file__))
14
15 # FIXME: Surely there is a better way to do this
16 pkg_lib = imp.load_source("", os.path.join(deb_root, os.pardir, "lib.py"))
17
18 # Common paths used in this script
19 templates_dir = os.path.join(deb_root, "templates")
20
21 # Templates that can be specialized per-build
22 # NB: Templates can't be concatenated with other Templates or strings, or
23 # cast to strings for concatenation. If they could, we would do elegant
24 # refactoring like concatenating paths to templates here and only calling
25 # Template.substitute in the build_rpm function.
26 deb_template = Template("opendaylight/opendaylight_$version_major."
27                         "$version_minor.$version_patch-${pkg_version}_all.deb")
28 src_in_dir_template = Template("opendaylight/opendaylight-$version_major."
29                                "$version_minor.$version_patch-$pkg_version/")
30 cfg_in_dir_template = Template("opendaylight/opendaylight-$version_major."
31                                "$version_minor.$version_patch-$pkg_version/"
32                                "debian")
33
34
35 def build_deb(build):
36     """Build the .deb described by the given build description.
37
38     :param build: Description of a Debian build
39     :type build: dict
40
41     """
42     # Specialize templates for the given build
43     cfg_in_dir = os.path.join(
44         deb_root,
45         cfg_in_dir_template.substitute(build))
46     control_file_path = os.path.join(
47         deb_root,
48         src_in_dir_template.substitute(build),
49         "debian/control")
50     deb = os.path.join(deb_root, deb_template.substitute(build))
51     src_in_dir_path = os.path.join(
52         deb_root,
53         src_in_dir_template.substitute(build))
54
55     # Cache ODL distro and systemd unit file to package
56     distro_tar_path = pkg_lib.cache_distro(build)
57     distro_tar_name = os.path.basename(distro_tar_path)
58     build['tarball_name'] = distro_tar_name
59     unitfile_path = pkg_lib.cache_sysd(build)["unitfile_path"]
60
61     # Call helper script to build the required Debian files
62     build_debfiles.build_debfiles(build)
63
64     # Copy ODL tarball into src input directory
65     shutil.copy(distro_tar_path, src_in_dir_path)
66
67     # Copy ODL systemd unit file to src input directory
68     shutil.copy(unitfile_path, cfg_in_dir)
69
70     # Build Debian package
71     os.chdir(src_in_dir_path)
72     subprocess.call(["dpkg-buildpackage", "-us -uc -b", src_in_dir_path],
73                     shell=True)
74
75     # Build package that includes build dependencies
76     subprocess.call(["sudo mk-build-deps -i " + control_file_path], shell=True)
77     os.chdir(deb_root)
78
79     # Copy the .debs from their output dir to the cache dir
80     shutil.copy(deb, pkg_lib.cache_dir)