|
| 1 | +# This file should be written to the wheel package as |
| 2 | +# `executorch/data/bin/__init__.py`. |
| 3 | +# |
| 4 | +# Setuptools will expect to be able to say something like `from |
| 5 | +# executorch.data.bin import mybin; mybin()` for each entry listed in the |
| 6 | +# [project.scripts] section of pyproject.toml. This file makes the `mybin()` |
| 7 | +# function execute the binary at `executorch/data/bin/mybin` and exit with that |
| 8 | +# binary's exit status. |
| 9 | + |
| 10 | +import subprocess |
| 11 | +import os |
| 12 | +import sys |
| 13 | +import types |
| 14 | + |
| 15 | +# This file should live in the target `bin` directory. |
| 16 | +_bin_dir = os.path.join(os.path.dirname(__file__)) |
| 17 | + |
| 18 | +def _find_executable_files_under(dir): |
| 19 | + """Lists all executable files in the given directory.""" |
| 20 | + bin_names = [] |
| 21 | + for filename in os.listdir(dir): |
| 22 | + filepath = os.path.join(dir, filename) |
| 23 | + if os.path.isfile(filepath) and os.access(filepath, os.X_OK): |
| 24 | + bin_names.append(filename) |
| 25 | + return bin_names |
| 26 | + |
| 27 | +# The list of binaries to create wrapper functions for. An executable should |
| 28 | +# exist for each name under `data/bin/<bin-name>`. |
| 29 | +# @nocommit: see if we can just scrape this from the filesystem to avoid the template |
| 30 | +_bin_names = _find_executable_files_under(_bin_dir) |
| 31 | + |
| 32 | +# We'll define functions named after each binary. Make them importable. |
| 33 | +__all__ = _bin_names |
| 34 | + |
| 35 | +def _run(name): |
| 36 | + """Runs the named binary, which should live under _bin_dir. |
| 37 | + |
| 38 | + Exits the current process with the return code of the subprocess. |
| 39 | + """ |
| 40 | + raise SystemExit(subprocess.call([os.path.join(_bin_dir, name)] + sys.argv[1:], close_fds=False)) |
| 41 | + |
| 42 | +# Define a function named after each of the binaries. |
| 43 | +for bin_name in _bin_names: |
| 44 | + exec(f"def {bin_name}(): _run('{bin_name}')") |
| 45 | + ### globals()[bin_name] = types.FunctionType( |
| 46 | + ### code=f"def {bin_name}(): _run('{bin_name}')", |
| 47 | + ### globals=globals(), |
| 48 | + ### ) |
0 commit comments