install.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import subprocess
  16. import sys
  17. import tempfile
  18. from packaging.requirements import Requirement
  19. from . import logging
  20. def install_packages_from_requirements_file(
  21. requirements_file_path,
  22. pip_install_opts=None,
  23. constraints="base",
  24. ):
  25. from .deps import BASE_DEP_SPECS, REQUIRED_DEP_SPECS
  26. if constraints not in ("base", "required", "none"):
  27. raise ValueError(f"Invalid constraints setting: {constraints}")
  28. args = [
  29. sys.executable,
  30. "-m",
  31. "pip",
  32. "install",
  33. *(pip_install_opts or []),
  34. "-r",
  35. requirements_file_path,
  36. ]
  37. if constraints == "base":
  38. dep_specs = BASE_DEP_SPECS
  39. elif constraints == "required":
  40. dep_specs = REQUIRED_DEP_SPECS
  41. else:
  42. dep_specs = None
  43. if dep_specs:
  44. # TODO: Precompute or cache the constraints
  45. with tempfile.NamedTemporaryFile("w", suffix=".txt", delete=False) as f:
  46. for reqs in dep_specs.values():
  47. for req in reqs:
  48. req = Requirement(req)
  49. if req.marker and not req.marker.evaluate():
  50. continue
  51. if req.url:
  52. req = f"{req.name}@{req.url}"
  53. else:
  54. req = f"{req.name}{req.specifier}"
  55. f.write(req + "\n")
  56. constraints_file_path = f.name
  57. args.extend(["-c", constraints_file_path])
  58. logging.debug("Command: %s", args)
  59. try:
  60. return subprocess.check_call(args)
  61. finally:
  62. os.unlink(constraints_file_path)
  63. def install_packages(requirements, pip_install_opts=None, constraints="base"):
  64. with tempfile.NamedTemporaryFile("w", suffix=".txt", delete=False) as f:
  65. for req in requirements:
  66. f.write(req + "\n")
  67. reqs_file_path = f.name
  68. try:
  69. return install_packages_from_requirements_file(
  70. reqs_file_path,
  71. pip_install_opts=pip_install_opts,
  72. constraints=constraints,
  73. )
  74. finally:
  75. os.unlink(reqs_file_path)
  76. def uninstall_packages(packages, pip_uninstall_opts=None):
  77. args = [
  78. sys.executable,
  79. "-m",
  80. "pip",
  81. "uninstall",
  82. "-y",
  83. *(pip_uninstall_opts or []),
  84. *packages,
  85. ]
  86. logging.debug("Command: %s", args)
  87. return subprocess.check_call(args)