-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathlist_command.py
More file actions
305 lines (252 loc) · 9.09 KB
/
list_command.py
File metadata and controls
305 lines (252 loc) · 9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import json
import sys
from .exceptions import ArgumentError
from .logging import LOGGER
def _exe_partition(n):
n1, sep, n2 = n.rpartition(".")
n2 = sep + n2
while n1 and n1[-1] in "0123456789.-":
n2 = n1[-1] + n2
n1 = n1[:-1]
w = ""
if n1 and n1[-1] == "w":
w = "w"
n1 = n1[:-1]
return n1, w, n2
def _format_alias(i, seen):
try:
alias = i["alias"]
except KeyError:
return ""
if not alias:
return ""
if len(alias) == 1:
a = i["alias"][0]
n = a["name"].casefold()
if n in seen:
return ""
seen.add(n)
return i["alias"][0]["name"]
names = {_exe_partition(a["name"].casefold()): a["name"] for a in alias
if a["name"].casefold() not in seen}
seen.update(a["name"].casefold() for a in alias)
for n1, w, n2 in list(names):
k = (n1, "", n2)
if w and k in names:
del names[n1, w, n2]
n1, _, n2 = _exe_partition(names[k])
names[k] = f"{n1}[w]{n2}"
return ", ".join(names[n] for n in sorted(names))
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 i["company"].casefold() in ("PythonCore".casefold(), "---", ""):
return t
return rf"{i['company']}\{t}"
def _ljust(s, n):
if len(s) <= n:
return s.ljust(n)
return s[:n - 3] + "..."
def format_table(cmd, installs):
columns = {
"tag-with-co": "Tag",
"default-star": " ",
"display-name": "Name",
"company": "Managed By",
"sort-version": "Version",
"alias": "Alias",
}
seen_alias = set()
installs = [{
**i,
"alias": _format_alias(i, seen_alias),
"sort-version": str(i['sort-version']),
"default-star": "",
"tag-with-co": _format_tag_with_co(cmd, i),
} for i in installs]
for i in installs:
if i.get("default"):
i["default-star"] = "*"
break
cwidth = {k: len(v) for k, v in columns.items()}
for i in installs:
for k, v in i.items():
try:
cwidth[k] = max(cwidth[k], len(v))
except LookupError:
pass
# Maximum column widths
show_truncated_warning = False
mwidth = {"company": 30, "tag-with-co": 30, "display-name": 60, "sort-version": 15, "alias": 50}
for k in list(cwidth):
try:
if cwidth[k] > mwidth[k]:
cwidth[k] = mwidth[k]
show_truncated_warning = True
except LookupError:
pass
LOGGER.print("!B!%s!W!", " ".join(columns[c].ljust(cwidth[c]) for c in columns), always=True)
any_shown = False
for i in installs:
if not i.get("unmanaged"):
clr = "!G!" if i.get("default-star") else ""
LOGGER.print(f"{clr}%s!W!", " ".join(_ljust(i.get(c, ""), cwidth[c]) for c in columns), always=True)
any_shown = True
if not any_shown:
LOGGER.print("!Y!-- No runtimes. Use 'py install <version>' to install one. --!W!")
shown_header = False
for i in installs:
if i.get("unmanaged"):
if not shown_header:
LOGGER.print(always=True)
LOGGER.print("!B!* These runtimes were found, but cannot be updated or uninstalled. *!W!", always=True)
shown_header = True
clr = "!G!" if i.get("default") else ""
LOGGER.print(f"{clr}%s!W!", " ".join(i.get(c, "").ljust(cwidth[c]) for c in columns), always=True)
if show_truncated_warning:
LOGGER.print()
LOGGER.print("!B!Some columns were truncated. Use '!G!--format=json!B!'"
" or '!G!--format=jsonl!B!' for full information.!W!")
CSV_EXCLUDE = {
"schema", "unmanaged",
# Complex columns of limited value
"install-for", "shortcuts", "__original-shortcuts",
"executable", "executable_args",
}
CSV_EXPAND = ["run-for", "alias"]
def _csv_filter_and_expand(installs):
for i in installs:
i = {k: v for k, v in i.items() if k not in CSV_EXCLUDE}
to_expand = {k: i.pop(k, ()) for k in CSV_EXPAND}
yield i
for k2, vlist in to_expand.items():
for vv in vlist:
yield {f"{k2}.{k}": v for k, v in vv.items()}
def format_csv(cmd, installs):
import csv
installs = list(_csv_filter_and_expand(installs))
if not installs:
return
s = set()
columns = [c for i in installs for c in i
if c not in s and (s.add(c) or True)]
writer = csv.DictWriter(sys.stdout, columns)
writer.writeheader()
writer.writerows(installs)
def format_json(cmd, installs):
print(json.dumps({"versions": installs}, default=str))
def format_json_lines(cmd, installs):
for i in installs:
print(json.dumps(i, default=str))
def format_bare_id(cmd, installs):
for i in installs:
print(i["id"])
def format_bare_exe(cmd, installs):
for i in installs:
print(i["executable"])
def format_bare_prefix(cmd, installs):
for i in installs:
try:
print(i["prefix"])
except KeyError:
pass
def format_bare_url(cmd, installs):
for i in installs:
try:
print(i["url"])
except KeyError:
pass
def format_legacy(cmd, installs, paths=False):
seen_default = False
# TODO: Filter out unmanaged runtimes that have managed equivalents
# The default order (which should be preserved) of 'installs' will put the
# unmanaged runtimes first. So we can't just filter as we go, it needs to
# be a separate pass. We should also reorder PythonCore prereleases above
# non-PythonCore installs, since the Company will distinguish.
# But legacy output is uninteresting, so it can be done later.
for i in installs:
if i["id"] == "__active-virtual-env":
tag = " *"
seen_default = True
else:
tag = _format_tag_with_co(cmd, i)
if tag:
tag = f" -V:{tag}"
if not seen_default and i.get("default"):
tag = f"{tag} *"
seen_default = True
print(tag.ljust(17), i["executable"] if paths else i["display-name"])
FORMATTERS = {
"table": format_table,
"csv": format_csv,
"json": format_json,
"jsonl": format_json_lines,
"id": format_bare_id,
"exe": format_bare_exe,
"prefix": format_bare_prefix,
"url": format_bare_url,
"legacy": format_legacy,
"legacy-paths": lambda cmd, i: format_legacy(cmd, i, paths=True),
}
def _get_installs_from_index(indexes, filters):
from .urlutils import sanitise_url
installs = []
seen_ids = set()
for index in indexes:
count = 0
for i in index.find_all(filters, seen_ids=seen_ids, with_prerelease=True):
installs.append(i)
count += 1
LOGGER.debug("Fetched %i installs from %s", count, sanitise_url(index.source_url))
return installs
def execute(cmd):
LOGGER.debug("BEGIN list_command.execute: %r", cmd.args)
try:
LOGGER.debug("Get formatter %s", cmd.format)
formatter = FORMATTERS[cmd.format]
except LookupError:
expect = ", ".join(sorted(FORMATTERS))
raise ArgumentError(f"'{cmd.format}' is not a valid format; expect one of: {expect}") from None
from .tagutils import tag_or_range, install_matches_any
tags = []
for arg in cmd.args:
if arg.casefold() == "default".casefold():
LOGGER.debug("Replacing 'default' with '%s'", cmd.default_tag)
tags.append(tag_or_range(cmd.default_tag))
else:
try:
tags.append(tag_or_range(arg))
except ValueError as ex:
LOGGER.warn("%s", ex)
if cmd.source:
from .indexutils import Index
from .urlutils import IndexDownloader
try:
installs = _get_installs_from_index(
IndexDownloader(cmd.source, Index),
tags,
)
except OSError as ex:
raise SystemExit(1) from ex
elif cmd.install_dir:
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 not cmd.unmanaged:
# Just in case any leak through (e.g. active venv)
installs = [i for i in installs if not i.get("unmanaged")]
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")