#-*- 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. __version__ = "2.2.0.1" __app__ = "calculate-assemble" import os import re import sys import traceback from os import path from cl_template import template from cl_utils import process,pathJoin,getRunCommands,getTupleVersion from subprocess import STDOUT,PIPE from cl_print import color_print from cl_datavars import DataVars from cl_distr import IsoDistributive, DirectoryDistributive, \ DistributiveError from cl_vars_share import varsShare from cl_lang import lang from server.utils import dialogYesNo import cl_overriding class printNoColor: def colorPrint(self,attr,fg,bg,string): sys.stdout.write(string) class AssembleError(Exception): """Installation Error""" assemble_errors = "" def installExit(self,*args,**kwars): raise AssembleError(self.__class__.assemble_errors) def overprintERROR(self,error): self.__class__.assemble_errors += str(error) + "\n" def getAssembleErrors(self): return self.__class__.assemble_errors def popAssembleErrors(self): res = self.__class__.assemble_errors self.__class__.assemble_errors = "" return res def getOverrideMethods(self): return self.installExit, self.overprintERROR cl_overriding.exit, cl_overriding.printERROR = \ AssembleError().getOverrideMethods() class DataVarsAssemble(DataVars): """Variable class for assemble""" def importAssemble(self, **args): '''Get variables for assemble''' # section name in calculate.env envSection = "assemble" # import assemble variables self.importData(envSection, ('cl_vars_assemble','cl_fill_assemble')) class cl_assemble(color_print): """Primary class for assemble actions""" def __init__(self): self.clVars = None self.startMessage = "" self.lenStartMessage = 0 self.clTempl = None self.force = False def setNoColor(self): self.color = False def initVars(self): """Primary initialization of variables""" self.clVars = DataVarsAssemble() self.clVars.importAssemble() self.clVars.flIniFile() def applyTemplatesForSquash(self,directory): """Apply templates for root of system.""" #self.clVars.Set("cl_root_path","/", True) self.clVars.Set("cl_action","squash", True) self.clVars.Set("cl_chroot_path",directory, True) self.clTempl = template(self.clVars) dirsFiles = self.clTempl.applyTemplates() self.clTempl.closeFiles() if self.clTempl.getError(): raise AssembleError(self.clTempl.getError()) else: return dirsFiles def printMessageForTest(self, message, lenMsg=False): """Print waiting message and OK or Error by func result""" message = "%s ..." % message self.printSUCCESS(message,printBR=False) self.startMessage = message if lenMsg: self.lenStartMessage = lenMsg else: self.lenStartMessage = self.lenString(self.startMessage) def printOnlyNotOK(self, string, offsetL=0, printBR=True): """Вывод на печать в случае сбоя""" self._printSysOut = sys.stdout self.printLine((('', string),), (('blueBr','['), ('redBr',' !! '), ('blueBr',']'), ), offsetL, printBR) def printByResult(self,result,failMessage=None): if self.startMessage: offset = 3 if result: self.printOnlyOK(" ", self.lenStartMessage + offset) else: self.printOnlyNotOK(" ", self.lenStartMessage + offset) if failMessage: self.printERROR(failMessage) self.startMessage = "" self.lenStartMessage = 0 def make(self,logicFunc,*argv): """Make iso image by variables""" self.sourceDistr = None self.targetDistr = None error = None try: if not logicFunc(*argv): return False except (EOFError), e: error = e except (AssembleError,DistributiveError),e: error = e except (Exception),e: error = "" for i in apply(traceback.format_exception, sys.exc_info()): error += i except KeyboardInterrupt,e: self.printByResult(False) self.printWARNING("Interrupting the system assembling") error = _("System assembling manually interrupt") if error: self.printByResult(False) try: if self.clTempl: self.clTempl.closeFiles() if self.sourceDistr: self.printMessageForTest(_("Releasing source data")) self.sourceDistr.close() self.printByResult(True) if self.targetDistr: self.printMessageForTest(_("Unmount assembling volume")) self.targetDistr.close() self.printByResult(True) except (AssembleError,DistributiveError),e: error = "%s\n%s" % (str(error),_("Unmounting error")) except KeyboardInterrupt,e: pass if error: self.printByResult(False) if error: for line in filter(lambda x: x,str(error).split('\n')): self.printERROR(line) self.printERROR(_("System assembling failed")) return False self.printSUCCESS(_("System was have assembled successfully")) return True