bootstrap-vz/bootstrapvz/common/tools.py

94 lines
2.7 KiB
Python
Raw Normal View History

def log_check_call(command, stdin=None, env=None, shell=False):
status, stdout, stderr = log_call(command, stdin, env, shell)
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
def log_call(command, stdin=None, env=None, shell=False):
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
popen_args = {'args': command,
'env': env,
'shell': shell,
'stdin': subprocess.PIPE,
'stdout': subprocess.PIPE,
'stderr': subprocess.PIPE, }
2013-08-13 20:40:16 +02:00
if stdin is not None:
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:
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():
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():
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
def sed_i(file_path, pattern, subst):
import fileinput
import re
for line in fileinput.input(files=file_path, inplace=True):
print re.sub(pattern, subst, line),
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))
def load_yaml(path):
import yaml
with open(path, 'r') as fobj:
return yaml.load(fobj)
def config_get(path, config_path):
config = load_json(path)
for key in config_path:
config = config.get(key)
return config
def copy_tree(from_path,to_path):
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))