Skip to content

Commit 7f24d36

Browse files
committed
Minor refactoring on alias creation
1 parent c67f0e5 commit 7f24d36

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

src/manage/aliasutils.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def _if_exists(launcher, plat):
4646
return launcher
4747

4848

49-
def create_alias(cmd, install, alias, target, *, script_code=None, _link=os.link):
49+
def create_alias(cmd, install, alias, target, aliases_written, *, script_code=None, _link=os.link):
5050
p = cmd.global_dir / alias["name"]
5151
if not p.match("*.exe"):
5252
p = p.with_name(p.name + ".exe")
@@ -56,12 +56,11 @@ def create_alias(cmd, install, alias, target, *, script_code=None, _link=os.link
5656
if alias.get("windowed"):
5757
launcher = cmd.launcherw_exe or launcher
5858

59-
alias_written = cmd.scratch.setdefault("aliasutils.create_alias.alias_written", set())
6059
n = p.stem.casefold()
61-
if n in alias_written:
60+
if n in aliases_written:
6261
# We've already written this alias in this session, so skip it.
6362
return
64-
alias_written.add(n)
63+
aliases_written.add(n)
6564

6665
plat = install["tag"].rpartition("-")[-1]
6766
if plat:
@@ -231,13 +230,9 @@ def _scan(prefix, dirs):
231230
yield from _scan_one(root)
232231

233232

234-
def scan_and_create_entrypoints(cmd, install, shortcut, *, _create_alias=create_alias, _scan=_scan):
233+
def scan_and_create_entrypoints(cmd, install, shortcut, aliases_written, *, _create_alias=create_alias, _scan=_scan):
235234
prefix = install["prefix"]
236235

237-
# We will be called multiple times, so need to keep the list of names we've
238-
# already used in this session.
239-
known = cmd.scratch.setdefault("aliasutils.scan_and_create_entrypoints.known", set())
240-
241236
aliases = list(install.get("alias", ()))
242237
alias_1 = [a for a in aliases if not a.get("windowed")]
243238
# If no windowed targets, we'll use the non-windowed one
@@ -254,19 +249,13 @@ def scan_and_create_entrypoints(cmd, install, shortcut, *, _create_alias=create_
254249
return
255250

256251
for alias, code in _scan(prefix, shortcut.get("dirs")):
257-
# Only create names once per install command
258-
n = alias["name"].casefold()
259-
if n in known:
260-
continue
261-
known.add(n)
262-
263252
# Copy the launcher template and create a standard __target__ file
264253
target = targets[1 if alias.get("windowed", 0) else 0]
265254
if not target:
266255
LOGGER.debug("No suitable alias found for %s. Skipping this " +
267256
"entrypoint", alias["name"])
268257
continue
269-
_create_alias(cmd, install, alias, target, script_code=code)
258+
_create_alias(cmd, install, alias, target, aliases_written, script_code=code)
270259

271260

272261
def cleanup_alias(cmd, site_dirs_written, *, _unlink_many=atomic_unlink, _scan=_scan):

src/manage/install_command.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ def _cleanup_arp_entries(cmd, install_shortcut_pairs):
250250

251251
def _create_entrypoints(cmd, install, shortcut):
252252
from .aliasutils import scan_and_create_entrypoints
253-
scan_and_create_entrypoints(cmd, install, shortcut)
253+
aliases_written = cmd.scratch.setdefault("aliasutils.create_alias.aliases_written", set())
254+
scan_and_create_entrypoints(cmd, install, shortcut, aliases_written)
254255

255256

256257
def _cleanup_entrypoints(cmd, install_shortcut_pairs):

tests/test_alias.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ def __exit__(self, *exc_info):
4949
pass
5050

5151
def check(self, cmd, tag, name, expect, windowed=0, script_code=None):
52+
created = set()
5253
AU.create_alias(
5354
cmd,
5455
{"tag": tag},
5556
{"name": name, "windowed": windowed},
5657
self._expect_target,
58+
created,
5759
script_code=script_code,
5860
)
5961
print(*cmd.global_dir.glob("*"), sep="\n")
@@ -64,7 +66,7 @@ def check(self, cmd, tag, name, expect, windowed=0, script_code=None):
6466
if script_code:
6567
assert (cmd.global_dir / f"{name}.exe.__script__.py").is_file()
6668
assert (cmd.global_dir / f"{name}.exe.__script__.py").read_text() == script_code
67-
assert name.casefold() in cmd.scratch["aliasutils.create_alias.alias_written"]
69+
assert name.casefold() in created
6870

6971
def check_32(self, cmd, tag, name):
7072
self.check(cmd, tag, name, self._expect["-32"])
@@ -122,11 +124,13 @@ def test_write_alias_launcher_missing(fake_config, assert_log, tmp_path):
122124
fake_config.launcher_exe = tmp_path / "non-existent.exe"
123125
fake_config.default_platform = '-32'
124126
fake_config.global_dir = tmp_path / "bin"
127+
created = set()
125128
AU.create_alias(
126129
fake_config,
127130
{"tag": "test"},
128131
{"name": "test.exe"},
129132
tmp_path / "target.exe",
133+
created,
130134
)
131135
assert_log(
132136
"Checking for launcher.*",
@@ -136,6 +140,7 @@ def test_write_alias_launcher_missing(fake_config, assert_log, tmp_path):
136140
"Skipping %s alias because the launcher template was not found.",
137141
assert_log.end_of_log(),
138142
)
143+
assert "test".casefold() in created
139144

140145

141146
def test_write_alias_launcher_unreadable(fake_config, assert_log, tmp_path):
@@ -156,11 +161,13 @@ def read_bytes():
156161
fake_config.launcher_exe = FakeLauncherPath
157162
fake_config.default_platform = '-32'
158163
fake_config.global_dir = tmp_path / "bin"
164+
created = set()
159165
AU.create_alias(
160166
fake_config,
161167
{"tag": "test"},
162168
{"name": "test.exe"},
163169
tmp_path / "target.exe",
170+
created,
164171
)
165172
assert_log(
166173
"Checking for launcher.*",
@@ -169,6 +176,7 @@ def read_bytes():
169176
"Failed to read %s",
170177
assert_log.end_of_log(),
171178
)
179+
assert "test".casefold() in created
172180

173181

174182
def test_write_alias_launcher_unlinkable(fake_config, assert_log, tmp_path):
@@ -180,11 +188,13 @@ def fake_link(x, y):
180188
fake_config.launcher_exe.write_bytes(b'Arbitrary contents')
181189
fake_config.default_platform = '-32'
182190
fake_config.global_dir = tmp_path / "bin"
191+
created = set()
183192
AU.create_alias(
184193
fake_config,
185194
{"tag": "test"},
186195
{"name": "test.exe"},
187196
tmp_path / "target.exe",
197+
created,
188198
_link=fake_link
189199
)
190200
assert_log(
@@ -194,6 +204,7 @@ def fake_link(x, y):
194204
"Created %s as copy of %s",
195205
assert_log.end_of_log(),
196206
)
207+
assert "test".casefold() in created
197208

198209

199210
def test_write_alias_launcher_unlinkable_remap(fake_config, assert_log, tmp_path):
@@ -216,11 +227,13 @@ def fake_link(x, y):
216227
(tmp_path / "actual_launcher.txt").write_bytes(b'Arbitrary contents')
217228
fake_config.default_platform = '-32'
218229
fake_config.global_dir = tmp_path / "bin"
230+
created = set()
219231
AU.create_alias(
220232
fake_config,
221233
{"tag": "test"},
222234
{"name": "test.exe"},
223235
tmp_path / "target.exe",
236+
created,
224237
_link=fake_link
225238
)
226239
assert_log(
@@ -230,6 +243,7 @@ def fake_link(x, y):
230243
("Created %s as hard link to %s", ("test.exe", "actual_launcher.txt")),
231244
assert_log.end_of_log(),
232245
)
246+
assert "test".casefold() in created
233247

234248

235249
def test_parse_entrypoint_line():
@@ -266,6 +280,7 @@ def test_scan_create_entrypoints(fake_config, tmp_path):
266280
fake_config,
267281
install,
268282
dict(dirs=["site-packages"]),
283+
set(),
269284
_create_alias=lambda *a, **kw: created.append((a, kw)),
270285
)
271286
assert 2 == len(created)
@@ -312,6 +327,7 @@ def fake_scan(*a):
312327
fake_config,
313328
dict(prefix=fake_config.root, id="test", alias=alias),
314329
{},
330+
set(),
315331
_create_alias=lambda *a, **kw: created.append((a, kw)),
316332
_scan=fake_scan,
317333
)

tests/test_install_command.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def test_preserve_site(tmp_path):
103103
preserved = tmp_path / "_root"
104104
site = root / "site-packages"
105105
not_site = root / "site-not-packages"
106+
not_site.mkdir(parents=True, exist_ok=True)
106107
A = site / "A"
107108
B = site / "B.txt"
108109
C = site / "C.txt"

0 commit comments

Comments
 (0)