cdftools.py 1.78 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)
            else:
                for element in input:
                    line.append(element)
            if isinstance(options, basestring):
                options = options.split()
            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')
        Log.info('Executing {0}', ' '.join(line))
        process = subprocess.Popen(line, stdout=subprocess.PIPE)
        for line in process.communicate():
            if not line:
                continue
            Log.log.log(log_level, line)
        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)