Skip to content

Commit

Permalink
Fix sdist installation (#3080)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
ddelange and zeroepoch committed Jul 14, 2023
1 parent 6e5bebd commit ba459b4
Showing 1 changed file with 85 additions and 22 deletions.
107 changes: 85 additions & 22 deletions python/packaging/frontend_sdist/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,103 @@
# limitations under the License.
#

import os
import subprocess
import sys

from setuptools import setup
from setuptools.command.install import install
import subprocess as sp

tensorrt_module = "##TENSORRT_MODULE##"
tensorrt_version = "##TENSORRT_PYTHON_VERSION##"
tensorrt_submodules = [
"{}_libs=={}".format(tensorrt_module, tensorrt_version),
"{}_bindings=={}".format(tensorrt_module, tensorrt_version),
]
nvidia_pip_index_url = os.environ.get("NVIDIA_PIP_INDEX_URL", "https://pypi.nvidia.com")
disable_internal_pip = os.environ.get("NVIDIA_TENSORRT_DISABLE_INTERNAL_PIP", False)


def run_pip_command(args, call_func):
try:
return call_func([sys.executable, "-m", "pip"] + args)
except subprocess.CalledProcessError:
return call_func([os.path.join(sys.exec_prefix, "bin", "pip")] + args)


class InstallCommand(install):
def run(self):
def install_dep(package_name):
status = sp.run(
[
sys.executable,
"-m",
"pip",
"install",
"{:}==##TENSORRT_PYTHON_VERSION##".format(package_name),
"--index-url",
"https://pypi.nvidia.com",
]
)
status.check_returncode()

install_dep("{:}_libs".format(tensorrt_module))
install_dep("{:}_bindings".format(tensorrt_module))

install.run(self)
# pip-inside-pip hack ref #3080
run_pip_command(
[
"install",
"--extra-index-url",
nvidia_pip_index_url,
*tensorrt_submodules,
],
subprocess.check_call,
)

super().run()


def pip_config_list():
"""Get the current pip config (env vars, config file, etc)."""
return run_pip_command(["config", "list"], subprocess.check_output).decode()


def parent_command_line():
"""Get the command line of the parent PID."""
pid = os.getppid()
# try retrieval using psutil
try:
import psutil

return " ".join(psutil.Process(pid).cmdline())
except ModuleNotFoundError:
pass
# fall back to shell
try:
return subprocess.check_output(
["ps", "-p", str(pid), "-o", "command", "--no-headers"]
).decode()
except subprocess.CalledProcessError:
return ""


# use pip-inside-pip hack only if the nvidia index is not set in the environment
if (
disable_internal_pip
or nvidia_pip_index_url in pip_config_list()
or nvidia_pip_index_url in parent_command_line()
):
install_requires = tensorrt_submodules
cmdclass = {}
else:
install_requires = []
cmdclass = {"install": InstallCommand}


setup(
name=tensorrt_module,
version="##TENSORRT_PYTHON_VERSION##",
version=tensorrt_version,
description="A high performance deep learning inference library",
long_description="A high performance deep learning inference library",
long_description="""A high performance deep learning inference library
To install, please execute the following:
```
pip install tensorrt --extra-index-url {}
```
Or add the index URL to the (space-separated) PIP_EXTRA_INDEX_URL environment variable:
```
export PIP_EXTRA_INDEX_URL='{}'
pip install tensorrt
```
When the extra index url does not contain `{}`, a nested `pip install` will run with the proper extra index url hard-coded.
""".format(
nvidia_pip_index_url, nvidia_pip_index_url, nvidia_pip_index_url
),
long_description_content_type="text/markdown",
author="NVIDIA Corporation",
license="Proprietary",
classifiers=[
Expand All @@ -59,12 +120,14 @@ def install_dep(package_name):
"Programming Language :: Python :: 3",
],
packages=[tensorrt_module],
install_requires=install_requires,
python_requires=">=3.6", # ref https://pypi.nvidia.com/tensorrt-bindings/
cmdclass=cmdclass,
extras_require={"numpy": "numpy"},
package_data={tensorrt_module: ["*.so*", "*.pyd", "*.pdb"]},
include_package_data=True,
zip_safe=True,
keywords="nvidia tensorrt deeplearning inference",
url="https://developer.nvidia.com/tensorrt",
download_url="https://github.com/nvidia/tensorrt/tags",
cmdclass={"install": InstallCommand},
)

0 comments on commit ba459b4

Please sign in to comment.