python-inspector 0.15.2__tar.gz → 0.15.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. {python_inspector-0.15.2 → python_inspector-0.15.4}/CHANGELOG.rst +25 -0
  2. {python_inspector-0.15.2 → python_inspector-0.15.4}/PKG-INFO +3 -3
  3. {python_inspector-0.15.2 → python_inspector-0.15.4}/README.rst +2 -2
  4. {python_inspector-0.15.2 → python_inspector-0.15.4}/setup.cfg +1 -1
  5. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/api.py +26 -2
  6. python_inspector-0.15.4/src/python_inspector/pylock_toml.py +146 -0
  7. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/resolution.py +240 -14
  8. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/resolve_cli.py +15 -2
  9. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/utils.py +37 -0
  10. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/utils_pypi.py +43 -1
  11. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector.egg-info/PKG-INFO +3 -3
  12. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector.egg-info/SOURCES.txt +3 -0
  13. {python_inspector-0.15.2 → python_inspector-0.15.4}/tests/test_api.py +14 -0
  14. {python_inspector-0.15.2 → python_inspector-0.15.4}/tests/test_cli.py +44 -2
  15. python_inspector-0.15.4/tests/test_pylock.py +82 -0
  16. python_inspector-0.15.4/tests/test_sdist_extract.py +281 -0
  17. {python_inspector-0.15.2 → python_inspector-0.15.4}/tests/test_utils.py +23 -0
  18. {python_inspector-0.15.2 → python_inspector-0.15.4}/tests/test_utils_pypi.py +89 -0
  19. {python_inspector-0.15.2 → python_inspector-0.15.4}/.gitignore +0 -0
  20. {python_inspector-0.15.2 → python_inspector-0.15.4}/.readthedocs.yml +0 -0
  21. {python_inspector-0.15.2 → python_inspector-0.15.4}/AUTHORS.rst +0 -0
  22. {python_inspector-0.15.2 → python_inspector-0.15.4}/CODE_OF_CONDUCT.rst +0 -0
  23. {python_inspector-0.15.2 → python_inspector-0.15.4}/MANIFEST.in +0 -0
  24. {python_inspector-0.15.2 → python_inspector-0.15.4}/Makefile +0 -0
  25. {python_inspector-0.15.2 → python_inspector-0.15.4}/NOTICE +0 -0
  26. {python_inspector-0.15.2 → python_inspector-0.15.4}/apache-2.0.LICENSE +0 -0
  27. {python_inspector-0.15.2 → python_inspector-0.15.4}/azure-pipelines.yml +0 -0
  28. {python_inspector-0.15.2 → python_inspector-0.15.4}/configure +0 -0
  29. {python_inspector-0.15.2 → python_inspector-0.15.4}/configure.bat +0 -0
  30. {python_inspector-0.15.2 → python_inspector-0.15.4}/docs/Makefile +0 -0
  31. {python_inspector-0.15.2 → python_inspector-0.15.4}/docs/make.bat +0 -0
  32. {python_inspector-0.15.2 → python_inspector-0.15.4}/docs/source/_static/theme_overrides.css +0 -0
  33. {python_inspector-0.15.2 → python_inspector-0.15.4}/docs/source/conf.py +0 -0
  34. {python_inspector-0.15.2 → python_inspector-0.15.4}/docs/source/contribute/contrib_doc.rst +0 -0
  35. {python_inspector-0.15.2 → python_inspector-0.15.4}/docs/source/dependencies-design.rst +0 -0
  36. {python_inspector-0.15.2 → python_inspector-0.15.4}/docs/source/index.rst +0 -0
  37. {python_inspector-0.15.2 → python_inspector-0.15.4}/docs/source/test-protocol.rst +0 -0
  38. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/ci/azure-container-deb.yml +0 -0
  39. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/ci/azure-container-rpm.yml +0 -0
  40. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/ci/azure-posix.yml +0 -0
  41. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/ci/azure-win.yml +0 -0
  42. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/ci/install_sudo.sh +0 -0
  43. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/ci/macports-ci +0 -0
  44. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/ci/macports-ci.ABOUT +0 -0
  45. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/ci/mit.LICENSE +0 -0
  46. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/README.rst +0 -0
  47. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/check_thirdparty.py +0 -0
  48. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/fetch_thirdparty.py +0 -0
  49. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/gen_pypi_simple.py +0 -0
  50. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/gen_pypi_simple.py.ABOUT +0 -0
  51. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/gen_pypi_simple.py.NOTICE +0 -0
  52. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/gen_requirements.py +0 -0
  53. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/gen_requirements_dev.py +0 -0
  54. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/requirements.txt +0 -0
  55. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/test_utils_pip_compatibility_tags.py +0 -0
  56. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/test_utils_pip_compatibility_tags.py.ABOUT +0 -0
  57. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/test_utils_pypi_supported_tags.py +0 -0
  58. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/test_utils_pypi_supported_tags.py.ABOUT +0 -0
  59. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/update_skeleton.py +0 -0
  60. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/utils_dejacode.py +0 -0
  61. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/utils_pip_compatibility_tags.py +0 -0
  62. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/utils_pip_compatibility_tags.py.ABOUT +0 -0
  63. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/utils_pypi_supported_tags.py +0 -0
  64. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/utils_pypi_supported_tags.py.ABOUT +0 -0
  65. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/utils_requirements.py +0 -0
  66. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/utils_thirdparty.py +0 -0
  67. {python_inspector-0.15.2 → python_inspector-0.15.4}/etc/scripts/utils_thirdparty.py.ABOUT +0 -0
  68. {python_inspector-0.15.2 → python_inspector-0.15.4}/pyproject.toml +0 -0
  69. {python_inspector-0.15.2 → python_inspector-0.15.4}/requirements-dev.txt +0 -0
  70. {python_inspector-0.15.2 → python_inspector-0.15.4}/requirements.txt +0 -0
  71. {python_inspector-0.15.2 → python_inspector-0.15.4}/requirements_builder.ABOUT +0 -0
  72. {python_inspector-0.15.2 → python_inspector-0.15.4}/setup.py +0 -0
  73. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/_packagedcode/__init__.py +0 -0
  74. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/_packagedcode/models.py +0 -0
  75. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/_packagedcode/pypi.py +0 -0
  76. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/_packagedcode/pypi.py.ABOUT +0 -0
  77. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/_packagedcode/pypi.py.NOTICE +0 -0
  78. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/_packagedcode/pypi_setup_py.py +0 -0
  79. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/_packagedcode/pypi_setup_py.py.ABOUT +0 -0
  80. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/_packagedcode/pypi_setup_py.py.LICENSE +0 -0
  81. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/_packagedcode/utils.py +0 -0
  82. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/__init__.py +0 -0
  83. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/cli_utils.py +0 -0
  84. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/dependencies.py +0 -0
  85. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/error.py +0 -0
  86. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/lockfile.py +0 -0
  87. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/package_data.py +0 -0
  88. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/resolution.py.ABOUT +0 -0
  89. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/resolution.py.LICENSE +0 -0
  90. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/settings.py +0 -0
  91. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/setup_py_live_eval.py +0 -0
  92. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/setup_py_live_eval.py.ABOUT +0 -0
  93. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/setup_py_live_eval.py.LICENSE +0 -0
  94. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/utils_pip_compatibility_tags.py +0 -0
  95. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/utils_pip_compatibility_tags.py.ABOUT +0 -0
  96. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/utils_pypi.py.ABOUT +0 -0
  97. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/utils_pypi_supported_tags.py +0 -0
  98. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector/utils_pypi_supported_tags.py.ABOUT +0 -0
  99. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector.egg-info/dependency_links.txt +0 -0
  100. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector.egg-info/entry_points.txt +0 -0
  101. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector.egg-info/not-zip-safe +0 -0
  102. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector.egg-info/requires.txt +0 -0
  103. {python_inspector-0.15.2 → python_inspector-0.15.4}/src/python_inspector.egg-info/top_level.txt +0 -0
  104. {python_inspector-0.15.2 → python_inspector-0.15.4}/tests/test_packagecode_pypi.py +0 -0
  105. {python_inspector-0.15.2 → python_inspector-0.15.4}/tests/test_pypi.py +0 -0
  106. {python_inspector-0.15.2 → python_inspector-0.15.4}/tests/test_requirement_parsing.py +0 -0
  107. {python_inspector-0.15.2 → python_inspector-0.15.4}/tests/test_resolution.py +0 -0
  108. {python_inspector-0.15.2 → python_inspector-0.15.4}/tests/test_setup_py_live_eval.py +0 -0
  109. {python_inspector-0.15.2 → python_inspector-0.15.4}/tests/test_setup_py_parsing.py +0 -0
  110. {python_inspector-0.15.2 → python_inspector-0.15.4}/tests/test_utils_pip_compatibility_tags.py +0 -0
  111. {python_inspector-0.15.2 → python_inspector-0.15.4}/tests/test_utils_pypi_supported_tags.py +0 -0
@@ -1,6 +1,31 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
+ v0.15.4
5
+ -----------
6
+ - Stop sending private-index credentials to wheel hosts that are not the index.
7
+ Netrc credentials were chosen for the index hostname and stored on the
8
+ repository. Wheel downloads then copied those credentials onto every linked
9
+ wheel, and the HTTP helper sent them as Basic authentication. A private feed
10
+ can link a wheel on another host, such as a public mirror or CDN, so that
11
+ host received the index token on a new request. Credentials are now attached
12
+ only when the wheel URL has the same origin as the index URL: scheme,
13
+ hostname, and port. A wheel served by the index itself still receives them.
14
+
15
+ v0.15.3
16
+ -----------
17
+ - Add support for PEP 751 pylock.toml lock files as input.
18
+ - Stop sdist extraction from writing outside the cache.
19
+ ``tarfile.extractall()`` was called with no filter. On Python 3.13 the default
20
+ tar policy still extracts a member whose name is an absolute path, so a
21
+ crafted sdist could change a file outside the cache while resolution
22
+ succeeded. The stdlib ``data`` filter keeps that member by stripping the
23
+ leading slash. Extraction now uses one restrictive filter on every supported
24
+ interpreter. A member is rejected when its resolved destination leaves a
25
+ fresh extraction directory, and so are symbolic and hard links that point
26
+ outside that directory. The package directory is checked again before
27
+ ``setup.py``, ``setup.cfg``, or ``requirements.txt`` are read.
28
+
4
29
  v0.15.2
5
30
  -----------
6
31
  - Correct the version number in the CLI and setup.cfg.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-inspector
3
- Version: 0.15.2
3
+ Version: 0.15.4
4
4
  Summary: python-inspector is is a collection of utilities to collect PyPI package metadata and resolve packages dependencies.
5
5
  Home-page: https://github.com/aboutcode-org/python-inspector
6
6
  Author: nexB. Inc. and others
@@ -67,8 +67,8 @@ python-inspector - inspect Python package dependencies and metadata
67
67
 
68
68
  - resolve PyPI packages dependencies
69
69
 
70
- - parse various requirements.txt files and setup.py files as input
71
- for resolving dependencies.
70
+ - parse various requirements.txt files, setup.py files and PEP 751
71
+ pylock.toml lock files as input for resolving dependencies.
72
72
 
73
73
  - parse various manifests and packages files such as
74
74
  Pipfile, pyproject.toml, poetry.lock and setup.cfg and legacy and
@@ -7,8 +7,8 @@ python-inspector - inspect Python package dependencies and metadata
7
7
 
8
8
  - resolve PyPI packages dependencies
9
9
 
10
- - parse various requirements.txt files and setup.py files as input
11
- for resolving dependencies.
10
+ - parse various requirements.txt files, setup.py files and PEP 751
11
+ pylock.toml lock files as input for resolving dependencies.
12
12
 
13
13
  - parse various manifests and packages files such as
14
14
  Pipfile, pyproject.toml, poetry.lock and setup.cfg and legacy and
@@ -1,7 +1,7 @@
1
1
  [metadata]
2
2
  name = python-inspector
3
3
  license = Apache-2.0
4
- version = 0.15.2
4
+ version = 0.15.4
5
5
  description = python-inspector is is a collection of utilities to collect PyPI package metadata and resolve packages dependencies.
6
6
  long_description = file:README.rst
7
7
  long_description_content_type = text/x-rst
@@ -30,6 +30,7 @@ from _packagedcode.pypi import can_process_dependent_package
30
30
  from _packagedcode.pypi import get_resolved_purl
31
31
  from python_inspector import dependencies
32
32
  from python_inspector import pyinspector_settings
33
+ from python_inspector import pylock_toml
33
34
  from python_inspector import utils
34
35
  from python_inspector import utils_pypi
35
36
  from python_inspector.package_data import get_pypi_data_from_purl
@@ -78,6 +79,7 @@ class Resolution(NamedTuple):
78
79
 
79
80
  def resolve_dependencies(
80
81
  requirement_files=tuple(),
82
+ pylock_files=tuple(),
81
83
  setup_py_file=None,
82
84
  specifiers=tuple(),
83
85
  python_version=None,
@@ -97,8 +99,8 @@ def resolve_dependencies(
97
99
  ):
98
100
  """
99
101
  Resolve the dependencies for the package requirements listed in one or
100
- more ``requirement_files``, one or more ``specifiers`` and one setuptools
101
- ``setup_py_file`` file.
102
+ more ``requirement_files``, one or more PEP 751 ``pylock_files``, one or
103
+ more ``specifiers`` and one setuptools ``setup_py_file`` file.
102
104
 
103
105
  Resolve the dependencies for the requested ``python_version`` PYVER and
104
106
  ``operating_system`` OS combination defaulting Python version 3.8 and
@@ -107,6 +109,7 @@ def resolve_dependencies(
107
109
  Download from the provided PyPI simple index_urls INDEX(s) URLs defaulting
108
110
  to PyPI.org or a configured setting.
109
111
  """
112
+ printer = utils.get_password_masking_printer(printer)
110
113
 
111
114
  if not operating_system:
112
115
  raise Exception(f"No operating system provided.")
@@ -170,6 +173,27 @@ def resolve_dependencies(
170
173
  )
171
174
  )
172
175
 
176
+ # pylock.toml
177
+ for pylock_file in pylock_files:
178
+ package_data = pylock_toml.parse_pylock_toml(location=pylock_file)
179
+ pylock_toml.validate_pylock_for_environment(
180
+ extra_data=package_data.extra_data,
181
+ python_version=python_version,
182
+ operating_system=operating_system,
183
+ )
184
+ index_urls = get_index_urls(index_urls, package_data.extra_data)
185
+ direct_dependencies.extend(package_data.dependencies)
186
+ pylock_path = pylock_file
187
+ if generic_paths:
188
+ pylock_path = utils.remove_test_data_dir_variable_prefix(path=pylock_file)
189
+ files.append(
190
+ dict(
191
+ type="file",
192
+ path=pylock_path,
193
+ package_data=[package_data.to_dict()],
194
+ )
195
+ )
196
+
173
197
  # specs
174
198
  for specifier in specifiers:
175
199
  dep = dependencies.get_dependency(specifier=specifier)
@@ -0,0 +1,146 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+ #
4
+ # Copyright (c) nexB Inc. and others. All rights reserved.
5
+ # ScanCode is a trademark of nexB Inc.
6
+ # SPDX-License-Identifier: Apache-2.0
7
+ # See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
8
+ # See https://github.com/aboutcode-org/python-inspector for support or download.
9
+ # See https://aboutcode.org for more information about nexB OSS projects.
10
+ #
11
+
12
+ from typing import Iterable
13
+ from typing import Mapping
14
+
15
+ from packvers.markers import Marker
16
+
17
+ from _packagedcode import models
18
+ from python_inspector.dependencies import get_dependency
19
+ from python_inspector.resolution import get_environment_marker_from_environment
20
+ from python_inspector.resolution import get_python_version_from_env_tag
21
+ from python_inspector.utils_pypi import Environment
22
+ from python_inspector.utils_pypi import valid_python_version
23
+
24
+ # PEP 751 pylock.toml major version this parser implements.
25
+ # 1.x files are accepted; a higher major version is a breaking format change.
26
+ SUPPORTED_LOCK_VERSION_MAJOR = 1
27
+
28
+
29
+ def load_toml(location: str) -> dict:
30
+ """
31
+ Return a mapping parsed from the TOML file at ``location``.
32
+ """
33
+ try:
34
+ import tomllib
35
+
36
+ with open(location, "rb") as f:
37
+ return tomllib.load(f)
38
+ except ImportError:
39
+ import toml
40
+
41
+ return toml.load(location)
42
+
43
+
44
+ def parse_pylock_toml(location: str) -> models.PackageData:
45
+ """
46
+ Return PackageData parsed from a PEP 751 pylock.toml file at ``location``.
47
+ """
48
+ data = load_toml(location)
49
+ lock_version = str(data.get("lock-version") or "")
50
+ if lock_version:
51
+ major = lock_version.split(".", 1)[0]
52
+ if major != str(SUPPORTED_LOCK_VERSION_MAJOR):
53
+ raise ValueError(
54
+ f"Unsupported pylock.toml lock-version {lock_version!r}. "
55
+ f"Only {SUPPORTED_LOCK_VERSION_MAJOR}.x is supported."
56
+ )
57
+
58
+ extra_index_urls = []
59
+ dependent_packages = []
60
+ for package in data.get("packages") or []:
61
+ name = package.get("name")
62
+ if not name:
63
+ continue
64
+ version = package.get("version")
65
+ if not version:
66
+ continue
67
+ specifier = f"{name}=={version}"
68
+ marker = package.get("marker")
69
+ if marker:
70
+ specifier = f"{specifier}; {marker}"
71
+ dependent_packages.append(get_dependency(specifier=specifier))
72
+ index_url = package.get("index")
73
+ if index_url and index_url not in extra_index_urls:
74
+ extra_index_urls.append(index_url)
75
+
76
+ extra_data = {}
77
+ for key in (
78
+ "lock-version",
79
+ "created-by",
80
+ "requires-python",
81
+ "environments",
82
+ "extras",
83
+ "dependency-groups",
84
+ "default-groups",
85
+ ):
86
+ value = data.get(key)
87
+ if value:
88
+ extra_data[key] = value
89
+ if extra_index_urls:
90
+ extra_data["extra_index_urls"] = extra_index_urls
91
+
92
+ return models.PackageData(
93
+ datasource_id="pypi_pylock_toml",
94
+ type="pypi",
95
+ primary_language="Python",
96
+ dependencies=dependent_packages,
97
+ extra_data=extra_data,
98
+ )
99
+
100
+
101
+ def get_dependencies_from_pylock(pylock_file: str) -> Iterable[models.DependentPackage]:
102
+ """
103
+ Yield DependentPackage for each locked package in a pylock.toml file.
104
+ """
105
+ yield from parse_pylock_toml(location=pylock_file).dependencies
106
+
107
+
108
+ def get_extra_data_from_pylock(pylock_file: str) -> Iterable[Mapping]:
109
+ """
110
+ Yield extra_data parsed from a pylock.toml file.
111
+ """
112
+ yield parse_pylock_toml(location=pylock_file).extra_data
113
+
114
+
115
+ def validate_pylock_for_environment(extra_data, python_version, operating_system):
116
+ """
117
+ Raise ValueError if the pylock metadata does not apply to ``python_version``
118
+ and ``operating_system``.
119
+ """
120
+ extra_data = extra_data or {}
121
+ python_dot_version = get_python_version_from_env_tag(python_version=python_version)
122
+ requires_python = extra_data.get("requires-python")
123
+ if requires_python and not valid_python_version(python_dot_version, requires_python):
124
+ raise ValueError(
125
+ f"Python version {python_dot_version} is not compatible with "
126
+ f"pylock.toml requires-python {requires_python}"
127
+ )
128
+
129
+ environments = extra_data.get("environments") or []
130
+ if not environments:
131
+ return
132
+
133
+ environment = Environment.from_pyver_and_os(
134
+ python_version=python_version, operating_system=operating_system
135
+ )
136
+ environment_marker = get_environment_marker_from_environment(environment)
137
+ for expression in environments:
138
+ try:
139
+ if Marker(expression).evaluate(environment_marker):
140
+ return
141
+ except Exception:
142
+ continue
143
+ raise ValueError(
144
+ f"No pylock.toml environments marker matches python {python_dot_version} "
145
+ f"on {operating_system}: {environments}"
146
+ )
@@ -9,6 +9,7 @@
9
9
 
10
10
  import ast
11
11
  import asyncio
12
+ import inspect
12
13
  import operator
13
14
  import os
14
15
  import tarfile
@@ -220,24 +221,237 @@ async def fetch_and_extract_sdist(
220
221
  return get_sdist_file_path_from_filename(sdist)
221
222
 
222
223
 
224
+ def _is_absolute_archive_name(name):
225
+ """
226
+ Return True if ``name`` is an absolute archive member or link target.
227
+ """
228
+ if os.path.isabs(name):
229
+ return True
230
+ if name.replace("\\", "/").startswith("/"):
231
+ return True
232
+ drive, _tail = os.path.splitdrive(name)
233
+ return bool(drive)
234
+
235
+
236
+ def _is_within_directory(directory, candidate):
237
+ """
238
+ Return True if the resolved ``candidate`` path stays inside ``directory``.
239
+ """
240
+ directory = os.path.realpath(directory)
241
+ try:
242
+ candidate = os.path.realpath(candidate)
243
+ return os.path.commonpath([directory, candidate]) == directory
244
+ except ValueError:
245
+ return False
246
+
247
+
248
+ def _member_destination(directory, name):
249
+ """
250
+ Return the path extraction would write for archive member ``name``.
251
+
252
+ Absolute names are not rewritten into ``directory``. ``os.path.join`` drops
253
+ the destination when the member name is absolute, which is the path a
254
+ default tar extraction writes.
255
+ """
256
+ if _is_absolute_archive_name(name):
257
+ return os.path.realpath(name)
258
+ return os.path.realpath(os.path.join(directory, name))
259
+
260
+
261
+ def _require_destination_within(directory, name):
262
+ """
263
+ Raise ValueError if member ``name`` resolves outside ``directory``.
264
+ """
265
+ normalized = name.replace("\\", "/").rstrip("/")
266
+ if not name or "\x00" in name or normalized in ("", "."):
267
+ raise ValueError(f"Refusing to extract archive member {name!r}")
268
+ destination = _member_destination(directory, name)
269
+ if not _is_within_directory(directory, destination):
270
+ raise ValueError(f"Refusing to extract {name!r} outside the extraction directory")
271
+ return destination
272
+
273
+
274
+ def _tarfile_extract_supports_filter():
275
+ """
276
+ Return True if ``TarFile.extract`` accepts an explicit extraction filter.
277
+ """
278
+ try:
279
+ parameters = inspect.signature(tarfile.TarFile.extract).parameters
280
+ except (TypeError, ValueError):
281
+ return False
282
+ return "filter" in parameters
283
+
284
+
285
+ def _restrictive_tar_filter(member, dest_path):
286
+ """
287
+ Return ``member`` when it is safe to extract into ``dest_path``.
288
+
289
+ Reject absolute paths and other members whose resolved destination leaves
290
+ ``dest_path`` instead of rewriting them into the destination. Also reject
291
+ symbolic and hard links that point outside ``dest_path``, and special files.
292
+ This filter is applied on every supported interpreter. TarFile's default
293
+ policy still extracts absolute names, and its data filter keeps those
294
+ members by stripping the leading slash.
295
+ """
296
+ destination = _require_destination_within(dest_path, member.name)
297
+ if member.issym() or member.islnk():
298
+ linkname = member.linkname or ""
299
+ if not linkname or "\x00" in linkname or _is_absolute_archive_name(linkname):
300
+ raise ValueError(f"Refusing to extract unsafe link {member.name!r}")
301
+ if member.issym():
302
+ link_target = os.path.realpath(os.path.join(os.path.dirname(destination), linkname))
303
+ else:
304
+ link_target = _member_destination(dest_path, linkname)
305
+ if not _is_within_directory(dest_path, link_target):
306
+ raise ValueError(f"Refusing to extract unsafe link {member.name!r}")
307
+ elif not (member.isreg() or member.isdir()):
308
+ raise ValueError(f"Refusing to extract special file {member.name!r}")
309
+ return member
310
+
311
+
312
+ def _extract_tar_archive(tar, dest_path):
313
+ """
314
+ Extract ``tar`` into ``dest_path`` using the restrictive tar filter.
315
+ """
316
+ dest_path = os.path.realpath(dest_path)
317
+ members = list(tar.getmembers())
318
+ for member in members:
319
+ _restrictive_tar_filter(member, dest_path)
320
+
321
+ extract_kwargs = {}
322
+ if _tarfile_extract_supports_filter():
323
+ extract_kwargs["filter"] = _restrictive_tar_filter
324
+ for member in members:
325
+ filtered = _restrictive_tar_filter(member, dest_path)
326
+ tar.extract(filtered, path=dest_path, **extract_kwargs)
327
+
328
+
329
+ def _extract_zip_archive(zip_file, dest_path):
330
+ """
331
+ Extract ``zip_file`` into ``dest_path``, rejecting members that leave it.
332
+ """
333
+ dest_path = os.path.realpath(dest_path)
334
+ members = zip_file.infolist()
335
+ for member in members:
336
+ _require_destination_within(dest_path, member.filename)
337
+ for member in members:
338
+ _require_destination_within(dest_path, member.filename)
339
+ zip_file.extract(member, path=dest_path)
340
+
341
+
342
+ def _remove_path_without_following_links(path):
343
+ """
344
+ Remove ``path`` without following a symbolic link at any level.
345
+ """
346
+ if not os.path.lexists(path):
347
+ return
348
+ if os.path.islink(path) or not os.path.isdir(path):
349
+ os.unlink(path)
350
+ return
351
+ for entry in os.scandir(path):
352
+ _remove_path_without_following_links(entry.path)
353
+ os.rmdir(path)
354
+
355
+
356
+ def _fresh_extraction_directory(sdist_file):
357
+ """
358
+ Return a new real directory for sdist ``sdist_file`` inside the cache.
359
+
360
+ Replace any existing path, including a symbolic link, without following it.
361
+ """
362
+ if (
363
+ not sdist_file
364
+ or sdist_file in (".", "..")
365
+ or _is_absolute_archive_name(sdist_file)
366
+ or os.path.basename(sdist_file) != sdist_file
367
+ ):
368
+ raise ValueError(
369
+ f"Refusing to extract sdist {sdist_file!r} outside the extraction directory"
370
+ )
371
+
372
+ cache_dir = os.path.realpath(settings.CACHE_THIRDPARTY_DIR)
373
+ extracted_root = os.path.join(cache_dir, "extracted_sdists")
374
+ if os.path.islink(extracted_root):
375
+ raise ValueError("Sdist extraction root is a symbolic link")
376
+ os.makedirs(extracted_root, exist_ok=True)
377
+ if os.path.islink(extracted_root) or not os.path.isdir(extracted_root):
378
+ raise ValueError("Sdist extraction root is not a real directory")
379
+ extracted_real = os.path.realpath(extracted_root)
380
+ if not _is_within_directory(cache_dir, extracted_real):
381
+ raise ValueError("Sdist extraction root resolves outside the cache")
382
+
383
+ destination = os.path.join(extracted_real, sdist_file)
384
+ _remove_path_without_following_links(destination)
385
+ os.mkdir(destination)
386
+ if os.path.islink(destination) or not os.path.isdir(destination):
387
+ raise ValueError("Sdist extraction directory is not a real directory")
388
+ destination_real = os.path.realpath(destination)
389
+ if not _is_within_directory(extracted_real, destination_real):
390
+ raise ValueError("Sdist extraction directory resolves outside the extraction root")
391
+ return destination_real
392
+
393
+
394
+ def _validate_extracted_package_directory(package_dir, extraction_dir):
395
+ """
396
+ Raise ValueError if ``package_dir`` is not a real directory inside ``extraction_dir``.
397
+
398
+ A missing package directory is left for the manifest readers to report.
399
+ """
400
+ if os.path.islink(extraction_dir) or not os.path.isdir(extraction_dir):
401
+ raise ValueError("Sdist extraction directory is not a real directory")
402
+ extraction_real = os.path.realpath(extraction_dir)
403
+ if not _is_within_directory(extraction_real, package_dir):
404
+ raise ValueError("Sdist package directory resolves outside the extraction directory")
405
+ if not os.path.lexists(package_dir):
406
+ return
407
+ if os.path.islink(package_dir) or not os.path.isdir(package_dir):
408
+ raise ValueError(
409
+ "Sdist package directory is not a real directory inside the extraction directory"
410
+ )
411
+
412
+
413
+ def _reject_manifest_outside_package(sdist_location, manifest_path):
414
+ """
415
+ Raise ValueError if ``manifest_path`` resolves outside ``sdist_location``.
416
+ """
417
+ if not sdist_location or not manifest_path:
418
+ return
419
+ if os.path.islink(sdist_location):
420
+ raise ValueError(f"Extracted sdist directory {sdist_location} is a symbolic link")
421
+ if not os.path.isdir(sdist_location):
422
+ return
423
+ if not _is_within_directory(sdist_location, manifest_path):
424
+ raise ValueError(f"Sdist manifest {manifest_path} resolves outside {sdist_location}")
425
+
426
+
223
427
  def get_sdist_file_path_from_filename(sdist):
428
+ """
429
+ Extract sdist ``sdist`` from the cache and return its package directory.
430
+
431
+ Tar members are extracted with an explicit restrictive filter. Members whose
432
+ resolved destination leaves the extraction directory, and unsafe symbolic or
433
+ hard links, are rejected. The extraction directory is created fresh and the
434
+ package directory is validated before callers read manifests from it.
435
+ """
224
436
  if sdist.endswith(".tar.gz"):
225
437
  sdist_file = sdist.rstrip(".tar.gz")
226
- with tarfile.open(os.path.join(settings.CACHE_THIRDPARTY_DIR, sdist)) as file:
227
- file.extractall(
228
- os.path.join(settings.CACHE_THIRDPARTY_DIR, "extracted_sdists", sdist_file)
229
- )
230
438
  elif sdist.endswith(".zip"):
231
439
  sdist_file = sdist.rstrip(".zip")
232
- with ZipFile(os.path.join(settings.CACHE_THIRDPARTY_DIR, sdist)) as zip:
233
- zip.extractall(
234
- os.path.join(settings.CACHE_THIRDPARTY_DIR, "extracted_sdists", sdist_file)
235
- )
236
-
237
440
  else:
238
441
  raise Exception(f"Unable to extract sdist {sdist}")
239
442
 
240
- return os.path.join(settings.CACHE_THIRDPARTY_DIR, "extracted_sdists", sdist_file, sdist_file)
443
+ archive_path = os.path.join(settings.CACHE_THIRDPARTY_DIR, sdist)
444
+ extraction_dir = _fresh_extraction_directory(sdist_file)
445
+ if sdist.endswith(".tar.gz"):
446
+ with tarfile.open(archive_path) as archive:
447
+ _extract_tar_archive(archive, extraction_dir)
448
+ else:
449
+ with ZipFile(archive_path) as archive:
450
+ _extract_zip_archive(archive, extraction_dir)
451
+
452
+ package_dir = os.path.join(extraction_dir, sdist_file)
453
+ _validate_extracted_package_directory(package_dir, extraction_dir)
454
+ return package_dir
241
455
 
242
456
 
243
457
  def get_requirements_from_dependencies(
@@ -274,11 +488,11 @@ def get_reqs_from_requirements_file_in_sdist(sdist_location: str, files: List) -
274
488
  """
275
489
  Return a list of parsed requirements from the ``sdist_location`` sdist location
276
490
  """
491
+ for manifest_path in files or []:
492
+ _reject_manifest_outside_package(sdist_location, manifest_path)
493
+ requirement_location = os.path.join(sdist_location, "requirements.txt")
494
+ _reject_manifest_outside_package(sdist_location, requirement_location)
277
495
  if contain_string(string="requirements.txt", files=files):
278
- requirement_location = os.path.join(
279
- sdist_location,
280
- "requirements.txt",
281
- )
282
496
  yield from get_requirements_from_distribution(
283
497
  handler=PipRequirementsFileHandler,
284
498
  location=requirement_location,
@@ -298,6 +512,11 @@ def get_requirements_from_python_manifest(
298
512
  """
299
513
  Return a list of parsed requirements from the ``sdist_location`` sdist location
300
514
  """
515
+ manifest_paths = [setup_py_location, os.path.join(sdist_location, "requirements.txt")]
516
+ manifest_paths.extend(files or [])
517
+ for manifest_path in manifest_paths:
518
+ _reject_manifest_outside_package(sdist_location, manifest_path)
519
+
301
520
  # Look in requirements file if and only if they are referred in setup.py or setup.cfg
302
521
  # And no deps have been yielded by requirements file.
303
522
  requirements = list(
@@ -531,6 +750,11 @@ class PythonInputProvider(AbstractProvider):
531
750
  sdist_location,
532
751
  "setup.cfg",
533
752
  )
753
+ _reject_manifest_outside_package(sdist_location, setup_py_location)
754
+ _reject_manifest_outside_package(sdist_location, setup_cfg_location)
755
+ _reject_manifest_outside_package(
756
+ sdist_location, os.path.join(sdist_location, "requirements.txt")
757
+ )
534
758
 
535
759
  if self.analyze_setup_py_insecurely:
536
760
  return get_reqs_insecurely(setup_py_location=setup_py_location)
@@ -816,6 +1040,8 @@ def get_setup_requirements(sdist_location: str, setup_py_location: str, setup_cf
816
1040
  Perform an insecure live evaluation of the Python code if needed and if
817
1041
  ``analyze_setup_py_insecurely`` is True.
818
1042
  """
1043
+ _reject_manifest_outside_package(sdist_location, setup_py_location)
1044
+ _reject_manifest_outside_package(sdist_location, setup_cfg_location)
819
1045
 
820
1046
  if not os.path.exists(setup_py_location) and not os.path.exists(setup_cfg_location):
821
1047
  raise Exception(f"No setup.py or setup.cfg found in pypi sdist {sdist_location}")
@@ -48,6 +48,16 @@ def print_version(ctx, param, value):
48
48
  help="Path to pip requirements file listing thirdparty packages. "
49
49
  "This option can be used multiple times.",
50
50
  )
51
+ @click.option(
52
+ "--pylock",
53
+ "pylock_files",
54
+ type=click.Path(exists=True, readable=True, path_type=str, dir_okay=False),
55
+ metavar="PYLOCK-FILE",
56
+ multiple=True,
57
+ required=False,
58
+ help="Path to a PEP 751 pylock.toml lock file listing locked thirdparty packages. "
59
+ "This option can be used multiple times.",
60
+ )
51
61
  @click.option(
52
62
  "-s",
53
63
  "--setup-py",
@@ -190,6 +200,7 @@ def print_version(ctx, param, value):
190
200
  def resolve_dependencies(
191
201
  ctx,
192
202
  requirement_files,
203
+ pylock_files,
193
204
  setup_py_file,
194
205
  specifiers,
195
206
  python_version,
@@ -209,8 +220,9 @@ def resolve_dependencies(
209
220
  ):
210
221
  """
211
222
  Resolve the dependencies for the package requirements listed in one or
212
- more REQUIREMENT-FILE file, one or more SPECIFIER and one setuptools
213
- SETUP-PY-FILE file and save the results as JSON to FILE.
223
+ more REQUIREMENT-FILE file, one or more PYLOCK-FILE file, one or more
224
+ SPECIFIER and one setuptools SETUP-PY-FILE file and save the results as
225
+ JSON to FILE.
214
226
 
215
227
  Resolve the dependencies for the requested ``--python-version`` PYVER and
216
228
  ``--operating_system`` OS combination.
@@ -259,6 +271,7 @@ def resolve_dependencies(
259
271
  try:
260
272
  resolution_result: Dict = resolver_api(
261
273
  requirement_files=requirement_files,
274
+ pylock_files=pylock_files,
262
275
  setup_py_file=setup_py_file,
263
276
  specifiers=specifiers,
264
277
  python_version=python_version,
@@ -11,6 +11,7 @@
11
11
 
12
12
  import json
13
13
  import os
14
+ import re
14
15
  from typing import Dict
15
16
  from typing import List
16
17
  from typing import NamedTuple
@@ -111,6 +112,42 @@ def remove_test_data_dir_variable_prefix(path, placeholder="<file>"):
111
112
  return placeholder
112
113
 
113
114
 
115
+ # Match quoted mapping values such as {'password': 'secret'} or {"password": "secret"}.
116
+ _PASSWORD_VALUE_RE = re.compile(
117
+ r"""(?P<prefix>(['\"])password\2\s*:\s*(['\"]))(?P<secret>.*?)(?P<suffix>\3)""",
118
+ re.IGNORECASE,
119
+ )
120
+
121
+ MASKED_PASSWORD = "********"
122
+
123
+
124
+ def mask_passwords(message, replacement=MASKED_PASSWORD):
125
+ """
126
+ Return ``message`` with password values replaced by ``replacement``.
127
+
128
+ This is used to avoid leaking credentials from verbose log output, for
129
+ example when printing a repository that includes a credentials mapping.
130
+ """
131
+ if message is None:
132
+ return message
133
+ return _PASSWORD_VALUE_RE.sub(
134
+ rf"\g<prefix>{replacement}\g<suffix>",
135
+ str(message),
136
+ )
137
+
138
+
139
+ def get_password_masking_printer(printer=print):
140
+ """
141
+ Return a printer callable that masks passwords in log messages before
142
+ forwarding them to ``printer``.
143
+ """
144
+
145
+ def password_masking_printer(message, *args, **kwargs):
146
+ return printer(mask_passwords(message), *args, **kwargs)
147
+
148
+ return password_masking_printer
149
+
150
+
114
151
  def unique(sequence):
115
152
  """
116
153
  Return a list of unique items found in sequence. Preserve the original sequence order.