From bc537259f20ec4a90cb7503c4925ad37dbdceb05 Mon Sep 17 00:00:00 2001 From: Gregory Ashton <gregory.ashton@ligo.org> Date: Mon, 15 Oct 2018 19:34:10 +1100 Subject: [PATCH] Move `run_command_line` to the core utils --- bilby/core/utils.py | 33 +++++++++++++++++++++++++++++++++ bilby/gw/utils.py | 35 +---------------------------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/bilby/core/utils.py b/bilby/core/utils.py index bf88828d9..38086043b 100644 --- a/bilby/core/utils.py +++ b/bilby/core/utils.py @@ -6,6 +6,7 @@ from math import fmod import argparse import traceback import inspect +import subprocess logger = logging.getLogger('bilby') @@ -617,6 +618,38 @@ def derivatives(vals, func, releps=1e-3, abseps=None, mineps=1e-9, reltol=1e-3, return grads +def run_commandline(cl, log_level=20, raise_error=True, return_output=True): + """Run a string cmd as a subprocess, check for errors and return output. + + Parameters + ---------- + cl: str + Command to run + log_level: int + See https://docs.python.org/2/library/logging.html#logging-levels, + default is '20' (INFO) + + """ + + logger.log(log_level, 'Now executing: ' + cl) + if return_output: + try: + out = subprocess.check_output( + cl, stderr=subprocess.STDOUT, shell=True, + universal_newlines=True) + except subprocess.CalledProcessError as e: + logger.log(log_level, 'Execution failed: {}'.format(e.output)) + if raise_error: + raise + else: + out = 0 + os.system('\n') + return(out) + else: + process = subprocess.Popen(cl, shell=True) + process.communicate() + + # Instantiate the default argument parser at runtime command_line_args, command_line_parser = set_up_command_line_arguments() # Instantiate the default logging diff --git a/bilby/gw/utils.py b/bilby/gw/utils.py index 233fcff08..7b7f85d44 100644 --- a/bilby/gw/utils.py +++ b/bilby/gw/utils.py @@ -1,12 +1,11 @@ from __future__ import division import os import json -import subprocess import numpy as np from ..core.utils import (gps_time_to_gmst, ra_dec_to_theta_phi, - speed_of_light, logger, + speed_of_light, logger, run_commandline, check_directory_exists_and_if_not_mkdir) try: @@ -513,38 +512,6 @@ def gw_data_find(observatory, gps_start_time, duration, calibration, return output_cache_file -def run_commandline(cl, log_level=20, raise_error=True, return_output=True): - """Run a string cmd as a subprocess, check for errors and return output. - - Parameters - ---------- - cl: str - Command to run - log_level: int - See https://docs.python.org/2/library/logging.html#logging-levels, - default is '20' (INFO) - - """ - - logger.log(log_level, 'Now executing: ' + cl) - if return_output: - try: - out = subprocess.check_output( - cl, stderr=subprocess.STDOUT, shell=True, - universal_newlines=True) - except subprocess.CalledProcessError as e: - logger.log(log_level, 'Execution failed: {}'.format(e.output)) - if raise_error: - raise - else: - out = 0 - os.system('\n') - return(out) - else: - process = subprocess.Popen(cl, shell=True) - process.communicate() - - def save_to_fits(posterior, outdir, label): """ Generate a fits file from a posterior array """ from astropy.io import fits -- GitLab