2820c61a |
import subprocess
import os
class CommandUtils(object):
def __init__(self): |
518d6a6f |
self.findBinary = "find" |
2820c61a |
|
87815216 |
def findFile(self, filename, sourcePath):
process = subprocess.Popen([self.findBinary, "-L", sourcePath, "-name", filename,
"-not", "-type", "d"], stdout=subprocess.PIPE)
# We don't check the return val here because find could return 1 but still be
# able to find |
b8ab7fb4 |
# the result. We shouldn't blindly return None without even checking the result.
# The reason we want to suppress this is because for built RPMs, we first copy it to
# the location with a random name and move it to the real name. find will complain our
# action and return 1.
# find's flag ignore_readdir_race can suppress this but it isn't working.
# https://bugs.centos.org/view.php?id=13685
#if returnVal != 0:
# return None |
87815216 |
result = process.communicate()[0] |
2820c61a |
if result is None:
return None |
87815216 |
return result.decode().split() |
2820c61a |
|
87815216 |
@staticmethod
def runCommandInShell(cmd, logfilePath=None, chrootCmd=None): |
2820c61a |
if chrootCmd is not None: |
87815216 |
cmd = chrootCmd + " " + cmd |
2820c61a |
if logfilePath is None: |
87815216 |
logfilePath = os.devnull
logfile = open(logfilePath, "w")
process = subprocess.Popen("%s" %cmd, shell=True, stdout=logfile, stderr=logfile) |
2820c61a |
retval = process.wait()
logfile.close() |
87815216 |
if retval == 0: |
2820c61a |
return True
return False |
87815216 |
@staticmethod
def runCommandInShell2(cmd, chrootCmd=None): |
2820c61a |
if chrootCmd is not None: |
87815216 |
cmd = chrootCmd + " " + cmd
process = subprocess.Popen("%s" %cmd, shell=True, stdout=subprocess.PIPE) |
2820c61a |
retval = process.wait() |
518d6a6f |
if retval != 0:
return None |
610ab83e |
return process.communicate()[0] |