Change v3 to v4 yumgroups.xml
[myplc.git] / plc-config
1 #!/usr/bin/python
2 #
3 # Script for basic access to the PlanetLab Central (PLC) configuration
4 # file store.
5 #
6 # Mark Huang <mlhuang@cs.princeton.edu>
7 # Copyright (C) 2006 The Trustees of Princeton University
8 #
9 # $Id: plc-config,v 1.1.1.1 2006/03/27 17:36:46 mlhuang Exp $
10 #
11
12 import sys
13 import os
14 import fcntl
15 import getopt
16 import signal
17 from plc_config import PLCConfiguration
18
19
20 def usage():
21     print """
22 Script to access the PLC configuration file store.
23     
24 Usage: %s [OPTION]... [FILES]
25         Conversion:
26
27         --shell         Output shell configuration file
28         --python        Output Python configuration file
29         --php           Output PHP configuration file
30
31         Information:
32
33         --variables     List names of all variables
34         --packages      List names of all packages
35         --comps         List comps.xml from configuration
36
37         Basic variable value manipulation:
38
39         --category=     Category identifier
40         --variable=     Variable identifier
41         --value=        Variable value
42
43         Basic package list manipulation:
44
45         --group=        Package group identifier
46         --package=      Package name
47         --type=         Package type
48
49         Miscellaneous:
50
51         -h, --help      This message
52         -s, --save      Save changes to first configuration file
53 """.lstrip() % sys.argv[0]
54     sys.exit(1)
55
56
57 def main():
58     plc = PLCConfiguration()
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, 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" or \
90              opt == "--python":
91             output = plc.output_shell
92         elif opt == "--php":
93             output = plc.output_php
94         elif opt == "--xml":
95             output = plc.output_xml
96         elif opt == "--variables":
97             output = plc.output_variables
98         elif opt == "--packages":
99             output = plc.output_packages
100         elif opt == "--groups":
101             output = plc.output_groups
102         elif opt == "--comps":
103             output = plc.output_comps
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         elif opt == "--package":
113             package['name'] = optval
114         elif opt == "--type":
115             package['type'] = optval
116         elif opt == '-s' or opt == "--save":
117             save = True
118         elif opt == '-h' or opt == "--help":
119             usage()
120
121     # Try the default
122     if not argv:
123         argv = ["/etc/planetlab/plc_config.xml"]
124
125     # Merge all files
126     for file in argv:
127         try:
128             plc.load(file)
129         except IOError:
130             pass
131         except Exception, err:
132             sys.stderr.write("Error: %s: %s" % (file, str(err)) + os.linesep)
133             sys.exit(1)
134
135     # --category, --variable, --value
136     if category.has_key('id') and variable.has_key('id'):
137         if variable.has_key('value'):
138             plc.set(category, variable)
139         else:
140             (category, variable) = plc.get(category['id'], variable['id'])
141             if variable.has_key('value'):
142                 print variable['value']
143
144     # --shell, --php, --xml
145     if output is not None:
146         sys.stdout.write(output())
147
148     # --save
149     if save:
150         plc.save()
151
152
153 if __name__ == '__main__':
154     main()