- initial import of revision 374 from cnc
[apt.git] / contrib / apt-groupinstall / groupinstall-backend-comps.py
1 #!/usr/bin/python
2
3 # apt-groupinstall v0.2
4 # groupinstall helper backend for for RHL/RHEL/FC systems 
5 # by pmatilai@welho.com
6
7 import rhpl.comps, sys
8
9 def findgroup(comps, grpname):
10         if comps.groups.has_key(grpname):
11                 return comps.groups[grpname]
12         for group in comps.groups.values():
13                 if group.id == grpname:
14                         return group
15
16 def grouppkgs(comps, grpname, recursive = 0, showall = 0):
17         group = findgroup(comps, grpname)
18         pkgs = []
19         if group and recursive:
20                 for grp in group.groups:
21                         pkgs += grouppkgs(comps, grp, recursive, showall)
22         if group and group.packages:
23                 for pkg in group.packages:
24                         type, name = group.packages[pkg]
25                         if not showall and type == "optional":
26                                 continue
27                         pkgs.append(pkg)
28         return pkgs
29
30 def groupnames(comps, showhidden = 0):
31         if not synaptic:
32                 print "%-40s %s" % ("Group name", "Description")
33                 print "%-40s %s" % ("----------", "-----------")
34         for group in comps.groups.values():
35                 if group.packages:
36                         if not showhidden and not group.user_visible:
37                                 continue
38                         if synaptic:
39                                 print "u %s\t%s" % (group.id, group.name)
40                         else:
41                                 print "%-40s %s" % (group.id, group.name)
42
43 def showgroup(comps, grpname, showall = 0):
44         group = findgroup(comps, grpname)
45         if not group or not group.packages:
46                 print "No such group: %s" % grpname
47                 return
48         if not synaptic: 
49                 print "Group:\n    %s" % group.id
50         print "Description:\n    %s" % group.description
51         print "Required groups: "
52         for grp in group.groups:
53                 print "    %s" % grp
54         print "Packages: "
55         for pkg in grouppkgs(comps, grpname, recursive=0, showall=showall):
56                 print "    %s" % pkg
57                 
58 def usage():
59         print "Usage:\n %s [-t] [-p <path>] [-h] --list-tasks" % sys.argv[0]
60         print " %s [-t] [-p <path>] [-a] --task-desc <group> [--task-desc <group2>...]" % sys.argv[0]
61         print " %s [-t] [-p <path>] [-a] [-r] --task-packages <group> [--task-packages <group>...]" % sys.argv[0]
62         sys.exit(1)
63
64 if __name__ == "__main__":
65         import getopt
66
67         recursive = 0
68         showhidden = 0
69         synaptic = 1
70         showall = 0
71         comps = None
72         groups = []
73         cmd = None
74         compspath = "/usr/share/comps/i386/comps.xml"
75         
76         try:
77                 optlist, args = getopt.getopt(sys.argv[1:], 'arhp:t',
78                                                 ['task-desc=', 'list-tasks', 'task-packages='])
79         except getopt.error:
80                 usage()
81
82
83         for opt, arg in optlist:
84                 if opt == '--task-desc':
85                         cmd = "showgroup"
86                         groups.append(arg)
87                 elif opt == '--task-packages':
88                         groups.append(arg)
89                         cmd = "grouppkgs"
90                 elif opt == '--list-tasks':
91                         cmd = "groupnames"
92                 elif opt == '-r':
93                         recursive = 1
94                 elif opt == '-h':
95                         showhidden = 1
96                 elif opt == '-a':
97                         showall = 1
98                 elif opt == '-p':
99                         compspath = arg
100                 elif opt == '-t':
101                         synaptic = 0
102                 else:
103                         usage()
104
105         if not cmd: usage()
106
107         try:
108                 comps = rhpl.comps.Comps(compspath)
109         except:
110                 print "Unable to open %s!" % compspath
111                 sys.exit(1)
112
113         if cmd == "groupnames":
114                 groupnames(comps, showhidden)
115         elif cmd == "grouppkgs":
116                 for grp in groups:
117                         for pkg in grouppkgs(comps, grp, recursive, showall):
118                                 print "%s" % pkg
119         elif cmd == "showgroup":
120                 for grp in groups:
121                         showgroup(comps, grp, showall)
122         else:
123                 usage()
124         
125 # vim:ts=4:sts=4