2013-06-27 23:26:29 +02:00
|
|
|
|
|
|
|
|
2013-12-29 16:09:47 +01:00
|
|
|
def log_check_call(command, stdin=None, env=None):
|
|
|
|
status, stdout, stderr = log_call(command, stdin, env)
|
2013-07-01 22:06:42 +02:00
|
|
|
if status != 0:
|
|
|
|
from subprocess import CalledProcessError
|
2013-07-07 21:29:15 +02:00
|
|
|
raise CalledProcessError(status, ' '.join(command), '\n'.join(stderr))
|
2013-07-13 15:01:21 +02:00
|
|
|
return stdout
|
2013-07-01 22:06:42 +02:00
|
|
|
|
|
|
|
|
2013-12-29 16:09:47 +01:00
|
|
|
def log_call(command, stdin=None, env=None):
|
2013-06-27 23:26:29 +02:00
|
|
|
import subprocess
|
|
|
|
import select
|
2013-07-01 22:06:42 +02:00
|
|
|
|
2013-07-01 22:21:47 +02:00
|
|
|
import logging
|
2013-07-07 21:29:15 +02:00
|
|
|
from os.path import realpath
|
|
|
|
command_log = realpath(command[0]).replace('/', '.')
|
2013-07-01 22:21:47 +02:00
|
|
|
log = logging.getLogger(__name__ + command_log)
|
2013-09-15 17:04:58 +02:00
|
|
|
log.debug('Executing: {command}'.format(command=' '.join(command)))
|
2014-01-15 21:15:06 +01:00
|
|
|
if stdin is not None:
|
|
|
|
log.debug(' stdin: {stdin}'.format(stdin=stdin))
|
2013-07-01 22:21:47 +02:00
|
|
|
|
2013-12-29 16:09:47 +01:00
|
|
|
popen_args = {'args': command,
|
|
|
|
'env': env,
|
|
|
|
'stdin': subprocess.PIPE,
|
|
|
|
'stdout': subprocess.PIPE,
|
|
|
|
'stderr': subprocess.PIPE, }
|
2013-08-13 20:40:16 +02:00
|
|
|
if stdin is not None:
|
2013-12-29 16:09:47 +01:00
|
|
|
popen_args['stdin'] = subprocess.PIPE
|
|
|
|
process = subprocess.Popen(**popen_args)
|
2013-08-17 17:28:46 +02:00
|
|
|
process.stdin.write(stdin + "\n")
|
2013-07-10 16:33:35 +02:00
|
|
|
process.stdin.flush()
|
|
|
|
process.stdin.close()
|
|
|
|
else:
|
2013-12-29 16:09:47 +01:00
|
|
|
process = subprocess.Popen(**popen_args)
|
2013-07-07 21:29:15 +02:00
|
|
|
stdout = []
|
|
|
|
stderr = []
|
2013-06-27 23:26:29 +02:00
|
|
|
while True:
|
|
|
|
reads = [process.stdout.fileno(), process.stderr.fileno()]
|
|
|
|
ret = select.select(reads, [], [])
|
|
|
|
for fd in ret[0]:
|
|
|
|
if fd == process.stdout.fileno():
|
2013-09-13 01:32:08 +02:00
|
|
|
for line in iter(process.stdout.readline, ''):
|
2013-07-01 22:21:47 +02:00
|
|
|
log.debug(line.strip())
|
2013-07-07 21:29:15 +02:00
|
|
|
stdout.append(line.strip())
|
2013-06-27 23:26:29 +02:00
|
|
|
if fd == process.stderr.fileno():
|
2013-09-13 01:32:08 +02:00
|
|
|
for line in iter(process.stderr.readline, ''):
|
2013-07-01 22:21:47 +02:00
|
|
|
log.error(line.strip())
|
2013-07-07 21:29:15 +02:00
|
|
|
stderr.append(line.strip())
|
2013-06-27 23:26:29 +02:00
|
|
|
if process.poll() is not None:
|
2013-07-07 21:29:15 +02:00
|
|
|
return process.returncode, stdout, stderr
|
2013-07-01 21:42:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def sed_i(file_path, pattern, subst):
|
2013-11-21 16:45:29 +01:00
|
|
|
import fileinput
|
2013-09-23 16:14:38 +00:00
|
|
|
import re
|
2013-11-21 16:45:29 +01:00
|
|
|
for line in fileinput.input(files=file_path, inplace=True):
|
|
|
|
print re.sub(pattern, subst, line),
|