Files
spark/tools/py/core/file_utils.py
T

135 lines
3.4 KiB
Python

import os
import datetime
from pathlib import Path
import shutil
class CWD:
def __init__(self, push_path, pop_path=os.getcwd()):
self.push_path = push_path
self.pop_path = pop_path
def __enter__(self):
if self.push_path is not None:
os.chdir(self.push_path)
def __exit__(self, exc_type, exc_val, exc_tb):
if self.pop_path is not None:
os.chdir(self.pop_path)
def _workspace_root():
tools_dir = Path(os.path.realpath(__file__)).parent.parent.parent
return os.path.abspath(f'{tools_dir}\\{os.path.pardir}')
class SparkPaths:
@staticmethod
def root():
return _workspace_root()
@staticmethod
def client_csproj(name):
return fr'{_workspace_root()}\src\client\{name}\{name}.csproj'
@staticmethod
def client_sln():
return fr'{_workspace_root()}\spark_client.sln'
@staticmethod
def tools():
return fr'{_workspace_root()}\tools'
@staticmethod
def default_build():
return fr'{_workspace_root()}\build'
@staticmethod
def externals():
return fr'{_workspace_root()}\externals'
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)