Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def actions_for_dir_path(path: str, path_acc: t.Set[str] = set()) -> t.List[Action]:
"""
Returns a list of actions that is needed to create a folder and its parent folders.
:param path:
:param path_acc: paths already examined
"""
path = abspath(path)
typecheck_locals(path=FileName(allow_non_existent=False)|DirName(), create=Bool())
assert os.path.exists(path)
if path == "" or path == "~":
return []
path = normalize_path(path)
parts = path.split("/")
ret = []
for i in range(2 if parts[0] == "~" else 1, len(parts) + 1 if os.path.isdir(abspath(path)) else len(parts)):
subpath = "/".join(parts[:i])
subpath_norm = normalize_path(subpath)
if subpath_norm in path_acc:
continue
ret.append(CreateDir(subpath_norm))
path_acc.add(subpath_norm)
return ret
def actions_for_dir_path(path: str, path_acc: t.Set[str] = set()) -> t.List[Action]:
"""
Returns a list of actions that is needed to create a folder and its parent folders.
:param path:
:param path_acc: paths already examined
"""
path = abspath(path)
typecheck_locals(path=FileName(allow_non_existent=False)|DirName(), create=Bool())
assert os.path.exists(path)
if path == "" or path == "~":
return []
path = normalize_path(path)
parts = path.split("/")
ret = []
for i in range(2 if parts[0] == "~" else 1, len(parts) + 1 if os.path.isdir(abspath(path)) else len(parts)):
subpath = "/".join(parts[:i])
subpath_norm = normalize_path(subpath)
if subpath_norm in path_acc:
continue
ret.append(CreateDir(subpath_norm))
path_acc.add(subpath_norm)
return ret
def _execute(self, db: Database):
if os.path.exists(self.file):
os.remove(abspath(self.file))
def _dry_run_message(self) -> str:
if not os.path.exists(abspath(self.directory)):
return "Would create directory {!r}".format(self.directory)
return "Wouldn't create directory {!r} as it already exists".format(self.directory)
def reverse(self) -> t.List[Action]:
if os.path.exists(abspath(self.dest)):
return [CopyFile(self.dest)]
return [RemoveFile(self.dest)]
def reverse(self) -> t.List['RemoveDir']:
if os.path.exists(abspath(self.directory)):
return []
return [RemoveDir(self.directory)]
def _execute(self, db: Database):
shutil.rmtree(abspath(self.directory))
def _execute(self, db: Database):
if not os.path.exists(abspath(self.directory)):
os.mkdir(abspath(self.directory))
def _execute(self, db: Database):
if not os.path.exists(abspath(self.directory)):
os.mkdir(abspath(self.directory))