1 |
4 |
danv |
#!/usr/bin/env python3
|
2 |
|
|
###############################################################################
|
3 |
|
|
#
|
4 |
|
|
# Copyright (C) 2018
|
5 |
|
|
# ASTRON (Netherlands Institute for Radio Astronomy) <http://www.astron.nl/>
|
6 |
|
|
# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
|
7 |
|
|
#
|
8 |
|
|
# This program is free software: you can redistribute it and/or modify
|
9 |
|
|
# it under the terms of the GNU General Public License as published by
|
10 |
|
|
# the Free Software Foundation, either version 3 of the License, or
|
11 |
|
|
# (at your option) any later version.
|
12 |
|
|
#
|
13 |
|
|
# This program is distributed in the hope that it will be useful,
|
14 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
|
|
# GNU General Public License for more details.
|
17 |
|
|
#
|
18 |
|
|
# You should have received a copy of the GNU General Public License
|
19 |
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
20 |
|
|
#
|
21 |
|
|
# $Id$
|
22 |
|
|
#
|
23 |
|
|
###############################################################################
|
24 |
|
|
import sys
|
25 |
|
|
import os
|
26 |
|
|
from os.path import expandvars, dirname, basename
|
27 |
|
|
from argparse import ArgumentParser
|
28 |
|
|
from configfile import ConfigFile
|
29 |
|
|
|
30 |
|
|
if __name__ == '__main__':
|
31 |
|
|
# setup parser and parse the arguments.
|
32 |
|
|
argparser = ArgumentParser(description="Options and arguments for constructing an 'export' command for shell based on the content of a hdltool config file.")
|
33 |
|
|
argparser.add_argument('configfile', help="Filename like 'hdl_buildset_<boardtype>.cfg'")
|
34 |
|
|
argparser.add_argument('keynames', help="Name(s) of the key(s) to show the value of. Use comma to seperate multiple keys.")
|
35 |
|
|
argparser.add_argument('--varnames', help="Name(s) of the environment variable(s) the keys are mapped to. Default the keynames in capitals are used as environment variable names.")
|
36 |
|
|
argparser.add_argument('--is-group', help="The keynames refer to groups of environment variables that must be set.",
|
37 |
|
|
action="store_true")
|
38 |
|
|
argparser.add_argument('--optional', help="The keynames are optional. When them do not exist not error is generated",
|
39 |
|
|
action="store_true")
|
40 |
|
|
args = argparser.parse_args()
|
41 |
|
|
|
42 |
|
|
# resolve full name of configfile and force it to be explicit absolute or relative.
|
43 |
|
|
full_configfile_name = expandvars(args.configfile)
|
44 |
|
|
if full_configfile_name[0] != '/':
|
45 |
|
|
full_configfile_name = "./" + full_configfile_name
|
46 |
|
|
# read the file
|
47 |
|
|
cfg_info = ConfigFile(full_configfile_name)
|
48 |
|
|
cfg_info.resolve_key_references()
|
49 |
|
|
|
50 |
|
|
# parse the keys if they are no group references
|
51 |
|
|
if not args.is_group:
|
52 |
|
|
# setup key- and variable- names
|
53 |
|
|
keys = args.keynames.split(',')
|
54 |
|
|
if args.varnames:
|
55 |
|
|
env_vars = args.varnames.split(',')
|
56 |
|
|
if len(keys) != len(env_vars):
|
57 |
|
|
argparser.error("Number of variable names must match the number of keys.")
|
58 |
|
|
else:
|
59 |
|
|
env_vars = []
|
60 |
|
|
for key in keys:
|
61 |
|
|
env_vars.append(key.upper())
|
62 |
|
|
|
63 |
|
|
# finally construct an export command for the key value pairs.
|
64 |
|
|
for idx, key in enumerate(keys):
|
65 |
|
|
print("export {}='{}'\n".format(env_vars[idx],
|
66 |
|
|
os.path.expandvars(cfg_info.get_value(key, must_exist=not(args.optional)))))
|
67 |
|
|
sys.exit(0)
|
68 |
|
|
|
69 |
|
|
# Each key contains key-value pairs that must be exported in stead of a value
|
70 |
|
|
if args.varnames:
|
71 |
|
|
argparser.error("The option --varnames can not be used in combination with the option --is-group.")
|
72 |
|
|
|
73 |
|
|
# print("args.keynames=%s" % str(args.keynames))
|
74 |
|
|
keys = args.keynames.split(',')
|
75 |
|
|
for key in keys:
|
76 |
|
|
kv_pairs = cfg_info.get_value(key, must_exist=not(args.optional))
|
77 |
|
|
# currently 'by definition' the value we got has the format: <key> <value> [<key> <value> [...]]
|
78 |
|
|
# check we have an even number of items
|
79 |
|
|
# print("KV_pairs={}".format(kv_pairs))
|
80 |
|
|
if kv_pairs is None:
|
81 |
|
|
continue
|
82 |
|
|
items = kv_pairs.split()
|
83 |
|
|
if len(items) % 2:
|
84 |
|
|
argparser.error("Key '{}' should contain an even number of items ({}).".format(key, items))
|
85 |
|
|
for idx in range(0, len(items)//2, 2):
|
86 |
|
|
print("export {}='{}'\n".format(items[idx].upper(), os.path.expandvars(items[idx+1])))
|
87 |
|
|
|