def setup_working_folder(path_org, max_update_rate_hrs): """Copies original folder to temp folder to preserve original files""" dir_path_org = Path(path_org) if not dir_path_org.exists() or not dir_path_org.is_dir(): log.w('folder not found') raise NotADirectoryError else: dir_copy = dir_path_org.stem + "_COPY" dir_path_copy = Path(dir_copy) if not dir_path_copy.exists(): log.w("temporary folder not found") dir_copy.mkdir() try: shutil.copy(dir_path_org, dir_path_copy) except OSError as e: if e.errno == OSError.errno.ENOTDIR: shutil.copy(path_org, dir_path_copy) else: raise log.w('original folder copied to temporary folder') else: pcopy_time_created = os.stat(dir_copy).st_ctime current_time = datetime.datetime.now().time() pcopy_last_update = (current_time - pcopy_time_created) / SECONDS_IN_HOUR if pcopy_last_update >= max_update_rate_hrs: shutil.rm_tree(dir_copy) shutil.copytree(dir_path_org, dir_copy) else: log.w("temporary folder was updated", pcopy_last_update, 'hours ago') return dir_copy
def clear_cache() -> None: try: path = Path(".cache") for child in path.iterdir(): if child.is_file(): child.unlink() else: shutil.rm_tree(child) path.rmdir() except Exception: pass
def ensure_nonexistent(dst, filenames, delete=False): """Ensure that each file does not exist in destination dir. If delete is False, then any files found to exist will raise an IOError. If delete is True, then any files found will be deleted.""" for f in filenames: destfile = os.path.join(dst, f) if os.path.lexists(destfile): if delete: if os.path.isdir(destfile): rm_tree(destfile) else: os.unlink(destfile) else: raise IOError("Destination file %s already exists in %s" % (f, dst))
def rm_tree(dirpath): try: shutil.rm_tree(dirpath) except: pass
def main(command, target_dir=os.getcwd(), temp_dir=gettempdir(), overwrite=False, preserve_temp_dir="failure", quiet=False, stdin_file=None, stdout_file=None, stderr_file=None, *arg): """Run a command in a temporary directory. If the command succeeds, then the contents of the temporary directory will be moved to the target directory (default pwd). If the command fails, the files will remain in the temporary directory (which may or may not be deleted). This bahavior ensures that only finished output files from successful commands will be put into the target directory, even if the command produces output files bit by bit and leaves incomplete output files upon failure. To separate options intended for intemp from those intended for the command itself, put a double dash by itself before the command, like this: intemp [intemp options here] -- command [command's options here] arg1 arg2 ... IMPORTANT: Since the specified program is run in a temporary directory, all paths to input files should be given as absolute paths, not relative ones. On the other hand, specifying relative paths for output files is recommended, since this will produce the output files in the temporary directory.""" work_dir = None success = False try: work_dir = tempfile.mkdtemp(dir=os.path.join(target_dir, temp_dir)) full_command = (command,) + arg if not quiet: print "Running in %s" % work_dir print "Command: %s" % list2cmdline(full_command) stdin=open(stdin_file, "r") if stdin_file is not None else None # Canonicalize paths stdout_file = os.path.realpath(os.path.join(work_dir, stdout_file)) if stdout_file else None stderr_file = os.path.realpath(os.path.join(work_dir, stderr_file)) if stderr_file else None if stdout_file: stdout = open(os.path.join(work_dir, stdout_file), "w") else: stdout = None if stderr_file == stdout_file: stderr = stdout elif stderr_file: stderr = open(os.path.join(work_dir, stderr_file), "w") else: stderr = None retval = subprocess.call(full_command, cwd=work_dir, stdin=stdin, stdout=stdout, stderr=stderr) for f in stdin, stdout, stderr: if f is not None: f.close() success = retval == 0 if success: if not quiet: print "Command successful: %s" % list2cmdline(full_command) try: do_sync(src=work_dir, dst=target_dir, overwrite=overwrite, move=preserve_temp_dir != 'always', quiet=quiet) except IOError: success = False if not quiet: print "Failed to copy result files to target dir" else: if not quiet: print "Command failed with return code %s: %s" % (retval, list2cmdline(full_command)) except KeyboardInterrupt: success = False retval = 1 if not quiet: print "\nJob canceled by Control + C." else: # Print a newline to make sure the '^C' that appears in # the terminal does not mess up the prompt. print "\n", finally: if not work_dir: pass elif not os.path.isdir(work_dir): pass else: if success: preserve = preserve_temp_dir == 'always' adjective = "successful" verb = "preserving" if preserve else "cleaning" else: preserve = preserve_temp_dir != 'never' adjective = "failed" verb = "preserving" if preserve else "deleting" if not quiet: print "%s working directory of %s run in %s" % (verb.title(), adjective, work_dir) if not preserve: rm_tree(work_dir) # Return the exit code of the command, but return a failing exit # code if the command succeeded but the post-command copying process # failed. if retval or success: return retval else: return 1