|
9 | 9 | # See the License for the specific language governing permissions and |
10 | 10 | # limitations under the License. |
11 | 11 |
|
| 12 | + |
| 13 | +import atexit |
12 | 14 | import site |
13 | 15 | import sys |
14 | 16 |
|
15 | 17 | from setuptools import find_namespace_packages, setup |
| 18 | +from setuptools.command.install import install |
16 | 19 |
|
17 | 20 | import versioneer |
18 | 21 |
|
19 | 22 | # Workaround for editable installs with system's Python venv. |
20 | 23 | # error: can't create or remove files in install directory |
21 | 24 | # (https://github.com/pypa/pip/issues/7953#issuecomment-645133255) |
22 | 25 | site.ENABLE_USER_SITE = "--user" in sys.argv[1:] |
| 26 | +class PostInstallCommand(install): |
| 27 | + """Contains post install actions.""" |
| 28 | + |
| 29 | + def __init__(self, *args, **kwargs): |
| 30 | + super(PostInstallCommand, self).__init__(*args, **kwargs) |
| 31 | + atexit.register(PostInstallCommand.patch_holoscan) |
| 32 | + |
| 33 | + @staticmethod |
| 34 | + def patch_holoscan(): |
| 35 | + """ Patch Holoscan for its known issue of missing one import.""" |
| 36 | + |
| 37 | + import importlib.util |
| 38 | + from pathlib import Path |
| 39 | + |
| 40 | + def needed_to_patch(): |
| 41 | + from importlib.metadata import version |
| 42 | + needed = False |
| 43 | + try: |
| 44 | + version = version("holoscan") |
| 45 | + # This issue exists in the following versions |
| 46 | + if "2.7" in version or "2.8" in version: |
| 47 | + print("Need to patch holoscan v2.7 and 2.8.") |
| 48 | + needed = True |
| 49 | + except Exception: |
| 50 | + pass |
| 51 | + |
| 52 | + return needed |
| 53 | + |
| 54 | + if not needed_to_patch(): |
| 55 | + return |
| 56 | + |
| 57 | + spec = importlib.util.find_spec("holoscan") |
| 58 | + if spec: |
| 59 | + # holoscan core misses one class in its import in __init__.py |
| 60 | + module_to_add = " MultiMessageConditionInfo," |
| 61 | + module_path = Path(str(spec.origin)).joinpath('core') |
| 62 | + if module_path.exists(): |
| 63 | + lines_r = [] |
| 64 | + existed = False |
| 65 | + with module_path.open("r") as f_to_patch: |
| 66 | + block_begin = False |
| 67 | + for line_r in f_to_patch.readline(): |
| 68 | + if "from ._core import (" in line_r: |
| 69 | + block_begin = True |
| 70 | + elif block_begin and module_to_add.strip() in line_r: |
| 71 | + existed = True |
| 72 | + break |
| 73 | + elif block_begin and ")" in line_r: |
| 74 | + # Need to add the missing class. |
| 75 | + line_r = f"{module_to_add}\n{line_r}" |
| 76 | + print("Added missing module in holoscan.") |
| 77 | + |
| 78 | + lines_r.append(line_r) |
| 79 | + |
| 80 | + if not existed: |
| 81 | + with module_path.open("w") as f_w: |
| 82 | + f_w.writelines(lines_r) |
| 83 | + print("Completed patching holoscan.") |
23 | 84 |
|
24 | 85 | setup( |
25 | 86 | version=versioneer.get_version(), |
26 | | - cmdclass=versioneer.get_cmdclass(), |
| 87 | + cmdclass=versioneer.get_cmdclass({'install': PostInstallCommand}), |
27 | 88 | packages=find_namespace_packages(include=["monai.*"]), |
28 | 89 | include_package_data=True, |
29 | 90 | zip_safe=False, |
|
0 commit comments