Change `rpm_release` to `pkg_version`
[integration/packaging.git] / rpm / cache / cache.py
1 #!/usr/bin/env python
2 """Read YAML description of RPM builds and cache the required artifacts."""
3
4 import os
5 from string import Template
6 import sys
7 import tarfile
8 import urllib
9
10 try:
11     import yaml
12 except ImportError:
13     sys.stderr.write("We recommend using our included Vagrant env.\n")
14     sys.stderr.write("Else, do `pip install -r requirements.txt` in a venv.\n")
15     raise
16
17 # Path to the directory that contains this file is assumed to be the cache dir
18 cache_dir = os.path.dirname(os.path.abspath(__file__))
19
20 # Templates that can be specialized into artifact names/paths per-build
21 odl_template = Template("opendaylight-$version_major.$version_minor."
22                         "$version_patch-$pkg_version.tar.gz")
23 unitfile_template = Template("opendaylight-$sysd_commit.service")
24 unitfile_url_template = Template("https://git.opendaylight.org/gerrit/"
25                                  "gitweb?p=integration/packaging.git;a="
26                                  "blob_plain;f=rpm/unitfiles/opendaylight."
27                                  "service;hb=$sysd_commit")
28 unitfile_tb_template = Template("opendaylight-$sysd_commit.service.tar.gz")
29
30
31 def cache_build(build):
32     """Cache the artifacts required for the given RPM build.
33
34     :param build: Description of an RPM build, typically from build_vars.yaml
35     :type build: dict
36
37     """
38     # Specialize a series of name/URL templates for the given build
39     odl_tarball = odl_template.substitute(build)
40     unitfile = unitfile_template.substitute(build)
41     unitfile_url = unitfile_url_template.substitute(build)
42     unitfile_tarball = unitfile_tb_template.substitute(build)
43
44     # After building strings from the name templates, build their full path
45     odl_tarball_path = os.path.join(cache_dir, odl_tarball)
46     unitfile_path = os.path.join(cache_dir, unitfile)
47     unitfile_tarball_path = os.path.join(cache_dir, unitfile_tarball)
48
49     # Cache appropriate version of OpenDaylight's release tarball
50     if not os.path.isfile(odl_tarball_path):
51         print("Downloading: {}".format(build["download_url"]))
52         urllib.urlretrieve(build["download_url"], odl_tarball_path)
53         print("Cached: {}".format(odl_tarball))
54     else:
55         print("Already cached: {}".format(odl_tarball))
56
57     # Cache appropriate version of OpenDaylight's systemd unitfile as a tarball
58     if not os.path.isfile(unitfile_tarball_path):
59         # Download ODL's systemd unitfile
60         urllib.urlretrieve(unitfile_url, unitfile_path)
61
62         # Using the full paths here creates those paths in the tarball, which
63         # breaks the build. There's a way to change the working dir during a
64         # single tar command using the system tar binary, but I don't see a
65         # way to do that with Python.
66         # TODO: Is there a good way to do this without changing directories?
67         cwd = os.getcwd()
68         os.chdir(cache_dir)
69         # Create a .tar.gz archive containing ODL's systemd unitfile
70         with tarfile.open(unitfile_tarball, "w:gz") as tb:
71             tb.add(unitfile)
72         os.chdir(cwd)
73
74         # Remove the now-archived unitfile
75         os.remove(unitfile_path)
76         print("Cached: {}".format(unitfile_tarball))
77     else:
78         print("Already cached: {}".format(unitfile_tarball))
79
80
81 # If run as a script, cache artifacts required for all builds
82 if __name__ == "__main__":
83     # Load RPM build variables from a YAML config file
84     with open(os.path.join(cache_dir, os.pardir, "build_vars.yaml")) as var_fd:
85         build_vars = yaml.load(var_fd)
86
87     for build in build_vars["builds"]:
88         cache_build(build)