|
1 | 1 | # This file is part of cjdk. |
2 | 2 | # Copyright 2022-25 Board of Regents of the University of Wisconsin System |
3 | 3 | # SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +""" |
| 6 | +Public API surface. |
| 7 | +
|
| 8 | +Exposes all user-facing functions (which are re-exported by __init__.py). |
| 9 | +Coordinates calls to other modules. |
| 10 | +""" |
| 11 | + |
4 | 12 | from __future__ import annotations |
5 | 13 |
|
6 | 14 | import hashlib |
|
9 | 17 | from contextlib import contextmanager |
10 | 18 | from typing import TYPE_CHECKING |
11 | 19 |
|
12 | | -from . import _cache, _conf, _index, _install, _jdk |
| 20 | +from . import _conf, _install, _jdk |
13 | 21 | from ._exceptions import ( |
14 | 22 | CjdkError, |
15 | 23 | ConfigError, |
@@ -64,7 +72,8 @@ def list_vendors(**kwargs: Unpack[ConfigKwargs]) -> list[str]: |
64 | 72 | InstallError |
65 | 73 | If fetching the index fails. |
66 | 74 | """ |
67 | | - return sorted(_get_vendors(**kwargs)) |
| 75 | + conf = _conf.configure(**kwargs) |
| 76 | + return sorted(_jdk.available_vendors(conf)) |
68 | 77 |
|
69 | 78 |
|
70 | 79 | def list_jdks( # type: ignore [misc] # overlap with kwargs |
@@ -113,9 +122,27 @@ def list_jdks( # type: ignore [misc] # overlap with kwargs |
113 | 122 | InstallError |
114 | 123 | If fetching the index fails. |
115 | 124 | """ |
116 | | - return _get_jdks( |
117 | | - vendor=vendor, version=version, cached_only=cached_only, **kwargs |
118 | | - ) |
| 125 | + jdk = kwargs.pop("jdk", None) |
| 126 | + if jdk: |
| 127 | + parsed_vendor, parsed_version = _conf.parse_vendor_version(jdk) |
| 128 | + vendor = vendor or parsed_vendor or None |
| 129 | + version = version or parsed_version or None |
| 130 | + |
| 131 | + if vendor is None: |
| 132 | + conf = _conf.configure(**kwargs) |
| 133 | + return [ |
| 134 | + jdk |
| 135 | + for v in sorted(_jdk.available_vendors(conf)) |
| 136 | + for jdk in list_jdks( |
| 137 | + vendor=v, |
| 138 | + version=version, |
| 139 | + cached_only=cached_only, |
| 140 | + **kwargs, |
| 141 | + ) |
| 142 | + ] |
| 143 | + |
| 144 | + conf = _conf.configure(vendor=vendor, version=version, **kwargs) |
| 145 | + return _jdk.matching_jdks(conf, cached_only=cached_only) |
119 | 146 |
|
120 | 147 |
|
121 | 148 | def clear_cache(**kwargs: Unpack[ConfigKwargs]) -> Path: |
@@ -425,84 +452,6 @@ def cache_package( |
425 | 452 | raise ConfigError(str(e)) from e |
426 | 453 |
|
427 | 454 |
|
428 | | -def _get_vendors(**kwargs: Unpack[ConfigKwargs]) -> set[str]: |
429 | | - conf = _conf.configure(**kwargs) |
430 | | - index = _index.jdk_index(conf) |
431 | | - return { |
432 | | - vendor.replace("jdk@", "") |
433 | | - for osys in index |
434 | | - for arch in index[osys] |
435 | | - for vendor in index[osys][arch] |
436 | | - } |
437 | | - |
438 | | - |
439 | | -def _get_jdks( |
440 | | - *, |
441 | | - vendor: str | None = None, |
442 | | - version: str | None = None, |
443 | | - cached_only: bool = True, |
444 | | - **kwargs: Unpack[ConfigKwargs], |
445 | | -) -> list[str]: |
446 | | - jdk = kwargs.pop("jdk", None) |
447 | | - if jdk: |
448 | | - parsed_vendor, parsed_version = _conf.parse_vendor_version(jdk) |
449 | | - vendor = vendor or parsed_vendor or None |
450 | | - version = version or parsed_version or None |
451 | | - |
452 | | - # Handle "all vendors" before creating Configuration. |
453 | | - if vendor is None: |
454 | | - return [ |
455 | | - jdk |
456 | | - for v in sorted(_get_vendors()) |
457 | | - for jdk in _get_jdks( |
458 | | - vendor=v, |
459 | | - version=version, |
460 | | - cached_only=cached_only, |
461 | | - **kwargs, |
462 | | - ) |
463 | | - ] |
464 | | - |
465 | | - conf = _conf.configure(vendor=vendor, version=version, **kwargs) |
466 | | - index = _index.jdk_index(conf) |
467 | | - jdks = _index.available_jdks(index, conf) |
468 | | - versions = _index._get_versions(jdks, conf) |
469 | | - matched = _index._match_versions(conf.vendor, versions, conf.version) |
470 | | - |
471 | | - if cached_only: |
472 | | - # Filter matches by existing key directories. |
473 | | - def is_cached(v: str) -> bool: |
474 | | - url = _index.jdk_url(index, conf, v) |
475 | | - key = (_jdk._JDK_KEY_PREFIX, _cache._key_for_url(url)) |
476 | | - keydir = _cache._key_directory(conf.cache_dir, key) |
477 | | - return keydir.exists() |
478 | | - |
479 | | - matched = {k: v for k, v in matched.items() if is_cached(v)} |
480 | | - |
481 | | - class VersionElement: |
482 | | - def __init__(self, value: int | str) -> None: |
483 | | - self.value = value |
484 | | - |
485 | | - def __eq__(self, other: VersionElement) -> bool: # type: ignore[override] |
486 | | - if isinstance(self.value, int) and isinstance(other.value, int): |
487 | | - return self.value == other.value |
488 | | - return str(self.value) == str(other.value) |
489 | | - |
490 | | - def __lt__(self, other: VersionElement) -> bool: |
491 | | - if isinstance(self.value, int) and isinstance(other.value, int): |
492 | | - return self.value < other.value |
493 | | - return str(self.value) < str(other.value) |
494 | | - |
495 | | - def version_key( |
496 | | - version_tuple: tuple[tuple[int | str, ...], str], |
497 | | - ) -> tuple[VersionElement, ...]: |
498 | | - return tuple(VersionElement(elem) for elem in version_tuple[0]) |
499 | | - |
500 | | - return [ |
501 | | - f"{conf.vendor}:{v}" |
502 | | - for k, v in sorted(matched.items(), key=version_key) |
503 | | - ] |
504 | | - |
505 | | - |
506 | 455 | def _make_hash_checker( |
507 | 456 | hashes: dict[str, str | None], |
508 | 457 | ) -> Callable[[Path], None]: |
|
0 commit comments