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
eoledir = '/usr/share/eole'
eoleroot = join(eoledir, 'creole')
eoleroot = join('.')
# chemin du répertoire source des fichiers templates
templatedir = '/var/lib/creole'
distrib_dir = join(eoleroot, 'distrib')
patch_dir = join(eoleroot, 'patch')
# repertoire de la dtd

View file

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