Merge branch 'prod' of bitbucket.org:bluetradesys/spark into prod

This commit is contained in:
Taha Afzal committed 2022-11-22 12:18:19 -05:00
commit cd8dc02487
8 files changed
+463 -4

No files matched your search

+10
View File
@@ -0,0 +1,10 @@
@echo off
@setlocal
set tools_dir=%~dp0
set repo_dir=%tools_dir%\..
call %tools_dir%\ensure_venv.cmd
call %repo_dir%\.venv\Scripts\activate.bat
touch %~dp0..\src\lib\CMakeLists.txt
call python %tools_dir%\py\build_spark.py %*
EXIT /B %ERRORLEVEL%
+2 -2
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
tools_path=$(dirname $(readlink -f $0))
eval "$tools_path/venv_exec.sh python $tools_path/py/build_sparkd.py $@"
exec "$tools_path/ensure_venv.sh"
exec "$tools_path/venv_exec.sh python $tools_path/py/build_sparkd.py $@"
+20
View File
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
declare tools_dir="$(readlink -f $(dirname ${BASH_SOURCE[0]}))"
declare repo_dir="$tools_dir/.."
declare venv_dir="$repo_dir/.venv"
declare venv_activate_sh="$repo_dir/.venv/bin/activate"
declare requirements_txt="$repo_dir/requirements.txt"
if [[ ! -f "$venv_activate_sh" ]]; then
echo "Creating python virtual environment '.venv'"
python3 -m pip install --upgrade pip
python3 -m venv $venv_dir
source $venv_activate_sh
echo "Upgrading pip"
python -m pip install --upgrade pip
else
source $venv_activate_sh
fi
pip install -q -r $requirements_txt
+9
View File
@@ -0,0 +1,9 @@
@echo on
@call %~dp0..\tools\ensure_venv.cmd
touch %~dp0..\src\lib\CMakeLists.txt
set PATH=%~dp0..\.venv\Scripts;%PATH%
set PYTHONPATH=%~dp0\..\.venv\Scripts\python
@call python %~dp0..\tools\py\publish_spark.py %*
EXIT /B %ERRORLEVEL%
+357
View File
@@ -0,0 +1,357 @@
import argparse
import os
from pathlib import Path
import subprocess
import shutil
from core.argparse_utils import add_toggle_opt, get_arg
from core.file_utils import CWD, SparkPaths, create_dir, link_dir, unlink
from core.logging import exit_msg
from typing import Tuple
class VisualStudioContext:
def __init__(self, version, year, prompt_path, msbuild_path, msbuild_arg_prefix):
self.version = version
self.year = year
self.prompt_path = prompt_path
self.msbuild_path = msbuild_path
self._msbuild_arg_prefix = msbuild_arg_prefix
def prompt_cmd(self):
return f'{self.prompt_path} -arch=x64 -host_arch=x64'
def configure_libspark(self, build_dir) -> subprocess.CompletedProcess:
with CWD(build_dir):
res = subprocess.run(
f'cmake.exe -G "Visual Studio {self.version} {self.year}" {SparkPaths.root()}'
)
shutil.copy(
SparkPaths.tools().joinpath('cpp.build.props'),
'Directory.Build.Props')
return res
def msbuild_flag(self, flag):
return f'{self._msbuild_arg_prefix}{flag}'
def msbuild_clp(self, key, value = None):
rv = f'{self._msbuild_arg_prefix}clp:{key}'
if value is not None:
rv = f'{rv}={value}'
return rv
def msbuild_target(self, target):
return f'{self._msbuild_arg_prefix}t:{target}'
def msbuild_prop(self, key, value):
return f'{self._msbuild_arg_prefix}p:{key}={value}'
def _deduce_visual_studio_context():
# Wrap paths in quotes so spaces don't need to be escaped
# promp_path values are tuples with fields 'version, year, prompt_path', respectively
vs_contexts = [
VisualStudioContext(
17, 2022,
r'C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat',
r'"C:\Program Files\Microsoft Visual Studio\2022\Community\Msbuild\Current\Bin\amd64\MSBuild.exe"',
'-'),
VisualStudioContext(
16, 2019,
r'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat',
r'"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe"',
'/')
]
for vs_context in vs_contexts:
if os.path.exists(f'{vs_context.prompt_path}'):
return vs_context
raise ValueError('Failed to deduce visual studio context')
class BuildTarget:
def __init__(self, name, path, cmd=None):
self.name = name
self.path = path
self.cmd = cmd
class BuildContext:
def __init__(self, workspace_root = None, build_dir = None, configuration = None):
workspace_root = workspace_root or SparkPaths.root()
build_dir = build_dir or SparkPaths.default_build()
self.workspace_root = workspace_root
self.build_dir = build_dir
def get_csharp_targets() -> dict:
targets = {}
csproj_root = workspace_root.joinpath('src', 'client')
def add_target(target: BuildTarget):
targets[target.name] = target
sln_tgt = BuildTarget('spark_client', workspace_root.joinpath('spark_client.sln'))
add_target(sln_tgt)
def add_csproj_target(name: str):
add_target(BuildTarget(name, csproj_root.joinpath(name, f'{name}.csproj')))
# TODO: rename 'Spark.csproj' -> 'spark.csproj' to be consistent
# with all other csproj filenames, which are snake_case
add_target(BuildTarget('spark', csproj_root.joinpath('spark', 'Spark.csproj')))
add_csproj_target('spark_api')
add_csproj_target('spark_core')
add_csproj_target('spark_sdk')
add_csproj_target('spark_test')
add_csproj_target('sparkcmd')
return targets
self.csharp_targets = get_csharp_targets()
self.csharp_aliases = {
'all': ['spark_client'],
'api': ['spark_api'],
'core': ['spark_core'],
'cmd': ['sparkcmd'],
'test': ['test'],
'sdk': ['spark_sdk']
}
def get_cpp_targets() -> dict:
targets = {}
def add_target(target: BuildTarget):
targets[target.name] = target
add_target(BuildTarget('libspark', build_dir.joinpath('ALL_BUILD.vcxproj')))
return targets
self.cpp_targets = get_cpp_targets()
self.cpp_aliases = { }
publish_spark = BuildTarget(
'publish',
build_dir.joinpath(configuration or 'release', 'bin', 'net471'))
self.publish_targets = {publish_spark.name: publish_spark}
self.publish_aliases = {}
def parse_targets(self, targets) -> Tuple[list, list, list]:
'''Parses target input (from cli) and returns a tuple whose items are a
list of csharp targets, and cpp targets, respectively'''
csharp_ts = []
cpp_ts = []
publish_ts = []
for t in targets:
# Always try to parse `t` as both cpp and csharp targets, since it
# could be an alias for a combination of csharp and cpp targets.
csharp_ts.extend(self._parse_target(t, self.csharp_targets, self.csharp_aliases))
cpp_ts.extend(self._parse_target(t, self.cpp_targets, self.cpp_aliases))
publish_ts.extend(self._parse_target(t, self.publish_targets, self.publish_aliases))
return (csharp_ts, cpp_ts, publish_ts)
def _parse_target(self, t, targets, aliases) -> list[BuildTarget]:
if t in targets:
return [targets[t]]
elif t in aliases:
alias_ts = aliases[t]
for a in alias_ts:
if a not in targets:
msg = f"Failed to find target for '{t}' (alias '{a}'). known targets: {targets}"
exit_msg(1, msg)
return [targets[alias_t] for alias_t in aliases[t]]
return []
def _get_arg_parser():
parser = argparse.ArgumentParser(description='Build spark_client.sln')
parser.add_argument(
'--configuration',
metavar='debug|release',
type=str,
nargs='?',
help='The build configuration',
default='Debug')
parser.add_argument(
'-o',
'--output-dir',
metavar='path',
type=str,
nargs='?',
help='The directory in which to build')
parser.add_argument(
'-l',
'--link-output-dir',
metavar='link_path',
type=str,
nargs='?',
help='The symbolink link to --output-dir')
parser.add_argument(
'-t',
'--target',
metavar='targets',
type=str,
action='append',
nargs='+',
help='The target(s) to build')
add_toggle_opt(parser, 'cpp', help='Whether or not to build cpp')
add_toggle_opt(parser, 'csharp', help='Whether or not to build csharp')
add_toggle_opt(
parser, 'restore', help='Whether or not to run msbuild /t:restore')
add_toggle_opt(
parser,
'clean',
help=
'Whether or not to delete and overwrite an existing build directory')
parser.add_argument(
'--verbosity',
metavar='[q(uiet)|m(inimal)|n(ormal)|d(etailed)|diag(nostic)]',
choices=['q', 'quiet', 'm', 'minimal', 'n', 'normal', 'd', 'detailed', 'diag', 'diagnostic'],
type=str,
help='The output verbosity level')
parser.set_defaults(restore=True, cpp=True, csharp=True, verbosity='minimal')
return parser
def build(
configuration: str | None = None,
restore: bool = True,
clean: bool = False,
build_dir: Path | None = None,
link_build_dir: Path | None = None,
targets: list[str] | None = None,
verbosity: str | None = None) -> list[subprocess.CompletedProcess]:
'''Builds the spark client'''
# check if explicit no-op
if targets is not None and len(targets) == 0:
return []
proc_results = []
# init unspecified args w/ defaults
if targets is None:
targets = ['all']
if configuration is None:
configuration = 'release' if 'publish' in targets else 'debug'
if build_dir is None:
build_dir = SparkPaths.default_build()
if verbosity is None:
verbosity = 'minimal'
vs_context = _deduce_visual_studio_context()
try:
build_dir = build_dir.absolute()
if link_build_dir is not None:
unlink(link_build_dir)
create_dir(build_dir, clean)
cfg_res = vs_context.configure_libspark(build_dir)
proc_results.append(cfg_res)
except Exception as e:
exit_msg(1, e)
build_ctx = BuildContext(
SparkPaths.root(),
build_dir=build_dir,
configuration=configuration)
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(
'BaseOutputPath', f'{build_dir}\\{configuration}')
# restore build (downloads nuget package deps)
if restore or clean:
restore_cmd_prefix = ' '.join([
f"{vs_context.msbuild_path}",
f"{vs_context.msbuild_flag('m')}",
f"{vs_context.msbuild_prop('platform', 'x64')}",
f"{vs_context.msbuild_prop('configuration', configuration)}",
f"{vs_context.msbuild_target('restore')}",
f"{vs_context.msbuild_clp('verbosity', verbosity)}",
f"{vs_context.msbuild_clp('summary')}"])
for t in cpp_tgts:
build_cmd_steps.append(
f'{restore_cmd_prefix} {t.path}')
for t in csharp_tgts:
build_cmd_steps.append(
f'{restore_cmd_prefix} {base_output_path_arg} {t.path}')
# main build
debugtype = 'portable' if configuration.lower() == 'debug' else 'none'
build_cmd_prefix = ' '.join([
f"{vs_context.msbuild_path}",
f"{vs_context.msbuild_flag('m')}",
f"{vs_context.msbuild_prop('platform', 'x64')}",
f"{vs_context.msbuild_prop('GenerateFullPaths', 'true')}",
f"{vs_context.msbuild_prop('configuration', configuration)}",
f"{vs_context.msbuild_prop('debugtype', debugtype)}",
f"{vs_context.msbuild_clp('verbosity', verbosity)}",
f"{vs_context.msbuild_clp('summary')}"])
for t in cpp_tgts:
build_cmd_steps.append(
f'{build_cmd_prefix} {t.path}')
for t in csharp_tgts:
build_cmd_steps.append(
f'{build_cmd_prefix} {base_output_path_arg} {t.path}')
bins = SparkPaths.client_bins(build_dir, configuration=configuration)
if len(publish_tgts) > 1:
raise ValueError(
f"Expected only 1 publish target, 'spark', for old prod branch build. "
f"Received: {', '.join(publish_tgts)}")
for t in publish_tgts:
publish_dir = build_dir.joinpath(configuration, t.name)
build_cmd_steps.append(
f'robocopy {t.path} {publish_dir} /E /IM /IS /IT /NFL /NP /NDL /NJH /NJS /PURGE')
build_cmd = ' && '.join(build_cmd_steps)
print(f'build_cmd: {build_cmd}', flush=True)
proc_results.append(subprocess.run(build_cmd))
if link_build_dir is not None:
link_dir(build_dir, link_build_dir)
return proc_results
def _main():
parser = _get_arg_parser()
args = parser.parse_args()
def arg_to_path(arg):
return None if arg is None else Path(arg)
def flatten_arglist(arglist):
return None if arglist is None else [a for grp in arglist for a in grp]
args_dict = vars(args)
build_dir = arg_to_path(get_arg(args_dict, 'output_dir'))
link_build_dir = arg_to_path(get_arg(args_dict, 'link_output_dir'))
targets = flatten_arglist(get_arg(args_dict, 'target'))
verbosity = get_arg(args_dict, 'verbosity') or None
build(
configuration=args.configuration,
clean=args.clean,
restore=args.restore,
build_dir=build_dir,
link_build_dir=link_build_dir,
targets=targets,
verbosity=verbosity)
if __name__ == "__main__":
_main()
+1 -1
View File
@@ -42,7 +42,7 @@ class SparkPaths:
@staticmethod
def client_bins(build_root=_default_build(), configuration='Release'):
return build_root.joinpath(configuration, 'bin', 'net6.0-windows', 'win-x64')
return build_root.joinpath(configuration, 'bin', 'net471')
@staticmethod
def client_sln():
+63
View File
@@ -0,0 +1,63 @@
import argparse
from pathlib import Path
import subprocess
from core.argparse_utils import add_toggle_opt
from build_spark import build
from build_spark import SparkPaths
def _get_arg_parser():
parser = argparse.ArgumentParser(
description='Publish a self-contined spark client application')
add_toggle_opt(
parser, 'zip', help='Whether or not to zip the build folder')
parser.add_argument(
'--build-id', help='A unique identifier for this build')
parser.add_argument(
'-o',
'--output-dir',
metavar='path',
type=str,
nargs='?',
help='The directory in which to build')
parser.add_argument(
'--verbosity',
metavar='[q(uiet)|m(inimal)|n(ormal)|d(etailed)|diag(nostic)]',
choices=[
'q', 'quiet', 'm', 'minimal', 'n', 'normal', 'd', 'detailed',
'diag', 'diagnostic'
],
type=str,
help='The output verbosity level')
parser.set_defaults(
restore=True, cpp=True, csharp=True, verbosity='minimal')
return parser
def _main():
parser = _get_arg_parser()
args = parser.parse_args()
output_dir = args.output_dir or SparkPaths.default_build()
output_dir = Path(output_dir)
build(
configuration='release',
restore=True,
build_dir=output_dir,
targets=['publish'],
verbosity=args.verbosity)
if args.zip:
publish_path = output_dir.joinpath('release', 'publish', '*')
subprocess.run(
f'7z a -mmt=30 spark_client_{args.build_id}.7z {publish_path}',
shell=True)
if __name__ == '__main__':
_main()