Build deb's using common build.py
[integration/packaging.git] / packages / deb / cache / cache.py
1 #!/usr/bin/env python
2 """Read YAML description of ODL builds and cache the required artifacts."""
3
4 import os
5 import sys
6 import urllib
7
8 try:
9     import yaml
10 except ImportError:
11     sys.stderr.write("We recommend using our included Vagrant env.\n")
12     sys.stderr.write("Else, install the Python libs it installs.\n")
13     raise
14
15 # Path to the directory that contains this file is assumed to be the cache dir
16 cache_dir = os.path.dirname(os.path.abspath(__file__))
17
18
19 def cache_build(build):
20     """Cache the artifacts required for the given debian build.
21
22     :param build: Description of an ODL build, typically from build_vars.yaml
23     :type build: dict
24
25     """
26     # OpenDaylight's tarball release name for the given build
27     odl_tarball = build["download_url"].split('/')[-1]
28
29     # After getting the string, build the tarball's full path
30     odl_tarball_path = os.path.join(cache_dir, odl_tarball)
31
32     # Cache appropriate version of OpenDaylight's release tarball
33     if not os.path.isfile(odl_tarball_path):
34         print("Downloading: {}".format(build["download_url"]))
35         urllib.urlretrieve(build["download_url"], odl_tarball_path)
36         print("Cached: {}".format(odl_tarball))
37     else:
38         print("Already cached: {}".format(odl_tarball))
39
40     return odl_tarball_path
41
42
43 # If run as a script, cache artifacts required for all builds
44 if __name__ == "__main__":
45     # Load debian build variables from a YAML config file
46     with open(os.path.join(cache_dir, os.pardir, "build_vars.yaml")) as var_fd:
47         build_vars = yaml.load(var_fd)
48     for build in build_vars["builds"]:
49         odl_tarball_path = cache_build(build)