85 lines
2.5 KiB
Python

"""
SCons++ Bootstrapper
"""
import os
from pathlib import Path
import shutil
import subprocess
import sys
from SCons.Environment import Environment
Import('config')
_SPP_FOLDER_NAME = 'scons-plus-plus'
_SPP_DEFAULT_REPOSITORY = 'https://git.mewin.de/mewin/scons-plus-plus.git'
_SPP_DEFAULT_BRANCH = 'master'
spp_root: Path
spp_repository: str
spp_branch: str
def _main() -> Environment:
global spp_root, spp_repository, spp_branch
spp_root = config.get('SPP_ROOT')
if spp_root is None:
spp_root = _get_default_spp_root()
elif not isinstance(spp_root, Path):
spp_root = Path(str(spp_root))
spp_root = spp_root.absolute()
spp_repository = config.get('SPP_REPOSITORY', _SPP_DEFAULT_REPOSITORY)
spp_branch = config.get('SPP_BRANCH', _SPP_DEFAULT_BRANCH)
_printinfo(f'Using SCons++ root at: {spp_root}')
if not spp_root.exists():
_printinfo('SCons++ does not yet exist, downloading it.')
_install_spp()
spp_script = spp_root / 'SConscript'
if not spp_script.exists():
_printerr(f'SCons++ main script not found at {spp_script}!')
sys.exit(1)
return SConscript(spp_script, exports=['config'])
def _get_default_spp_root() -> Path:
if os.name == 'posix':
# follow XDG specification -> first try $XDG_DATA_HOME, then $HOME/.local/share
data_home = os.environ.get('XDG_DATA_HOME')
if data_home is not None:
return Path(data_home, _SPP_FOLDER_NAME)
home = os.environ.get('HOME')
if home is not None:
return Path(home, '.local', 'share', _SPP_FOLDER_NAME)
elif os.name == 'nt':
# just use LocalAppData, which should always be set on Windows
return Path(os.environ['LocalAppData'], _SPP_FOLDER_NAME)
_printinfo(f'Could not detect SCons++ root directory, falling back to ./{_SPP_FOLDER_NAME}.')
return Path(_SPP_FOLDER_NAME)
def _install_spp() -> None:
git_exe = shutil.which('git')
if git_exe is None:
_printerr('No git executable found, cannot install SCons++.')
sys.exit(1)
_exec_checked((git_exe, 'clone', '-b', spp_branch, '--progress', spp_repository, spp_root))
def _exec_checked(args: Sequence[str], **kwargs) -> None:
subprocess.run(args, stdout=sys.stdout, stderr=sys.stderr, check=True, **kwargs)
if not GetOption('silent'):
_printinfo = print
else:
def _printinfo(*args): ...
def _printerr(*args) -> None:
print(*args, file=sys.stderr)
env = _main()
Return('env')