90 lines
2.3 KiB
Python
90 lines
2.3 KiB
Python
import os
|
|
import datetime
|
|
import shutil
|
|
|
|
|
|
def fmt_datetime_suffix(dt: datetime.datetime = None):
|
|
return (dt or datetime.datetime.now()).strftime("%Y-%m%d-%H%M")
|
|
|
|
|
|
def append_datetime_suffix(path, dt: datetime.datetime = None):
|
|
return f'{path}_{fmt_datetime_suffix(dt)}'
|
|
|
|
|
|
def get_unique_path(
|
|
path: str, max_tries: int = 100):
|
|
if path is None or len(path) == 0:
|
|
raise ValueError("'path' is none or empty")
|
|
|
|
if not os.path.exists(path):
|
|
return path
|
|
|
|
max_tries = 100
|
|
i = 1
|
|
orig_path = path
|
|
while i < max_tries:
|
|
path = f'{orig_path}({i})'
|
|
if not os.path.exists(path):
|
|
break
|
|
i += 1
|
|
if i == max_tries:
|
|
raise ValueError(
|
|
f"Unable to make path unique by appending appending an incrementing integer. "
|
|
f"Maximum attempts exceeded ({max_tries})")
|
|
return path
|
|
|
|
|
|
def unlink(path):
|
|
if not os.path.exists(path):
|
|
return
|
|
if not os.path.islink(path):
|
|
raise ValueError(
|
|
'The specified path already exists and is not a symbolic link')
|
|
os.unlink(path)
|
|
|
|
|
|
def rmdir_or_unlink(dir):
|
|
if not os.path.exists(dir):
|
|
return
|
|
if os.path.islink(dir):
|
|
os.unlink(dir)
|
|
else:
|
|
shutil.rmtree(dir)
|
|
|
|
|
|
def link_dir(source_path, link_path):
|
|
if source_path is None:
|
|
raise TypeError('source_path is None')
|
|
if not os.path.isdir(source_path):
|
|
raise ValueError('source_path is not a directory')
|
|
if link_path is None:
|
|
raise TypeError('link_path is None')
|
|
if source_path == link_path:
|
|
return
|
|
|
|
if not os.path.exists(source_path):
|
|
os.makedirs(source_path)
|
|
if os.path.exists(link_path):
|
|
if os.path.islink(link_path):
|
|
if os.readlink(link_path) == source_path:
|
|
return
|
|
os.unlink(link_path)
|
|
elif os.path.isdir(link_path):
|
|
shutil.rmtree(link_path)
|
|
elif os.path.isfile(link_path):
|
|
os.remove(link_path)
|
|
else:
|
|
raise ValueError('link_path is an unexpected filetype')
|
|
os.symlink(source_path, link_path)
|
|
|
|
|
|
def create_dir(path, overwrite_if_exists = False):
|
|
if path is None:
|
|
raise TypeError('path is None')
|
|
|
|
if not os.path.exists(path):
|
|
os.makedirs(path)
|
|
elif overwrite_if_exists:
|
|
rmdir_or_unlink(path)
|
|
os.makedirs(path)
|