cosmetic
[sfa.git] / config / sfa-config
1 #!/usr/bin/python
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 def main():
57     config = Config()
58     fileobjs = []
59     output = None
60     category = {}
61     variable = {}
62     group = {}
63     package = {}
64     save = False
65
66     # Standard options
67     shortopts = "hs:"
68     longopts = ["shell", "bash", "python",
69                 "php",
70                 "xml",
71                 "variables",
72                 "packages",
73                 "groups",
74                 "comps",
75                 "category=", "variable=", "value=",
76                 "group=", "package=", "type=",
77                 "help",
78                 "save="]
79
80     try:
81         (opts, argv) = getopt.gnu_getopt(sys.argv[1:], shortopts, longopts)
82     except Exception, err:
83         sys.stderr.write("Error: " + str(err) + os.linesep)
84         sys.exit(1)
85
86     for (opt, optval) in opts:
87         if opt == "--shell" or \
88              opt == "--bash":
89             output = config.output_shell
90         elif opt == "--python":
91             output = config.output_python
92         elif opt == "--php":
93             output = config.output_php
94         elif opt == "--xml":
95             output = config.output_xml
96         elif opt == "--variables":
97             output = config.output_variables
98         elif opt == "--packages":
99             deprecated("option --packages deprecated -- use .lst files instead")
100         elif opt == "--groups":
101             deprecated("option --groups deprecated -- use .lst files instead")
102         elif opt == "--comps":
103             deprecated("option --comps deprecated -- use .lst files instead")
104         elif opt == "--category":
105             category['id'] = optval
106         elif opt == "--variable":
107             variable['id'] = optval
108         elif opt == "--value":
109             variable['value'] = optval
110         elif opt == "--group":
111 #            group['id'] = optval
112             deprecated("option --group deprecated -- use .lst files instead")
113         elif opt == "--package":
114 #            package['name'] = optval
115             deprecated("option --package deprecated -- use .lst files instead")
116         elif opt == "--type":
117             package['type'] = optval
118         elif opt == '-s' or opt == "--save":
119             if not optval:
120                 usage()
121             print 'parsed save option',optval
122             save = optval
123         elif opt == '-h' or opt == "--help":
124             usage()
125
126     # Try the default
127     if not argv:
128         argv = ["/etc/sfa/sfa_config"]
129
130     # Merge all files
131     for file in argv:
132         try:
133             config.load(file)
134         except IOError:
135             pass
136         except Exception, err:
137             sys.stderr.write("Error: %s: %s" % (file, str(err)) + os.linesep)
138             sys.exit(1)
139
140     # --category, --variable, --value
141     if category.has_key('id') and variable.has_key('id'):
142         if variable.has_key('value'):
143             config.set(category['id'], variable['id'], variable['value'])
144         else:
145             value = config.get(category['id'], variable['id'])
146             print value
147
148     # --shell, --php, --xml
149     if output is not None:
150         sys.stdout.write(output())
151
152     # --save
153     if save:
154         # create directory if needed
155         # so that plc.d/{api,postgres} can create configs/site.xml 
156         dirname = os.path.dirname (save)
157         if (not os.path.exists (dirname)):
158             os.makedirs(dirname,0755)
159             if (not os.path.exists (dirname)):
160                 print "Cannot create dir %s - exiting" % dirname
161                 sys.exit(1)
162         config.save(save)
163
164
165 if __name__ == '__main__':
166     main()