def has_chroot_instance(self): lock = os.path.join(self.root, ".chroot.lock") try: return my_fuser(lock) # After reading my_fuser code, it seems that catching # "file not found" exception is equivalent to False. except IOError as e: msg = u"Got an exception while testing if chroot is in use: %s" % unicode(e) d = {'exception': e} ObsLightPrintManager.getLogger().error(msg, extra=d) return False
def execPipeSubprocess(self, command, command2): ObsLightPrintManager.getLogger().info("command: " + command + " | " + command2) splittedCommand1 = shlex.split(str(command)) splittedCommand2 = shlex.split(str(command2)) p1 = subprocess.Popen(splittedCommand1, stdout=subprocess.PIPE) p2 = subprocess.Popen(splittedCommand2, stdin=p1.stdout, stdout=subprocess.PIPE) p1.stdout.close() p2.stdout.close() return 0
def parseFile(self, value=None): def testSection(line): for sect in self.__listSection: if line.startswith(sect): return True return False self.__orderList = [] self.__spectDico = {} path = self.__path if path != None: if not os.path.exists(path): raise ObsLightErr.ObsLightSpec("parseFile: the path: " + path + ", do not exist") if value is None: tmpLineList = [] #Load the file in a list f = open(path, 'rb') for line in f: tmpLineList.append(line) f.close() else: tmpLineList = value.split("\n") for i in range(len(tmpLineList)): tmpLineList[i] = tmpLineList[i] + "\n" #init variable self.__spectDico = {} currentSection = self.__introduction_section self.__spectDico[currentSection] = [] self.__orderList.append(currentSection) for line in tmpLineList: #use a clean string tmp_line = self.__cleanline(line) if testSection(tmp_line): while tmp_line in self.__spectDico.keys(): tmp_line = tmp_line + "(1)" currentSection = tmp_line self.__spectDico[currentSection] = [] self.__orderList.append(currentSection) self.__spectDico[currentSection].append(line) else: ObsLightPrintManager.obsLightPrint("ERROR")
def getLocalProjectList(self, maintainer=False, bugowner=False, arch=None, remoteurl=False, raw=True): logger = ObsLightPrintManager.getLogger() logger.info("Getting project list from %s" % self.__serverAPI) if ("https://api.meego.com" in self.__serverAPI): msg = "api.meego.com doesn't support search request, Bug 24979" logger.warning(msg) obsLightOsc = ObsLightOsc.getObsLightOsc() if (not raw) and not ("https://api.meego.com" in self.__serverAPI): aBugowner = None if bugowner: aBugowner = self.__user aMaintainer = None if maintainer: aMaintainer = self.__user return obsLightOsc.getFilteredProjectListFromServer( self.__serverAPI, aMaintainer, aBugowner, arch, remoteurl) else: return obsLightOsc.getProjectListFromServer(self.__serverAPI)
def testRepositoryUrl(url): ''' Return True if `url` is a package repository. ''' logger = ObsLightPrintManager.getLogger() logger.info("Testing if '%s' is a package repository", url) (scheme, netloc, path, _params, _query, _fragment) = urlparse(str(url)) if ":" in netloc: (host, port) = netloc.split(":") else: host = netloc if scheme == "https": port = "443" else: port = "80" conn = createConn(host, port, scheme) try: if not path.endswith("/"): repomdUrl = path + "/repodata/repomd.xml" else: repomdUrl = path + "repodata/repomd.xml" logger.debug("Calling %s" % repomdUrl) conn.request('HEAD', repomdUrl) response = conn.getresponse() logger.debug("Response status: %d" % response.status) return response.status == 200 except BaseException: logger.warning("Error while connecting to '%s'", url) return False finally: conn.close()
def testHost(url): ''' Test if we can connect to the host of `url` (not to the complete URL). ''' (scheme, netloc, _path, _params, _query, _fragment) = urlparse(str(url)) if len(urllib.getproxies_environment()) > 0: return testUrl(scheme + "://" + netloc) if ":" in netloc: (host, port) = netloc.split(":") else: host = netloc if scheme == "https": port = "443" else: port = "80" logger = ObsLightPrintManager.getLogger() logger.info("Testing connection to '%s:%s'", host, port) conn = createConn(host, port, scheme) try: conn.connect() except BaseException, e: message = "Could not connect to %s: %s" % (str(host), str(e)) logger.warning(message) return False
def addpatch(self, aFile): #init the aId of the patch patchID = 0 for line in self.__spectDico[self.__introduction_section]: #a regular expression sould be better if line.startswith("Patch") and (":" in line): try: if aFile == self.__cleanline(line.split(":")[1]): return 1 aId = line.split(":")[0].replace("Patch", "") #the aId can be null if (aId != "") and (patchID <= int(aId)): patchID = int(aId) + 1 except ValueError: ObsLightPrintManager.obsLightPrint(ValueError) except IndexError: ObsLightPrintManager.obsLightPrint(IndexError) patch_Val_Prep = "Patch" + str(patchID) patch_Val_Build = "%patch" + str(patchID) patchCommand = patch_Val_Prep + ": " + aFile + "\n" self.__spectDico[self.__introduction_section].insert(0, patchCommand) comment = "# This line is insert automatically , please comment and clean the code\n" self.__spectDico[self.__introduction_section].insert(0, comment) #You can have not %prep section #add the patch after the last one or if any patch present in the prep part, at the end. if self.__prepFlag in self.__spectDico.keys(): i = 0 res = 0 for line in self.__spectDico[self.__prepFlag]: i += 1 if line.startswith("%patch"): res = i if res == 0: res = i self.__spectDico[self.__prepFlag].insert( res, patch_Val_Build + " -p1 \n") comment = "# This line is insert automatically, please comment and clean the code\n" self.__spectDico[self.__prepFlag].insert(res, comment) return 0
def addFile(self, baseFile=None, aFile=None): #init the aId of the Source SourceID = 1 for line in self.__spectDico[self.__introduction_section]: #a regular expression sould be better if line.startswith("Source") and (":" in line): try: if aFile == self.__cleanline(line.split(":")[1]): return None aId = line.split(":")[0].replace("Source", "") #the aId can be null if (aId != "") and (SourceID <= int(aId)): SourceID = int(aId) + 1 except ValueError: ObsLightPrintManager.obsLightPrint(ValueError) except IndexError: ObsLightPrintManager.obsLightPrint(IndexError) source_Val_Prep = "Source" + str(SourceID) source_Val_Build = "SOURCE" + str(SourceID) insertLine = source_Val_Prep + ": " + baseFile + "\n" self.__spectDico[self.__introduction_section].insert(0, insertLine) comment = "# This line is insert automatically, please comment and clean the code\n" self.__spectDico[self.__introduction_section].insert(0, comment) #You can have not %prep section if not self.__prepFlag in self.__spectDico.keys(): self.__spectDico[self.__prepFlag] = [] self.__orderList.append(self.__prepFlag) self.__spectDico[self.__prepFlag].append(self.__prepFlag + "\n") self.__spectDico[self.__prepFlag].append("cp %{" + source_Val_Build + "} " + aFile + "\n") return None
def testUrl(url): ''' Test if we can reach `url`. ''' logger = ObsLightPrintManager.getLogger() logger.info("Testing URL '%s'", url) opener = urllib2.build_opener( urllib2.ProxyHandler(urllib.getproxies_environment())) urllib2.install_opener(opener) try: _test = urllib2.urlopen(url, timeout=SOCKETTIMEOUT) return True except urllib2.URLError, e: message = "Could not reach %s: %s" % (str(url), str(e)) logger.warning(message) return False
def openFileWithDefaultProgram(filePath): logger = ObsLightPrintManager.getLogger() openCommand = ObsLightConfig.getOpenFileCommand() if openCommand is None: message = u"No 'openFile' command configured." raise ObsLightErr.ConfigurationError(message) logger.info(u"Opening %s", filePath) logger.debug(u"Running command: '%s %s'", openCommand, filePath) try: retVal = call([openCommand, filePath]) return retVal except BaseException: logger.error("Failed to run '%s %s'", openCommand, filePath, exc_info=True) raise
def execSubprocess(self, command, *_args, **_kwargs): ''' Execute the "command" in a sub process, the "command" must be a valid bash command. _args and _kwargs are for compatibility. ''' if "stdout" in _kwargs.keys(): stdout = _kwargs["stdout"] else: stdout = False if "noOutPut" in _kwargs.keys(): noOutPut = _kwargs["noOutPut"] else: noOutPut = False outPutRes = "" ObsLightPrintManager.getLogger().debug("command: " + command) #need Python 2.7.3 to do shlex.split(command) splittedCommand = shlex.split(str(command)) p = subprocess.Popen(splittedCommand, stdout=subprocess.PIPE, stderr=subprocess.PIPE) f_stdout = p.stdout f_stderr = p.stderr flags = fcntl.fcntl(f_stdout, fcntl.F_GETFL) if not f_stdout.closed: fcntl.fcntl(f_stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK) flags = fcntl.fcntl(f_stderr, fcntl.F_GETFL) if not f_stderr.closed: fcntl.fcntl(f_stderr, fcntl.F_SETFL, flags | os.O_NONBLOCK) logger = ObsLightPrintManager.getSubprocessLogger() outputs = { f_stdout: { "EOF": False, "logcmd": logger.info }, f_stderr: { "EOF": False, "logcmd": logger.warning } } idleTime = 0 while ((not outputs[f_stdout]["EOF"] and not outputs[f_stderr]["EOF"]) or (p.poll() == None)): try: timedOut = True selectTimeout = 60 for fd in select.select([f_stdout, f_stderr], [], [], selectTimeout)[0]: timedOut = False output = fd.read() if stdout and (f_stdout == fd): outPutRes += output for line in output.split("\n"): if not line == b"" or not output.endswith("\n"): outputs[fd]["EOF"] = False if line == b"" and not output.endswith("\n"): outputs[fd]["EOF"] = True elif line != "" and not noOutPut: res = line.decode("utf8", "replace").rstrip() outputs[fd]["logcmd"](res) if timedOut: idleTime += selectTimeout message = "Subprocess still working for %s" message = message % time.strftime("%Hh%Mm%Ss", time.gmtime(idleTime)) ObsLightPrintManager.getLogger().debug(message) else: idleTime = 0 except select.error as error: # see http://bugs.python.org/issue9867 if error.args[0] == errno.EINTR: ObsLightPrintManager.getLogger().warning( "Got select.error: %s", unicode(error)) continue else: raise # maybe p.wait() is better ? res = p.poll() msg = "command finished: '%s', return code: %s" % (command, unicode(res)) ObsLightPrintManager.getLogger().debug(msg) if stdout: return outPutRes else: if res is None: res = 0 return res