You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
calculate-utils-3-install/pym/cl_apply_template.py

178 lines
7.7 KiB

#-*- coding: utf-8 -*-
# Copyright 2010 Mir 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 sys, os, re
import cl_install
__version__ = cl_install.__version__
__app__ = cl_install.__app__
from cl_datavars import DataVars
from cl_template import template, templateClt
from cl_print import color_print
from cl_lang import lang
tr = lang()
tr.setGlobalDomain('cl_install')
tr.setLanguage(sys.modules[__name__])
#sys.path.insert(0, "/usr/lib/calculate-2.2/calculate-lib/pym")
#packagePath = "/usr/lib/calculate-2.2"
#for path in os.listdir(packagePath):
#realPath = os.path.join(packagePath,path)
#if os.path.isdir(realPath):
#pymPath = os.path.join(realPath,"pym")
#if len(filter(lambda x:x.startswith("cl_vars_") and\
#x.endswith(".py") or x.startswith("cl_fill_") and\
#x.endswith(".py"), os.listdir(pymPath)))==2:
#sys.path.insert(0, os.path.abspath(pymPath))
class DataVarsObject(DataVars):
"""Класс переменных для десктопа"""
def __init__(self, section):
DataVars.__init__(self)
self.section=section
def importDataObject(self, **args):
'''Заполнить конфигурацию переменных, для десктопа'''
# Имя секции в calculate.env
#envSection = "calculate-desktop"
# заполнить переменные окружения алгоритмом по умолнанию
self.importData(self.section, ('cl_vars_%s' %self.section,
'cl_fill_%s' %self.section))
self.flIniFile()
class updateConfFiles(color_print):
"""Для обновления конфигурационных файлов"""
patternSect = re.compile("^\s*\[([^\[\]]+)\]\s*")
firstEnvFile = "/etc/calculate/calculate2.env"
def __init__(self):
clVars = DataVarsObject("install")
clVars.importDataObject()
self.clVars = clVars
def applyCltTemplate(self, cltTemplatePath):
"""Применяем clt шаблон"""
realPath = "/usr/lib/calculate-2.2/calculate-install"
pymPath = os.path.join(realPath,"pym")
if not os.path.isdir(pymPath) or\
not len(filter(lambda x:x.startswith("cl_vars_") and\
x.endswith(".py") or x.startswith("cl_fill_") and\
x.endswith(".py"), os.listdir(pymPath)))==2:
self.printERROR(_("Can not installed package %s")
%"calculate-install")
return False
dirsTemplates = self.clVars.Get("cl_template_clt_path")
flagFoundPath = False
for dirTemplates in dirsTemplates:
if cltTemplatePath.startswith(dirTemplates):
flagFoundPath = True
if not flagFoundPath:
self.printERROR(_("Incorrect path %s")%cltTemplatePath)
self.printWARNING(_("Use the following directories and their \
subdirectories %s")%', '.join(dirsTemplates))
return False
if not os.path.exists(cltTemplatePath):
self.printERROR(_("File '%s' does not exist")%cltTemplatePath)
return False
if not os.access(cltTemplatePath, os.R_OK):
self.printERROR(_("Permission denied: '%s'")%cltTemplatePath)
return False
if not os.path.isfile(cltTemplatePath):
self.printERROR(_("'%s' is not file")%cltTemplatePath)
return False
clTempl = templateClt(self.clVars)
if not cltTemplatePath.endswith(clTempl.extFileTemplate):
self.printERROR(_("extension of the file '%s' not '.clt'")
%cltTemplatePath)
return False
calcPkg = self.clVars.Get("cl_name")+"-" + self.clVars.Get("cl_ver")
nameFile = clTempl.applyTemplate(cltTemplatePath)
if nameFile is False:
self.printERROR(_("Error template in a package %s")\
%calcPkg)
for errMess in clTempl.getError().splitlines():
self.printERROR(errMess)
return False
self.printWARNING(_("Package %s has changed files")%calcPkg+":")
self.printWARNING(" "*5 + nameFile)
return True
def printVars(self, opts):
"""Печать существующих переменных"""
if opts == ["all"]:
self.clVars.printVars()
else:
self.clVars.printVars(opts)
def applyAllTemplates(self):
"""Обновление конфигурационных файлов"""
if not os.path.exists(self.firstEnvFile):
self.printERROR(_("File '%s' does not exist")%self.firstEnvFile)
return False
if not os.access(self.firstEnvFile, os.R_OK):
self.printERROR(_("Permission denied: '%s'")%self.firstEnvFile)
return False
sectionsWork = []
for line in open(self.firstEnvFile).readlines():
sRet = self.patternSect.search(line)
if sRet:
sectionsWork.append(sRet.group(1))
installSect = "install"
if not installSect in sectionsWork:
sectionsWork.insert(0, installSect)
installSect = "install"
dictPakkages = {}
oldPymPath = ""
for sectName in sectionsWork:
realPath = "/usr/lib/calculate-2.2/calculate-%s"%sectName
pymPath = os.path.join(realPath,"pym")
if not os.path.isdir(pymPath):
continue
if len(filter(lambda x:x.startswith("cl_vars_") and\
x.endswith(".py") or x.startswith("cl_fill_") and\
x.endswith(".py"), os.listdir(pymPath)))==2:
if oldPymPath:
sys.path = filter(lambda x: x!=oldPymPath, sys.path)
sys.path.insert(0, os.path.abspath(pymPath))
oldPymPath = os.path.abspath(pymPath)
clVars = DataVarsObject(sectName)
clVars.importDataObject()
# будут применены все шаблоны .clt (cltFilter=False)
# и обычные шаблоны
clTempl = template(clVars, cltFilter=False)
dirsFiles = clTempl.applyTemplates()
nameAndVerPkg = clVars.Get("cl_name")+"-"+\
clVars.Get("cl_ver")
if dirsFiles is False:
self.printERROR(_("Error template in a package %s")\
%nameAndVerPkg)
for errMess in clTempl.getError().splitlines():
self.printERROR(errMess)
return False
if dirsFiles and dirsFiles[1]:
dictPakkages[nameAndVerPkg] =\
sorted(list(set(dirsFiles[1])))
if dictPakkages:
for calcPkg in dictPakkages:
self.printWARNING(_("Package %s has changed files")%calcPkg+":")
for nameF in dictPakkages[calcPkg]:
nameFile = nameF
if nameFile[:1] != "/":
nameFile = "/" + nameFile
self.printWARNING(" "*5 + nameFile)
return True