Files
controller-wisetopdataserver/python/biopro/util/sys.py
T
2021-12-22 14:29:56 +08:00

53 lines
1.3 KiB
Python

from os import getpid as _getpid
from subprocess import Popen, check_output, CalledProcessError, PIPE, DEVNULL
from typing import List
PID = _getpid()
def current_command() -> List[str]:
ret = []
with open('/proc/%d/cmdline' % PID) as cmdline:
for content in cmdline:
for line in content.replace('\0', '\n').split('\n'):
ret.append(line)
return ret
def current_command_header() -> List[str]:
cmd = current_command()
for i, content in enumerate(cmd):
if content.endswith('biopro.main'):
break
else:
raise RuntimeError('cannot reflect current process command line : ' + ' '.join(cmd))
# force data server storage root
return cmd[:i + 1]
def system_call(*argv: str) -> str:
# print(' '.join(argv))
output: bytes = check_output(argv, stderr=DEVNULL, cwd='/home/pi/wisetopdataserver')
return output.decode()
def system_sudo(*argv: str) -> str:
# print('sudo ' + ' '.join(argv))
cmd = ['sudo', '-A']
cmd.extend(argv)
with Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=DEVNULL) as process:
o, _ = process.communicate(b'')
if process.returncode != 0:
raise CalledProcessError(process.returncode, cmd)
return o.decode()