refactor (build): Move CWD & SparkPaths to common module file_utils
This commit is contained in:
1 parent
649ba11edb
commit
81f7fe72ab
2 files changed
+51
-51
No files matched your search
+3
-48
@@ -5,7 +5,7 @@ import subprocess
|
||||
import shutil
|
||||
|
||||
from core.argparse_utils import add_toggle_opt, get_arg
|
||||
from core.file_utils import create_dir, link_dir, unlink
|
||||
from core.file_utils import CWD, SparkPaths, create_dir, link_dir, unlink
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
@@ -68,37 +68,6 @@ def _deduce_visual_studio_context():
|
||||
raise ValueError('Failed to deduce visual studio context')
|
||||
|
||||
|
||||
def _workspace_root():
|
||||
tools_dir = Path(os.path.realpath(__file__)).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'
|
||||
|
||||
|
||||
class BuildTarget:
|
||||
def __init__(self, name, path, cmd=None):
|
||||
self.name = name
|
||||
@@ -108,7 +77,7 @@ class BuildTarget:
|
||||
|
||||
class BuildContext:
|
||||
def __init__(self, workspace_root = None, build_dir = None):
|
||||
workspace_root = workspace_root or _workspace_root()
|
||||
workspace_root = workspace_root or SparkPaths.root()
|
||||
build_dir = build_dir or f'{workspace_root}\\build'
|
||||
|
||||
self.workspace_root = workspace_root
|
||||
@@ -193,20 +162,6 @@ class BuildContext:
|
||||
return []
|
||||
|
||||
|
||||
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 _get_arg_parser():
|
||||
parser = argparse.ArgumentParser(description='Build spark_client.sln')
|
||||
|
||||
@@ -310,7 +265,7 @@ def build(
|
||||
except Exception as e:
|
||||
_exit(1, e)
|
||||
|
||||
build_ctx = BuildContext(_workspace_root(), build_dir)
|
||||
build_ctx = BuildContext(SparkPaths.root(), build_dir)
|
||||
csharp_tgts, cpp_tgts, publish_tgts = build_ctx.parse_targets(targets)
|
||||
build_cmd_steps = [vs_context.prompt_cmd()]
|
||||
base_output_path_arg = vs_context.msbuild_prop(
|
||||
|
||||
@@ -1,8 +1,54 @@
|
||||
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")
|
||||
|
||||
@@ -11,8 +57,7 @@ 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):
|
||||
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")
|
||||
|
||||
@@ -78,7 +123,7 @@ def link_dir(source_path, link_path):
|
||||
os.symlink(source_path, link_path)
|
||||
|
||||
|
||||
def create_dir(path, overwrite_if_exists = False):
|
||||
def create_dir(path, overwrite_if_exists=False):
|
||||
if path is None:
|
||||
raise TypeError('path is None')
|
||||
|
||||
|
||||
Reference in new issue
Block a user