Skip to content

Commit ba459b4

Browse files
ddelangezeroepoch
andauthored
Fix sdist installation (#3080)
* Fix sdist installation Signed-off-by: ddelange <[email protected]> * Unify project description with tensorrt_libs Signed-off-by: ddelange <[email protected]> * Switch requirements to PEP440 direct references https://peps.python.org/pep-0440/#direct-references Signed-off-by: ddelange <[email protected]> * Revert "Switch requirements to PEP440 direct references" This reverts commit 5334f0d. ref #3080 (comment) Signed-off-by: ddelange <[email protected]> * Make pip-inside-pip more robust, avoid it when possible Signed-off-by: ddelange <[email protected]> * Add parent_command_line check Signed-off-by: ddelange <[email protected]> * Use pip config list to detect index url Signed-off-by: ddelange <[email protected]> * PR Suggestion Co-authored-by: Eric Work <[email protected]> Signed-off-by: ddelange <[email protected]> * Add --no-headers to ps command Signed-off-by: ddelange <[email protected]> * Introduce NVIDIA_PIP_INDEX_URL env var Signed-off-by: ddelange <[email protected]> * Typo in description Signed-off-by: ddelange <[email protected]> * Run black Signed-off-by: ddelange <[email protected]> * Replace f-string with .format, add python_requires Signed-off-by: ddelange <[email protected]> * Introduce NVIDIA_TENSORRT_DISABLE_INTERNAL_PIP env var Signed-off-by: ddelange <[email protected]> * PR Suggestion Signed-off-by: ddelange <[email protected]> --------- Signed-off-by: ddelange <[email protected]> Co-authored-by: Eric Work <[email protected]>
1 parent 6e5bebd commit ba459b4

File tree

1 file changed

+85
-22
lines changed

1 file changed

+85
-22
lines changed

python/packaging/frontend_sdist/setup.py

Lines changed: 85 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,103 @@
1515
# limitations under the License.
1616
#
1717

18+
import os
19+
import subprocess
1820
import sys
1921

2022
from setuptools import setup
2123
from setuptools.command.install import install
22-
import subprocess as sp
2324

2425
tensorrt_module = "##TENSORRT_MODULE##"
26+
tensorrt_version = "##TENSORRT_PYTHON_VERSION##"
27+
tensorrt_submodules = [
28+
"{}_libs=={}".format(tensorrt_module, tensorrt_version),
29+
"{}_bindings=={}".format(tensorrt_module, tensorrt_version),
30+
]
31+
nvidia_pip_index_url = os.environ.get("NVIDIA_PIP_INDEX_URL", "https://pypi.nvidia.com")
32+
disable_internal_pip = os.environ.get("NVIDIA_TENSORRT_DISABLE_INTERNAL_PIP", False)
33+
34+
35+
def run_pip_command(args, call_func):
36+
try:
37+
return call_func([sys.executable, "-m", "pip"] + args)
38+
except subprocess.CalledProcessError:
39+
return call_func([os.path.join(sys.exec_prefix, "bin", "pip")] + args)
2540

2641

2742
class InstallCommand(install):
2843
def run(self):
29-
def install_dep(package_name):
30-
status = sp.run(
31-
[
32-
sys.executable,
33-
"-m",
34-
"pip",
35-
"install",
36-
"{:}==##TENSORRT_PYTHON_VERSION##".format(package_name),
37-
"--index-url",
38-
"https://pypi.nvidia.com",
39-
]
40-
)
41-
status.check_returncode()
42-
43-
install_dep("{:}_libs".format(tensorrt_module))
44-
install_dep("{:}_bindings".format(tensorrt_module))
45-
46-
install.run(self)
44+
# pip-inside-pip hack ref #3080
45+
run_pip_command(
46+
[
47+
"install",
48+
"--extra-index-url",
49+
nvidia_pip_index_url,
50+
*tensorrt_submodules,
51+
],
52+
subprocess.check_call,
53+
)
54+
55+
super().run()
56+
57+
58+
def pip_config_list():
59+
"""Get the current pip config (env vars, config file, etc)."""
60+
return run_pip_command(["config", "list"], subprocess.check_output).decode()
61+
62+
63+
def parent_command_line():
64+
"""Get the command line of the parent PID."""
65+
pid = os.getppid()
66+
# try retrieval using psutil
67+
try:
68+
import psutil
69+
70+
return " ".join(psutil.Process(pid).cmdline())
71+
except ModuleNotFoundError:
72+
pass
73+
# fall back to shell
74+
try:
75+
return subprocess.check_output(
76+
["ps", "-p", str(pid), "-o", "command", "--no-headers"]
77+
).decode()
78+
except subprocess.CalledProcessError:
79+
return ""
80+
81+
82+
# use pip-inside-pip hack only if the nvidia index is not set in the environment
83+
if (
84+
disable_internal_pip
85+
or nvidia_pip_index_url in pip_config_list()
86+
or nvidia_pip_index_url in parent_command_line()
87+
):
88+
install_requires = tensorrt_submodules
89+
cmdclass = {}
90+
else:
91+
install_requires = []
92+
cmdclass = {"install": InstallCommand}
4793

4894

4995
setup(
5096
name=tensorrt_module,
51-
version="##TENSORRT_PYTHON_VERSION##",
97+
version=tensorrt_version,
5298
description="A high performance deep learning inference library",
53-
long_description="A high performance deep learning inference library",
99+
long_description="""A high performance deep learning inference library
100+
101+
To install, please execute the following:
102+
```
103+
pip install tensorrt --extra-index-url {}
104+
```
105+
Or add the index URL to the (space-separated) PIP_EXTRA_INDEX_URL environment variable:
106+
```
107+
export PIP_EXTRA_INDEX_URL='{}'
108+
pip install tensorrt
109+
```
110+
When the extra index url does not contain `{}`, a nested `pip install` will run with the proper extra index url hard-coded.
111+
""".format(
112+
nvidia_pip_index_url, nvidia_pip_index_url, nvidia_pip_index_url
113+
),
114+
long_description_content_type="text/markdown",
54115
author="NVIDIA Corporation",
55116
license="Proprietary",
56117
classifiers=[
@@ -59,12 +120,14 @@ def install_dep(package_name):
59120
"Programming Language :: Python :: 3",
60121
],
61122
packages=[tensorrt_module],
123+
install_requires=install_requires,
124+
python_requires=">=3.6", # ref https://pypi.nvidia.com/tensorrt-bindings/
125+
cmdclass=cmdclass,
62126
extras_require={"numpy": "numpy"},
63127
package_data={tensorrt_module: ["*.so*", "*.pyd", "*.pdb"]},
64128
include_package_data=True,
65129
zip_safe=True,
66130
keywords="nvidia tensorrt deeplearning inference",
67131
url="https://developer.nvidia.com/tensorrt",
68132
download_url="https://github.com/nvidia/tensorrt/tags",
69-
cmdclass={"install": InstallCommand},
70133
)

0 commit comments

Comments
 (0)