ballistica/tools/efrotools/genwrapper.py
2024-04-23 13:44:40 -07:00

80 lines
2.4 KiB
Python

# Released under the MIT License. See LICENSE for details.
#
"""Functionality related to android builds."""
from __future__ import annotations
import os
import sys
import stat
from typing import TYPE_CHECKING
if TYPE_CHECKING:
pass
if __name__ == '__main__':
if len(sys.argv) not in (3, 4):
raise RuntimeError('Expected 2 args')
from efrotools.project import (
getprojectconfig,
get_public_legal_notice,
get_non_public_legal_notice,
)
gentype = sys.argv[1]
path = sys.argv[2]
module = sys.argv[3] if len(sys.argv) > 3 else None
# We technically could stick the 'python' or 'python3' path in, but
# let's go with the full versioned one just to keep it clear what
# we're using currently. Just need to make sure this gets re-run
# when that changes.
pybinpath = os.path.join(
os.path.abspath(os.getcwd()), '.venv', 'bin', 'python3.12'
)
public = getprojectconfig(projroot='.').get('public', False)
assert isinstance(public, bool)
legalnotice: str = (
get_public_legal_notice('raw')
if public
else get_non_public_legal_notice()
)
if gentype == 'pcommand':
assert module is not None
contents = (
f'#!{pybinpath}\n'
f'# {legalnotice}\n'
'# This file is autogenerated; do not hand edit.\n'
'#\n'
'"""Simple wrapper so pcommand uses our internal virtual'
' environment."""\n'
f'from {module} import run_pcommand_main\n'
'\n'
'if __name__ == "__main__":\n'
' run_pcommand_main()\n'
)
elif gentype == 'cloudshell':
contents = (
f'#!{pybinpath}\n'
f'# {legalnotice}\n'
'# This file is autogenerated; do not hand edit.\n'
'#\n'
'"""Simple wrapper so cloudshell uses our'
' internal virtual environment."""\n'
'from efrotoolsinternal.cloudshell import run_cloudshell_main\n'
'\n'
'if __name__ == "__main__":\n'
' run_cloudshell_main()\n'
)
else:
raise RuntimeError(f'Unsupported gentype: {gentype}')
with open(path, 'w', encoding='utf-8') as outfile:
outfile.write(contents)
os.chmod(
path, os.stat(path).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
)