From 5ce14bee2cf6d3f243caa85a8f4932ae5c9f255e Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 6 Dec 2015 18:39:41 +0000 Subject: [PATCH] Catch output and error from shell commands --- project/sync/cli.py | 18 ++++++++++++------ project/utils/repos.py | 42 ++++++++++++++++++++++++++++-------------- project/utils/shell.py | 6 ++++++ 3 files changed, 46 insertions(+), 20 deletions(-) create mode 100644 project/utils/shell.py diff --git a/project/sync/cli.py b/project/sync/cli.py index 253e1c6..7d3aee3 100644 --- a/project/sync/cli.py +++ b/project/sync/cli.py @@ -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) diff --git a/project/utils/repos.py b/project/utils/repos.py index 1713275..2b8435a 100644 --- a/project/utils/repos.py +++ b/project/utils/repos.py @@ -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) diff --git a/project/utils/shell.py b/project/utils/shell.py new file mode 100644 index 0000000..d6d68d6 --- /dev/null +++ b/project/utils/shell.py @@ -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