From f60303310b3b84303442f82443eac108dd589cde Mon Sep 17 00:00:00 2001 From: Andrew Gaffney Date: Mon, 12 Jan 2009 18:45:30 -0600 Subject: [PATCH] Add new global config singleton object and Spec class and convert snapshot and generic targets to use it --- ChangeLog | 8 +++ catalyst | 14 ++++- modules/catalyst/__init__.py | 13 ----- modules/catalyst/config.py | 67 ++++++++++++++++++++++++ modules/catalyst/target/generic.py | 12 +++-- modules/catalyst/target/generic_stage.py | 2 +- modules/catalyst/target/snapshot.py | 4 +- 7 files changed, 99 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index 376df557..05198336 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,14 @@ # Copyright 2002-2009 Gentoo Foundation; 2008-2009 Various authors (see AUTHORS) # Distributed under the GPL v2 + 13 Jan 2009; Andrew Gaffney 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 modules/catalyst/__init__.py: Add base singleton class definition diff --git a/catalyst b/catalyst index af555995..50a7af7f 100755 --- a/catalyst +++ b/catalyst @@ -20,6 +20,7 @@ __maintainer__="Chris Gianelloni " __version__="2.99" conf_values={} +config = catalyst.config.config() def usage(): print "Usage catalyst [options] [-C variable=value...] [ -s identifier]" @@ -145,6 +146,7 @@ def build_target(addlargs, targetmap): 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() @@ -290,19 +292,27 @@ if __name__ == "__main__": 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." diff --git a/modules/catalyst/__init__.py b/modules/catalyst/__init__.py index 3b1ea8d1..a7d2aaff 100644 --- a/modules/catalyst/__init__.py +++ b/modules/catalyst/__init__.py @@ -15,16 +15,3 @@ error = catalyst.error 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 - diff --git a/modules/catalyst/config.py b/modules/catalyst/config.py index 4fc62911..4e372e50 100644 --- a/modules/catalyst/config.py +++ b/modules/catalyst/config.py @@ -145,3 +145,70 @@ class ConfigParser(ParserBase): 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 diff --git a/modules/catalyst/target/generic.py b/modules/catalyst/target/generic.py index 6bcec8be..3e09da48 100644 --- a/modules/catalyst/target/generic.py +++ b/modules/catalyst/target/generic.py @@ -7,9 +7,15 @@ from catalyst.output import * 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" diff --git a/modules/catalyst/target/generic_stage.py b/modules/catalyst/target/generic_stage.py index 931539d1..12bf1de1 100644 --- a/modules/catalyst/target/generic_stage.py +++ b/modules/catalyst/target/generic_stage.py @@ -14,7 +14,7 @@ from catalyst.target.generic import * 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"]) diff --git a/modules/catalyst/target/snapshot.py b/modules/catalyst/target/snapshot.py index e88ad8ca..50bf6500 100644 --- a/modules/catalyst/target/snapshot.py +++ b/modules/catalyst/target/snapshot.py @@ -14,8 +14,8 @@ class snapshot_target(generic_target): 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"]\ -- 2.26.2