set good path in include file and patch file
This commit is contained in:
@ -9,8 +9,8 @@ from shutil import copy
|
||||
import logging
|
||||
from typing import Dict, Any
|
||||
from subprocess import call
|
||||
from os import listdir, makedirs
|
||||
from os.path import dirname, join, isfile, abspath, normpath
|
||||
from os import listdir, makedirs, getcwd, chdir
|
||||
from os.path import dirname, join, isfile, abspath, normpath, relpath
|
||||
|
||||
from Cheetah.Template import Template as ChtTemplate
|
||||
from Cheetah.NameMapper import NotFound as CheetahNotFound
|
||||
@ -79,7 +79,8 @@ class CheetahTemplate(ChtTemplate):
|
||||
context,
|
||||
eosfunc: Dict,
|
||||
destfilename,
|
||||
variable):
|
||||
variable,
|
||||
):
|
||||
"""Initialize Creole CheetahTemplate
|
||||
"""
|
||||
extra_context = {'is_defined' : IsDefined(context),
|
||||
@ -93,11 +94,15 @@ class CheetahTemplate(ChtTemplate):
|
||||
searchList=[context, eosfunc, extra_context])
|
||||
|
||||
# FORK of Cheetah fonction, do not replace '\\' by '/'
|
||||
def serverSidePath(self, path=None,
|
||||
def serverSidePath(self,
|
||||
path=None,
|
||||
normpath=normpath,
|
||||
abspath=abspath
|
||||
):
|
||||
|
||||
# strange...
|
||||
if path is None and isinstance(self, str):
|
||||
path = self
|
||||
if path:
|
||||
return normpath(abspath(path))
|
||||
# return normpath(abspath(path.replace("\\", '/')))
|
||||
@ -107,7 +112,6 @@ class CheetahTemplate(ChtTemplate):
|
||||
return None
|
||||
|
||||
|
||||
|
||||
class CreoleLeader:
|
||||
def __init__(self, value, follower=None, index=None):
|
||||
"""
|
||||
@ -302,37 +306,43 @@ class CreoleTemplateEngine:
|
||||
return CreoleExtra(families)
|
||||
|
||||
def patch_template(self,
|
||||
filename: str):
|
||||
filename: str,
|
||||
tmp_dir: str,
|
||||
patch_dir: str,
|
||||
) -> None:
|
||||
"""Apply patch to a template
|
||||
"""
|
||||
patch_cmd = ['patch', '-d', self.tmp_dir, '-N', '-p1']
|
||||
patch_cmd = ['patch', '-d', tmp_dir, '-N', '-p1']
|
||||
patch_no_debug = ['-s', '-r', '-', '--backup-if-mismatch']
|
||||
|
||||
# patches variante + locaux
|
||||
for directory in [join(Config['patch_dir'], 'variante'), Config['patch_dir']]:
|
||||
patch_file = join(directory, f'{filename}.patch')
|
||||
if isfile(patch_file):
|
||||
log.info(_("Patching template '{filename}' with '{patch_file}'"))
|
||||
ret = call(patch_cmd + patch_no_debug + ['-i', patch_file])
|
||||
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, self.tmp_dir)
|
||||
patch_file = join(patch_dir, f'{filename}.patch')
|
||||
if isfile(patch_file):
|
||||
log.info(_("Patching template '{filename}' with '{patch_file}'"))
|
||||
rel_patch_file = relpath(patch_file, tmp_dir)
|
||||
ret = call(patch_cmd + patch_no_debug + ['-i', rel_patch_file])
|
||||
if ret:
|
||||
patch_cmd_err = ' '.join(patch_cmd + ['-i', rel_patch_file])
|
||||
log.error(_(f"Error applying patch: '{rel_patch_file}'\nTo reproduce and fix this error {patch_cmd_err}"))
|
||||
copy(join(self.distrib_dir, filename), tmp_dir)
|
||||
|
||||
def prepare_template(self,
|
||||
filename: str):
|
||||
filename: str,
|
||||
tmp_dir: str,
|
||||
patch_dir: str,
|
||||
) -> None:
|
||||
"""Prepare template source file
|
||||
"""
|
||||
log.info(_("Copy template: '{filename}' -> '{self.tmp_dir}'"))
|
||||
copy(filename, self.tmp_dir)
|
||||
self.patch_template(filename)
|
||||
log.info(_("Copy template: '{filename}' -> '{tmp_dir}'"))
|
||||
copy(filename, tmp_dir)
|
||||
self.patch_template(filename, tmp_dir, patch_dir)
|
||||
|
||||
def process(self,
|
||||
source: str,
|
||||
true_destfilename: str,
|
||||
destfilename: str,
|
||||
filevar: Dict,
|
||||
variable: Any):
|
||||
variable: Any,
|
||||
):
|
||||
"""Process a cheetah template
|
||||
"""
|
||||
# full path of the destination file
|
||||
@ -356,7 +366,10 @@ class CreoleTemplateEngine:
|
||||
|
||||
def instance_file(self,
|
||||
filevar: Dict,
|
||||
service_name: str) -> None:
|
||||
service_name: str,
|
||||
tmp_dir: str,
|
||||
dest_dir: str,
|
||||
) -> None:
|
||||
"""Run templatisation on one file
|
||||
"""
|
||||
log.info(_("Instantiating file '{filename}'"))
|
||||
@ -370,13 +383,13 @@ class CreoleTemplateEngine:
|
||||
if variable:
|
||||
variable = [variable]
|
||||
for idx, filename in enumerate(filenames):
|
||||
destfilename = join(self.dest_dir, filename[1:])
|
||||
destfilename = join(dest_dir, filename[1:])
|
||||
makedirs(dirname(destfilename), exist_ok=True)
|
||||
if variable:
|
||||
var = variable[idx]
|
||||
else:
|
||||
var = None
|
||||
source = join(self.tmp_dir, filevar['source'])
|
||||
source = join(tmp_dir, filevar['source'])
|
||||
if filevar['templating']:
|
||||
self.process(source,
|
||||
filename,
|
||||
@ -389,6 +402,11 @@ class CreoleTemplateEngine:
|
||||
async def instance_files(self) -> None:
|
||||
"""Run templatisation on all files
|
||||
"""
|
||||
ori_dir = getcwd()
|
||||
tmp_dir = relpath(self.tmp_dir, self.distrib_dir)
|
||||
dest_dir = relpath(self.dest_dir, self.distrib_dir)
|
||||
patch_dir = relpath(Config['patch_dir'], self.distrib_dir)
|
||||
chdir(self.distrib_dir)
|
||||
for option in await self.config.option.list(type='all'):
|
||||
namespace = await option.option.name()
|
||||
if namespace == Config['variable_namespace']:
|
||||
@ -397,8 +415,8 @@ class CreoleTemplateEngine:
|
||||
families = await self.load_eole_variables(namespace,
|
||||
option)
|
||||
self.rougail_variables_dict[namespace] = families
|
||||
for template in listdir(self.distrib_dir):
|
||||
self.prepare_template(join(self.distrib_dir, template))
|
||||
for template in listdir('.'):
|
||||
self.prepare_template(template, tmp_dir, patch_dir)
|
||||
for service_obj in await self.config.option('services').list('all'):
|
||||
service_name = await service_obj.option.doc()
|
||||
for fills in await service_obj.list('all'):
|
||||
@ -406,15 +424,17 @@ class CreoleTemplateEngine:
|
||||
for fill_obj in await fills.list('all'):
|
||||
fill = await fill_obj.value.dict()
|
||||
filename = fill['source']
|
||||
distib_file = join(self.distrib_dir, filename)
|
||||
if not isfile(distib_file):
|
||||
raise FileNotFound(_(f"File {distib_file} does not exist."))
|
||||
if not isfile(filename):
|
||||
raise FileNotFound(_(f"File {filename} does not exist."))
|
||||
if fill.get('activate', False):
|
||||
self.instance_file(fill,
|
||||
service_name,
|
||||
tmp_dir,
|
||||
dest_dir,
|
||||
)
|
||||
else:
|
||||
log.debug(_("Instantiation of file '{filename}' disabled"))
|
||||
chdir(ori_dir)
|
||||
|
||||
|
||||
async def generate(config: Config,
|
||||
|
Reference in New Issue
Block a user