Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update commandutils.py #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions photon_installer/commandutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,37 @@ def __init__(self, logger):
self.logger = logger
self.hostRpmIsNotUsable = -1

def run(self, cmd, update_env = False):
self.logger.debug(cmd)
use_shell = not isinstance(cmd, list)
process = subprocess.Popen(cmd, shell=use_shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out,err = process.communicate()
retval = process.returncode
if out != b'':
self.logger.info(out.decode())
if update_env:
os.environ.clear()
os.environ.update(dict(line.partition('=')[::2] for line in out.decode('utf8').split('\0') if line))
if retval != 0:
self.logger.info("Command failed: {}".format(cmd))
self.logger.info("Error code: {}".format(retval))
self.logger.error(err.decode())
return retval
def run(self, cmd, update_env=False):
"""if FileNotFoundError raised by subprocess,
error code 127 report up on the stack"""
retval = 127
try:
use_shell = not isinstance(cmd, list)
process = subprocess.Popen(
cmd, shell=use_shell,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
out, err = process.communicate()
retval = process.returncode
if out != b'':
self.logger.info(out.decode())
if update_env:
os.environ.clear()
os.environ.update(
dict(line.partition('=')[::2]
for line in out.decode('utf8').split('\0')
if line)
)
if retval != 0:
self.logger.info("Command failed: {}".format(cmd))
self.logger.info("Error code: {}".format(retval))
self.logger.error(err.decode())
except FileNotFoundError as _:
self.logger.info(f"Command {cmd} not found")

finally:
return retval

def run_in_chroot(self, chroot_path, cmd, update_env = False):
# Use short command here. Initial version was:
Expand Down