cdftools.py 1.83 KB
Newer Older
from autosubmit.config.log import Log
class CDFTools(object):
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
    # 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
        :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
        """
        line = [os.path.join(self.path, command)]
        if input:
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
            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)
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
            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 isinstance(options, basestring):
                options = options.split()
                line.append(str(option))
            if input == output:
                raise Exception('Input and output file can not be the same on CDFTools')
        Log.info('Executing {0}', ' '.join(line))
        line = 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))