Files
spark/tools/py/publish_api_server.py
T

209 lines
5.8 KiB
Python

import argparse
import os
import subprocess
import sys
from shutil import copytree
from typing import Optional
from build_spark import SparkPaths
from core.iter_utils import at_bounded
def _dotnet_verbosity(level):
names = ["quiet", "minimal", "normal", "detailed", "diagnostic"]
return at_bounded(names, level - 1)
def _cmake_verbosity(level):
# Skip 'WARNING' and 'NOTICE' levels, since the default CMake verbosity,
# 'STATUS', doesn't produce that much output. This also better normalizes
# numeric representation of verbosity w/ dotnet, which only has 5 levels.
names = [
"ERROR",
# 'WARNING',
# 'NOTICE',
"STATUS",
"VERBOSE",
"DEBUG",
"TRACE",
]
return at_bounded(names, level - 1)
def _cmake_preset():
match sys.platform:
case "win32":
return "spark-api-server-windows"
case "linux":
return "spark-api-server-linux"
case _:
raise ValueError(f"Unsupported platform {sys.platform}")
def _dotnet_runtime():
match sys.platform:
case "win32":
return "win-x64"
case "linux":
return "linux-x64"
case _:
raise ValueError(f"Unsupported platform {sys.platform}")
class PublishArgs:
def __init__(
self,
configuration: str = "release",
cmake_jobs: Optional[int] = None,
clean: bool = False,
verbosity: int = 2,
dry_run: bool = False,
output: Optional[str] = None,
framework="net8.0",
runtime=_dotnet_runtime(),
):
self.configuration = configuration
self.cmake_jobs = cmake_jobs
self.clean = clean
self.verbosity = verbosity
self.dry_run = dry_run
self.dotnet_verbosity = at_bounded(
["quiet", "minimal", "normal", "detailed", "diagnostic"], verbosity
)
# Leave out WARNING and NOTICE to normalize cmake log level and dotnet verbosity
# (so both can be expressed as an int 1-5)
self.cmake_log_level = at_bounded(
[
"ERROR",
# 'WARNING',
# 'NOTICE',
"STATUS",
"VERBOSE",
"DEBUG",
"TRACE",
],
verbosity,
)
self.output = output
self.framework = framework
self.runtime = runtime
def _get_arg_parser():
parser = argparse.ArgumentParser(
description="Publish a self-contined spark client application"
)
parser.add_argument(
"--configuration",
metavar="debug|release",
help="The build configuration",
)
parser.add_argument(
"--cmake-jobs",
type=int,
help="The number of cmake parallel jobs",
)
parser.add_argument(
"--clean",
action="store_true",
help="Perform a clean build (remove artifacts from previous build)",
)
parser.add_argument(
"-o", "--output", type=str, help="The output directory for published artifacts."
)
parser.add_argument(
"--verbosity",
metavar="{0-5}",
choices=range(1, 6),
type=int,
help="The output verbosity level",
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Preview the build commands without running them",
)
defaults = PublishArgs()
parser.set_defaults(
configuration=defaults.configuration,
clean=defaults.clean,
output=defaults.output,
verbosity=defaults.verbosity,
dry_run=defaults.dry_run,
)
return parser
def publish(args: PublishArgs):
def run(cmd):
print(cmd)
if args.dry_run:
return
subprocess.run(cmd, shell=True).check_returncode()
sln = SparkPaths.root().joinpath("Spark.Api.Server.sln")
cmake_preset = _cmake_preset()
cmake_config = args.configuration.title()
cmake_jobs = args.cmake_jobs
cmake_verbosity = _cmake_verbosity(args.verbosity)
dotnet_config = args.configuration.title()
dotnet_verbosity = _dotnet_verbosity(args.verbosity)
publish_dir = SparkPaths.default_build().joinpath(
dotnet_config,
"bin",
args.framework,
args.runtime,
"publish",
)
os.chdir(SparkPaths.root())
if args.clean:
run(
f"cmake --build build/{cmake_preset} --config {cmake_config} --target clean"
)
run(f"dotnet clean {sln} -c {dotnet_config} -v {dotnet_verbosity}")
run(f"cmake --preset {cmake_preset} -DCMAKE_MESSAGE_LOG_LEVEL={cmake_verbosity}")
cmake_build_cmd = (
f"cmake --build build/{cmake_preset} --config {cmake_config} --target install"
)
if cmake_jobs:
cmake_build_cmd += f" -j {cmake_jobs}"
run(cmake_build_cmd)
run(
f"dotnet publish {sln} -c {dotnet_config} -v {dotnet_verbosity} -f {args.framework} -r {args.runtime} --sc"
)
# Copy cpp bins
print(f"[pseudo shell] cp -r install/{cmake_preset}/bin/* {publish_dir}")
if not args.dry_run:
copytree(f"install/{cmake_preset}/bin", publish_dir, dirs_exist_ok=True)
if args.output:
# Move to '--output' dir
print(f"[pseudo shell] mv {publish_dir} {args.output}")
if not args.dry_run:
os.rename(publish_dir, args.output)
def _main():
parser = _get_arg_parser()
args = parser.parse_args()
publish(
PublishArgs(
configuration=args.configuration,
clean=args.clean,
cmake_jobs=args.cmake_jobs,
verbosity=args.verbosity,
dry_run=args.dry_run,
output=args.output,
)
)
if __name__ == "__main__":
try:
_main()
except Exception as e:
print(f"publish_api_server.py failed: {e}")
exit(1)