2014-06-19 17:14:43 -07:00
|
|
|
def log_check_call(command, stdin=None, env=None, shell=False, cwd=None):
|
|
|
|
status, stdout, stderr = log_call(command, stdin, env, shell, cwd)
|
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
|
|
|
|
|
|
|
|
2014-06-19 17:14:43 -07:00
|
|
|
def log_call(command, stdin=None, env=None, shell=False, cwd=None):
|
2013-06-27 23:26:29 +02:00
|
|
|
import subprocess
|
2013-07-01 22:21:47 +02:00
|
|
|
import logging
|
2014-04-17 16:35:25 -03:00
|
|
|
from multiprocessing.dummy import Pool as ThreadPool
|
2013-07-07 21:29:15 +02:00
|
|
|
from os.path import realpath
|
2014-05-03 19:41:55 +02:00
|
|
|
|
2013-07-07 21:29:15 +02:00
|
|
|
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-05-03 19:41:55 +02:00
|
|
|
|
2014-06-19 17:14:43 -07:00
|
|
|
process = subprocess.Popen(args=command, env=env, shell=shell, cwd=cwd,
|
2014-05-03 19:41:55 +02:00
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
|
|
|
|
2013-08-13 20:40:16 +02:00
|
|
|
if stdin is not None:
|
2014-05-03 22:24:13 +02:00
|
|
|
log.debug(' stdin: ' + stdin)
|
2014-05-03 23:15:10 +02:00
|
|
|
process.stdin.write(stdin + "\n")
|
|
|
|
process.stdin.flush()
|
|
|
|
process.stdin.close()
|
2014-05-03 19:41:55 +02:00
|
|
|
|
2013-07-07 21:29:15 +02:00
|
|
|
stdout = []
|
|
|
|
stderr = []
|
2014-05-04 14:32:47 +02:00
|
|
|
|
|
|
|
def handle_stdout(line):
|
|
|
|
log.debug(line)
|
|
|
|
stdout.append(line)
|
|
|
|
|
|
|
|
def handle_stderr(line):
|
|
|
|
log.error(line)
|
|
|
|
stderr.append(line)
|
|
|
|
|
|
|
|
handlers = {process.stdout: handle_stdout,
|
|
|
|
process.stderr: handle_stderr}
|
|
|
|
|
|
|
|
def stream_readline(stream):
|
|
|
|
for line in iter(stream.readline, ''):
|
|
|
|
handlers[stream](line.strip())
|
|
|
|
|
2014-04-17 16:35:25 -03:00
|
|
|
pool = ThreadPool(2)
|
2014-05-04 14:32:47 +02:00
|
|
|
pool.map(stream_readline, [process.stdout, process.stderr])
|
2014-04-17 16:35:25 -03:00
|
|
|
pool.close()
|
2014-05-04 14:32:47 +02:00
|
|
|
pool.join()
|
|
|
|
process.wait()
|
|
|
|
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),
|
2014-02-23 20:14:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
def load_json(path):
|
|
|
|
import json
|
|
|
|
from minify_json import json_minify
|
|
|
|
with open(path) as stream:
|
|
|
|
return json.loads(json_minify(stream.read(), False))
|
|
|
|
|
2014-04-08 11:56:27 -05:00
|
|
|
|
2014-04-08 05:33:34 +00:00
|
|
|
def load_yaml(path):
|
|
|
|
import yaml
|
|
|
|
with open(path, 'r') as fobj:
|
2014-04-15 19:31:04 -05:00
|
|
|
return yaml.safe_load(fobj)
|
2014-02-23 20:14:23 +01:00
|
|
|
|
2014-04-08 11:56:27 -05:00
|
|
|
|
2014-02-23 20:14:23 +01:00
|
|
|
def config_get(path, config_path):
|
|
|
|
config = load_json(path)
|
|
|
|
for key in config_path:
|
|
|
|
config = config.get(key)
|
|
|
|
return config
|
2014-04-01 01:19:40 +00:00
|
|
|
|
2014-04-08 11:56:27 -05:00
|
|
|
|
|
|
|
def copy_tree(from_path, to_path):
|
2014-04-01 01:19:40 +00:00
|
|
|
from shutil import copy
|
|
|
|
import os
|
|
|
|
for abs_prefix, dirs, files in os.walk(from_path):
|
|
|
|
prefix = os.path.normpath(os.path.relpath(abs_prefix, from_path))
|
|
|
|
for path in dirs:
|
|
|
|
full_path = os.path.join(to_path, prefix, path)
|
|
|
|
if os.path.exists(full_path):
|
|
|
|
if os.path.isdir(full_path):
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
os.remove(full_path)
|
|
|
|
os.mkdir(full_path)
|
|
|
|
for path in files:
|
|
|
|
copy(os.path.join(abs_prefix, path),
|
|
|
|
os.path.join(to_path, prefix, path))
|