simplify and creole => rougail
This commit is contained in:
@ -112,16 +112,11 @@ class ServiceAnnotator:
|
||||
del self.space.services
|
||||
|
||||
def convert_service_to_family(self, service_name, service_family, service):
|
||||
# tranform service object to family object
|
||||
# add services, service_accesses, ...
|
||||
for elttype, values in vars(service).items():
|
||||
if elttype in ['name', 'index']:
|
||||
continue
|
||||
family = self.objectspace.family()
|
||||
if elttype.endswith('s'):
|
||||
family.name = elttype + 'es'
|
||||
else:
|
||||
family.name = elttype + 's'
|
||||
family.name = elttype + 's'
|
||||
if isinstance(values, dict):
|
||||
values = list(values.values())
|
||||
family.family = self.convert_subelement_service(elttype,
|
||||
@ -204,8 +199,7 @@ class ServiceAnnotator:
|
||||
var_data['type'] = type_
|
||||
|
||||
variable = self.objectspace.variable()
|
||||
if not HIGH_COMPATIBILITY:
|
||||
variable.mandatory = True
|
||||
variable.mandatory = True
|
||||
for key, value in var_data.items():
|
||||
if key == 'value':
|
||||
if value is None:
|
||||
@ -248,19 +242,15 @@ class ServiceAnnotator:
|
||||
'for {}').format(file_.name))
|
||||
|
||||
def _reorder_elts(self, name, elts, duplicate_list):
|
||||
"""Reorders by index the elts (the interface,
|
||||
the hosts, actions...)
|
||||
"""Reorders by index the elts
|
||||
"""
|
||||
dict_elts = OrderedDict()
|
||||
# reorder elts by index
|
||||
new_elts = {}
|
||||
not_indexed = []
|
||||
for elt in elts:
|
||||
if not hasattr(elt, 'index'):
|
||||
not_indexed.append(elt)
|
||||
else:
|
||||
idx = elt.index
|
||||
new_elts.setdefault(idx, []).append(elt)
|
||||
idx = elt.index
|
||||
new_elts.setdefault(idx, []).append(elt)
|
||||
idxes = list(new_elts.keys())
|
||||
idxes.sort()
|
||||
elts = not_indexed
|
||||
@ -273,10 +263,7 @@ class ServiceAnnotator:
|
||||
continue
|
||||
value = getattr(elt, key)
|
||||
if not elt_added:
|
||||
if hasattr(elt, 'name'):
|
||||
eltname = elt.name
|
||||
else:
|
||||
eltname = idx
|
||||
eltname = elt.name
|
||||
dict_elts.setdefault(eltname, []).append({'elt_name': name, 'elt': elt})
|
||||
|
||||
result_elts = []
|
||||
@ -397,7 +384,7 @@ class SpaceAnnotator(object):
|
||||
self.has_calc = []
|
||||
self.force_no_value = []
|
||||
self.force_not_mandatory = []
|
||||
if eosfunc_file is not None:
|
||||
if eosfunc_file:
|
||||
self.eosfunc = imp.load_source('eosfunc', eosfunc_file)
|
||||
else:
|
||||
self.eosfunc = None
|
||||
@ -544,10 +531,7 @@ class SpaceAnnotator(object):
|
||||
variable_mode = variable.mode
|
||||
if variable_mode is not None and modes[mode] > modes[variable_mode]:
|
||||
mode = variable_mode
|
||||
if family.name == 'Containers':
|
||||
family.mode = 'normal'
|
||||
else:
|
||||
family.mode = mode
|
||||
family.mode = mode
|
||||
|
||||
def dynamic_families(self): # pylint: disable=C0111
|
||||
if not hasattr(self.space, 'variables'):
|
||||
@ -585,8 +569,6 @@ class SpaceAnnotator(object):
|
||||
variable.mode = family_mode
|
||||
if variable.mode != None and variable.mode != modes_level[0] and modes[variable.mode] < modes[family_mode]:
|
||||
variable.mode = family_mode
|
||||
if variable.name == "available_probes":
|
||||
variable.force_default_on_freeze = False
|
||||
|
||||
def default_variable_options(self):
|
||||
if hasattr(self.space, 'variables'):
|
||||
@ -727,19 +709,9 @@ class SpaceAnnotator(object):
|
||||
is_follower = False
|
||||
path = '{}.{}.{}'.format(family.path, variable.name, follower.name)
|
||||
self._annotate_variable(follower, family_mode, path, is_follower)
|
||||
if HIGH_COMPATIBILITY:
|
||||
# leader's variable are right
|
||||
if modes[variable.variable[0].mode] > modes[follower.mode]:
|
||||
follower.mode = variable.variable[0].mode
|
||||
else:
|
||||
# auto_save's variable is set in 'basic' mode if its mode is 'normal'
|
||||
if follower.auto_save is True and follower.mode != modes_level[-1]:
|
||||
follower.mode = modes_level[0]
|
||||
if modes[mode] > modes[follower.mode]:
|
||||
mode = follower.mode
|
||||
if not HIGH_COMPATIBILITY:
|
||||
# the leader's mode is the lowest
|
||||
variable.variable[0].mode = mode
|
||||
# leader's mode is minimum level
|
||||
if modes[variable.variable[0].mode] > modes[follower.mode]:
|
||||
follower.mode = variable.variable[0].mode
|
||||
variable.mode = variable.variable[0].mode
|
||||
else:
|
||||
# auto_save's variable is set in 'basic' mode if its mode is 'normal'
|
||||
@ -770,13 +742,9 @@ class SpaceAnnotator(object):
|
||||
# sort fill/auto by index
|
||||
if 'fill' in vars(constraints):
|
||||
for idx, fill in enumerate(constraints.fill):
|
||||
if fill.index in fills:
|
||||
raise Exception('hu?')
|
||||
fills[fill.index] = {'idx': idx, 'fill': fill, 'type': 'fill'}
|
||||
if 'auto' in vars(constraints):
|
||||
for idx, fill in enumerate(constraints.auto):
|
||||
if fill.index in fills:
|
||||
raise Exception('hu?')
|
||||
fills[fill.index] = {'idx': idx, 'fill': fill, 'type': 'auto'}
|
||||
indexes = list(fills.keys())
|
||||
indexes.sort()
|
||||
@ -785,10 +753,7 @@ class SpaceAnnotator(object):
|
||||
remove_fills = []
|
||||
for idx in indexes:
|
||||
fill = fills[idx]['fill']
|
||||
if hasattr(fill, 'redefine'):
|
||||
redefine = bool(fill.redefine)
|
||||
else:
|
||||
redefine = False
|
||||
redefine = bool(fill.redefine)
|
||||
if fill.target in targets:
|
||||
if redefine:
|
||||
if targets[fill.target][1] == 'auto':
|
||||
@ -972,8 +937,6 @@ class SpaceAnnotator(object):
|
||||
'').format(check.target))
|
||||
param = check.param[0]
|
||||
if proposed_value_type:
|
||||
if param.type == 'eole':
|
||||
raise Exception('hu?')
|
||||
if param.type == 'variable':
|
||||
try:
|
||||
values = self.load_params_in_validenum(param)
|
||||
|
@ -3,18 +3,19 @@
|
||||
fichier de configuration pour créole
|
||||
|
||||
"""
|
||||
from os.path import join, isfile
|
||||
from os.path import join, isfile, abspath, dirname
|
||||
from pathlib import Path
|
||||
|
||||
eoleroot = join('.')
|
||||
eoleroot = dirname(abspath(join(__file__, '..', '..')))
|
||||
|
||||
# chemin du répertoire source des fichiers templates
|
||||
patch_dir = join(eoleroot, 'patch')
|
||||
|
||||
# repertoire de la dtd
|
||||
dtddir = '/usr/share/creole'
|
||||
if isfile('data/creole.dtd'):
|
||||
dtdfilename = 'data/creole.dtd'
|
||||
elif isfile('../creole/data/creole.dtd'):
|
||||
dtdfilename = '../creole/data/creole.dtd'
|
||||
dtddir = '/usr/share/rougail'
|
||||
if isfile(join(eoleroot, 'data/rougail.dtd')):
|
||||
dtdfilename = join(eoleroot, 'data/rougail.dtd')
|
||||
elif isfile('../rougail/data/rougail.dtd'):
|
||||
dtdfilename = '../rougail/data/rougail.dtd'
|
||||
else:
|
||||
dtdfilename = join(dtddir, 'creole.dtd')
|
||||
dtdfilename = join(dtddir, 'rougail.dtd')
|
||||
|
@ -124,7 +124,10 @@ class PopulateTiramisuObjects(object):
|
||||
|
||||
def make_tiramisu_objects(self, xmlroot, creolefunc_file):
|
||||
elt = Elt({'name': 'baseoption'})
|
||||
self.eosfunc = imp.load_source('eosfunc', creolefunc_file)
|
||||
if creolefunc_file is None:
|
||||
self.eosfunc = None
|
||||
else:
|
||||
self.eosfunc = imp.load_source('eosfunc', creolefunc_file)
|
||||
family = Family(elt, self.booleans, self.storage, self.eosfunc)
|
||||
self.storage.add('.', family)
|
||||
|
||||
@ -308,6 +311,7 @@ class Variable(Common):
|
||||
self.attrib['validators'] = []
|
||||
self.eosfunc = eosfunc
|
||||
self.storage = storage
|
||||
is_submulti = False
|
||||
for key, value in elt.attrib.items():
|
||||
if key in booleans:
|
||||
if value == 'True':
|
||||
@ -315,6 +319,7 @@ class Variable(Common):
|
||||
elif value == 'False':
|
||||
value = False
|
||||
elif key == 'multi' and value == 'submulti':
|
||||
is_submulti = True
|
||||
value = submulti
|
||||
else:
|
||||
raise CreoleLoaderError(_('unknown value {} for {}').format(value, key))
|
||||
@ -364,13 +369,22 @@ class Variable(Common):
|
||||
type_ = CONVERT_OPTION[child.attrib['type']]['opttype']
|
||||
else:
|
||||
type_ = self.object_type
|
||||
if self.attrib['multi'] and not is_follower:
|
||||
if self.attrib['multi'] is True and not is_follower:
|
||||
if 'default' not in self.attrib:
|
||||
self.attrib['default'] = []
|
||||
value = convert_tiramisu_value(child.text, type_)
|
||||
self.attrib['default'].append(value)
|
||||
if 'default_multi' not in self.attrib and not is_leader:
|
||||
self.attrib['default_multi'] = value
|
||||
elif self.attrib['multi'] == submulti:
|
||||
if 'default' not in self.attrib:
|
||||
self.attrib['default'] = []
|
||||
value = convert_tiramisu_value(child.text, type_)
|
||||
if not isinstance(value, list) and not is_follower:
|
||||
value = [value]
|
||||
self.attrib['default'].append(value)
|
||||
if 'default_multi' not in self.attrib and not is_leader:
|
||||
self.attrib['default_multi'] = value
|
||||
else:
|
||||
if 'default' in self.attrib:
|
||||
raise CreoleLoaderError(_('default value already set for {}'
|
||||
|
@ -39,7 +39,7 @@ FORCE_REDEFINABLES = ('family', 'slave', 'service', 'disknod', 'variables', 'fam
|
||||
# CreoleObjSpace's elements that shall be forced to the UnRedefinable type
|
||||
FORCE_UNREDEFINABLES = ('value', 'input', 'profile', 'ewtapp', 'tag', 'saltaction')
|
||||
# CreoleObjSpace's elements that shall be set to the UnRedefinable type
|
||||
UNREDEFINABLE = ('multi', 'type')
|
||||
UNREDEFINABLE = ('submulti', 'multi', 'type')
|
||||
|
||||
PROPERTIES = ('hidden', 'frozen', 'auto_freeze', 'auto_save', 'force_default_on_freeze',
|
||||
'force_store_value', 'disabled', 'mandatory')
|
||||
@ -47,6 +47,8 @@ CONVERT_PROPERTIES = {'auto_save': ['force_store_value'], 'auto_freeze': ['force
|
||||
|
||||
RENAME_ATTIBUTES = {'description': 'doc'}
|
||||
|
||||
INCOMPATIBLE_ATTRIBUTES = [['multi', 'submulti']]
|
||||
|
||||
#TYPE_TARGET_CONDITION = ('variable', 'family')
|
||||
|
||||
# _____________________________________________________________________________
|
||||
@ -88,8 +90,6 @@ class CreoleObjSpace(object):
|
||||
|
||||
# ['variable', 'separator', 'family']
|
||||
self.forced_text_elts = set()
|
||||
# ['disknod', 'follower', 'target', 'service', 'package', 'ip', 'value', 'tcpwrapper',
|
||||
# 'interface', 'input', 'port']
|
||||
self.forced_text_elts_as_name = set(['choice', 'property'])
|
||||
self.forced_choice_option = {}
|
||||
self.paths = Path()
|
||||
@ -357,6 +357,15 @@ class CreoleObjSpace(object):
|
||||
'not {}').format(attr, val))
|
||||
if not (attr == 'name' and getattr(creoleobj, 'name', None) != None):
|
||||
setattr(creoleobj, attr, val)
|
||||
keys = list(vars(creoleobj).keys())
|
||||
for incompatible in INCOMPATIBLE_ATTRIBUTES:
|
||||
found = False
|
||||
for inc in incompatible:
|
||||
if inc in keys:
|
||||
if found:
|
||||
raise CreoleDictConsistencyError(_('those attributes are incompatible {}').format(incompatible))
|
||||
found = True
|
||||
|
||||
|
||||
def _creoleobj_tree_visitor(self, child, creoleobj, namespace):
|
||||
"""Creole object tree manipulations
|
||||
|
@ -17,11 +17,13 @@ from os.path import dirname, basename, join, split, isfile, isdir
|
||||
from tempfile import mktemp
|
||||
|
||||
from Cheetah import Parser
|
||||
|
||||
|
||||
# l'encoding du template est déterminé par une regexp (encodingDirectiveRE dans Parser.py)
|
||||
# il cherche un ligne qui ressemble à '#encoding: utf-8
|
||||
# cette classe simule le module 're' et retourne toujours l'encoding utf-8
|
||||
# 6224
|
||||
class FakeEncoding():
|
||||
class FakeEncoding:
|
||||
def groups(self):
|
||||
return ('utf-8',)
|
||||
|
||||
@ -29,6 +31,7 @@ class FakeEncoding():
|
||||
return self
|
||||
Parser.encodingDirectiveRE = FakeEncoding()
|
||||
|
||||
|
||||
from Cheetah.Template import Template as ChtTemplate
|
||||
from Cheetah.NameMapper import NotFound as CheetahNotFound
|
||||
|
||||
@ -120,7 +123,6 @@ class CheetahTemplate(ChtTemplate):
|
||||
"""
|
||||
extra_context = {'is_defined' : IsDefined(context),
|
||||
# 'creole_client' : CreoleClient(config),
|
||||
# 'current_container':CreoleGet(current_container),
|
||||
'normalize_family': normalize_family,
|
||||
'rougail_filename': destfilename
|
||||
}
|
||||
@ -266,15 +268,15 @@ class CreoleTemplateEngine:
|
||||
self.tmp_dir = tmp_dir
|
||||
self.distrib_dir = distrib_dir
|
||||
eos = {}
|
||||
eosfunc = imp.load_source('eosfunc', eosfunc_file)
|
||||
for func in dir(eosfunc):
|
||||
if not func.startswith('_'):
|
||||
eos[func] = getattr(eosfunc, func)
|
||||
if eosfunc_file is not None:
|
||||
eosfunc = imp.load_source('eosfunc', eosfunc_file)
|
||||
for func in dir(eosfunc):
|
||||
if not func.startswith('_'):
|
||||
eos[func] = getattr(eosfunc, func)
|
||||
self.eosfunc = eos
|
||||
self.creole_variables_dict = {}
|
||||
|
||||
async def load_eole_variables_creole(self,
|
||||
config,
|
||||
optiondescription):
|
||||
for option in await optiondescription.list('all'):
|
||||
if await option.option.isoptiondescription():
|
||||
@ -284,17 +286,15 @@ class CreoleTemplateEngine:
|
||||
leader = CreoleLeader(await suboption.value.get())
|
||||
self.creole_variables_dict[await suboption.option.name()] = leader
|
||||
else:
|
||||
await leader.add_slave(config,
|
||||
await leader.add_slave(self.config,
|
||||
await suboption.option.name(),
|
||||
await suboption.option.path())
|
||||
else:
|
||||
await self.load_eole_variables_creole(config,
|
||||
option)
|
||||
await self.load_eole_variables_creole(option)
|
||||
else:
|
||||
self.creole_variables_dict[await option.option.name()] = await option.value.get()
|
||||
|
||||
async def load_eole_variables(self,
|
||||
config,
|
||||
namespace,
|
||||
optiondescription):
|
||||
families = {}
|
||||
@ -307,7 +307,7 @@ class CreoleTemplateEngine:
|
||||
leader = CreoleLeader(await suboption.value.get())
|
||||
leader_name = await suboption.option.name()
|
||||
else:
|
||||
await leader.add_slave(config,
|
||||
await leader.add_slave(self.config,
|
||||
await suboption.option.name(),
|
||||
await suboption.option.path())
|
||||
variables[leader_name] = leader
|
||||
@ -334,24 +334,6 @@ class CreoleTemplateEngine:
|
||||
log.error(_(f"Error applying patch: '{patch_file}'\nTo reproduce and fix this error {patch_cmd_err}"))
|
||||
copy(filename, self.tmp_dir)
|
||||
|
||||
def strip_template_comment(self,
|
||||
filename: str):
|
||||
"""Strip comment from template
|
||||
|
||||
This apply if filevar has a del_comment attribut
|
||||
"""
|
||||
# suppression des commentaires si demandé (attribut del_comment)
|
||||
if 'del_comment' in filevar and filevar['del_comment'] != '':
|
||||
strip_cmd = ['sed', '-i']
|
||||
log.info(_("Cleaning file '{0}'").format( filevar['source'] ))
|
||||
raise Exception('hu')
|
||||
#ret, out, err = pyeole.process.system_out(strip_cmd
|
||||
# + ['/^\s*{0}/d ; /^$/d'.format(filevar['del_comment']),
|
||||
# filevar['source'] ])
|
||||
#if ret != 0:
|
||||
# msg = _("Error removing comments '{0}': {1}")
|
||||
# raise TemplateError(msg.format(filevar['del_comment'], err))
|
||||
|
||||
def prepare_template(self,
|
||||
filename: str):
|
||||
"""Prepare template source file
|
||||
@ -359,12 +341,10 @@ class CreoleTemplateEngine:
|
||||
log.info(_("Copy template: '{filename}' -> '{self.tmp_dir}'"))
|
||||
copy(filename, self.tmp_dir)
|
||||
self.patch_template(filename)
|
||||
# self.strip_template_comment(filename)
|
||||
|
||||
def process(self,
|
||||
destfilename: str,
|
||||
filevar: Dict,
|
||||
container: str,
|
||||
variable: Any):
|
||||
"""Process a cheetah template
|
||||
"""
|
||||
@ -387,51 +367,12 @@ class CreoleTemplateEngine:
|
||||
with open(destfilename, 'w') as file_h:
|
||||
file_h.write(data)
|
||||
|
||||
def change_properties(self,
|
||||
destfilename:str,
|
||||
filevar: Dict):
|
||||
#chowncmd = ['chown']
|
||||
#chownarg = ''
|
||||
chmodcmd = ['chmod']
|
||||
chmodarg = ''
|
||||
|
||||
#if 'owner' in filevar and filevar['owner']:
|
||||
# chownarg = filevar['owner']
|
||||
#else:
|
||||
# chownarg = 'root'
|
||||
|
||||
#if 'group' in filevar and filevar['group']:
|
||||
# chownarg += ":" + filevar['group']
|
||||
#else:
|
||||
# chownarg += ':root'
|
||||
|
||||
if 'mode' in filevar and filevar['mode']:
|
||||
chmodarg = filevar['mode']
|
||||
else:
|
||||
chmodarg = '0644'
|
||||
|
||||
#chowncmd.extend( [chownarg, destfilename] )
|
||||
chmodcmd.extend([chmodarg, destfilename])
|
||||
|
||||
#log.info(_('Changing properties: {0}').format(' '.join(chowncmd)) )
|
||||
#ret = call(chowncmd)
|
||||
#if ret:
|
||||
# log.error(_('Error changing properties {0}: {1}').format(ret, err) )
|
||||
|
||||
log.info(_('Changing properties: {0}').format(' '.join(chmodcmd)) )
|
||||
ret = call(chmodcmd)
|
||||
if ret:
|
||||
chmod_cmd = ' '.join(chmodcmd)
|
||||
log.error(_(f'Error changing properties: {chmodcmd}'))
|
||||
|
||||
def instance_file(self,
|
||||
filevar: Dict,
|
||||
container: str):
|
||||
"""Run templatisation on one file of one container
|
||||
systemd_rights: list) -> None:
|
||||
"""Run templatisation on one file
|
||||
"""
|
||||
log.info(_("Instantiating file '{filename}'"))
|
||||
container_dir = join(self.dest_dir,
|
||||
container)
|
||||
filenames = filevar['name']
|
||||
if 'variable' in filevar:
|
||||
variable = filevar['variable']
|
||||
@ -442,7 +383,7 @@ class CreoleTemplateEngine:
|
||||
if variable:
|
||||
variable = [variable]
|
||||
for idx, filename in enumerate(filenames):
|
||||
destfilename = join(container_dir,
|
||||
destfilename = join(self.dest_dir,
|
||||
filename[1:])
|
||||
makedirs(dirname(destfilename), exist_ok=True)
|
||||
if variable:
|
||||
@ -451,35 +392,26 @@ class CreoleTemplateEngine:
|
||||
var = None
|
||||
self.process(destfilename,
|
||||
filevar,
|
||||
container,
|
||||
var)
|
||||
self.change_properties(destfilename,
|
||||
filevar)
|
||||
systemd_rights.append(f'C {filename} {filevar["mode"]} {filevar["owner"]} {filevar["group"]} - -')
|
||||
systemd_rights.append(f'z {filename} - - - - -')
|
||||
|
||||
async def instance_files(self,
|
||||
container=None):
|
||||
"""Run templatisation on all files of all containers
|
||||
|
||||
@param container: name of a container
|
||||
@type container: C{str}
|
||||
async def instance_files(self) -> None:
|
||||
"""Run templatisation on all files
|
||||
"""
|
||||
for option in await self.config.option.list(type='all'):
|
||||
namespace = await option.option.name()
|
||||
if namespace in ['services', 'actions']:
|
||||
continue
|
||||
elif namespace == 'creole':
|
||||
await self.load_eole_variables_creole(self.config,
|
||||
option)
|
||||
await self.load_eole_variables_creole(option)
|
||||
else:
|
||||
await self.load_eole_variables(self.config,
|
||||
namespace,
|
||||
await self.load_eole_variables(namespace,
|
||||
option)
|
||||
for template in listdir(self.distrib_dir):
|
||||
self.prepare_template(join(self.distrib_dir, template))
|
||||
systemd_rights = []
|
||||
for service_obj in await self.config.option('services').list('all'):
|
||||
current_container = await service_obj.option.doc()
|
||||
if container is not None and container != current_container:
|
||||
continue
|
||||
for fills in await service_obj.list('all'):
|
||||
if await fills.option.name() == 'files':
|
||||
for fill_obj in await fills.list('all'):
|
||||
@ -490,20 +422,23 @@ class CreoleTemplateEngine:
|
||||
raise FileNotFound(_(f"File {distib_file} does not exist."))
|
||||
if fill.get('activate', False):
|
||||
self.instance_file(fill,
|
||||
current_container)
|
||||
systemd_rights)
|
||||
else:
|
||||
log.debug(_("Instantiation of file '{filename}' disabled"))
|
||||
|
||||
with open(join(self.dest_dir, 'rougail.conf'), 'w') as fh:
|
||||
fh.write('\n'.join(systemd_rights))
|
||||
fh.write('\n')
|
||||
|
||||
|
||||
async def generate(config: Config,
|
||||
eosfunc_file: str,
|
||||
distrib_dir: str,
|
||||
tmp_dir: str,
|
||||
dest_dir: str,
|
||||
container: str=None):
|
||||
dest_dir: str) -> None:
|
||||
engine = CreoleTemplateEngine(config,
|
||||
eosfunc_file,
|
||||
distrib_dir,
|
||||
tmp_dir,
|
||||
dest_dir)
|
||||
await engine.instance_files(container=container)
|
||||
await engine.instance_files()
|
||||
|
Reference in New Issue
Block a user