Merge remote-tracking branch 'steven/master'
This commit is contained in:
commit
e89ca8cfc2
3 changed files with 75 additions and 9 deletions
|
@ -8,12 +8,8 @@ Object:
|
||||||
|
|
||||||
explorer_out_dir
|
explorer_out_dir
|
||||||
|
|
||||||
|
|
||||||
Type:
|
Type:
|
||||||
explorer_dir
|
|
||||||
remote_explorer_dir
|
|
||||||
|
|
||||||
explorer_remote_dir
|
|
||||||
|
|
||||||
type.manifest_path
|
type.manifest_path
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,8 @@ class FileList(collections.MutableSequence):
|
||||||
def __init__(self, path, initial=None):
|
def __init__(self, path, initial=None):
|
||||||
self._path = path
|
self._path = path
|
||||||
if initial:
|
if initial:
|
||||||
|
# delete existing file
|
||||||
|
os.unlink(self._path)
|
||||||
for i in initial:
|
for i in initial:
|
||||||
self.append(i)
|
self.append(i)
|
||||||
|
|
||||||
|
@ -40,7 +42,7 @@ class FileList(collections.MutableSequence):
|
||||||
try:
|
try:
|
||||||
with open(self._path) as fd:
|
with open(self._path) as fd:
|
||||||
for line in fd:
|
for line in fd:
|
||||||
lines.append(line.strip())
|
lines.append(line.rstrip('\n'))
|
||||||
except EnvironmentError as e:
|
except EnvironmentError as e:
|
||||||
# error ignored
|
# error ignored
|
||||||
pass
|
pass
|
||||||
|
@ -50,7 +52,7 @@ class FileList(collections.MutableSequence):
|
||||||
try:
|
try:
|
||||||
with open(self._path, 'w') as fd:
|
with open(self._path, 'w') as fd:
|
||||||
for line in lines:
|
for line in lines:
|
||||||
fd.write(line + '\n')
|
fd.write(str(line) + '\n')
|
||||||
except EnvironmentError as e:
|
except EnvironmentError as e:
|
||||||
# error ignored
|
# error ignored
|
||||||
raise
|
raise
|
||||||
|
@ -80,6 +82,26 @@ class FileList(collections.MutableSequence):
|
||||||
lines.insert(index, value)
|
lines.insert(index, value)
|
||||||
self.__write(lines)
|
self.__write(lines)
|
||||||
|
|
||||||
|
def sort(self):
|
||||||
|
lines = sorted(self)
|
||||||
|
self.__write(lines)
|
||||||
|
|
||||||
|
|
||||||
|
class FileListProperty(FileList):
|
||||||
|
# Descriptor Protocol
|
||||||
|
def __get__(self, obj, objtype=None):
|
||||||
|
if obj is None:
|
||||||
|
return self.__class__
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __set__(self, obj, value):
|
||||||
|
os.unlink(self._path)
|
||||||
|
for item in value:
|
||||||
|
self.append(item)
|
||||||
|
|
||||||
|
def __delete__(self, obj):
|
||||||
|
raise AttributeError("can't delete attribute")
|
||||||
|
|
||||||
|
|
||||||
class DirectoryDict(collections.MutableMapping):
|
class DirectoryDict(collections.MutableMapping):
|
||||||
"""A dict that stores it's state in a directory.
|
"""A dict that stores it's state in a directory.
|
||||||
|
@ -98,13 +120,13 @@ class DirectoryDict(collections.MutableMapping):
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
try:
|
try:
|
||||||
with open(os.path.join(self._path, key), "r") as fd:
|
with open(os.path.join(self._path, key), "r") as fd:
|
||||||
return fd.read()
|
return fd.read().rstrip('\n')
|
||||||
except EnvironmentError:
|
except EnvironmentError:
|
||||||
raise KeyError(key)
|
raise KeyError(key)
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
with open(os.path.join(self._path, key), "w") as fd:
|
with open(os.path.join(self._path, key), "w") as fd:
|
||||||
fd.write(value)
|
fd.write(str(value))
|
||||||
|
|
||||||
def __delitem__(self, key):
|
def __delitem__(self, key):
|
||||||
os.remove(os.path.join(self._path, key))
|
os.remove(os.path.join(self._path, key))
|
||||||
|
@ -114,3 +136,20 @@ class DirectoryDict(collections.MutableMapping):
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(os.listdir(self._path))
|
return len(os.listdir(self._path))
|
||||||
|
|
||||||
|
|
||||||
|
class DirectoryDictProperty(DirectoryDict):
|
||||||
|
# Descriptor Protocol
|
||||||
|
def __get__(self, obj, objtype=None):
|
||||||
|
if obj is None:
|
||||||
|
return self.__class__
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __set__(self, obj, value):
|
||||||
|
for name in self.keys():
|
||||||
|
del self[name]
|
||||||
|
if value is not None:
|
||||||
|
self.update(value)
|
||||||
|
|
||||||
|
def __delete__(self, obj):
|
||||||
|
raise AttributeError("can't delete attribute")
|
||||||
|
|
|
@ -25,6 +25,8 @@ import os
|
||||||
import cdist
|
import cdist
|
||||||
|
|
||||||
|
|
||||||
|
# FIXME: i should not have to care about prefix directory, local, remote and such.
|
||||||
|
# I know what my internals look like, the outside is none of my business.
|
||||||
class Type(object):
|
class Type(object):
|
||||||
"""Represents a cdist type.
|
"""Represents a cdist type.
|
||||||
|
|
||||||
|
@ -51,6 +53,28 @@ class Type(object):
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
raise cdist.MissingEnvironmentVariableError(e.args[0])
|
raise cdist.MissingEnvironmentVariableError(e.args[0])
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def remote_base_dir():
|
||||||
|
"""Return the absolute path to the top level directory where types
|
||||||
|
are kept on the remote/target host.
|
||||||
|
|
||||||
|
Requires the environment variable '__cdist_remote_base_dir' to be set.
|
||||||
|
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
return os.path.join(
|
||||||
|
os.environ['__cdist_remote_base_dir'],
|
||||||
|
'conf',
|
||||||
|
'type'
|
||||||
|
)
|
||||||
|
except KeyError as e:
|
||||||
|
raise cdist.MissingEnvironmentVariableError(e.args[0])
|
||||||
|
|
||||||
|
# FIXME: probably wrong place for this
|
||||||
|
@property
|
||||||
|
def remote_explorer_dir(self):
|
||||||
|
return os.path.join(self.remote_path, "explorer")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def list_types(cls):
|
def list_types(cls):
|
||||||
"""Return a list of type instances"""
|
"""Return a list of type instances"""
|
||||||
|
@ -80,6 +104,13 @@ class Type(object):
|
||||||
self.base_dir(),
|
self.base_dir(),
|
||||||
self.name
|
self.name
|
||||||
)
|
)
|
||||||
|
# FIXME: prefix directory should not leak into me
|
||||||
|
@property
|
||||||
|
def remote_path(self):
|
||||||
|
return os.path.join(
|
||||||
|
self.remote_base_dir(),
|
||||||
|
self.name
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_singleton(self):
|
def is_singleton(self):
|
||||||
|
|
Loading…
Reference in a new issue