2 """Build RPM .spec files from YAML RPM config and a Jinja2 .spec template."""
6 from string import Template
10 from jinja2 import Environment, FileSystemLoader
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")
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__))
19 # Create the Jinja2 Environment
20 env = Environment(loader=FileSystemLoader(spec_dir))
22 # Load the OpenDaylight RPM .spec file Jinja2 template
23 template = env.get_template("opendaylight.spec")
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")
30 def build_spec(build):
31 """Build the RPM .spec file from a template for the given build description.
33 :param build: Description of an RPM build, typically from build_vars.yaml
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))
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)
49 for build in build_vars["builds"]: