Skip to content

Commit ae7bcdf

Browse files
committed
Improved sort algorithm for alias names
1 parent 40a9752 commit ae7bcdf

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

src/manage/installs.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from .exceptions import NoInstallFoundError, NoInstallsError
44
from .logging import DEBUG, LOGGER
55
from .pathutils import Path
6-
from .tagutils import CompanyTag, tag_or_range, companies_match
6+
from .tagutils import CompanyTag, tag_or_range, companies_match, split_platform
77
from .verutils import Version
88

99

@@ -121,23 +121,22 @@ def get_installs(
121121

122122

123123
def _make_alias_key(alias):
124-
from .tagutils import SUPPORTED_PLATFORM_SUFFIXES
125124
n1, sep, n3 = alias.rpartition(".")
126125
n2 = ""
127126
n3 = sep + n3
128-
if n1.endswith(SUPPORTED_PLATFORM_SUFFIXES):
129-
n1, sep, plat = n1.rpartition("-")
130-
plat = sep + plat
131-
else:
132-
plat = ""
127+
128+
n1, plat = split_platform(n1)
133129

134130
while n1 and n1[-1] in "0123456789.-":
135131
n2 = n1[-1] + n2
136132
n1 = n1[:-1]
137-
w = ""
133+
138134
if n1 and n1[-1].casefold() == "w".casefold():
139135
w = "w"
140136
n1 = n1[:-1]
137+
else:
138+
w = ""
139+
141140
return n1, w, n2, plat, n3
142141

143142

@@ -149,6 +148,26 @@ def _make_opt_part(parts):
149148
return "[{}]".format("|".join(sorted(p for p in parts if p)))
150149

151150

151+
def _sk_sub(m):
152+
n = m.group(1)
153+
if not n:
154+
return ""
155+
if n == ".":
156+
return "-"
157+
if n == "-":
158+
return "."
159+
try:
160+
return f"{int(n):020}"
161+
except ValueError:
162+
pass
163+
return n
164+
165+
166+
def _make_alias_name_sortkey(n):
167+
import re
168+
return re.sub(r"(\d+|.|-)", _sk_sub, n)
169+
170+
152171
def get_install_alias_names(aliases, friendly=True, windowed=True):
153172
if not windowed:
154173
aliases = [a for a in aliases if not a.get("windowed")]
@@ -173,7 +192,7 @@ def get_install_alias_names(aliases, friendly=True, windowed=True):
173192
_make_opt_part(plats.get(k)),
174193
n3,
175194
]))
176-
return sorted(result)
195+
return sorted(result, key=_make_alias_name_sortkey)
177196

178197

179198
def _patch_install_to_run(i, run_for):

src/manage/list_command.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ def _format_alias(i, seen):
1111
from manage.installs import get_install_alias_names
1212
aliases = [a for a in i.get("alias", ()) if a["name"].casefold() not in seen]
1313
seen.update(a["name"].casefold() for a in aliases)
14-
return ", ".join(get_install_alias_names(aliases))
14+
15+
include_w = LOGGER.would_log_to_console(logging.VERBOSE)
16+
names = get_install_alias_names(aliases, windowed=include_w)
17+
return ", ".join(names)
1518

1619

1720
def _format_tag_with_co(cmd, i):

src/manage/tagutils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def __lt__(self, other):
123123
return self.sortkey > other.sortkey
124124

125125

126-
def _split_platform(tag):
126+
def split_platform(tag):
127127
if tag.endswith(SUPPORTED_PLATFORM_SUFFIXES):
128128
for t in SUPPORTED_PLATFORM_SUFFIXES:
129129
if tag.endswith(t):
@@ -178,7 +178,7 @@ def __init__(self, company_or_tag, tag=None, *, loose_company=True):
178178
else:
179179
assert isinstance(company_or_tag, _CompanyKey)
180180
self._company = company_or_tag
181-
self.tag, self.platform = _split_platform(tag)
181+
self.tag, self.platform = split_platform(tag)
182182
self._sortkey = _sort_tag(self.tag)
183183

184184
@property

tests/test_installs.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ def test_get_install_to_run_with_range(patched_installs):
128128
assert i["executable"].match("python.exe")
129129

130130

131+
def test_install_alias_make_alias_sortkey():
132+
assert ("pythonw00000000000000000003.00000000000000000064-exe"
133+
== installs._make_alias_name_sortkey("pythonw3-64.exe"))
134+
131135
def test_install_alias_make_alias_key():
132136
assert ("python", "w", "3", "-64", ".exe") == installs._make_alias_key("pythonw3-64.exe")
133137
assert ("python", "w", "3", "", ".exe") == installs._make_alias_key("pythonw3.exe")

0 commit comments

Comments
 (0)