Add missing opendaylight.service template file
[integration/packaging.git] / packages / deb / templates / build_debianfiles.py
1 #!/usr/bin/env python
2 """Build debian files from build descriptionand Jinja2 debian templates."""
3
4 import os
5 import re
6 import shutil
7 from string import Template
8 import sys
9
10 try:
11     from jinja2 import Environment, FileSystemLoader
12 except ImportError:
13     sys.stderr.write("We recommend using our included Vagrant env.\n")
14     sys.stderr.write("Else, install the Python libs it installs.\n")
15     raise
16
17 debian_files_dynamic = ["changelog", "rules", "control"]
18 debian_files_static = ["compat", "karaf", "opendaylight.install",
19                        "opendaylight.postrm", "opendaylight.postinst",
20                        "opendaylight.upstart", "opendaylight.service"]
21
22 # Path to the directory that contains this file is assumed to be the
23 # debian templates dir
24 templates_dir = os.path.dirname(os.path.abspath(__file__))
25
26 # Create the Jinja2 Environment
27 env = Environment(loader=FileSystemLoader(templates_dir))
28
29 # Python string Template, specialized into an Opendaylight directory name
30 # per-build
31 odl_dir_template = Template("opendaylight/opendaylight-$version_major.$version_minor."
32                             "$version_patch-$pkg_version/")
33 odl_deb_template = Template("opendaylight/opendaylight_$version_major.$version_minor."
34                             "$version_patch-${pkg_version}_all.deb")
35 odl_files_template = Template("opendaylight_$version_major.$version_minor."
36                               "$version_patch-${pkg_version}*")
37
38
39 def build_debfiles(build):
40     """Builds Debian files from templates for the given build description.
41
42     Creates a debian dir for the build, copies static build files into it,
43     specializes templates with build-spicific vars to create dynamic files,
44     downloads build-specific systemd unitfile based on commit hash.
45
46     :param build: Description of a debian build
47     :type build: dict
48
49     """
50     # Specialize a series of name templates for the given build
51     odl_dir_name = odl_dir_template.substitute(build)
52     odl_dir_path = os.path.join(templates_dir, os.pardir, odl_dir_name)
53
54     # Clean up opendaylight dir structure if it exists
55     if os.path.isdir(odl_dir_path):
56         shutil.rmtree(odl_dir_path)
57
58     # Delete old .deb file if it exists
59     odl_deb_name = odl_deb_template.substitute(build)
60     odl_deb_path = os.path.join(templates_dir, os.pardir, odl_deb_name)
61     if os.path.isfile(odl_deb_path):
62         os.remove(odl_deb_path)
63
64     # Delete old .changes, .dsc, .tar.gz files if they exist
65     odl_files_regex = odl_files_template.substitute(build)
66     odl_par_dir = os.path.join(templates_dir, os.pardir, "opendaylight")
67     if os.path.isdir(odl_par_dir):
68         for f in os.listdir(odl_par_dir):
69             if re.search(odl_files_regex, f):
70                 os.remove(os.path.join(odl_par_dir, f))
71
72     # Create debian directory
73     debian_dir_path = os.path.join(odl_dir_path, "debian")
74     os.makedirs(debian_dir_path)
75
76     # Copy files common to all .debs to the specific debian dir
77     for file_name in debian_files_static:
78         file_path = os.path.join(templates_dir, file_name)
79         shutil.copy(file_path, debian_dir_path)
80
81     # Copy templated files to debian build dir, specialize templates
82     for file_name in debian_files_dynamic:
83         # Load OpenDaylight debian files Jinja2 template
84         template = env.get_template(file_name + "_template")
85         file_path = os.path.join(debian_dir_path, file_name)
86         with open(file_path, "w") as debian_file:
87             debian_file.write(template.render(build))