def move_posix(self, movetype): """Create symlinks/copies using POSIX shell command specified in metadata. Parameters ---------- movetype : str Type of file movement. Takes either `copy` or `symlink`. Returns ------- None """ for source, destination in self.move_list: if movetype == 'copy': command = metadata.commands[self.osname]['makecopy'] % ( source, destination) elif movetype == 'symlink': command = metadata.commands[self.osname]['makelink'] % ( source, destination) process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) stdout, stderr = process.communicate() if process.returncode != 0: error_message = messages.crit_error_move_command % command error_message = error_message + format_traceback(stderr) raise CritError(error_message)
def move_program_output(self, program_output, log_file=''): """Move program outputs. Notes ----- Certain applications create program outputs that need to be moved to appropriate logging files. Parameters ---------- program_output : str Path of program output. log_file : str, optional Path of log file. Log file is only written if specified. Defaults to ``''`` (i.e., not written). """ program_output = norm_path(program_output) try: with io.open(program_output, 'r', encoding='utf-8', errors='ignore') as f: out = f.read() except: error_message = messages.crit_error_no_program_output % ( program_output, self.program) error_message = error_message + format_traceback() raise_from(CritError(error_message), None) if self.makelog: if not (metadata.makelog_started and os.path.isfile(self.makelog)): raise CritError(messages.crit_error_no_makelog % self.makelog) with io.open(self.makelog, 'a', encoding='utf-8', errors='ignore') as f: print(out, file=f) if log_file: if program_output != log_file: shutil.copy2(program_output, log_file) os.remove(program_output) else: os.remove(program_output) return (out)
def execute_command(self, command): """Execute shell command. Parameters ---------- command : str Shell command to execute. Returns ------- exit : tuple Tuple (exit code, error message) for shell command. """ self.output = 'Executing command: `%s`' % command print(colored(self.output, metadata.color_in_process)) try: if not self.shell: command = command.split() process = subprocess_fix.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=self.shell, universal_newlines=True) process.wait() stdout, stderr = process.communicate() exit = (process.returncode, stderr) if stdout: self.output += '\n' + decode(stdout) if stderr: self.output += '\n' + decode(stderr) pass return (exit) except: error_message = messages.crit_error_bad_command % command error_message = error_message + format_traceback() raise_from(CritError(error_message), None)
def get_paths(self): """Parse sources and destinations from line. Returns ------- None """ try: self.line = self.line.split('|') self.line = [l.strip() for l in self.line] self.line = [l.strip('"\'') for l in self.line] self.destination, self.source = self.line except Exception: error_message = messages.crit_error_bad_move % (self.raw_line, self.file) error_message = error_message + format_traceback() raise_from(CritError(error_message), None) self.source = norm_path(self.source) self.destination = norm_path( os.path.join(self.move_dir, self.destination))
def move_nt(self, movetype): """Create symlinks/copies using NT shell command specified in metadata. Parameters ---------- movetype : str Type of file movement. Takes either ``'copy'`` or ``'symlink'``. Returns ------- None """ for source, destination in self.move_list: if os.path.isdir(source): link_option = '/d' copy_option = '' elif os.path.isfile(source): link_option = '' copy_option = 'cmd /c echo F | ' if movetype == 'copy': command = metadata.commands[self.osname]['makecopy'] % ( copy_option, source, destination) elif movetype == 'symlink': command = metadata.commands[self.osname]['makelink'] % ( link_option, destination, source) process = subprocess_fix.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) process.wait() stdout, stderr = process.communicate() if process.returncode != 0: error_message = messages.crit_error_move_command % command error_message = error_message + format_traceback(stderr) raise CritError(error_message)
def get_move_directive_list(self): """Parse list of files to create symlink directives. Returns ------- None """ lines = [] for file in self.file_list: for raw_line in file_to_array(file): try: line = str(raw_line).format(**self.mapping_dict) lines.append((file, raw_line, line)) except KeyError as e: key = str(e).lstrip("u'").rstrip("'") error_message = messages.crit_error_path_mapping % ( key, key, file, raw_line, key) error_message = error_message + format_traceback() raise_from(CritError(error_message), None) self.move_directive_list = [ MoveDirective(file, raw_line, line, self.move_dir) for (file, raw_line, line) in lines ]