python-inspector 0.15.0__py3-none-any.whl → 0.15.1__py3-none-any.whl

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.
python_inspector/api.py CHANGED
@@ -296,7 +296,11 @@ def resolve_dependencies(
296
296
  async def gather_pypi_data():
297
297
  async def get_pypi_data(package):
298
298
  data = await get_pypi_data_from_purl(
299
- package, repos=repos, environment=environment, prefer_source=prefer_source
299
+ package,
300
+ repos=repos,
301
+ environment=environment,
302
+ prefer_source=prefer_source,
303
+ index_urls=list(repos_by_url.keys()),
300
304
  )
301
305
 
302
306
  if verbose:
@@ -9,6 +9,9 @@
9
9
  # See https://aboutcode.org for more information about nexB OSS projects.
10
10
  #
11
11
 
12
+ import os
13
+ from urllib.parse import urlparse, urlunparse
14
+
12
15
  from typing import Dict
13
16
  from typing import List
14
17
  from typing import Optional
@@ -27,7 +30,11 @@ from python_inspector.utils_pypi import PypiSimpleRepository
27
30
 
28
31
 
29
32
  async def get_pypi_data_from_purl(
30
- purl: str, environment: Environment, repos: List[PypiSimpleRepository], prefer_source: bool
33
+ purl: str,
34
+ environment: Environment,
35
+ repos: List[PypiSimpleRepository],
36
+ prefer_source: bool,
37
+ index_urls: List[str],
31
38
  ) -> Optional[PackageData]:
32
39
  """
33
40
  Generate `Package` object from the `purl` string of pypi type
@@ -43,7 +50,22 @@ async def get_pypi_data_from_purl(
43
50
  version = parsed_purl.version
44
51
  if not version:
45
52
  raise Exception("Version is not specified in the purl")
46
- base_path = "https://pypi.org/pypi"
53
+
54
+ # Todo: address the case where several index URLs are passed
55
+ if index_urls:
56
+ # Backward compatibility: If pypi.org is passed as index url, always resolve against it.
57
+ # When multiple index URLs are supported and the todo above is fixed, then this hack can be removed.
58
+ if "https://pypi.org/simple" in index_urls:
59
+ index_url = None
60
+ else:
61
+ index_url = index_urls[0]
62
+ else:
63
+ index_url = None
64
+
65
+ base_path = (
66
+ index_url.removesuffix("/simple") + "/pypi" if index_url else "https://pypi.org/pypi"
67
+ )
68
+
47
69
  api_url = f"{base_path}/{name}/{version}/json"
48
70
 
49
71
  from python_inspector.utils import get_response_async
@@ -62,10 +84,32 @@ async def get_pypi_data_from_purl(
62
84
  sdist_url = await get_sdist_download_url(
63
85
  purl=parsed_purl, repos=repos, python_version=python_version
64
86
  )
87
+
88
+ def canonicalize_url(url: str):
89
+ # Parse the URL into its components
90
+ parsed = urlparse(url)
91
+
92
+ # Canonicalize the path component to resolve ".."
93
+ # os.path.normpath will handle segments like '.' and '..'
94
+ canonical_path = os.path.normpath(parsed.path)
95
+
96
+ # On Windows, normpath uses backslashes ('\\').
97
+ # We must replace them with forward slashes ('/') for a valid URL path.
98
+ if os.path.sep == "\\":
99
+ canonical_path = canonical_path.replace("\\", "/")
100
+
101
+ # Rebuild the URL with the canonicalized path
102
+ # We replace the original path with the new one
103
+ parsed = parsed._replace(path=canonical_path)
104
+ canonical_url = urlunparse(parsed)
105
+
106
+ return canonical_url
107
+
65
108
  if sdist_url:
66
109
  valid_distribution_urls.append(sdist_url)
67
110
 
68
111
  valid_distribution_urls = [url for url in valid_distribution_urls if url]
112
+ valid_distribution_urls = list(map(canonicalize_url, valid_distribution_urls))
69
113
 
70
114
  # if prefer_source is True then only source distribution is used
71
115
  # in case of no source distribution available then wheel is used
@@ -81,28 +125,60 @@ async def get_pypi_data_from_purl(
81
125
  ]
82
126
  wheel_url = choose_single_wheel(wheel_urls)
83
127
  if wheel_url:
84
- valid_distribution_urls.insert(0, wheel_url)
128
+ valid_distribution_urls.insert(0, canonicalize_url(wheel_url))
85
129
 
86
130
  urls = {url.get("url"): url for url in response.get("urls") or []}
131
+
132
+ # Sanitize all URLs that are relative and canonicalize them
133
+ urls_sanitized = {}
134
+ for url in urls:
135
+ value = urls.get(url)
136
+
137
+ # remove the URL anchor fragment
138
+ url_parsed = urlparse(url)
139
+ url = urlunparse(url_parsed._replace(fragment=""))
140
+
141
+ if url.startswith("https"):
142
+ url_sanitized = canonicalize_url(url)
143
+ else:
144
+ url_sanitized = canonicalize_url(base_path + url)
145
+
146
+ urls_sanitized[url_sanitized] = value
147
+
148
+ def remove_credentials_from_url(url: str):
149
+ # Parse the URL into its components
150
+ parsed = urlparse(url)
151
+
152
+ new_netloc = parsed.hostname
153
+ if parsed.port:
154
+ new_netloc += f":{parsed.port}"
155
+
156
+ # Create a new parsed result object, replacing the old netloc
157
+ # with our new one that has no credentials.
158
+ parsed = parsed._replace(netloc=new_netloc)
159
+ url_without_credentials = urlunparse(parsed)
160
+
161
+ return url_without_credentials
162
+
87
163
  # iterate over the valid distribution urls and return the first
88
164
  # one that is matching.
89
165
  for dist_url in valid_distribution_urls:
90
- if dist_url not in urls:
166
+ if dist_url not in urls_sanitized:
91
167
  continue
92
168
 
93
- url_data = urls.get(dist_url)
169
+ url_data = urls_sanitized.get(dist_url)
94
170
  digests = url_data.get("digests") or {}
95
171
 
96
172
  return PackageData(
97
173
  primary_language="Python",
98
174
  description=get_description(info),
99
175
  homepage_url=homepage_url,
100
- api_data_url=api_url,
176
+ api_data_url=remove_credentials_from_url(api_url),
101
177
  bug_tracking_url=bug_tracking_url,
102
178
  code_view_url=code_view_url,
103
179
  license_expression=info.get("license_expression"),
104
180
  declared_license=get_declared_license(info),
105
- download_url=dist_url,
181
+ download_url=remove_credentials_from_url(dist_url),
106
182
  size=url_data.get("size"),
107
183
  md5=digests.get("md5") or url_data.get("md5_digest"),
108
184
  sha256=digests.get("sha256"),
@@ -122,6 +198,8 @@ async def get_pypi_data_from_purl(
122
198
 
123
199
 
124
200
  def choose_single_wheel(wheel_urls: List[str]) -> Optional[str]:
201
+ # fix Issue 240 - TypeError: '<' not supported between instances of 'str' and 'NoneType'
202
+ wheel_urls = [url for url in wheel_urls if url is not None]
125
203
  """
126
204
  Sort wheel urls descendingly and return the first one
127
205
  """
@@ -34,6 +34,7 @@ import aiohttp
34
34
  import attr
35
35
  import packageurl
36
36
  import requests
37
+ import zipfile
37
38
  from bs4 import BeautifulSoup
38
39
  from commoncode import fileutils
39
40
  from commoncode.hash import multi_checksums
@@ -1698,6 +1699,21 @@ class Cache:
1698
1699
 
1699
1700
  cache_valid = os.path.exists(cached) and os.path.getsize(cached) > 0
1700
1701
 
1702
+ # Validate cached wheel/egg files.
1703
+ if cache_valid and not as_text:
1704
+ if path_or_url.endswith((".whl", ".egg", ".zip")):
1705
+ try:
1706
+ cached_resolved = os.path.realpath(cached)
1707
+ if not zipfile.is_zipfile(cached_resolved):
1708
+ if TRACE_DEEP:
1709
+ print(f" FILE CACHE INVALID (corrupted zip): {path_or_url}")
1710
+ cache_valid = False
1711
+ except (FileNotFoundError, OSError):
1712
+ # File was deleted/modified by another task - treat as cache miss
1713
+ if TRACE_DEEP:
1714
+ print(f" FILE CACHE VANISHED during validation: {path_or_url}")
1715
+ cache_valid = False
1716
+
1701
1717
  if force or not cache_valid:
1702
1718
  if not cache_valid and os.path.exists(cached):
1703
1719
  if TRACE_DEEP:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-inspector
3
- Version: 0.15.0
3
+ Version: 0.15.1
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
@@ -49,13 +49,13 @@ Requires-Dist: twine; extra == "dev"
49
49
  Requires-Dist: ruff; extra == "dev"
50
50
  Requires-Dist: pytest-rerunfailures; extra == "dev"
51
51
  Requires-Dist: pytest-asyncio>=0.21; extra == "dev"
52
- Requires-Dist: Sphinx>=6.2.0; extra == "dev"
53
- Requires-Dist: sphinx-rtd-theme>=1.0.0; extra == "dev"
54
- Requires-Dist: sphinx-reredirects>=0.1.2; extra == "dev"
55
- Requires-Dist: doc8>=0.11.2; extra == "dev"
56
- Requires-Dist: sphinx-autobuild; extra == "dev"
52
+ Requires-Dist: Sphinx<9,>=7.3; extra == "dev"
53
+ Requires-Dist: sphinx_rtd_theme<4,>=3; extra == "dev"
54
+ Requires-Dist: sphinx-autobuild>=2024.10.3; extra == "dev"
55
+ Requires-Dist: sphinx_reredirects; extra == "dev"
56
+ Requires-Dist: doc8>=1.1.2; extra == "dev"
57
57
  Requires-Dist: sphinx-rtd-dark-mode>=1.3.0; extra == "dev"
58
- Requires-Dist: sphinx-copybutton; extra == "dev"
58
+ Requires-Dist: sphinx-copybutton>=0.5; extra == "dev"
59
59
  Dynamic: license-file
60
60
 
61
61
  python-inspector - inspect Python package dependencies and metadata
@@ -8,12 +8,12 @@ _packagedcode/pypi_setup_py.py.ABOUT,sha256=7FLC7R73YDfzEu47fij39PxteCqmojKSexCh
8
8
  _packagedcode/pypi_setup_py.py.LICENSE,sha256=EsVAqcPMzlhNkyJ3j6EW_yF02WLs94p0w2igJYrUcxs,1077
9
9
  _packagedcode/utils.py,sha256=uxkWaSeZRY69LinSFdVFHirpuyLI4YcvYb2wLztU6T4,6970
10
10
  python_inspector/__init__.py,sha256=p_CXZmB5YEm2rH9pXV5TPOVBW5rKhmD-2yNI5xQSGaI,554
11
- python_inspector/api.py,sha256=nYM_w3Xtuc42YNxMBY730X-BuJxIKhwQNMCbuKZyNjo,17431
11
+ python_inspector/api.py,sha256=9lL9reZQjlsnbxZHDD5QE4VfnKV5wEEofoPmefAz_Zw,17534
12
12
  python_inspector/cli_utils.py,sha256=EOgbryv9DppCjP1-IbVHg1QnWJANJcvdQ3BhM9huN98,1139
13
13
  python_inspector/dependencies.py,sha256=4-y2MprEg91cGR0NPJpiMc4tbwJZky4L7ZrIB-lk8mc,2944
14
14
  python_inspector/error.py,sha256=K3kRjIr8AXgSFMyhLa9MerhMfq8MhPKxLvDT6RiWD4M,457
15
15
  python_inspector/lockfile.py,sha256=I-xLXQjluh7RXJIB6-P9L_qR3reFpYioR0a4gApcfM0,790
16
- python_inspector/package_data.py,sha256=D4ilQazsa898_ysKI73Vg9DoTEK5eNctXNys7E20BCc,6552
16
+ python_inspector/package_data.py,sha256=EcfHY-4-NPTbg3zAWBaaO9PjB98OkUCpC-fR2QstZKo,9271
17
17
  python_inspector/resolution.py,sha256=ufStgDuFNuR2nRJrBC9MamBH_4F9-MCOTjyTRSN-AiE,30076
18
18
  python_inspector/resolution.py.ABOUT,sha256=tYLeHLgyo4zZrPlMXlVF59EjC-GGDaQeFKogf7M7rXg,705
19
19
  python_inspector/resolution.py.LICENSE,sha256=84j9OMrRMRLB3A9mm76A5_hFQe26-3LzAw0sp2QsPJ0,751
@@ -25,18 +25,18 @@ python_inspector/setup_py_live_eval.py.LICENSE,sha256=5wfEqJLf5ANizw20WUURuTgoZO
25
25
  python_inspector/utils.py,sha256=cQWtdVT0RZ8XGj_sxYu_lX_sg-Iz3NCCtsg-haYbwiM,3541
26
26
  python_inspector/utils_pip_compatibility_tags.py,sha256=8ZQkXLldhqqO4sJ0aVXQwWYdw3Qo1_C9MhntShVlgE8,6831
27
27
  python_inspector/utils_pip_compatibility_tags.py.ABOUT,sha256=tFhzj7qUEA9H-Tg6K1BI5QBtmQEq0vHg0X2Ws7t073U,540
28
- python_inspector/utils_pypi.py,sha256=cT2FCKOqbaxQucDScTs_lAdgNqbEGWIRyoroX3cxcuU,59362
28
+ python_inspector/utils_pypi.py,sha256=QzMvW1IR5pc2k5VxppOF_Dob5cA_3FkaBxxW5cUGi1Y,60160
29
29
  python_inspector/utils_pypi.py.ABOUT,sha256=7s9WCNmS-qJJgHC979fI4whrM5Y41-Ar-LZoIfZXPVU,635
30
30
  python_inspector/utils_pypi_supported_tags.py,sha256=SdbIlKoCgyqBZIesEdrFhVMnYikBirvSDvPUPIomTSo,2850
31
31
  python_inspector/utils_pypi_supported_tags.py.ABOUT,sha256=ndJ1kQS6u7caYmLc6XVnNIdKfpnvGjKepxpmhQJWJyA,741
32
- python_inspector-0.15.0.dist-info/licenses/AUTHORS.rst,sha256=cLH597eiqyd4cSHJ9Ny_CcbSWzTT7_VTCg3Zw43b4Ns,107
33
- python_inspector-0.15.0.dist-info/licenses/CHANGELOG.rst,sha256=pqXm6Y962ADyckNliN4bjPs2Z-iHce5-poMAxj4LWyY,6419
34
- python_inspector-0.15.0.dist-info/licenses/CODE_OF_CONDUCT.rst,sha256=xXDTFpYY3Y7JoxrWADihieUvhGlwJc_oxsbmLJaBvE4,3422
35
- python_inspector-0.15.0.dist-info/licenses/NOTICE,sha256=fkLIlzyP8zWNnPbFSXkTInNgYl35THH5-J8vMKuNzhc,761
36
- python_inspector-0.15.0.dist-info/licenses/README.rst,sha256=ucUCSQt39WlNfVdOl_1WbuvBqEN0NZm_h5o_vltNFps,7033
37
- python_inspector-0.15.0.dist-info/licenses/apache-2.0.LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
38
- python_inspector-0.15.0.dist-info/METADATA,sha256=MF4_8Jdalv3aNviIrcIty7lbhBhyT7EJ5aZKdSHOh0U,9381
39
- python_inspector-0.15.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
40
- python_inspector-0.15.0.dist-info/entry_points.txt,sha256=2s9F95rPWbgz3cOnKN3Z7PwzglPt2EsX2qf7W0Xubpk,87
41
- python_inspector-0.15.0.dist-info/top_level.txt,sha256=C-KAHTL8C6jd7QULvFk83YjOBVxDrnyefG3RikRHRlw,31
42
- python_inspector-0.15.0.dist-info/RECORD,,
32
+ python_inspector-0.15.1.dist-info/licenses/AUTHORS.rst,sha256=cLH597eiqyd4cSHJ9Ny_CcbSWzTT7_VTCg3Zw43b4Ns,107
33
+ python_inspector-0.15.1.dist-info/licenses/CHANGELOG.rst,sha256=0of_WFoqu0B5laIoE9v-EsYupuXKhTNFkcQGskO4sHg,6655
34
+ python_inspector-0.15.1.dist-info/licenses/CODE_OF_CONDUCT.rst,sha256=xXDTFpYY3Y7JoxrWADihieUvhGlwJc_oxsbmLJaBvE4,3422
35
+ python_inspector-0.15.1.dist-info/licenses/NOTICE,sha256=fkLIlzyP8zWNnPbFSXkTInNgYl35THH5-J8vMKuNzhc,761
36
+ python_inspector-0.15.1.dist-info/licenses/README.rst,sha256=ucUCSQt39WlNfVdOl_1WbuvBqEN0NZm_h5o_vltNFps,7033
37
+ python_inspector-0.15.1.dist-info/licenses/apache-2.0.LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
38
+ python_inspector-0.15.1.dist-info/METADATA,sha256=cutMVt_iFImbKsfV44wiikNwVhQaAp-nptq7j7za6Eo,9389
39
+ python_inspector-0.15.1.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
40
+ python_inspector-0.15.1.dist-info/entry_points.txt,sha256=2s9F95rPWbgz3cOnKN3Z7PwzglPt2EsX2qf7W0Xubpk,87
41
+ python_inspector-0.15.1.dist-info/top_level.txt,sha256=C-KAHTL8C6jd7QULvFk83YjOBVxDrnyefG3RikRHRlw,31
42
+ python_inspector-0.15.1.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.9.0)
2
+ Generator: setuptools (82.0.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,6 +1,13 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
+ v0.15.1
5
+ -----------
6
+
7
+ - Fetch package metadata from private artifactory if specified https://github.com/aboutcode-org/python-inspector/pull/261
8
+ - Add zip file cache validation https://github.com/aboutcode-org/python-inspector/pull/256
9
+
10
+
4
11
  v0.15.0
5
12
  -----------
6
13