Even though they're specific to the web CMakeLists proj, it's good to have all build scripts in one place
179 lines
4.6 KiB
Python
179 lines
4.6 KiB
Python
import os
|
|
import datetime
|
|
from pathlib import Path
|
|
import shutil
|
|
import sys
|
|
|
|
|
|
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():
|
|
return Path(os.path.realpath(__file__)).parent.parent.parent.parent.absolute()
|
|
|
|
|
|
class SparkPaths:
|
|
@staticmethod
|
|
def root():
|
|
return _workspace_root()
|
|
|
|
@staticmethod
|
|
def client_csproj(name):
|
|
return _workspace_root().joinpath('src', 'client', 'name', f'{name}.csproj')
|
|
|
|
@staticmethod
|
|
def client_sln():
|
|
return _workspace_root().joinpath('spark_client.sln')
|
|
|
|
@staticmethod
|
|
def tools():
|
|
return _workspace_root().joinpath('tools')
|
|
|
|
@staticmethod
|
|
def default_build():
|
|
if sys.platform == 'win32':
|
|
return _workspace_root().joinpath('build')
|
|
elif sys.platform == 'linux':
|
|
return _workspace_root().joinpath('build', 'gcc', 'release')
|
|
raise ValueError(f"Unknown platform '{sys.platform}'")
|
|
|
|
@staticmethod
|
|
def externals():
|
|
return _workspace_root().joinpath('externals')
|
|
|
|
|
|
class PathOrNone:
|
|
def __init__(self, path):
|
|
self.path = path
|
|
|
|
def value(self):
|
|
if self.path is None:
|
|
raise ValueError('path is None')
|
|
return self.path
|
|
|
|
def __str__(self):
|
|
return '' if self.path is None else str(self.path)
|
|
|
|
|
|
def as_path(path):
|
|
return PathOrNone(
|
|
path if isinstance(path, Path) else
|
|
Path(str(path)) if isinstance(path, str) else
|
|
None)
|
|
|
|
|
|
def as_absolute_paths(relative_paths: list, with_respect_to = None):
|
|
if relative_paths is None:
|
|
raise ValueError('relative_path is None')
|
|
if len(relative_paths) == 0:
|
|
return []
|
|
|
|
def impl():
|
|
return [as_path(p).value().absolute() for p in relative_paths]
|
|
|
|
if with_respect_to is None:
|
|
return impl()
|
|
with CWD(with_respect_to):
|
|
return impl()
|
|
|
|
|
|
def ensure_exists(path):
|
|
if not os.path.exists(path):
|
|
raise ValueError(f"Path '{path}' does not exist")
|
|
|
|
|
|
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)
|