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