def get_command(self, values): arg_str = '' for extra_option in self.extra_options: delim_name = extra_option.lower().replace(' ', '_') arg_str += delim_name + ':%(' + delim_name + ')s ' # Add "'s as otherwise args get parsed as separate for k, v in values.items(): if isinstance(v, str) and len(v.split()) > 1: values[k] = system.fix_quotes(v) # XXX: hack for image size (code above should catch this!!!) image_size = values['image_size'] if len(image_size.split()) > 1: values['image_size'] = system.fix_quotes(image_size) return COMMAND % arg_str % values
def call(self, command, check_exe=True, size=None, unlock=False, output_filename=None, mode=None): def replace(old, new): for i in range(len(command)): command[i] = command[i].replace(old, new) # ensure command is a list if type(command) in types.StringTypes: command = shlex.split(command) # unpack values info = self.info layer = self.get_layer() image = layer.image # adapt image if mode != image.mode: image = imtools.convert(image, mode) if size != None and size[0] < image.size[0]: image = image.copy() image.thumbnail(size, Image.ANTIALIAS) # different image types -> loop over input -> save to temp files temp_files = [] done = [] error = True try: for arg in command: for match in RE_FILE_IN.finditer(arg): source = match.group() if source in done: continue ext = match.group(1) target = system.TempFile(ext) temp_files.append((source, target)) imtools.save_safely(image, target.path) done.append(source) error = False finally: # check if we have a file_in # clean up in case of error if error: for source, target in temp_files: target.close() try: # loop over output file(s), only one is allowed output = None index = 0 for pos, arg in enumerate(command): for match in RE_FILE_OUT.finditer(arg): if index > 0: # only 1 is allowed raise Exception('Only one file_out.* is allowed.') source = match.group() ext = match.group(1) output = system.TempFile(ext, output_filename) # fix quotes because of blender action command[pos] = arg.replace( source, system.fix_quotes(output.path), ) index += 1 # tweak command line for source, target in temp_files: for i in range(len(command)): replace(source, system.fix_quotes(target.path)) # execute stdout, stderr, exit_code = system.call_out_err(command) # check if there was output if not exit_code: if output and not os.path.exists(output.path): no_output = True else: no_output = False finally: # clean up temp files for source, target in temp_files: target.close() # = os.remove(target) # raise errors if necessary if exit_code: raise Exception('\n'.join([ _('Command returned with error "%s":') % exit_code, subprocess.list2cmdline(command), '', 'stderr:', stderr, 'stdout:', stdout, ])) if no_output: raise Exception('\n'.join([ _('Command did not produce an output image:'), subprocess.list2cmdline(command), '', 'stderr:', stderr, 'stdout:', stdout, ])) # open the output image if output: layer.open(output.path) # DO NOT REMOVE image.load() or output.close will fail on windows layer.image.load() output.close()
# clean up in case of error if error: for source, target in temp_files: target.close() # os.remove(target) raise error # loop over output output = None for index, match in \ enumerate(RE_FILE_OUT.finditer(command)): if index > 0: # only 1 is allowed raise Exception('Only one file_out.* is allowed.') source = match.group() ext = match.group(1) output = system.TempFile(ext, output_filename) command = command.replace(source, system.fix_quotes(output.path)) # tweak command line for source, target in temp_files: command = command.replace(source, system.fix_quotes(target.path)) # execute system.call(command, shell=shell) # give back filename if output and not os.path.exists(output.path): error = True else: error = False for source, target in temp_files: target.close() # os.remove(target) if error: raise Exception(
def get_command_line(self, action, photo, input, output): return '%s %s %s > %s' % (self.command, self.get_command_line_args(action, photo), system.fix_quotes(input), system.fix_quotes(output))
def get_command_line(self, action, photo, input, output): return '%s %s %s > %s' % ( self.command, self.get_command_line_args(action, photo), system.fix_quotes(input), system.fix_quotes(output))