Patch for Bug #227013 - /boot not mounted
[moblin-image-creator.eeepc.git] / libs / fsets.py
blob366943a456d9101452b60adb1f24e61a7e83f656
1 #!/usr/bin/python -tt
2 # vim: ai ts=4 sts=4 et sw=4
4 # Copyright (c) 2007 Intel Corporation
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the Free
8 # Software Foundation; version 2 of the License
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc., 59
17 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 import ConfigParser
20 import gettext
21 import os
22 import re
23 import sys
24 import unittest
26 _ = gettext.lgettext
28 class FSet(object):
29 """
30 An FSet object represents a functional set of packages to install in a
31 target filesystem. An FSet contains an array of package names in
32 FSet.packages, an array of additional debug packages in
33 FSet.debug_packages, and an array of dependant FSet names in FSet.deps.
34 """
35 def __init__(self):
36 self.filenames = []
37 self.__fsets = {}
39 def addFile(self, filename):
40 """Add a config file to the FSet"""
41 filename = os.path.realpath(os.path.abspath(os.path.expanduser(filename)))
42 self.filenames.append(filename)
43 self.__parseFile(filename)
46 def __parseFile(self, filename):
47 valid_values = { 'desc' : '', 'pkgs' : [], 'debug_pkgs' : [],
48 'deps' : [] }
49 if not os.path.isfile(filename):
50 return False
51 p = ConfigParser.ConfigParser()
52 filenames = p.read(filename)
53 for section in p.sections():
54 orig_section = section
55 section = section.lower()
56 if section in self.__fsets:
57 raise ValueError, _("Error: Already have a section called: %s") % section
58 work_dict = {}
59 work_dict['filename'] = filename
60 fset = FsetInstance(section)
61 for name, value in p.items(orig_section):
62 fset.add(name, value)
63 self.__fsets[section] = fset
64 return True
66 def __getitem__(self, key):
67 return self.__fsets[key.lower()]
68 def __iter__(self):
69 return self.__fsets.__iter__()
70 def iterkeys(self):
71 return self.__fsets.iterkeys()
72 def __len__(self):
73 return len(self.__fsets)
74 def __str__(self):
75 return ('<data="%s">'
76 % (self.__fsets))
77 def __repr__(self):
78 return "FSet()"
80 class FsetInstance(object):
81 valid_values = { 'desc' : '', 'pkgs' : [], 'debug_pkgs' : [], 'deps' : [] }
82 def __init__(self, name):
83 self.name = name.lower()
84 self.data = {}
85 def add(self, key, value):
86 key = key.lower()
87 if key not in FsetInstance.valid_values:
88 print _("Found unsupported value, ignoring: %s = %s") % (key, value)
89 return
90 work_type = type(FsetInstance.valid_values[key])
91 if work_type == type([]):
92 value = value.split()
93 elif work_type == type(''):
94 pass
95 else:
96 print _("Error: Unsupported type specified in FsetInstance.valid_values")
97 print _("Type was: %s") % work_type
98 raise ValueError
99 self.data[key] = value
100 def get(self, key):
101 key = key.lower()
102 if key not in FsetInstance.valid_values:
103 raise KeyError
104 if key in self.data:
105 return self.data[key]
106 else:
107 return FsetInstance.valid_values[key]
108 def __getitem__(self, key):
109 return self.get(key)
110 def __getattr__(self, key):
111 return self.get(key)
112 def __repr__(self):
113 return ('FsetInstance("%s", %s)' % (self.name, self.data))
114 def __str__(self):
115 return ('<fset name="%s" data="%s">' % (self.name, self.data))
117 if __name__ == '__main__':
118 if len(sys.argv) == 1:
119 print >> sys.stderr, _("USAGE: %s FSET_FILE ...") % (sys.argv[0])
120 else:
121 fset = FSet()
122 for filename in sys.argv[1:]:
123 fset.addFile(filename)
124 print fset
125 for key in fset.fsets:
126 print
127 print key, fset[key]
128 print fset[key].filename, fset[key]['filename']