Merge remote-tracking branch 'steven/master'

This commit is contained in:
Nico Schottelius 2011-10-07 16:21:08 +02:00
commit 95c5ecf789
2 changed files with 67 additions and 96 deletions

View file

@ -42,113 +42,52 @@ class Object(object):
""" """
@staticmethod
def base_dir():
"""Return the absolute path to the top level directory where objects
are defined.
Requires the environment variable '__cdist_out_dir' to be set.
"""
try:
base_dir = os.path.join(
os.environ['__cdist_out_dir'],
'object'
)
except KeyError as e:
raise cdist.MissingEnvironmentVariableError(e.args[0])
# FIXME: should directory be created elsewhere?
if not os.path.isdir(base_dir):
os.mkdir(base_dir)
return base_dir
@classmethod @classmethod
def remote_base_dir(): def list_objects(cls, object_base_path, type_base_path):
"""Return the absolute path to the top level directory where objects
are kept on the remote/target host.
Requires the environment variable '__cdist_remote_out_dir' to be set.
"""
try:
return os.path.join(
os.environ['__cdist_remote_out_dir'],
'object',
)
except KeyError as e:
raise cdist.MissingEnvironmentVariableError(e.args[0])
def list_objects(cls):
"""Return a list of object instances""" """Return a list of object instances"""
for object_name in cls.list_object_names(): for object_name in cls.list_object_names(object_base_path):
type_name = object_name.split(os.sep)[0] type_name = object_name.split(os.sep)[0]
# FIXME: allow object without object_id? e.g. for singleton
object_id = os.sep.join(object_name.split(os.sep)[1:]) object_id = os.sep.join(object_name.split(os.sep)[1:])
yield cls(cdist.core.Type(type_name), object_id=object_id) yield cls(cdist.core.Type(type_base_path, type_name), object_base_path, object_id=object_id)
@classmethod @classmethod
def list_type_names(cls): def list_type_names(cls, object_base_path):
"""Return a list of type names""" """Return a list of type names"""
return os.listdir(cls.base_dir()) return os.listdir(object_base_path)
@classmethod @classmethod
def list_object_names(cls): def list_object_names(cls, object_base_path):
"""Return a list of object names""" """Return a list of object names"""
for path, dirs, files in os.walk(cls.base_dir()): for path, dirs, files in os.walk(object_base_path):
if DOT_CDIST in dirs: if DOT_CDIST in dirs:
yield os.path.relpath(path, cls.base_dir()) yield os.path.relpath(path, object_base_path)
def __init__(self, type, object_id=None, parameters=None, requirements=None): def __init__(self, type, base_path, object_id=None):
self.type = type # instance of Type self.type = type # instance of Type
self.base_path = base_path
self.object_id = object_id self.object_id = object_id
self.name = os.path.join(self.type.name, self.object_id) self.name = os.path.join(self.type.name, self.object_id)
self.parameters = parameters or {} self.path = os.path.join(self.type.path, self.object_id)
self.requirements = requirements or [] self.absolute_path = os.path.join(self.base_path, self.path)
self.code_local_path = os.path.join(self.path, "code-local")
self.code_remote_path = os.path.join(self.path, "code-remote")
self.parameter_path = os.path.join(self.path, "parameter")
self.__parameters = None self.__parameters = None
self.__requirements = None self.__requirements = None
# Whether this object was prepared/ran
self.prepared = False
self.ran = False
def __repr__(self): def __repr__(self):
return '<Object %s>' % self.name return '<Object %s>' % self.name
@property @property
def path(self): def explorer_path(self):
return os.path.join( # create absolute path
self.base_dir(), path = os.path.join(self.absolute_path, "explorer")
self.name,
DOT_CDIST
)
@property
def code_local(self):
return os.path.join(self.path, "code-local")
@property
def code_remote(self):
return os.path.join(self.path, "code-remote")
@property
def explorer_out_dir(self):
path = os.path.join(self.path, "explorer")
if not os.path.isdir(path): if not os.path.isdir(path):
os.mkdir(path) os.mkdir(path)
return path # return relative path
return os.path.join(self.path, "explorer")
# FIXME: prefix directory should not leak into me
@property
def remote_path(self):
return os.path.join(
self.remote_base_dir(),
self.name,
DOT_CDIST
)
@property
def remote_code_remote(self):
return os.path.join(self.remote_path, "code-remote")
### requirements ### requirements
@ -204,20 +143,43 @@ class Object(object):
### /changed ### /changed
### prepared
@property
def prepared(self):
"""Check whether the object has been prepared."""
return os.path.isfile(os.path.join(self.path, "prepared"))
# FIXME: Object @prepared.setter
def object_parameter_dir(self, cdist_object): def prepared(self, value):
"""Returns the dir to the object parameter""" """Change the objects prepared status."""
return os.path.join(self.object_dir(cdist_object), "parameter") path = os.path.join(self.path, "prepared")
if value:
open(path, "w").close()
else:
try:
os.remove(path)
except EnvironmentError:
# ignore
pass
### /prepared
# FIXME: object
def remote_object_parameter_dir(self, cdist_object):
"""Returns the remote dir to the object parameter"""
return os.path.join(self.remote_object_dir(cdist_object), "parameter")
# FIXME: object ### ran
def object_code_paths(self, cdist_object): @property
"""Return paths to code scripts of object""" def ran(self):
return [os.path.join(self.object_dir(cdist_object), "code-local"), """Check whether the object has been ran."""
os.path.join(self.object_dir(cdist_object), "code-remote")] return os.path.isfile(os.path.join(self.path, "ran"))
@ran.setter
def ran(self, value):
"""Change the objects ran status."""
path = os.path.join(self.path, "ran")
if value:
open(path, "w").close()
else:
try:
os.remove(path)
except EnvironmentError:
# ignore
pass
### /ran

View file

@ -46,6 +46,16 @@ class Type(object):
return os.listdir(base_path) return os.listdir(base_path)
_instances = {}
def __new__(cls, *args, **kwargs):
"""only one instance of each named type may exist"""
# name is second argument
name = args[1]
if not name in cls._instances:
instance = super(Type, cls).__new__(cls, *args, **kwargs)
cls._instances[name] = instance
return cls._instances[name]
def __init__(self, base_path, name): def __init__(self, base_path, name):
self._base_path = base_path self._base_path = base_path
self.name = name self.name = name
@ -64,7 +74,6 @@ class Type(object):
self.__required_parameters = None self.__required_parameters = None
self.__optional_parameters = None self.__optional_parameters = None
def __repr__(self): def __repr__(self):
return '<Type %s>' % self.name return '<Type %s>' % self.name