personalise directories

This commit is contained in:
Emmanuel Garette 2019-12-02 14:23:02 +01:00
parent 79a7950b0b
commit 06535b42b8
2 changed files with 43 additions and 27 deletions

View file

@ -5,14 +5,9 @@ fichier de configuration pour créole
""" """
from os.path import join, isfile from os.path import join, isfile
eoledir = '/usr/share/eole' eoleroot = join('.')
eoleroot = join(eoledir, 'creole')
# chemin du répertoire source des fichiers templates # chemin du répertoire source des fichiers templates
templatedir = '/var/lib/creole'
distrib_dir = join(eoleroot, 'distrib')
patch_dir = join(eoleroot, 'patch') patch_dir = join(eoleroot, 'patch')
# repertoire de la dtd # repertoire de la dtd

View file

@ -11,8 +11,8 @@ import logging
from typing import Dict from typing import Dict
from subprocess import call from subprocess import call
from os import listdir, unlink from os import listdir, unlink, mkdir
from os.path import basename, join, split, isfile from os.path import basename, join, split, isfile, isdir
from tempfile import mktemp from tempfile import mktemp
@ -34,7 +34,7 @@ from Cheetah.NameMapper import NotFound as CheetahNotFound
from tiramisu import Config from tiramisu import Config
from .config import patch_dir, templatedir, distrib_dir from .config import patch_dir
from .error import FileNotFound, TemplateError, TemplateDisabled from .error import FileNotFound, TemplateError, TemplateDisabled
from .i18n import _ from .i18n import _
@ -250,8 +250,14 @@ class CreoleTemplateEngine:
""" """
def __init__(self, def __init__(self,
config: Config, config: Config,
eosfunc_file: str): eosfunc_file: str,
distrib_dir: str,
tmp_dir: str,
dest_dir:str) -> None:
self.config = config self.config = config
self.dest_dir = dest_dir
self.tmp_dir = tmp_dir
self.distrib_dir = distrib_dir
eos = {} eos = {}
eosfunc = imp.load_source('eosfunc', eosfunc_file) eosfunc = imp.load_source('eosfunc', eosfunc_file)
for func in dir(eosfunc): for func in dir(eosfunc):
@ -267,7 +273,7 @@ class CreoleTemplateEngine:
if option.option.isoptiondescription(): if option.option.isoptiondescription():
if option.option.isleadership(): if option.option.isleadership():
print('leadership') print('leadership')
raise Exception('a faire') # raise Exception('a faire')
else: else:
self.load_eole_variables(option) self.load_eole_variables(option)
else: else:
@ -294,7 +300,7 @@ class CreoleTemplateEngine:
filename: str): filename: str):
"""Apply patch to a template """Apply patch to a template
""" """
patch_cmd = ['patch', '-d', templatedir, '-N', '-p1'] patch_cmd = ['patch', '-d', self.tmp_dir, '-N', '-p1']
patch_no_debug = ['-s', '-r', '-', '--backup-if-mismatch'] patch_no_debug = ['-s', '-r', '-', '--backup-if-mismatch']
# patches variante + locaux # patches variante + locaux
@ -306,7 +312,7 @@ class CreoleTemplateEngine:
if ret: if ret:
patch_cmd_err = ' '.join(patch_cmd + ['-i', patch_file]) patch_cmd_err = ' '.join(patch_cmd + ['-i', patch_file])
log.error(_(f"Error applying patch: '{patch_file}'\nTo reproduce and fix this error {patch_cmd_err}")) log.error(_(f"Error applying patch: '{patch_file}'\nTo reproduce and fix this error {patch_cmd_err}"))
copy(filename, templatedir) copy(filename, self.tmp_dir)
def strip_template_comment(self, def strip_template_comment(self,
filename: str): filename: str):
@ -330,22 +336,21 @@ class CreoleTemplateEngine:
filename: str): filename: str):
"""Prepare template source file """Prepare template source file
""" """
log.info(_("Copy template: '{filename}' -> '{templatedir}'")) log.info(_("Copy template: '{filename}' -> '{self.tmp_dir}'"))
copy(filename, templatedir) copy(filename, self.tmp_dir)
self.patch_template(filename) self.patch_template(filename)
# self.strip_template_comment(filename) # self.strip_template_comment(filename)
def process(self, def process(self,
destfilename: str,
filevar: Dict, filevar: Dict,
container: str): container: str):
"""Process a cheetah template """Process a cheetah template
""" """
# full path of the destination file # full path of the destination file
destfilename = join(dest_dir, filevar['source'])
log.info(_(f"Cheetah processing: '{destfilename}'")) log.info(_(f"Cheetah processing: '{destfilename}'"))
try: try:
cheetah_template = CheetahTemplate(join(templatedir, filevar['source']), cheetah_template = CheetahTemplate(join(self.tmp_dir, filevar['source']),
self.creole_variables_dict, self.creole_variables_dict,
self.eosfunc, self.eosfunc,
self.config.config.copy(), self.config.config.copy(),
@ -361,8 +366,8 @@ class CreoleTemplateEngine:
file_h.write(data) file_h.write(data)
def change_properties(self, def change_properties(self,
destfilename:str,
filevar: Dict): filevar: Dict):
destfilename = join(dest_dir, filevar['source'])
#chowncmd = ['chown'] #chowncmd = ['chown']
#chownarg = '' #chownarg = ''
chmodcmd = ['chmod'] chmodcmd = ['chmod']
@ -403,9 +408,17 @@ class CreoleTemplateEngine:
"""Run templatisation on one file of one container """Run templatisation on one file of one container
""" """
log.info(_("Instantiating file '{filename}'")) log.info(_("Instantiating file '{filename}'"))
self.process(filevar, container_dir = join(self.dest_dir,
container)
if not isdir(container_dir):
mkdir(container_dir)
destfilename = join(container_dir,
filevar['source'])
self.process(destfilename,
filevar,
container) container)
self.change_properties(filevar) self.change_properties(destfilename,
filevar)
def instance_files(self, def instance_files(self,
container=None): container=None):
@ -414,8 +427,8 @@ class CreoleTemplateEngine:
@param container: name of a container @param container: name of a container
@type container: C{str} @type container: C{str}
""" """
for template in listdir(distrib_dir): for template in listdir(self.distrib_dir):
self.prepare_template(join(distrib_dir, template)) self.prepare_template(join(self.distrib_dir, template))
for container_obj in self.config.option('containers').list('all'): for container_obj in self.config.option('containers').list('all'):
current_container = container_obj.option.doc() current_container = container_obj.option.doc()
if container is not None and container != current_container: if container is not None and container != current_container:
@ -425,17 +438,25 @@ class CreoleTemplateEngine:
for fill_obj in fills.list('all'): for fill_obj in fills.list('all'):
fill = fill_obj.value.dict() fill = fill_obj.value.dict()
filename = fill['source'] filename = fill['source']
if not isfile(join(distrib_dir, filename)): distib_file = join(self.distrib_dir, filename)
raise FileNotFound(_(f"File {filename} does not exist.")) if not isfile(distib_file):
raise FileNotFound(_(f"File {distib_file} does not exist."))
if fill.get('activate', False): if fill.get('activate', False):
self.instance_file(fill, current_container) self.instance_file(fill,
current_container)
else: else:
log.debug(_("Instantiation of file '{filename}' disabled")) log.debug(_("Instantiation of file '{filename}' disabled"))
def generate(config: Config, def generate(config: Config,
eosfunc_file: str, eosfunc_file: str,
distrib_dir: str,
tmp_dir: str,
dest_dir: str,
container: str=None): container: str=None):
engine = CreoleTemplateEngine(config, engine = CreoleTemplateEngine(config,
eosfunc_file) eosfunc_file,
distrib_dir,
tmp_dir,
dest_dir)
engine.instance_files(container=container) engine.instance_files(container=container)