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.option('--private/--no-private', default=False)
def cli(private):
@click.option('--private', default=False)
@click.option('--clean', default=False)
def cli(private, clean):
if clean:
print("Cleaning")
repos.clean()
if not config.has_basics():
print("You do not have all the basic requirements set.")
return 1
exit_code = repos.clone_public_data()
if private and exit_code == 0:
error = repos.clone_public_data()
if error:
return 1
if private:
if not config.get('private_repo'):
print("private repo not set")
return 1
exit_code = repos.clone_private_data()
return exit_code
error = repos.clone_private_data()
return int(error)

View file

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