2 """This module does blah blah."""
7 from jinja2 import Environment, FileSystemLoader
9 PARSER = argparse.ArgumentParser()
10 PARSER.add_argument("--yaml", "-y", type=str, required=True)
11 PARSER.add_argument("--jinja2", "-j", type=str, required=True)
12 ARGS = PARSER.parse_args()
14 # Processor architecture vs DPKG architecture mapping
19 ARCH_DPKG_TABLE = dict(zip(DPKG_ARCH_TABLE.values(), DPKG_ARCH_TABLE.keys()))
21 # Custom filter to allow simple IP address operations returning
22 # a new address from an upper or lower (negative) index
23 def ipaddr_index(base_address, index):
24 """Return IP address in given network at given index"""
26 base_address_str = unicode(base_address)
27 #pylint: disable=unused-variable
28 except NameError as ex:
29 base_address_str = str(base_address)
30 return ipaddress.ip_address(base_address_str) + int(index)
32 # Custom filter to convert between processor architecture
33 # (as reported by $(uname -m)) and DPKG-style architecture
34 def dpkg_arch(arch, to_dpkg=True):
35 """Return DPKG-compatible from processor arch and vice-versa"""
37 return DPKG_ARCH_TABLE[arch]
39 return ARCH_DPKG_TABLE[arch]
41 ENV = Environment(loader=FileSystemLoader('./'))
42 ENV.filters['ipaddr_index'] = ipaddr_index
43 ENV.filters['dpkg_arch'] = dpkg_arch
45 with open(ARGS.yaml) as _:
46 DICT = yaml.safe_load(_)
48 # If an installer descriptor file (IDF) exists, include it (temporary)
49 IDF_PATH = '/idf-'.join(os.path.split(ARGS.yaml))
50 if os.path.exists(IDF_PATH):
51 with open(IDF_PATH) as _:
52 IDF = yaml.safe_load(_)
53 DICT['idf'] = IDF['idf']
55 # Print dictionary generated from yaml (uncomment for debug)
58 # Render template and print generated conf to console
59 TEMPLATE = ENV.get_template(ARGS.jinja2)
60 #pylint: disable=superfluous-parens
61 print(TEMPLATE.render(conf=DICT))