from earthdiagnostics.utils import Utils import os from autosubmit.config.log import Log class CDFTools(object): """ Class to run CDFTools executables """ def __init__(self, path=''): self.path = path # noinspection PyShadowingBuiltins def run(self, command, input, output=None, options=None, log_level=Log.INFO): """ Runs one of the CDFTools :param command: executable to run :type command: str :param input: input file :type input: str :param output: output file. Not all tools support this parameter :type options: str :param options: options for the tool. :type options: str | list :param log_level: log level at which the output of the cdftool command will be added :type log_level: int """ line = [os.path.join(self.path, command)] if not os.path.exists(line[0]): raise Exception('Error executing {0}\n Command does not exist in {1}', command, self.path) if input: if isinstance(input, basestring): line.append(input) if not os.path.exists(input): raise Exception('Error executing {0}\n Input file {1} file does not exist', command, input) else: for element in input: line.append(element) if not os.path.exists(element): raise Exception('Error executing {0}\n Input file {1} file does not exist', command, element) if options: if isinstance(options, basestring): options = options.split() for option in options: line.append(str(option)) if output: if input == output: raise Exception('Input and output file can not be the same on CDFTools') line.append('-o') line.append(output) Log.debug('Executing {0}', ' '.join(line)) shell_output = Utils.execute_shell_command(line, log_level) if output: if not os.path.exists(output): raise Exception('Error executing {0}\n Output file not created', ' '.join(line)) return shell_output