Newer
Older
Javier Vegas-Regidor
committed
import subprocess
import os
from autosubmit.config.log import Log
Javier Vegas-Regidor
committed
Javier Vegas-Regidor
committed
def __init__(self, path=''):
self.path = path
def run(self, command, input, output=None, options=None, log_level=Log.INFO):
"""
Runs one of the CDFTools
:param command: executable to run
:param input: input file
:param output: output file. Not all tools support this parameter
:param options: options for the tool.
:param log_level: log level at which the output of the cdftool command will be added
"""
Javier Vegas-Regidor
committed
line = [os.path.join(self.path, command)]
if input:
if isinstance(input, basestring):
line.append(input)
else:
for element in input:
line.append(element)
Javier Vegas-Regidor
committed
if options:
if isinstance(options, basestring):
options = options.split()
Javier Vegas-Regidor
committed
for option in options:
line.append(option)
if output:
if input == output:
raise Exception('Input and output file can not be the same on CDFTools')
Javier Vegas-Regidor
committed
line.append('-o')
line.append(output)
Log.info('Executing {0}', ' '.join(line))
Javier Vegas-Regidor
committed
process = subprocess.Popen(line, stdout=subprocess.PIPE)
for line in process.communicate():
if not line:
continue
Log.log.log(log_level, line)
Javier Vegas-Regidor
committed
if process.returncode != 0:
raise Exception('Error executing {0}\n Return code: {1}', ' '.join(line), process.returncode)
if output:
if not os.path.exists(output):
raise Exception('Error executing {0}\n Output file not created', ' '.join(line), process.returncode)