def _source_create_delete(self, if_exists, create): if not is_master_process(): return path = Path(self.source) if if_exists == OVERWRITE and path.exists(): try: path.unlink() except PermissionError: # probably user accidentally created a directory shutil.rmtree(path) return locked_id = _db_lockid(self.source) pid = os.getpid() if path.exists() and if_exists == RAISE: msg = f"'{path}' exists" if locked_id is not None: msg = ( f"{msg}, and is locked by process pid {locked_id}." f" Current pid is {pid}" ) raise FileExistsError(msg) if if_exists == IGNORE and locked_id is not None: warn(f"'{self.source}' is locked to {locked_id}, current pid is {pid}.") return if path.parent and not path.parent.exists() and not create: raise FileNotFoundError(f"'{path.parent}' does not exist, set create=True") path.parent.mkdir(parents=True, exist_ok=True)
def _source_create_delete(self, if_exists, create): if not is_master_process(): return path = Path(self.source) if path.exists() and if_exists == RAISE: raise FileExistsError(f"'{self.source}' exists") elif path.exists() and if_exists == OVERWRITE: if self._has_other_suffixes(self.source, self.suffix): raise RuntimeError( f"Unsafe to delete {self.source} as it contains ", f"files other than .{self.suffix} or .log files." " You will need to remove this directly yourself.", ) try: shutil.rmtree(self.source) except NotADirectoryError: os.remove(self.source) elif not path.exists() and not create: raise FileNotFoundError(f"'{self.source}' does not exist") if create: path.mkdir(parents=True, exist_ok=True)
def check_is_master_process(n): return parallel.is_master_process()