Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# check for actual incorrect code.
ignore = [
"B006",
"B008",
"BLE001",
"C401",
"C408",
Expand All @@ -12,6 +13,7 @@ ignore = [
"I001",
"PIE808",
"PLC",
"PLR0206",
"PLR1730",
"RUF012",
"RUF015",
Expand Down
4 changes: 2 additions & 2 deletions scripts/repartition-index.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def _number_sortkey(self, k):
return tuple(bits)

def _sort_key(self, v):
from manage.tagutils import _CompanyKey, _DescendingVersion
from manage.tagutils import _CompanyKey, DescendingVersion
return (
_DescendingVersion(v["sort-version"]),
DescendingVersion(v["sort-version"]),
_CompanyKey(v["company"]),
self._number_sortkey(v["id"]),
)
Expand Down
42 changes: 30 additions & 12 deletions src/manage/indexutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ def __init__(self, source_url, d):
raise InvalidFeedError(feed_url=source_url) from ex
self.source_url = source_url
self.next_url = validated.get("next")
versions = [_patch(source_url, v) for v in validated["versions"]]
self.versions = sorted(versions, key=lambda v: v["sort-version"], reverse=True)
# Feed order is retained intentionally for package selection.
self.versions = [_patch(source_url, v) for v in validated["versions"]]

def __repr__(self):
return "<Index({!r}, next={!r}, versions=[...{} entries])>".format(
Expand All @@ -214,7 +214,7 @@ def __repr__(self):
len(self.versions),
)

def find_all(self, tags, *, seen_ids=None, loose_company=False, with_prerelease=False):
def find_all(self, tags, *, seen_ids=None, with_prerelease=False):
filters = []
for tag in tags:
try:
Expand All @@ -226,20 +226,38 @@ def find_all(self, tags, *, seen_ids=None, loose_company=False, with_prerelease=
if i["id"].casefold() in seen_ids:
continue
if with_prerelease or not i["sort-version"].is_prerelease:
if not filters or install_matches_any(i, filters, loose_company=loose_company):
if not filters or install_matches_any(i, filters):
if seen_ids is not None:
seen_ids.add(i["id"].casefold())
yield i

def find_to_install(self, tag, *, loose_company=False, prefer_prerelease=False):
tag_list = [tag] if tag else []
def find_to_install(self, tag, *, prefer_prerelease=None):
tag_list = [tag_or_range(tag)] if tag else []
LOGGER.debug("Finding %s to install", tag_list)
for i in self.find_all(tag_list, loose_company=loose_company, with_prerelease=prefer_prerelease):
return {**i, "source": self.source_url}
if not loose_company:
return self.find_to_install(tag, loose_company=True, prefer_prerelease=prefer_prerelease)
if not prefer_prerelease:
for i in self.find_all(tag_list, loose_company=loose_company, with_prerelease=True):
if prefer_prerelease is None:
prefer_prerelease = any(t.prefers_prerelease for t in tag_list)
# We prioritise a full company name match (find_all only filters using
# the prefix) and stable releases (unless prefer_prerelease is True).
# The feed order is authoritative though, so we store our fallback
# options
fallback = []
for i in self.find_all(tag_list, with_prerelease=True):
if not prefer_prerelease and i["sort-version"].is_prerelease:
# It's a matching prerelease, but we prefer stable releases
fallback.append(i)
elif any(not t.matches_company_exactly(i["company"]) for t in tag_list):
# It's a prefix match, not a full match, so save it for later
fallback.append(i)
else:
# This is a perfect match, so let's return immediately
return {**i, "source": self.source_url}
if not prefer_prerelease:
# Need to use a fallback, so let's first look for a company prefix match
for i in fallback:
if not i["sort-version"].is_prerelease:
return {**i, "source": self.source_url}
# Return the first fallback (if any)
for i in fallback:
return {**i, "source": self.source_url}
LOGGER.debug("No install found for %s", tag_list)
raise LookupError(tag)
63 changes: 44 additions & 19 deletions src/manage/installs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .exceptions import NoInstallFoundError, NoInstallsError
from .logging import DEBUG, LOGGER
from .pathutils import Path
from .tagutils import CompanyTag, tag_or_range, companies_match, split_platform
from .tagutils import CompanyTag, tag_or_range, companies_match, split_platform, DescendingVersion
from .verutils import Version


Expand All @@ -14,6 +14,16 @@ def _make_sort_key(install):
0 if not Version(install["sort-version"]).is_prerelease else 1,
# Order by descending tags
CompanyTag(install.get("company"), install.get("tag")),
# Disambiguate by sort version where necessary
DescendingVersion(install["sort-version"]),
)


def make_sort_key_ignore_pre(install):
# Alternate sort key for "-dev" that treats prerelease as equal to release
return (
CompanyTag(install.get("company"), install.get("tag")),
DescendingVersion(install["sort-version"]),
)


Expand Down Expand Up @@ -186,6 +196,8 @@ def get_install_alias_names(aliases, friendly=True, windowed=True, default_platf
aliases = [a for a in aliases if not a.get("windowed")]
if not friendly:
return sorted(a["name"] for a in aliases)
if default_platform:
default_platform = default_platform.casefold()

seen = {}
has_w = {}
Expand Down Expand Up @@ -217,11 +229,11 @@ def _patch_install_to_run(i, run_for):


def get_matching_install_tags(
installs,
tag,
windowed=None,
default_platform=None,
single_tag=False,
installs, # previously loaded from disk
tag, # provided by user
windowed=None, # inferred from context (e.g. launched via pyw.exe)
default_platform=None, # from config
single_tag=False, # "get the single best result for this tag"
):
exact_matches = []
core_matches = []
Expand Down Expand Up @@ -258,8 +270,6 @@ def get_matching_install_tags(
else:
matches.append((i, t))
matched_any = True
if single_tag and matched_any:
break
if LOGGER.would_log_to_console(DEBUG):
# Don't bother listing all installs unless the user has asked
# for console output.
Expand Down Expand Up @@ -298,19 +308,30 @@ def get_matching_install_tags(
# If none or only prereleases, keep them all
if default_platform:
default_platform = default_platform.casefold()
# Remember whether all source versions were prereleases, so we don't
# reset our results if all post-filter versions are prereleases.
all_pre = all(Version(i["sort-version"]).is_prerelease for i, t in best)
best2 = best
best = [(i, t) for i, t in best
if i.get("__any-platform")
or t["tag"].casefold().endswith(default_platform)]
LOGGER.debug("default_platform '%s' matched %s %s", default_platform,
len(best), "install" if len(best) == 1 else "installs")
if (
not best or
(not all_pre and all(Version(i["sort-version"]).is_prerelease for i, t in best))
):
LOGGER.debug("Reusing unfiltered list")
if not best:
LOGGER.debug("Reusing unfiltered list because all candidates were "
"excluded")
best = best2
elif (not all_pre
and (not tag or not tag.prefers_prerelease)
and all(Version(i["sort-version"]).is_prerelease for i, t in best)):
LOGGER.debug("Reusing unfiltered list because all candidates were "
"prereleases")
best = best2

if best and single_tag:
if tag and tag.prefers_prerelease:
best = sorted(best, key=lambda i_t: make_sort_key_ignore_pre(i_t[0]))
return best[:1]

return best

Expand All @@ -323,19 +344,22 @@ def get_install_to_run(
windowed=False,
virtual_env=None,
default_platform=None,
*,
installs=None,
):
"""Returns the first install matching 'tag'.
"""
installs = get_installs(
install_dir,
include_unmanaged=include_unmanaged,
virtual_env=virtual_env,
)
if installs is None:
installs = get_installs(
install_dir,
include_unmanaged=include_unmanaged,
virtual_env=virtual_env,
)

if not installs:
raise NoInstallsError

if not tag or tag.casefold() == "default".casefold():
if not tag or (isinstance(tag, str) and tag.casefold() == "default".casefold()):
# We know we want default, so try filtering first. If any are explicitly
# tagged (e.g. active venv), they will be the only candidates.
# Otherwise, we'll do a regular search as if 'default_tag' was provided.
Expand All @@ -355,6 +379,7 @@ def get_install_to_run(
tag,
windowed=windowed,
default_platform=default_platform,
single_tag=True,
)

if best:
Expand Down
65 changes: 47 additions & 18 deletions src/manage/list_command.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json

from . import logging
from .exceptions import ArgumentError
from .exceptions import ArgumentError, NoInstallFoundError, NoInstallsError

LOGGER = logging.LOGGER

Expand All @@ -22,8 +22,9 @@ def _format_alias(cmd, i, seen):
def _format_tag_with_co(cmd, i):
t = i["tag"]
# Show the default platform as optional when configured.
if cmd and cmd.default_platform and t.endswith(cmd.default_platform):
t = t.removesuffix(cmd.default_platform) + f"[{cmd.default_platform}]"
if cmd and cmd.default_platform and t.casefold().endswith(cmd.default_platform.casefold()):
n = len(cmd.default_platform)
t = f"{t[:-n]}[{t[-n:]}]"
if i["company"].casefold() in ("PythonCore".casefold(), "---", ""):
return t
return rf"{i['company']}\{t}"
Expand Down Expand Up @@ -331,6 +332,9 @@ def execute(cmd):
plat = plat or cmd.default_platform

if cmd.source:
# When listing online packages, we filter by install-for tags to better
# match 'py install <tag>' behaviour

from .indexutils import Index
from .urlutils import IndexDownloader
installs = []
Expand All @@ -354,36 +358,61 @@ def execute(cmd):
if first_exc:
raise SystemExit(1) from first_exc
if cmd.one:
# Pick the first non-prerelease that'll install for our platform
best = [i for i in installs
if any(t.endswith(plat) for t in i.get("install-for", []))]
for i in best:
if not i["sort-version"].is_prerelease:
installs = [i]
break
else:
if any(t.casefold().endswith(plat.casefold()) for t in i.get("install-for", []))]
if any(t.prefers_prerelease for t in tags):
# Prefer the first platform match in feed order.
installs = best[:1] or installs
Comment thread
Copilot marked this conversation as resolved.
Resolved
else:
# Pick the first non-prerelease that'll install for our platform
for i in best:
if not i["sort-version"].is_prerelease:
installs = [i]
break
else:
installs = best[:1] or installs
elif cmd.install_dir:
# When listing installed runtimes, we filter based on their run-for tags
# to represent 'py exec -V:<tag>' behaviour.

try:
installs = cmd.get_installs(include_unmanaged=cmd.unmanaged)
except OSError:
LOGGER.debug("Unable to read installs", exc_info=True)
installs = []

if tags:
LOGGER.debug("Filtering to following items")
for t in tags:
LOGGER.debug("* %r", t)
installs = [i for i in installs if install_matches_any(i, tags, loose_company=True)]
if tags and installs:
LOGGER.debug("Filtering to following items: %r", tags)
installs = [i for i in installs
if install_matches_any(i, tags, for_run=True)]
if any(t.prefers_prerelease for t in tags):
from .installs import make_sort_key_ignore_pre
installs.sort(key=make_sort_key_ignore_pre)

if not cmd.unmanaged:
if not cmd.unmanaged and installs:
# Just in case any leak through (e.g. active venv)
installs = [i for i in installs if not i.get("unmanaged")]

if cmd.one and installs:
# Use the filtered list and select the best option for each tag.
# Then return the best of those including prereleases
from .installs import get_install_to_run, make_sort_key_ignore_pre
best = []
for t in (tags or [None]):
try:
best.append(get_install_to_run(
cmd.install_dir,
cmd.default_tag,
t,
default_platform=cmd.default_platform,
installs=installs,
))
except (NoInstallFoundError, NoInstallsError):
continue
installs = sorted(best or installs, key=make_sort_key_ignore_pre)[:1]
else:
raise ArgumentError("Configuration file does not specify install directory.")

if cmd.one:
installs = [i for i in installs if i.get("default")][:1] or installs[:1]
formatter(cmd, installs)

LOGGER.debug("END list_command.execute")
1 change: 0 additions & 1 deletion src/manage/scriptutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ def _maybe_quote(a):
bits.append(b)
if b[-1:] == "\\":
bits.append("\\" * (len(b) - len(b.rstrip("\\"))))
print(a.split('"'), bits)
a = ''.join(bits)
return f'"{a}"' if ' ' in a else a

Expand Down
Loading