Skip to content

Commit ef1b18f

Browse files
Next stesp to improve readability and maintainability
1 parent 592253e commit ef1b18f

File tree

2 files changed

+60
-41
lines changed

2 files changed

+60
-41
lines changed

modules/nvidia_plugin/wheel/setup.py

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
OPENVINO_INSTALL_BUILD_DEPS_SCRIPT = "install_build_dependencies.sh"
4949

5050
openvino_src_dir: Optional[str] = None
51+
build_configuration_name: Optional[str] = None
52+
package_data: typing.Dict[str, list] = {}
5153

5254

5355
def is_tool(name):
@@ -206,9 +208,11 @@ def finalize_options(self):
206208
super().finalize_options()
207209

208210
global openvino_src_dir
211+
global build_configuration_name
212+
209213
self.git_exec = shutil.which("git")
210214
self.cmake_exec = shutil.which("cmake")
211-
self.build_configuration_name = 'Debug' if self.debug else 'Release'
215+
build_configuration_name = 'Debug' if self.debug else 'Release'
212216
self.nvidia_plugin_src_dir = os.path.abspath(os.path.dirname(__file__))
213217
self.build_lib_dir = os.path.join(self.nvidia_plugin_src_dir, "../build/lib")
214218
self.openvino_contrib_src_dir = os.path.normpath(os.path.join(self.nvidia_plugin_src_dir, "../../.."))
@@ -286,7 +290,7 @@ def configure_openvino_cmake(self):
286290
f'-DPYTHON_EXECUTABLE={sys.executable}',
287291
f'-DPYTHON_INCLUDE_DIR={python_include_dir}',
288292
'-DNGRAPH_PYTHON_BUILD_ENABLE=ON',
289-
f'-DCMAKE_BUILD_TYPE={self.build_configuration_name}',
293+
f'-DCMAKE_BUILD_TYPE={build_configuration_name}',
290294
f'-DOPENVINO_EXTRA_MODULES={self.openvino_contrib_src_dir}/modules/nvidia_plugin',
291295
'-DENABLE_WHEEL=ON',
292296
f'-DWHEEL_VERSION={WHEEL_VERSION}']
@@ -306,7 +310,7 @@ def build_openvino(self):
306310

307311
def get_build_env(self):
308312
build_env = os.environ.copy()
309-
build_env['BUILD_TYPE'] = self.build_configuration_name
313+
build_env['BUILD_TYPE'] = build_configuration_name
310314
build_env['BUILD_TARGETS'] = NVIDIA_PLUGIN_CMAKE_TARGET_NAME
311315
build_env['OPENVINO_HOME'] = self.openvino_src_dir
312316
build_env['OPENVINO_CONTRIB'] = self.openvino_contrib_src_dir
@@ -367,53 +371,67 @@ def locate_built_lib(self):
367371
set_rpath(LIBS_RPATH, os.path.realpath(path))
368372

369373

370-
class InstallCMakeLib(install_lib, build_clib):
371-
def initialize_options(self):
372-
install_lib.initialize_options(self)
373-
build_clib.initialize_options(self)
374-
374+
class InstallLib(install_lib):
375375
def finalize_options(self):
376376
install_lib.finalize_options(self)
377-
build_clib.finalize_options(self)
378377

379378
self.git_exec = shutil.which("git")
380379
self.force = None
381-
self.deps_dir = os.path.abspath(os.path.join(self.build_temp, "deps"))
382380
self.set_undefined_options('install', ('force', 'force'))
381+
print(f"self.force = {self.force}")
383382

384383
def run(self):
384+
openvino_nvidia_gpu_library = f'{openvino_src_dir}/bin/intel64/{build_configuration_name}/lib/libopenvino_nvidia_gpu_plugin.{platform_specifics.get_lib_file_extension()}'
385+
package_data.update({
386+
'': [openvino_nvidia_gpu_library]
387+
})
388+
389+
self.build()
390+
self.install()
391+
385392
try:
386-
self.build()
387-
self.install()
388-
self.install_openvino_package_and_other_dependencies()
393+
if self.force:
394+
self.install_openvino_package()
395+
self.install_python_dependencies()
389396
self.register_nvidia_plugin()
390397
self.test_nvidia_plugin()
391-
openvino_nvidia_gpu_library = f'{openvino_src_dir}/bin/intel64/Release/lib/libopenvino_nvidia_gpu_plugin.{platform_specifics.get_lib_file_extension()}'
392-
package_data.update({
393-
'': [openvino_nvidia_gpu_library]
394-
})
395-
finally:
398+
except Exception as ex:
396399
self.unregister_nvidia_plugin()
397400

398-
def install_openvino_package_and_other_dependencies(self):
399-
if self.force:
400-
py_tag=tags.interpreter_name() + tags.interpreter_version()
401-
abi_tag=wheel.bdist_wheel.get_abi_tag()
402-
platform_tag=next(tags.platform_tags())
403-
git_commits=get_command_output([self.git_exec, 'rev-list', '--count', '--first-parent', 'HEAD'],
404-
cwd=openvino_src_dir, on_fail_msg='Failed to count OpenVINO commits').strip()
405-
openvino_wheel_name="-".join(["openvino", WHEEL_VERSION, git_commits, py_tag, abi_tag, platform_tag]) + ".whl"
406-
wheels_path = os.path.abspath(os.path.join(openvino_src_dir, "build/wheels", openvino_wheel_name))
407-
self.announce(f"Installing OpenVINO package with {wheels_path}", level=3)
408-
openvino_install_py = create_pip_command(wheels_path)
409-
run_command(openvino_install_py,
410-
on_fail_msg=f'Failed to install OpenVINO wheel package with {wheels_path}')
411-
401+
def install_openvino_package(self):
402+
py_tag=tags.interpreter_name() + tags.interpreter_version()
403+
abi_tag=wheel.bdist_wheel.get_abi_tag()
404+
platform_tag=next(tags.platform_tags())
405+
git_commits=get_command_output([self.git_exec, 'rev-list', '--count', '--first-parent', 'HEAD'],
406+
cwd=openvino_src_dir,
407+
on_fail_msg='Failed to count OpenVINO commits').strip()
408+
openvino_wheel_name="-".join(["openvino", WHEEL_VERSION, git_commits, py_tag, abi_tag, platform_tag]) + ".whl"
409+
wheels_path = os.path.abspath(os.path.join(openvino_src_dir, "build/wheels", openvino_wheel_name))
410+
self.announce(f"Installing OpenVINO package with {wheels_path}", level=3)
411+
openvino_install_py = create_pip_command(wheels_path)
412+
run_command(openvino_install_py,
413+
on_fail_msg=f'Failed to install OpenVINO wheel package with {wheels_path}')
414+
415+
def install_python_dependencies(self):
412416
path_to_requirements_txt = os.path.join(os.path.abspath(os.path.dirname(__file__)), "requirements.txt")
413417
requirements_py = create_pip_command('-r', path_to_requirements_txt)
414418
run_command(requirements_py,
415419
on_fail_msg=f'Failed to install dependencies from {path_to_requirements_txt}')
416420

421+
def check_plugins_xml_if_exists(self):
422+
openvino_package_libs_dir = self.get_openvino_package_dir()
423+
xml_file = os.path.join(openvino_package_libs_dir, "plugins.xml")
424+
if not os.path.exists(xml_file):
425+
plugins_xml_src = dedent('''\
426+
<ie>
427+
<plugins>
428+
</plugins>
429+
</ie>
430+
''')
431+
tree = ET.fromstring(plugins_xml_src)
432+
with open(xml_file, "w") as f:
433+
f.write(ET.tostring(tree).decode('utf8'))
434+
417435
def update_plugins_xml(self, xml_file, openvino_nvidia_gpu_library):
418436
if not os.path.exists(xml_file):
419437
plugins_xml_src = dedent('''\
@@ -438,14 +456,17 @@ def get_openvino_package_dir(self):
438456
openvino_package_libs_dir = os.path.join(openvino_package_dir, "libs")
439457
return openvino_package_libs_dir
440458

459+
def get_openvino_nvidia_lib_path(self):
460+
import openvino_nvidia
461+
openvino_nvidia_package_dir = os.path.dirname(os.path.abspath(openvino_nvidia.__file__))
462+
openvino_nvidia_gpu_library = f'{openvino_nvidia_package_dir}/libopenvino_nvidia_gpu_plugin.{platform_specifics.get_lib_file_extension()}'
463+
return openvino_nvidia_gpu_library
464+
441465
def register_nvidia_plugin(self):
466+
self.check_plugins_xml_if_exists()
442467
openvino_package_libs_dir = self.get_openvino_package_dir()
443-
self.copy_tree(self.build_dir, openvino_package_libs_dir)
444-
openvino_nvidia_gpu_library = os.path.join(openvino_package_libs_dir,
445-
f"libopenvino_nvidia_gpu_plugin.{platform_specifics.get_lib_file_extension()}")
446-
468+
openvino_nvidia_gpu_library = self.get_openvino_nvidia_lib_path()
447469
xml_file = os.path.join(openvino_package_libs_dir, "plugins.xml")
448-
449470
self.update_plugins_xml(xml_file, openvino_nvidia_gpu_library)
450471

451472
def unregister_nvidia_plugin(self):
@@ -508,8 +529,6 @@ def test_nvidia_plugin(self):
508529
f'Possible ABI version mismatch. {recommendations_msg}') from e
509530

510531

511-
package_data: typing.Dict[str, list] = {}
512-
513532
setup(
514533
version=config('WHEEL_VERSION', WHEEL_VERSION),
515534
author_email=config('WHEEL_AUTHOR_EMAIL', '[email protected]'),
@@ -525,12 +544,12 @@ def test_nvidia_plugin(self):
525544
libraries=[(PACKAGE_NAME, {'sources': []})],
526545
packages=["openvino_nvidia"],
527546
package_dir={
528-
"openvino_nvidia": f"{os.path.abspath(os.path.dirname(__file__))}/openvino_nvidia",
547+
"openvino_nvidia": f"{os.path.abspath(os.path.dirname(__file__))}/packages/openvino_nvidia",
529548
},
530549
package_data=package_data,
531550
cmdclass={
532551
'build_clib': BuildCMakeLib,
533-
'install_lib': InstallCMakeLib,
552+
'install_lib': InstallLib,
534553
},
535554
zip_safe=False,
536555
)

0 commit comments

Comments
 (0)