a little nicer wrt pep8
[sfa.git] / config / sfa-config
1 #!/usr/bin/env python3
2 #
3 # Script for basic access to the SFA configuration file store.
4 #
5
6 import sys
7 import os
8 #import fcntl
9 import getopt
10 #import signal
11 from sfa.util.config import Config
12
13
14 def usage():
15     print("""
16 Script to access the SFA configuration file store.
17
18 Usage: %s [OPTION]... [FILES]
19         Conversion:
20
21         --shell         Output shell configuration file
22         --python        Output Python configuration file
23         --php           Output PHP configuration file
24
25         Information:
26
27         --variables     List names of all variables
28         --packages      List names of all packages
29         --comps         List comps.xml from configuration
30
31         Basic variable value manipulation:
32
33         --category=     Category identifier
34         --variable=     Variable identifier
35         --value=        Variable value
36
37         Basic package list manipulation:
38
39         --group=        Package group identifier
40         --package=      Package name
41         --type=         Package type
42
43         Miscellaneous:
44
45         -h, --help      This message
46         -s, --save      Save changes to first configuration file
47 """.lstrip() % sys.argv[0])
48     sys.exit(1)
49
50
51 def deprecated(message):
52     print("%s: deprecated usage" % sys.argv[0])
53     print(message)
54     sys.exit(1)
55
56
57 def main():
58     config = Config()
59     fileobjs = []
60     output = None
61     category = {}
62     variable = {}
63     group = {}
64     package = {}
65     save = False
66
67     # Standard options
68     shortopts = "hs:"
69     longopts = ["shell", "bash", "python",
70                 "php",
71                 "xml",
72                 "variables",
73                 "packages",
74                 "groups",
75                 "comps",
76                 "category=", "variable=", "value=",
77                 "group=", "package=", "type=",
78                 "help",
79                 "save="]
80
81     try:
82         (opts, argv) = getopt.gnu_getopt(sys.argv[1:], shortopts, longopts)
83     except Exception as err:
84         sys.stderr.write("Error: " + str(err) + os.linesep)
85         sys.exit(1)
86
87     for (opt, optval) in opts:
88         if opt == "--shell" or \
89                 opt == "--bash":
90             output = config.output_shell
91         elif opt == "--python":
92             output = config.output_python
93         elif opt == "--php":
94             output = config.output_php
95         elif opt == "--xml":
96             output = config.output_xml
97         elif opt == "--variables":
98             output = config.output_variables
99         elif opt == "--packages":
100             deprecated("option --packages deprecated -- use .lst files instead")
101         elif opt == "--groups":
102             deprecated("option --groups deprecated -- use .lst files instead")
103         elif opt == "--comps":
104             deprecated("option --comps deprecated -- use .lst files instead")
105         elif opt == "--category":
106             category['id'] = optval
107         elif opt == "--variable":
108             variable['id'] = optval
109         elif opt == "--value":
110             variable['value'] = optval
111         elif opt == "--group":
112             #            group['id'] = optval
113             deprecated("option --group deprecated -- use .lst files instead")
114         elif opt == "--package":
115             #            package['name'] = optval
116             deprecated("option --package deprecated -- use .lst files instead")
117         elif opt == "--type":
118             package['type'] = optval
119         elif opt == '-s' or opt == "--save":
120             if not optval:
121                 usage()
122             print('parsed save option', optval)
123             save = optval
124         elif opt == '-h' or opt == "--help":
125             usage()
126
127     # Try the default
128     if not argv:
129         argv = ["/etc/sfa/sfa_config"]
130
131     # Merge all files
132     for file in argv:
133         try:
134             config.load(file)
135         except IOError:
136             pass
137         except Exception as err:
138             sys.stderr.write("Error: %s: %s" % (file, str(err)) + os.linesep)
139             sys.exit(1)
140
141     # --category, --variable, --value
142     if 'id' in category and 'id' in variable:
143         if 'value' in variable:
144             config.set(category['id'], variable['id'], variable['value'])
145         else:
146             value = config.get(category['id'], variable['id'])
147             print(value)
148
149     # --shell, --php, --xml
150     if output is not None:
151         sys.stdout.write(output())
152
153     # --save
154     if save:
155         # create directory if needed
156         # so that plc.d/{api,postgres} can create configs/site.xml
157         dirname = os.path.dirname(save)
158         if (not os.path.exists(dirname)):
159             os.makedirs(dirname, 0o755)
160             if (not os.path.exists(dirname)):
161                 print("Cannot create dir %s - exiting" % dirname)
162                 sys.exit(1)
163         config.save(save)
164
165
166 if __name__ == '__main__':
167     main()