Build deb's using common build.py
[integration/packaging.git] / rpm / specs / build_specs.py
1 #!/usr/bin/env python
2 """Build RPM .spec files from build description and a Jinja2 .spec template."""
3
4 import os
5 from string import Template
6 import sys
7
8 try:
9     from jinja2 import Environment, FileSystemLoader
10 except ImportError:
11     sys.stderr.write("We recommend using our included Vagrant env.\n")
12     sys.stderr.write("Else, do `pip install -r requirements.txt` in a venv.\n")
13     raise
14
15 # Path to the directory that contains this file is assumed to be the spec dir
16 spec_dir = os.path.dirname(os.path.abspath(__file__))
17
18 # Create the Jinja2 Environment
19 env = Environment(loader=FileSystemLoader(spec_dir))
20
21 # Load the OpenDaylight RPM .spec file Jinja2 template
22 template = env.get_template("opendaylight.spec")
23
24 # Python string Template, specialized into a specfile file name per-build
25 specfile_template = Template("opendaylight-$version_major.$version_minor."
26                              "$version_patch-$pkg_version.spec")
27
28
29 def build_spec(build):
30     """Build the RPM .spec file from a template for the given build description.
31
32     :param build: Description of an RPM build
33     :type build: dict
34
35     """
36     specfile_name = specfile_template.substitute(build)
37     specfile_path = os.path.join(spec_dir, specfile_name)
38     with open(specfile_path, "w") as specfile:
39         specfile.write(template.render(build))