Fix Coala Jinja linting, errors
[integration/packaging.git] / packages / deb / build.py
1 #!/usr/bin/env python
2 """Build OpenDaylight's .debs using build description and Jinja2 templates."""
3
4 import os
5 import shutil
6 from string import Template
7 import subprocess
8
9 import cache.cache as cache
10 import templates.build_debianfiles as build_debfiles
11
12 # Common paths used in this script
13 # This file is assumed to be in the root of the .deb build logic's dir
14 # structure
15 project_root = os.path.dirname(os.path.abspath(__file__))
16
17 # Cache directory for OpenDaylight's release tarball
18 cache_dir = os.path.join(project_root, "cache")
19
20 # Debian templates directory
21 templates_dir = os.path.join(project_root, "templates")
22
23 # Specialized opendaylight directory for each build
24 odl_dir_template = Template("opendaylight/opendaylight-$version_major.$version_minor."
25                             "$version_patch-$pkg_version/")
26 odl_deb_template = Template("opendaylight/opendaylight_$version_major.$version_minor."
27                             "$version_patch-${pkg_version}_all.deb")
28
29
30 def build_deb(build):
31     """Build the .deb described by the given build description.
32
33     :param build: Description of a debian build
34     :type build: dict
35
36     """
37     # Specialize a series of name templates for the given build
38     odl_dir_name = odl_dir_template.substitute(build)
39     odl_dir_path = os.path.join(templates_dir, os.pardir, odl_dir_name)
40     odl_deb = odl_deb_template.substitute(build)
41
42     # Call helper script to build the required debian files
43     build_debfiles.build_debfiles(build)
44
45     # Call a helper function to cache the artifacts required for each build
46     odl_tarball_path = cache.cache_build(build)
47
48     # Move ODL's tarball to the specialized OpenDaylight directory
49     shutil.copy(odl_tarball_path, odl_dir_path)
50
51     # Build debian package
52     os.chdir(odl_dir_path)
53     subprocess.call(["dpkg-buildpackage", "-us -uc -b",
54                      odl_dir_path], shell=True)
55
56     # Install opendaylight's dependencies
57     control_file_path = os.path.join(odl_dir_path, "debian/control")
58     subprocess.call(["sudo mk-build-deps -i " + control_file_path], shell=True)
59
60     os.chdir(project_root)
61
62     # Copy the .debs from their output dir to the cache dir
63     shutil.copy(odl_deb, cache_dir)