2011-10-06 10:57:38 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2011-10-06 18:34:36 +00:00
|
|
|
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
|
|
|
# 2011 Nico Schottelius (nico-cdist at schottelius.org)
|
2011-10-06 10:57:38 +00:00
|
|
|
#
|
|
|
|
# This file is part of cdist.
|
|
|
|
#
|
|
|
|
# cdist is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# cdist is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
2011-10-05 23:04:09 +00:00
|
|
|
|
2011-10-06 18:27:07 +00:00
|
|
|
import logging
|
2011-10-06 10:57:38 +00:00
|
|
|
import os
|
2011-10-06 15:15:45 +00:00
|
|
|
import collections
|
2011-10-05 22:58:43 +00:00
|
|
|
|
2011-10-06 10:57:38 +00:00
|
|
|
import cdist
|
2011-10-06 15:15:45 +00:00
|
|
|
import cdist.core.property
|
|
|
|
|
2011-10-06 18:27:07 +00:00
|
|
|
log = logging.getLogger(__name__)
|
2011-10-06 15:15:45 +00:00
|
|
|
|
|
|
|
DOT_CDIST = '.cdist'
|
2011-10-05 22:58:43 +00:00
|
|
|
|
2011-10-07 11:50:17 +00:00
|
|
|
# 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.
|
2011-10-06 10:57:38 +00:00
|
|
|
class Object(object):
|
|
|
|
"""Represents a cdist object.
|
2011-10-05 22:58:43 +00:00
|
|
|
|
2011-10-06 10:57:38 +00:00
|
|
|
All interaction with objects in cdist should be done through this class.
|
|
|
|
Directly accessing an object through the file system from python code is
|
|
|
|
a bug.
|
2011-10-05 22:58:43 +00:00
|
|
|
|
2011-10-06 10:57:38 +00:00
|
|
|
"""
|
2011-10-05 22:58:43 +00:00
|
|
|
|
|
|
|
@classmethod
|
2011-10-07 13:42:04 +00:00
|
|
|
def list_objects(cls, object_base_path, type_base_path):
|
2011-10-05 22:58:43 +00:00
|
|
|
"""Return a list of object instances"""
|
2011-10-07 13:42:04 +00:00
|
|
|
for object_name in cls.list_object_names(object_base_path):
|
2011-10-05 22:58:43 +00:00
|
|
|
type_name = object_name.split(os.sep)[0]
|
2011-10-07 13:42:04 +00:00
|
|
|
# FIXME: allow object without object_id? e.g. for singleton
|
2011-10-05 22:58:43 +00:00
|
|
|
object_id = os.sep.join(object_name.split(os.sep)[1:])
|
2011-10-07 13:42:04 +00:00
|
|
|
yield cls(cdist.core.Type(type_base_path, type_name), object_base_path, object_id=object_id)
|
2011-10-05 22:58:43 +00:00
|
|
|
|
|
|
|
@classmethod
|
2011-10-07 13:42:04 +00:00
|
|
|
def list_type_names(cls, object_base_path):
|
2011-10-05 22:58:43 +00:00
|
|
|
"""Return a list of type names"""
|
2011-10-07 13:42:04 +00:00
|
|
|
return os.listdir(object_base_path)
|
2011-10-05 22:58:43 +00:00
|
|
|
|
|
|
|
@classmethod
|
2011-10-07 13:42:04 +00:00
|
|
|
def list_object_names(cls, object_base_path):
|
2011-10-05 22:58:43 +00:00
|
|
|
"""Return a list of object names"""
|
2011-10-07 13:42:04 +00:00
|
|
|
for path, dirs, files in os.walk(object_base_path):
|
2011-10-06 15:15:45 +00:00
|
|
|
if DOT_CDIST in dirs:
|
2011-10-07 13:42:04 +00:00
|
|
|
yield os.path.relpath(path, object_base_path)
|
2011-10-05 22:58:43 +00:00
|
|
|
|
2011-10-07 14:03:37 +00:00
|
|
|
def __init__(self, type, base_path, object_id=None):
|
2011-10-05 22:58:43 +00:00
|
|
|
self.type = type # instance of Type
|
2011-10-07 13:42:04 +00:00
|
|
|
self.base_path = base_path
|
2011-10-05 22:58:43 +00:00
|
|
|
self.object_id = object_id
|
2011-10-06 15:15:45 +00:00
|
|
|
self.name = os.path.join(self.type.name, self.object_id)
|
2011-10-07 14:49:18 +00:00
|
|
|
self.path = os.path.join(self.type.path, self.object_id, DOT_CDIST)
|
2011-10-07 14:59:52 +00:00
|
|
|
self.absolute_path = os.path.join(self.base_path, self.path)
|
2011-10-07 13:42:04 +00:00
|
|
|
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")
|
2011-10-06 15:15:45 +00:00
|
|
|
|
|
|
|
self.__parameters = None
|
|
|
|
self.__requirements = None
|
2011-10-06 17:40:19 +00:00
|
|
|
|
2011-10-05 22:58:43 +00:00
|
|
|
def __repr__(self):
|
2011-10-06 15:15:45 +00:00
|
|
|
return '<Object %s>' % self.name
|
2011-10-05 22:58:43 +00:00
|
|
|
|
|
|
|
@property
|
2011-10-07 13:42:04 +00:00
|
|
|
def explorer_path(self):
|
|
|
|
# create absolute path
|
|
|
|
path = os.path.join(self.absolute_path, "explorer")
|
2011-10-07 11:15:11 +00:00
|
|
|
if not os.path.isdir(path):
|
|
|
|
os.mkdir(path)
|
2011-10-07 13:42:04 +00:00
|
|
|
# return relative path
|
|
|
|
return os.path.join(self.path, "explorer")
|
2011-10-07 11:50:17 +00:00
|
|
|
|
|
|
|
|
2011-10-06 15:15:45 +00:00
|
|
|
### requirements
|
|
|
|
@property
|
|
|
|
def requirements(self):
|
|
|
|
if not self.__requirements:
|
|
|
|
self.__requirements = cdist.core.property.FileList(os.path.join(self.path, "require"))
|
|
|
|
return self.__requirements
|
|
|
|
|
|
|
|
@requirements.setter
|
|
|
|
def requirements(self, value):
|
|
|
|
if isinstance(value, cdist.core.property.FileList):
|
|
|
|
self.__requirements = value
|
|
|
|
else:
|
|
|
|
self.__requirements = cdist.core.property.FileList(os.path.join(self.path, "require"), value)
|
|
|
|
### /requirements
|
|
|
|
|
|
|
|
|
|
|
|
### parameters
|
|
|
|
@property
|
|
|
|
def parameters(self):
|
|
|
|
if not self.__parameters:
|
|
|
|
self.__parameters = cdist.core.property.DirectoryDict(os.path.join(self.path, "parameter"))
|
|
|
|
return self.__parameters
|
|
|
|
|
|
|
|
@parameters.setter
|
|
|
|
def parameters(self, value):
|
|
|
|
if isinstance(value, cdist.core.property.DirectoryDict):
|
|
|
|
self.__parameters = value
|
|
|
|
else:
|
|
|
|
self.__parameters = cdist.core.property.DirectoryDict(os.path.join(self.path, "parameter"), value)
|
|
|
|
### /parameters
|
|
|
|
|
|
|
|
|
|
|
|
### changed
|
2011-10-05 22:58:43 +00:00
|
|
|
@property
|
|
|
|
def changed(self):
|
|
|
|
"""Check whether the object has been changed."""
|
2011-10-07 14:12:01 +00:00
|
|
|
return os.path.isfile(os.path.join(self.absolute_path, "changed"))
|
2011-10-05 22:58:43 +00:00
|
|
|
|
|
|
|
@changed.setter
|
|
|
|
def changed(self, value):
|
|
|
|
"""Change the objects changed status."""
|
2011-10-07 14:12:01 +00:00
|
|
|
path = os.path.join(self.absolute_path, "changed")
|
2011-10-05 22:58:43 +00:00
|
|
|
if value:
|
|
|
|
open(path, "w").close()
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
os.remove(path)
|
|
|
|
except EnvironmentError:
|
|
|
|
# ignore
|
|
|
|
pass
|
2011-10-06 15:15:45 +00:00
|
|
|
### /changed
|
2011-10-05 22:58:43 +00:00
|
|
|
|
2011-10-07 07:05:42 +00:00
|
|
|
|
2011-10-07 13:42:04 +00:00
|
|
|
### prepared
|
|
|
|
@property
|
|
|
|
def prepared(self):
|
|
|
|
"""Check whether the object has been prepared."""
|
2011-10-07 14:12:01 +00:00
|
|
|
return os.path.isfile(os.path.join(self.absolute_path, "prepared"))
|
2011-10-07 13:42:04 +00:00
|
|
|
|
|
|
|
@prepared.setter
|
|
|
|
def prepared(self, value):
|
|
|
|
"""Change the objects prepared status."""
|
2011-10-07 14:12:01 +00:00
|
|
|
path = os.path.join(self.absolute_path, "prepared")
|
2011-10-07 13:42:04 +00:00
|
|
|
if value:
|
|
|
|
open(path, "w").close()
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
os.remove(path)
|
|
|
|
except EnvironmentError:
|
|
|
|
# ignore
|
|
|
|
pass
|
|
|
|
### /prepared
|
2011-10-07 07:05:42 +00:00
|
|
|
|
|
|
|
|
2011-10-07 13:42:04 +00:00
|
|
|
### ran
|
|
|
|
@property
|
|
|
|
def ran(self):
|
|
|
|
"""Check whether the object has been ran."""
|
2011-10-07 14:12:01 +00:00
|
|
|
return os.path.isfile(os.path.join(self.absolute_path, "ran"))
|
2011-10-07 13:42:04 +00:00
|
|
|
|
|
|
|
@ran.setter
|
|
|
|
def ran(self, value):
|
|
|
|
"""Change the objects ran status."""
|
2011-10-07 14:12:01 +00:00
|
|
|
path = os.path.join(self.absolute_path, "ran")
|
2011-10-07 13:42:04 +00:00
|
|
|
if value:
|
|
|
|
open(path, "w").close()
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
os.remove(path)
|
|
|
|
except EnvironmentError:
|
|
|
|
# ignore
|
|
|
|
pass
|
|
|
|
### /ran
|