pylock.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from collections.abc import Iterable
  2. from pathlib import Path
  3. from pip._vendor.packaging.pylock import (
  4. Package,
  5. PackageArchive,
  6. PackageDirectory,
  7. PackageSdist,
  8. PackageVcs,
  9. PackageWheel,
  10. Pylock,
  11. )
  12. from pip._vendor.packaging.version import Version
  13. from pip._internal.models.direct_url import ArchiveInfo, DirInfo, VcsInfo
  14. from pip._internal.models.link import Link
  15. from pip._internal.req.req_install import InstallRequirement
  16. from pip._internal.utils.urls import url_to_path
  17. def _pylock_package_from_install_requirement(
  18. ireq: InstallRequirement, base_dir: Path
  19. ) -> Package:
  20. base_dir = base_dir.resolve()
  21. dist = ireq.get_dist()
  22. download_info = ireq.download_info
  23. assert download_info
  24. package_version = None
  25. package_vcs = None
  26. package_directory = None
  27. package_archive = None
  28. package_sdist = None
  29. package_wheels = None
  30. if ireq.is_direct:
  31. if isinstance(download_info.info, VcsInfo):
  32. package_vcs = PackageVcs(
  33. type=download_info.info.vcs,
  34. url=download_info.url,
  35. path=None,
  36. requested_revision=download_info.info.requested_revision,
  37. commit_id=download_info.info.commit_id,
  38. subdirectory=download_info.subdirectory,
  39. )
  40. elif isinstance(download_info.info, DirInfo):
  41. package_directory = PackageDirectory(
  42. path=(
  43. Path(url_to_path(download_info.url))
  44. .resolve()
  45. .relative_to(base_dir)
  46. .as_posix()
  47. ),
  48. editable=(
  49. download_info.info.editable if download_info.info.editable else None
  50. ),
  51. subdirectory=download_info.subdirectory,
  52. )
  53. elif isinstance(download_info.info, ArchiveInfo):
  54. if not download_info.info.hashes:
  55. raise NotImplementedError()
  56. package_archive = PackageArchive(
  57. url=download_info.url,
  58. path=None,
  59. hashes=download_info.info.hashes,
  60. subdirectory=download_info.subdirectory,
  61. )
  62. else:
  63. # should never happen
  64. raise NotImplementedError()
  65. else:
  66. package_version = dist.version
  67. if isinstance(download_info.info, ArchiveInfo):
  68. if not download_info.info.hashes:
  69. raise NotImplementedError()
  70. link = Link(download_info.url)
  71. if link.is_wheel:
  72. package_wheels = [
  73. PackageWheel(
  74. name=link.filename,
  75. url=download_info.url,
  76. hashes=download_info.info.hashes,
  77. )
  78. ]
  79. else:
  80. package_sdist = PackageSdist(
  81. name=link.filename,
  82. url=download_info.url,
  83. hashes=download_info.info.hashes,
  84. )
  85. else:
  86. # should never happen
  87. raise NotImplementedError()
  88. return Package(
  89. name=dist.canonical_name,
  90. version=package_version,
  91. vcs=package_vcs,
  92. directory=package_directory,
  93. archive=package_archive,
  94. sdist=package_sdist,
  95. wheels=package_wheels,
  96. )
  97. def pylock_from_install_requirements(
  98. install_requirements: Iterable[InstallRequirement], base_dir: Path
  99. ) -> Pylock:
  100. return Pylock(
  101. lock_version=Version("1.0"),
  102. created_by="pip",
  103. packages=sorted(
  104. (
  105. _pylock_package_from_install_requirement(ireq, base_dir)
  106. for ireq in install_requirements
  107. ),
  108. key=lambda p: p.name,
  109. ),
  110. )