Skip to content

Commit c60696a

Browse files
committed
Add post intall action to patch holoscan package, v2.7 and 2.8
Signed-off-by: M Q <[email protected]>
1 parent 9608cd7 commit c60696a

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

requirements-dev.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pyflakes
1212
black
1313
isort
1414
pytype>=2020.6.1; platform_system != "Windows"
15-
types-pkg_resources
1615
mypy>=0.790
1716
psutil
1817
Sphinx==4.1.2

setup.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,82 @@
99
# See the License for the specific language governing permissions and
1010
# limitations under the License.
1111

12+
13+
import atexit
1214
import site
1315
import sys
1416

1517
from setuptools import find_namespace_packages, setup
18+
from setuptools.command.install import install
1619

1720
import versioneer
1821

1922
# Workaround for editable installs with system's Python venv.
2023
# error: can't create or remove files in install directory
2124
# (https://github.com/pypa/pip/issues/7953#issuecomment-645133255)
2225
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.")
2384

2485
setup(
2586
version=versioneer.get_version(),
26-
cmdclass=versioneer.get_cmdclass(),
87+
cmdclass=versioneer.get_cmdclass({'install': PostInstallCommand}),
2788
packages=find_namespace_packages(include=["monai.*"]),
2889
include_package_data=True,
2990
zip_safe=False,

0 commit comments

Comments
 (0)