Added command line tool and removed lib folder from gitignore.
This commit is contained in:
24
util/python_module/sppcmd/__init__.py
Normal file
24
util/python_module/sppcmd/__init__.py
Normal file
@@ -0,0 +1,24 @@
|
||||
"""
|
||||
Scons++ Command Line Interface
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
|
||||
from .ccjson import make_ccjson_parser
|
||||
|
||||
_STDOUT_LOG_FORMAT = '%(message)s'
|
||||
|
||||
def run_spp_cmd() -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--verbose', '-v', action='store_true')
|
||||
subparsers = parser.add_subparsers(required=True)
|
||||
|
||||
make_ccjson_parser(subparsers)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
logging.basicConfig(format=_STDOUT_LOG_FORMAT, level=logging.DEBUG if args.verbose else logging.INFO)
|
||||
args.handler(args)
|
||||
|
||||
return 0
|
||||
18
util/python_module/sppcmd/ccjson.py
Normal file
18
util/python_module/sppcmd/ccjson.py
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
import argparse
|
||||
from .common import exec_spp, get_config_cache, require_project_file
|
||||
|
||||
def _cmd(args: argparse.Namespace) -> None:
|
||||
require_project_file()
|
||||
|
||||
build_type = args.build_type
|
||||
if build_type == 'auto':
|
||||
cache = get_config_cache()
|
||||
build_type = cache.get('build_type', 'debug')
|
||||
|
||||
exec_spp((f'--build_type={build_type}', '--unity=disable', 'compile_commands.json'))
|
||||
|
||||
def make_ccjson_parser(subparsers) -> None:
|
||||
parser : argparse.ArgumentParser = subparsers.add_parser('ccjson', help='Generate compile_commands.json')
|
||||
parser.set_defaults(handler=_cmd)
|
||||
parser.add_argument('--build_type', choices=('auto', 'debug', 'release_debug', 'release', 'profile'), default='auto')
|
||||
51
util/python_module/sppcmd/common.py
Normal file
51
util/python_module/sppcmd/common.py
Normal file
@@ -0,0 +1,51 @@
|
||||
|
||||
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
import shlex
|
||||
import subprocess
|
||||
import sys
|
||||
from typing import Sequence
|
||||
|
||||
_project_root = Path('.').absolute()
|
||||
|
||||
def get_project_root() -> Path:
|
||||
return _project_root
|
||||
|
||||
def set_project_root(path: Path) -> None:
|
||||
global _project_root
|
||||
_project_root = path
|
||||
|
||||
def get_config_cache() -> dict:
|
||||
cache_file = get_project_root() / 'cache' / 'config_cache.json'
|
||||
if not cache_file.exists():
|
||||
return {}
|
||||
|
||||
try:
|
||||
with cache_file.open('r') as f:
|
||||
cache = json.load(f)
|
||||
if not isinstance(cache, dict):
|
||||
logging.warning('Config cache is not a dictionary, ignoring it.')
|
||||
return {}
|
||||
return cache
|
||||
except Exception as e:
|
||||
logging.error(f'Error while reading config cache: {e}.')
|
||||
return {}
|
||||
|
||||
def require_project_file() -> None:
|
||||
if not (get_project_root() / 'SConstruct').exists():
|
||||
logging.error('This command has to be run inside an existing S++ project folder. Exiting.')
|
||||
sys.exit(1)
|
||||
|
||||
def exec_checked(args: Sequence[str], **kwargs) -> None:
|
||||
logging.debug('exec_checked: "%s"', shlex.join(args))
|
||||
subprocess.run(args, stdout=sys.stdout, stderr=sys.stderr, check=True, **kwargs)
|
||||
|
||||
def exec_get_output(args: Sequence[str], **kwargs) -> str:
|
||||
logging.debug('exec_get_output: "%s"', shlex.join(args))
|
||||
return subprocess.run(args, text=True, check=True, capture_output=True, **kwargs).stdout
|
||||
|
||||
def exec_spp(args: Sequence[str], **kwargs):
|
||||
full_cmd = ('scons', '-s', '--disable_auto_update', *args)
|
||||
exec_checked(full_cmd, **kwargs)
|
||||
Reference in New Issue
Block a user