1
Fork 0

Catch output and error from shell commands

This commit is contained in:
Jake Howard 2015-12-06 18:39:41 +00:00
parent f820d575dd
commit 5ce14bee2c
3 changed files with 46 additions and 20 deletions

View file

@ -3,15 +3,21 @@ from project.utils import config, repos
@click.command('sync') @click.command('sync')
@click.option('--private/--no-private', default=False) @click.option('--private', default=False)
def cli(private): @click.option('--clean', default=False)
def cli(private, clean):
if clean:
print("Cleaning")
repos.clean()
if not config.has_basics(): if not config.has_basics():
print("You do not have all the basic requirements set.") print("You do not have all the basic requirements set.")
return 1 return 1
exit_code = repos.clone_public_data() error = repos.clone_public_data()
if private and exit_code == 0: if error:
return 1
if private:
if not config.get('private_repo'): if not config.get('private_repo'):
print("private repo not set") print("private repo not set")
return 1 return 1
exit_code = repos.clone_private_data() error = repos.clone_private_data()
return exit_code return int(error)

View file

@ -1,20 +1,24 @@
import os import os, shutil
from distutils.dir_util import copy_tree from distutils.dir_util import copy_tree
from . import config, constants from . import config, constants, shell
def clone_public_data(): def clone_public_data():
if os.path.isdir(constants.PUBLIC_DATA_DIR): if os.path.isdir(constants.PUBLIC_DATA_DIR):
initial = os.getcwd() initial = os.getcwd()
go_to_data() go_to_data()
exit_code = os.system("git pull") out, error = shell.call("git pull")
os.chdir(initial) os.chdir(initial)
return exit_code if error:
print("Error:", out)
return error
exit_code = os.system("git clone -b master --single-branch {} {}" out, error = shell.call(
.format(config.get('public_repo'), "git clone -b master --single-branch {} {}".format(
constants.PUBLIC_DATA_DIR)) config.get('public_repo'),
return exit_code constants.PUBLIC_DATA_DIR)
)
return error
def clone_private_data(): def clone_private_data():
@ -22,19 +26,25 @@ def clone_private_data():
if os.path.isdir(constants.PRIVATE_DATA_DIR): if os.path.isdir(constants.PRIVATE_DATA_DIR):
initial = os.getcwd() initial = os.getcwd()
os.chdir(constants.PRIVATE_DATA_DIR) os.chdir(constants.PRIVATE_DATA_DIR)
exit_code = os.system("git pull") out, error = shell.call("git pull")
os.chdir(initial) os.chdir(initial)
if error:
print("Error:", out)
return error
else: else:
exit_code = os.system("git clone -b master --single-branch {} {}" out, error = shell.call(
.format(config.get('private_repo'), constants.PRIVATE_DATA_DIR)) "git clone -b master --single-branch {} {}".format(
if exit_code != 0: config.get('private_repo'),
return exit_code constants.PRIVATE_DATA_DIR)
)
if error:
return error
copy_tree( copy_tree(
constants.PRIVATE_DATA_DIR, constants.PRIVATE_DATA_DIR,
constants.PUBLIC_DATA_DIR constants.PUBLIC_DATA_DIR
) )
return exit_code return error
def has_data(data): def has_data(data):
@ -47,3 +57,7 @@ def go_to_data(subdir=''):
path = os.path.join(constants.PUBLIC_DATA_DIR, subdir) path = os.path.join(constants.PUBLIC_DATA_DIR, subdir)
os.chdir(path) os.chdir(path)
return path return path
def clean():
shutil.rmtree(constants.PUBLIC_DATA_DIR)
shutil.rmtree(constants.PRIVATE_DATA_DIR)

6
project/utils/shell.py Normal file
View file

@ -0,0 +1,6 @@
import subprocess
def call(command):
shell = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
output, error = shell.communicate()
return str(output), error