first new type emulator parameter support in cdist

Signed-off-by: Nico Schottelius <nico@kr.ethz.ch>
This commit is contained in:
Nico Schottelius 2011-09-16 16:08:25 +02:00
parent f720ce9243
commit 75510d88c9
2 changed files with 35 additions and 25 deletions

View file

@ -62,6 +62,21 @@ VERSION = "2.0.0"
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
log = logging.getLogger()
def file_to_list(filename):
"""Return list from \n seperated file"""
if os.path.isfile(filename):
file_fd = open(filename, "r")
lines = file_fd.readlines()
file_fd.close()
# Remove \n from all lines
lines = map(lambda s: s.strip(), lines)
else:
lines = []
return lines
class TypeEmulator:
def __init__(self, name):
self.name = name
@ -467,28 +482,11 @@ class Cdist:
self.shell_run_or_debug_fail(manifest, [manifest], env=env)
def list_object_requirements(self, cdist_object):
"""Return list of requirements for specific object"""
file=os.path.join(self.object_dir(cdist_object), "require")
if os.path.isfile(file):
file_fd = open(file, "r")
requirements = file_fd.readlines()
file_fd.close()
# Remove \n from all lines
requirements = map(lambda s: s.strip(), requirements)
log.debug("Requirements for %s: %s", cdist_object, requirements)
else:
requirements = []
return requirements
def object_run(self, cdist_object, mode):
"""Run gencode or code for an object"""
log.debug("Running %s from %s", mode, cdist_object)
requirements = self.list_object_requirements(cdist_object)
file=os.path.join(self.object_dir(cdist_object), "require")
requirements = file_to_list(file)
type = self.get_type_from_object(cdist_object)
for requirement in requirements:
@ -638,21 +636,32 @@ def install(args):
def emulator():
"""Emulate type commands (i.e. __file and co)"""
type = os.path.basename(sys.argv[0])
type_dir = os.environ['__cdist_type_base_dir']
type_dir = os.path.join(os.environ['__cdist_type_base_dir'], type)
param_dir = os.path.join(type_dir, "parameter")
parser = argparse.ArgumentParser(add_help=False)
# Setup optional parameters
if os.path.isfile(os.path.join(type_dir, "parameter", "optional"):
__cdist_type_base_dir
for parameter in file_to_list(os.path.join(param_dir, "optional")):
argument = "--" + parameter
print("Adding" + argument)
parser.add_argument(argument, action='store', required=False)
# Setup required parameters
for parameter in file_to_list(os.path.join(param_dir, "required")):
argument = "--" + parameter
print("Adding" + argument)
parser.add_argument(argument, action='store', required=True)
# Setup positional parameter, if not singleton
# And finally verify parameter
args = parser.parse_args(sys.argv[1:])
print("Oh, noe")
sys.exit(1)
def commandline():
"""Parse command line"""
# Construct parser others can reuse

View file

@ -27,8 +27,9 @@
- dependent on -c to cdist!
- submit via internal env!
- need to build parameter parser
- need to read file
- refactor list_object_requirements
Test:
__cdist_type_base_dir=$(pwd -P)/conf/type __file
- Allow manifest to be read from stdin
- Create new video for cdist 2.0.0