80 lines
2.3 KiB
Python
80 lines
2.3 KiB
Python
import argparse
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
|
|
from core.file_utils import SparkPaths, create_dir, find_files, find_file_tree
|
|
from core.iter_utils import at_bounded
|
|
from core.logging import exit_msg
|
|
|
|
|
|
def _get_arg_parser():
|
|
parser = argparse.ArgumentParser(
|
|
description='Generates cmd scripts (aliases) for py scripts')
|
|
parser.add_argument(
|
|
'--sources',
|
|
nargs='+',
|
|
help='The source files for which to generate cs',
|
|
)
|
|
parser.add_argument(
|
|
'--out-dir',
|
|
metavar='PATH',
|
|
help='The directory in which to dump the aliases',
|
|
)
|
|
parser.add_argument(
|
|
'--python-exe',
|
|
metavar='PATH',
|
|
help='The python exe to use in the cmd aliases',
|
|
)
|
|
parser.add_argument(
|
|
'--clean',
|
|
action='store_true',
|
|
help='Delete existing lexer.cs and parser.cs files in --out-dir',
|
|
)
|
|
parser.add_argument(
|
|
'--overwrite-existing',
|
|
action='store_true',
|
|
help=
|
|
'Overwrite existing files with the generated aliases (if necessary)',
|
|
)
|
|
parser.add_argument(
|
|
'--recursive',
|
|
help='Whether or not to generate aliases recursively',
|
|
)
|
|
this_dir = Path(__file__).parent.absolute()
|
|
parser.set_defaults(
|
|
sources=[this_dir],
|
|
python_exe='python',
|
|
clean=False,
|
|
)
|
|
return parser
|
|
|
|
|
|
def _main():
|
|
parser = _get_arg_parser()
|
|
args = parser.parse_args()
|
|
if args.sources is None:
|
|
print('No sources specified')
|
|
sources = [Path(s).absolute() for s in args.sources]
|
|
scripts = [f.path for s in sources for f in find_file_tree(s, '.py', 1)]
|
|
static_out_dir = None if args.out_dir is None else Path(args.out_dir)
|
|
|
|
for script in scripts:
|
|
out_dir = static_out_dir or script.parent.with_name(
|
|
f'{script.parent.name}.aliases')
|
|
create_dir(out_dir)
|
|
alias = out_dir.joinpath(f'{script.stem}.cmd')
|
|
if alias.exists():
|
|
print(f'Skipping {alias} because it already exists')
|
|
continue
|
|
print(f'Generating {alias}')
|
|
with open(alias, 'w') as alias_out:
|
|
alias_out.write(f'@{args.python_exe} {script.absolute()} %*\n')
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
_main()
|
|
except Exception as e:
|
|
print(f'gen_cmd_aliases.py failed: {e}')
|