2020-03-27 16:58:28 +00:00
|
|
|
import os, sys
|
2020-03-17 10:00:51 +00:00
|
|
|
from pathlib import Path
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
def S(cmd, trace=False):
|
|
|
|
if type(cmd) == list:
|
|
|
|
cmd = ' '.join(cmd)
|
|
|
|
if trace:
|
|
|
|
print(cmd)
|
|
|
|
return os.system(cmd)
|
|
|
|
|
|
|
|
def SC(cmd):
|
|
|
|
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
|
|
|
|
[out, err] = proc.communicate()
|
|
|
|
return (out, err, proc)
|
|
|
|
|
|
|
|
def A(args, q=0):
|
|
|
|
if type(args) == list:
|
|
|
|
args = ' '.join([str(a) for a in args])
|
|
|
|
if q == 1:
|
|
|
|
return f"'{args}'"
|
|
|
|
elif q == 2:
|
|
|
|
return f'"{args}"'
|
|
|
|
return str(args)
|
|
|
|
|
|
|
|
def p(path_str):
|
|
|
|
return Path(path_str).expanduser()
|
2020-03-21 14:18:34 +00:00
|
|
|
|
2020-03-27 16:58:28 +00:00
|
|
|
def eprint(*args, **kwargs):
|
|
|
|
print(*args, file=sys.stderr, **kwargs)
|
|
|
|
|
|
|
|
def eeprint(*args, **kwargs):
|
|
|
|
return_code=1
|
|
|
|
if 'return_code' in kwargs:
|
|
|
|
return_code = kwargs['return_code']
|
|
|
|
del kwargs['return_code']
|
|
|
|
print(*args, file=sys.stderr, **kwargs)
|
|
|
|
exit(return_code)
|
|
|
|
|
2020-03-21 14:18:34 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
print('Example')
|
|
|
|
[o, e, p] = SC('ls')
|
|
|
|
|
|
|
|
if p.returncode == 0:
|
|
|
|
print(f'OUTPUT: {o}')
|
|
|
|
else:
|
|
|
|
print(f'ERROR: {e}')
|