# Copyright 2002-2009 Gentoo Foundation; 2008-2009 Various authors (see AUTHORS)
# Distributed under the GPL v2
+ 13 Jan 2009; Andrew Gaffney <agaffney@gentoo.org> catalyst,
+ modules/catalyst/__init__.py, modules/catalyst/config.py,
+ modules/catalyst/target/generic.py,
+ modules/catalyst/target/generic_stage.py,
+ modules/catalyst/target/snapshot.py:
+ Add new global config singleton object and Spec class and convert snapshot
+ and generic targets to use it
+
12 Jan 2009; Andrew Gaffney <agaffney@gentoo.org>
modules/catalyst/__init__.py:
Add base singleton class definition
__version__="2.99"
conf_values={}
+config = catalyst.config.config()
def usage():
print "Usage catalyst [options] [-C variable=value...] [ -s identifier]"
if not addlargs["target"] in targetmap:
raise CatalystError,"Target \""+addlargs["target"]+"\" not available."
+ config.get_spec().set_target(addlargs["target"])
mytarget=targetmap[addlargs["target"]](conf_values, addlargs)
mytarget.run()
targetmap = catalyst.target.build_target_map()
addlargs={}
+ spec = catalyst.config.Spec()
if myspecfile:
- spec = catalyst.config.SpecParser(myspecfile)
- addlargs.update(spec.get_values())
+ specparser = catalyst.config.SpecParser(myspecfile)
+ spec_values = specparser.get_values()
+ addlargs.update(spec_values)
+ spec.parse_values(spec_values)
if mycmdline:
try:
cmdline = catalyst.config.ConfigParser()
cmdline.parse_lines(mycmdline)
+ cmdline_values = cmdline.get_values()
addlargs.update(cmdline.get_values())
+ spec.parse_values(cmdline_values)
except CatalystError:
die("Could not parse commandline, exiting.")
+ config.set_spec(spec)
+ config.set_conf(conf_values)
+
if not "target" in addlargs:
raise CatalystError, "Required value \"target\" not specified."
spawn = catalyst.spawn
target = catalyst.target
config = catalyst.config
-
-"""
-This class implements a proper singleton. This is useful for having a "global"
-var with config and spec values instead of passing them around between objects
-and functions
-"""
-class Singleton(object):
-
- def __new__(type):
- if not '_the_instance' in type.__dict__:
- type._the_instance = object.__new__(type)
- return type._the_instance
-
if filename:
self.parse_file(filename)
+class Spec:
+
+ special_prefixes = ('boot', )
+
+ def __init__(self, values=None):
+ self.values = { 'global': {} }
+ self.target = None
+ if values:
+ self.parse_values(values)
+
+ def parse_values(self, values):
+ for x in values:
+ parts = x.split('/')
+ if len(parts) == 1 or parts[0] in self.special_prefixes:
+ self.values['global'][x] = values[x]
+ else:
+ if not parts[0] in self.values:
+ self.values[parts[0]] = {}
+ # We need to stick the key including the prefix in here until the targets are updated
+ self.values[parts[0]][x] = values[x]
+ self.values[parts[0]]['/'.join(parts[1:])] = values[x]
+
+ def set_target(self, target):
+ self.target = target
+
+ def get_values(self, target=None):
+ if target is None:
+ target = self.target
+ tmp = self.values['global']
+ if target in self.values:
+ tmp.update(self.values[target])
+ return tmp
+
+class Singleton(type):
+
+ def __init__(self, *args):
+ type.__init__(self, *args)
+ self._instances = {}
+
+ def __call__(self, *args):
+ if not args in self._instances:
+ self._instances[args] = type.__call__(self, *args)
+ return self._instances[args]
+
+class config:
+
+ __metaclass__ = Singleton
+
+ def __init__(self):
+ if not hasattr(self, 'spec'):
+ self.spec = None
+ self.conf = {}
+
+ def set_spec(self, spec):
+ self.spec = spec
+
+ def get_spec(self):
+ return self.spec
+
+ def set_conf(self, conf):
+ self.conf = conf
+
+ def update_conf(self, conf):
+ self.conf.update(conf)
+
+ def get_conf(self):
+ return self.conf
class generic_target:
- def __init__(self,myspec,addlargs):
- catalyst.util.addl_arg_parse(myspec,addlargs,self.required_values,self.valid_values)
- self.settings=myspec
+ def __init__(self, myspec=None, addlargs=None):
+ if myspec and addlargs:
+ catalyst.util.addl_arg_parse(myspec,addlargs,self.required_values,self.valid_values)
+ self.settings=myspec
+ else:
+ self.config = catalyst.config.config()
+ self.settings = self.config.get_spec().get_values()
+ self.settings.update(self.config.get_conf())
+
self.env={}
self.env["PATH"]="/bin:/sbin:/usr/bin:/usr/sbin"
class generic_stage_target(generic_target):
- def __init__(self,myspec,addlargs):
+ def __init__(self, myspec=None, addlargs=None):
self.required_values.extend(["version_stamp","target","subarch",\
"rel_type","profile","snapshot","source_subpath"])
self.required_values=["version_stamp","target"]
self.valid_values=["version_stamp","target"]
- generic_target.__init__(self,myspec,addlargs)
- self.settings=myspec
+ generic_target.__init__(self)
+# self.settings=myspec
self.settings["target_subpath"]="portage"
st=self.settings["storedir"]
self.settings["snapshot_path"]=catalyst.util.normpath(st+"/snapshots/portage-"+self.settings["version_stamp"]\