|
| 1 | +import argparse |
| 2 | +import hashlib |
| 3 | +import os |
| 4 | +import sys |
| 5 | +from base64 import urlsafe_b64encode |
| 6 | +from importlib.metadata import distribution, packages_distributions |
| 7 | +from importlib.resources import files |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +from .module_alias_map import get_meta_path_insertion_index, AliasedModuleFinder |
| 11 | + |
| 12 | +__MLIR_PYTHON_PACKAGE_PREFIX__ = "__MLIR_PYTHON_PACKAGE_PREFIX__" |
| 13 | +PACKAGE = __package__.split(".")[0] |
| 14 | +PACKAGE_ROOT_PATH = files(PACKAGE) |
| 15 | +DIST = distribution(packages_distributions()[PACKAGE][0]) |
| 16 | +MLIR_PYTHON_PACKAGE_PREFIX_TOKEN_PATH = ( |
| 17 | + Path(__file__).parent / __MLIR_PYTHON_PACKAGE_PREFIX__ |
| 18 | +).absolute() |
| 19 | + |
| 20 | + |
| 21 | +def _add_file_to_sources_txt_file(file_path: Path): |
| 22 | + assert file_path.exists(), f"file being added doesn't exist at {file_path}" |
| 23 | + relative_file_path = Path(PACKAGE) / file_path.relative_to(PACKAGE_ROOT_PATH) |
| 24 | + if DIST._read_files_egginfo() is not None: |
| 25 | + with open(DIST._path / "SOURCES.txt", "a") as sources_file: |
| 26 | + sources_file.write(f"\n{relative_file_path}") |
| 27 | + if DIST._read_files_distinfo(): |
| 28 | + with open(file_path, "rb") as file, open( |
| 29 | + DIST._path / "RECORD", "a" |
| 30 | + ) as sources_file: |
| 31 | + # https://packaging.python.org/en/latest/specifications/recording-installed-packages/#the-record-file |
| 32 | + m = hashlib.sha256() |
| 33 | + file = file.read() |
| 34 | + m.update(file) |
| 35 | + encoded = urlsafe_b64encode(m.digest()) |
| 36 | + sources_file.write( |
| 37 | + f"{relative_file_path},sha256={encoded[:-1].decode()},{len(file)}\n" |
| 38 | + ) |
| 39 | + else: |
| 40 | + raise RuntimeError("unsupported distribution scheme; please file a bug!") |
| 41 | + |
| 42 | + |
| 43 | +def _get_mlir_package_prefix(): |
| 44 | + mlir_python_package_prefix = None |
| 45 | + if MLIR_PYTHON_PACKAGE_PREFIX_TOKEN_PATH.exists(): |
| 46 | + with open(MLIR_PYTHON_PACKAGE_PREFIX_TOKEN_PATH) as f: |
| 47 | + mlir_python_package_prefix = f.read().strip() |
| 48 | + |
| 49 | + if os.getenv("MLIR_PYTHON_PACKAGE_PREFIX"): |
| 50 | + mlir_python_package_prefix = os.getenv("MLIR_PYTHON_PACKAGE_PREFIX") |
| 51 | + |
| 52 | + return mlir_python_package_prefix |
| 53 | + |
| 54 | + |
| 55 | +def alias_upstream_bindings(): |
| 56 | + if mlir_python_package_prefix := _get_mlir_package_prefix(): |
| 57 | + sys.meta_path.insert( |
| 58 | + get_meta_path_insertion_index(), |
| 59 | + AliasedModuleFinder({"mlir": mlir_python_package_prefix}), |
| 60 | + ) |
| 61 | + elif not ( |
| 62 | + sys.argv[0].endswith("configure-mlir-utils") |
| 63 | + or ("-m" in sys.orig_argv and "mlir_utils.__configuration" in sys.orig_argv) |
| 64 | + ): |
| 65 | + raise Exception( |
| 66 | + "mlir-utils not configured and MLIR_PYTHON_PACKAGE_PREFIX env variable not set" |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +def configure_host_bindings(): |
| 71 | + parser = argparse.ArgumentParser( |
| 72 | + prog="configure-mlir-utils", |
| 73 | + description="Configure mlir-utils", |
| 74 | + ) |
| 75 | + parser.add_argument("-y", "--yes", action="store_true", default=False) |
| 76 | + parser.add_argument("mlir_python_package_prefix") |
| 77 | + args = parser.parse_args() |
| 78 | + mlir_python_package_prefix = args.mlir_python_package_prefix |
| 79 | + mlir_python_package_prefix = ( |
| 80 | + mlir_python_package_prefix.replace("'", "").replace('"', "").strip() |
| 81 | + ) |
| 82 | + |
| 83 | + if current_mlir_python_package_prefix := _get_mlir_package_prefix(): |
| 84 | + print( |
| 85 | + f'mlir_python_package_prefix has already been set to "{current_mlir_python_package_prefix}"' |
| 86 | + ) |
| 87 | + if not args.yes: |
| 88 | + answer = input("do you want to reset? [y/n]: ") |
| 89 | + if answer.lower() not in {"1", "true", "yes", "y"}: |
| 90 | + return |
| 91 | + |
| 92 | + if not args.yes: |
| 93 | + answer = input(f"new {mlir_python_package_prefix=}; continue? [y/n]: ") |
| 94 | + if answer.lower() not in {"1", "true", "yes", "y"}: |
| 95 | + return |
| 96 | + else: |
| 97 | + print(f"new {mlir_python_package_prefix=}") |
| 98 | + |
| 99 | + # check if valid package/module |
| 100 | + try: |
| 101 | + _host_bindings_mlir = __import__(f"{mlir_python_package_prefix}._mlir_libs") |
| 102 | + except (ImportError, ModuleNotFoundError) as e: |
| 103 | + print(f"couldn't import {mlir_python_package_prefix=} due to: {e}") |
| 104 | + raise e |
| 105 | + |
| 106 | + with open(MLIR_PYTHON_PACKAGE_PREFIX_TOKEN_PATH, "w") as f: |
| 107 | + f.write(mlir_python_package_prefix) |
| 108 | + |
| 109 | + _add_file_to_sources_txt_file(MLIR_PYTHON_PACKAGE_PREFIX_TOKEN_PATH) |
| 110 | + |
| 111 | + alias_upstream_bindings() |
| 112 | + |
| 113 | + from .generate_trampolines import generate_all_upstream_trampolines |
| 114 | + |
| 115 | + generate_all_upstream_trampolines() |
0 commit comments