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