forked from Mirror/pmbootstrap
Hello, there!
This commit is contained in:
parent
bfde354b22
commit
ae950fb9f7
64 changed files with 3923 additions and 0 deletions
70
pmb/helpers/run.py
Normal file
70
pmb/helpers/run.py
Normal file
|
@ -0,0 +1,70 @@
|
|||
"""
|
||||
Copyright 2017 Oliver Smith
|
||||
|
||||
This file is part of pmbootstrap.
|
||||
|
||||
pmbootstrap 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 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
pmbootstrap 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 General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with pmbootstrap. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
import subprocess
|
||||
import logging
|
||||
|
||||
|
||||
def core(args, cmd, log_message, log, return_stdout, check=True):
|
||||
logging.debug(log_message)
|
||||
"""
|
||||
Run the command and write the output to the log.
|
||||
|
||||
:param check: raise an exception, when the command fails
|
||||
"""
|
||||
|
||||
try:
|
||||
ret = None
|
||||
if log:
|
||||
if return_stdout:
|
||||
ret = subprocess.run(cmd, stdout=subprocess.PIPE,
|
||||
check=check).stdout.decode('utf-8')
|
||||
args.logfd.write(ret)
|
||||
else:
|
||||
subprocess.run(cmd, stdout=args.logfd, stderr=args.logfd,
|
||||
check=check)
|
||||
args.logfd.flush()
|
||||
else:
|
||||
logging.debug("*** output passed to pmbootstrap stdout, not" +
|
||||
" to this log ***")
|
||||
subprocess.run(cmd, check=check)
|
||||
|
||||
except subprocess.CalledProcessError as exc:
|
||||
raise RuntimeError("Command failed: " + log_message) from exc
|
||||
return ret
|
||||
|
||||
|
||||
def user(args, cmd, log=True, working_dir=None, return_stdout=False,
|
||||
check=True):
|
||||
"""
|
||||
:param working_dir: defaults to args.work
|
||||
"""
|
||||
if not working_dir:
|
||||
working_dir = args.work
|
||||
|
||||
# TODO: maintain and check against a whitelist
|
||||
return core(args, cmd, "% " + " ".join(cmd), log, return_stdout, check)
|
||||
|
||||
|
||||
def root(args, cmd, log=True, working_dir=None, return_stdout=False,
|
||||
check=True):
|
||||
"""
|
||||
:param working_dir: defaults to args.work
|
||||
"""
|
||||
cmd = ["sudo"] + cmd
|
||||
return user(args, cmd, log, working_dir, return_stdout, check)
|
Loading…
Add table
Add a link
Reference in a new issue