Exemplo n.º 1
0
 def prepare_argument_path(
         self, argument_noun, raw_arguments, draft_folder, default_path):
     data_type = get_data_type(argument_noun)
     # If client sent direct content (x_table_csv), save it
     for file_format in data_type.formats:
         raw_argument_name = '%s_%s' % (argument_noun, file_format)
         if raw_argument_name not in raw_arguments:
             continue
         source_text = raw_arguments[raw_argument_name]
         target_name = '%s.%s' % (argument_noun, file_format)
         return copy_text(join(draft_folder, target_name), source_text)
     # Raise KeyError if client did not specify noun (x_table)
     v = raw_arguments[argument_noun]
     # If client sent multipart content, save it
     if hasattr(v, 'file'):
         target_name = argument_noun + get_file_extension(v.filename)
         return copy_file(join(draft_folder, target_name), v.file)
     # If client sent empty content, use default
     if v == '':
         if not default_path:
             raise KeyError
         target_name = argument_noun + get_file_extension(default_path)
         return link_path(join(draft_folder, target_name), default_path)
     # If client sent a relative path (x_table=11/x/y.csv), find it
     if '/' in v:
         source_path = self.get_file_path(*parse_result_relative_path(v))
         target_name = argument_noun + get_file_extension(source_path)
         return link_path(join(draft_folder, target_name), source_path)
     # If client sent an upload id (x_table=x), find it
     upload = get_upload(self, upload_id=v)
     source_path = join(upload.folder, data_type.get_file_name())
     target_name = argument_noun + get_file_extension(source_path)
     target_path = move_path(join(draft_folder, target_name), source_path)
     remove_safely(upload.folder)
     return target_path
Exemplo n.º 2
0
 def prepare_argument_path(self, argument_noun, raw_arguments, draft_folder,
                           default_path):
     data_type = get_data_type(argument_noun)
     # If client sent direct content (x_table_csv), save it
     for file_format in data_type.formats:
         raw_argument_name = '%s_%s' % (argument_noun, file_format)
         if raw_argument_name not in raw_arguments:
             continue
         source_text = raw_arguments[raw_argument_name]
         target_name = '%s.%s' % (argument_noun, file_format)
         return copy_text(join(draft_folder, target_name), source_text)
     # Raise KeyError if client did not specify noun (x_table)
     v = raw_arguments[argument_noun]
     # If client sent multipart content, save it
     if hasattr(v, 'file'):
         target_name = argument_noun + get_file_extension(v.filename)
         return copy_file(join(draft_folder, target_name), v.file)
     # If client sent empty content, use default
     if v == '':
         if not default_path:
             raise KeyError
         target_name = argument_noun + get_file_extension(default_path)
         return link_path(join(draft_folder, target_name), default_path)
     # If client sent a relative path (x_table=11/x/y.csv), find it
     if '/' in v:
         source_path = self.get_file_path(*parse_result_relative_path(v))
         target_name = argument_noun + get_file_extension(source_path)
         return link_path(join(draft_folder, target_name), source_path)
     # If client sent an upload id (x_table=x), find it
     upload = get_upload(self, upload_id=v)
     source_path = realpath(join(upload.folder, data_type.get_file_name()))
     target_name = argument_noun + get_file_extension(source_path)
     target_path = move_path(join(draft_folder, target_name), source_path)
     remove_safely(upload.folder)
     return target_path
Exemplo n.º 3
0
 def save_tool_location(self, tool_definition):
     with suppress(ValueError):
         link_path(join(self.result_folder, 'f'), tool_definition[
             'configuration_folder'])
     configuration_path = tool_definition['configuration_path']
     d = {
         'tool_location': OrderedDict([
             ('tool_name', tool_definition['tool_name']),
             ('configuration_path', configuration_path),
         ]),
     }
     print(format_settings(d))
     print('')
     d['tool_location']['configuration_path'] = join('f', basename(
         configuration_path))
     return save_settings(join(self.result_folder, 'f.cfg'), d)
Exemplo n.º 4
0
def run_script(tool_definition,
               result_arguments,
               result_folder,
               target_folder=None,
               environment=None):
    timestamp, environment = time.time(), environment or {}
    if 'target_folder' in tool_definition['argument_names']:
        y = make_folder(abspath(target_folder or join(result_folder, 'y')))
        result_arguments = OrderedDict(result_arguments, target_folder=y)
    # Record
    result_configuration = ResultConfiguration(result_folder)
    result_configuration.save_tool_location(tool_definition)
    result_configuration.save_result_arguments(result_arguments, environment)
    # Run
    command_terms = split_arguments(
        render_command(tool_definition['command_template'],
                       result_arguments).replace('\n', ' '))
    result_properties = OrderedDict()
    try:
        with cd(tool_definition['configuration_folder']):
            command_process = subprocess.Popen(command_terms,
                                               stdout=subprocess.PIPE,
                                               stderr=subprocess.PIPE,
                                               env=merge_dictionaries(
                                                   environment,
                                                   SCRIPT_ENVIRONMENT))
    except OSError:
        standard_output, standard_error = None, 'Command not found'
    else:
        standard_output, standard_error = [
            x.rstrip().decode('utf-8') for x in command_process.communicate()
        ]
        if command_process.returncode:
            result_properties['return_code'] = command_process.returncode
    # Save
    result_properties.update(
        _process_streams(standard_output, standard_error, result_folder,
                         tool_definition))
    result_properties['execution_time_in_seconds'] = time.time() - timestamp
    result_configuration.save_result_properties(result_properties)
    result_configuration.save_result_script(tool_definition, result_arguments)
    if 'target_folder' in tool_definition['argument_names']:
        link_path(join(result_folder, 'y'), result_arguments['target_folder'])
    return result_properties
Exemplo n.º 5
0
def run_script(
        tool_definition, result_arguments, result_folder, target_folder=None,
        environment=None):
    timestamp, environment = time.time(), environment or {}
    if 'target_folder' in tool_definition['argument_names']:
        y = make_folder(abspath(target_folder or join(result_folder, 'y')))
        result_arguments = OrderedDict(result_arguments, target_folder=y)
    # Record
    result_configuration = ResultConfiguration(result_folder)
    result_configuration.save_tool_location(tool_definition)
    result_configuration.save_result_arguments(result_arguments, environment)
    # Run
    command_terms = split_arguments(render_command(tool_definition[
        'command_template'], result_arguments).replace('\n', ' '))
    result_properties = OrderedDict()
    try:
        with cd(tool_definition['configuration_folder']):
            command_process = subprocess.Popen(
                command_terms, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                env=merge_dictionaries(environment, SCRIPT_ENVIRONMENT))
    except OSError:
        standard_output, standard_error = None, 'Command not found'
    else:
        standard_output, standard_error = [x.rstrip().decode(
            'utf-8') for x in command_process.communicate()]
        if command_process.returncode:
            result_properties['return_code'] = command_process.returncode
    # Save
    result_properties.update(_process_streams(
        standard_output, standard_error, result_folder, tool_definition))
    result_properties['execution_time_in_seconds'] = time.time() - timestamp
    result_configuration.save_result_properties(result_properties)
    result_configuration.save_result_script(tool_definition, result_arguments)
    if 'target_folder' in tool_definition['argument_names']:
        link_path(join(result_folder, 'y'), result_arguments['target_folder'])
    return result_properties