|
|
|
#-*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 2010 Calculate Ltd. http://www.calculate-linux.org
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import os, sys
|
|
|
|
|
|
|
|
from cl_apply_template import updateConfFiles, __app__, __version__
|
|
|
|
from cl_opt import opt
|
|
|
|
from cl_share_cmd import share_cmd
|
|
|
|
from cl_lang import lang
|
|
|
|
lang().setLanguage(sys.modules[__name__])
|
|
|
|
|
|
|
|
USAGE = _("%prog [options] path_clt_file")
|
|
|
|
|
|
|
|
COMMENT_EXAMPLES = _("Apply all of the templates for all packages")
|
|
|
|
|
|
|
|
EXAMPLES = _("%prog --all")
|
|
|
|
|
|
|
|
DESCRIPTION = _("The Calculate Linux utility to use templates")
|
|
|
|
|
|
|
|
CMD_OPTIONS = [{'longOption':"clt",
|
|
|
|
'optVal':"CLT_TEMPLATE",
|
|
|
|
'help':_("process clt template (default)")
|
|
|
|
},
|
|
|
|
{'longOption':"all",
|
|
|
|
'help':_("process all templates")
|
|
|
|
},
|
|
|
|
{'shortOption':"v",
|
|
|
|
'longOption':"vars",
|
|
|
|
'help':_("print variables")
|
|
|
|
}]
|
|
|
|
|
|
|
|
class apply_template_cmd(share_cmd):
|
|
|
|
def __init__(self):
|
|
|
|
# Объект опций командной строки
|
|
|
|
self.optobj = opt(\
|
|
|
|
package=__app__,
|
|
|
|
version=__version__,
|
|
|
|
usage=USAGE,
|
|
|
|
examples=EXAMPLES,
|
|
|
|
comment_examples=COMMENT_EXAMPLES,
|
|
|
|
description=DESCRIPTION,
|
|
|
|
option_list=CMD_OPTIONS + opt.color_control,
|
|
|
|
check_values=self.checkOpts)
|
|
|
|
# Создаем объект логики
|
|
|
|
self.logicObj = updateConfFiles()
|
|
|
|
# Названия несовместимых опций
|
|
|
|
self.optionsNamesIncompatible = ["all", "clt"]
|
|
|
|
# Названия обязательных опций
|
|
|
|
self.optionsNamesRequired = self.optionsNamesIncompatible + ["v"]
|
|
|
|
|
|
|
|
|
|
|
|
def getOptionsRequired(self, optObj):
|
|
|
|
"""Получаем обязательные опции"""
|
|
|
|
retList = []
|
|
|
|
for nameOpt in self.optionsNamesRequired:
|
|
|
|
retList.append(getattr(optObj, nameOpt))
|
|
|
|
return retList
|
|
|
|
|
|
|
|
def _getNamesAllSetOptions(self):
|
|
|
|
"""Выдает словарь измененных опций"""
|
|
|
|
setOptDict = self.optobj.values.__dict__.items()
|
|
|
|
defaultOptDict = self.optobj.get_default_values().__dict__.items()
|
|
|
|
return dict(set(setOptDict) - set(defaultOptDict)).keys()
|
|
|
|
|
|
|
|
def getStringIncompatibleOptions(self):
|
|
|
|
"""Форматированная строка несовместимых опций разделенных ','"""
|
|
|
|
listOpt = list(set(self.optionsNamesIncompatible) &\
|
|
|
|
set(self._getNamesAllSetOptions()))
|
|
|
|
return ", ".join(map(lambda x: len(x) == 1 and "'-%s'"%x or "'--%s'"%x,\
|
|
|
|
listOpt))
|
|
|
|
|
|
|
|
def checkOpts(self, optObj, args):
|
|
|
|
"""Проверка опций командной строки"""
|
|
|
|
if len(args)==0:
|
|
|
|
optionsRequired = self.getOptionsRequired(optObj)
|
|
|
|
if len(filter(lambda x: x, optionsRequired))>1:
|
|
|
|
errMsg = _("incompatible options")+":"+" %s"\
|
|
|
|
%self.getStringIncompatibleOptions()
|
|
|
|
self.optobj.error(errMsg)
|
|
|
|
return False
|
|
|
|
elif not filter(lambda x: x, optionsRequired):
|
|
|
|
errMsg = _("required option")+":"+" %s"\
|
|
|
|
%" or ".join(map(lambda x:\
|
|
|
|
len(x) == 1 and "'-%s'"%x or "'--%s'"%x,\
|
|
|
|
self.optionsNamesRequired))
|
|
|
|
self.optobj.error(errMsg)
|
|
|
|
return False
|
|
|
|
elif len(args)==1:
|
|
|
|
if optObj.all:
|
|
|
|
errMsg = _("incorrect option")+":"+" --%s" %"all"
|
|
|
|
self.optobj.error(errMsg)
|
|
|
|
return False
|
|
|
|
elif optObj.clt:
|
|
|
|
errMsg = _("incorrect option")+":"+" --%s" %"clt"
|
|
|
|
self.optobj.error(errMsg)
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
errMsg = _("incorrect arguments") + ":" + " %s" %" ".join(args)
|
|
|
|
self.optobj.error(errMsg)
|
|
|
|
return False
|
|
|
|
return optObj, args
|
|
|
|
|
|
|
|
def applyCltTemplate(self, cltTemplate):
|
|
|
|
"""Применяем clt шаблон"""
|
|
|
|
if cltTemplate[0] != "/":
|
|
|
|
currentPath = os.getcwd()
|
|
|
|
cltTemplate = os.path.join(currentPath, cltTemplate)
|
|
|
|
return self.logicObj.applyCltTemplate(cltTemplate)
|
|
|
|
|
|
|
|
def applyAllTemplates(self):
|
|
|
|
"""Применяем все шаблоны"""
|
|
|
|
return self.logicObj.applyAllTemplates()
|