""" This class represents the list of commands """ # kate: tab-width 4; indent-width 4; space-indent on; # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Library General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Copyright 2004 Dries Verachtert import config, os, shutil, base64, time import storagefactory from mastercommand import MasterCommand from log4py import Logger from buildrootresult import BuildRootResult import smtplib from email.MIMEText import MIMEText from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email import Encoders class CommandList: def __init__(self): self.__config = config.Config.getInstance() self.__storage = storagefactory.StorageFactory.getStorage() self.__cat = Logger().get_instance(self) def addCommand(self, commandName,userId,specRepoName,specFilePath,toEmail,distroArchTag,priority,targetName): self.__cat.debug("start") aCommand = self.__checkAndCreateCommand(commandName,userId,specRepoName,specFilePath,toEmail,distroArchTag,priority,targetName) self.__cat.debug("command created") self.__createCommandDir(aCommand) self.__cat.debug("end") return aCommand def addTestCommand(self,commandName,userId,fileContentsEncoded,toEmail,distroArchTag,priority): self.__cat.debug("start") # check if a target 'test' and a specrepo 'test' exists testRepo = self.__config.getSpecRepositoryList().getSpecRepositoryByName('test') testTarget = self.__config.getTargetList().getTargetByName('test') if testRepo == None or testTarget == None: self.__cat.warn("no test target and/or test spec repo") return None # add this specfile to the specrepo fileContents = base64.decodestring(fileContentsEncoded) # current timestamp should be quite unique for queued test spec files fileName = userId + "test" + str(time.time()) + ".spec" self.__cat.debug("fileName: " + fileName) # create specfile and write contents f = open(testRepo.getRootDirectory() + "/" + fileName,"w") f.write(fileContents) f.close() self.__cat.debug("length, encoded: " + str(len(fileContentsEncoded)) + ", normal: "+ str(len(fileContents))) testRepo.updateFileList() return self.addCommand("BUILDSPECBYPATH", userId, testRepo.getName(), "/" + fileName, toEmail, distroArchTag, priority, testTarget.getName()) def __createCommandDir(self,aCommand): brr = BuildRootResult(None) commanddir = self.__config.getMasterWebRoot() + "/" + str(aCommand.getId()) self.__cat.debug("command dir=" + commanddir) os.mkdir(commanddir) shutil.copy(aCommand.getSpecRepositorySpecFile().getFullPath(), commanddir + "/") # now we run some user defined scripts which may download sources, alter the spec file and so on # first the scripts defined by the specrepository, then the scripts defined by the target aCommand.getSpecRepository().runPrepareSpecFileScripts(aCommand,commanddir, brr) aCommand.getTarget().runPrepareSpecFileScripts(aCommand,commanddir, brr) # also create a filelist self.__cat.debug("creating a filelist") fl = open(commanddir + "/filelist.txt","w") dirlist = os.listdir(commanddir) for entry in dirlist: if entry != "filelist.txt": fl.write(entry + "\n") fl.close() self.__mailPrepareInfo(aCommand, brr, commanddir) def __mailPrepareInfo(self, command, brr, commanddir): self.__cat.debug("start") subject = "pydar2:: prepare: " + command.getDistroArchTag() + " - " + command.getSpecFileShortFileName() self.__cat.debug("subject set") output = brr.getContainsErrorsText() + "\n" self.__cat.debug("errors descr added") output = output + "commanddir: "+ commanddir + "\n" output = output + command.toMultiLineString() output = output + "\nLog:\n" output = output + brr.getLog() + "\n" specfilecontents = self.__getSpecFileContents(command, commanddir) msg = MIMEMultipart() msg['Subject'] = subject msg['From'] = self.__config.getFromEmail() msg['To'] = command.getToEmail() me = self.__config.getFromEmail() you = command.getToEmail() msg.attach(MIMEText(output)) msg.attach(MIMEText(specfilecontents)) s = smtplib.SMTP(self.__config.getSmtpServer()) s.sendmail(me, [you], msg.as_string()) s.close() def __getSpecFileContents(self, command, commanddir): fp = open(os.path.join(commanddir, command.getSpecFileShortFileName())) retval = fp.read() fp.close() return retval def __checkAndCreateCommand(self,commandName,userId,specRepoName,specFilePath,toEmail,distroArchTag,priority,targetName): self.__cat.debug("userId:"+str(userId)+",specRepoName:"+str(specRepoName) + ",specFilePath:" + specFilePath + ",distroArchTag:" + str(distroArchTag)) if commandName != "BUILDSPECBYPATH": raise Exception("unknown command name") # the userId is already checked, now check some other values specRepo = self.__config.getSpecRepositoryList().getSpecRepositoryByName(specRepoName) if specRepo == None: raise Exception("unknown spec repository name") specRepoSpecFile = specRepo.getSpecRepositorySpecFileByFilePath(specFilePath) if specRepoSpecFile == None: raise Exception("unknown spec file path") self.__cat.debug("targetName=" + targetName) target = self.__config.getTargetList().getTargetByName(targetName) if target == None: self.__cat.info("unknown targetName=" + targetName) for j in self.__config.getTargetList().getTargetNames(): self.__cat.debug("known target: " + j) raise Exception("unknown target") self.__cat.debug("command looks ok") aCommand = MasterCommand(commandName,userId,specRepo.getId(),specRepoSpecFile.getId(),toEmail,distroArchTag,priority,target.getId()) self.__storage.createCommand(aCommand) self.__cat.debug("command created in storage") aCommand.setVersion(self.__storage.getNewestVersion(specRepoSpecFile.getId())) self.__cat.debug("end") return aCommand def moveCommandResults(self, commandId): self.__cat.debug("start") # get the command about this commandId aCommand = self.__storage.getCommand(commandId) self.__cat.debug("got aCommand:" + str(aCommand)) buildMachineId = self.__storage.getBuildMachineIdOfCommand(commandId) self.__cat.debug("got buildMachineId:" + str(buildMachineId)) commanddir = os.path.join(os.path.join(self.__config.getMasterBuildResultsDir(), buildMachineId),commandId) self.__cat.debug("got commanddir:" + str(commanddir)) self.__cat.debug("isdir: " + str(os.path.isdir(commanddir))) if os.path.isdir(commanddir): # nice, the dir exists.. self.__cat.debug("going to call moveCommandResults with command: " + str(aCommand) + ", commanddir: " + str(commanddir)) return aCommand.getTarget().moveCommandResults(aCommand, commanddir) else: return False