Skip to content

Commit 7ff8626

Browse files
committed
Add test
1 parent 8b83213 commit 7ff8626

File tree

2 files changed

+75
-32
lines changed

2 files changed

+75
-32
lines changed

src/manage/aliasutils.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,10 @@ def create_alias(cmd, install, alias, target, *, script_code=None, _link=os.link
127127

128128

129129
def _parse_entrypoint_line(line):
130+
line = line.partition("#")[0]
130131
name, sep, rest = line.partition("=")
131132
name = name.strip()
132-
if name and sep and rest:
133+
if name and name[0].isalnum() and sep and rest:
133134
mod, sep, rest = rest.partition(":")
134135
mod = mod.strip()
135136
if mod and sep and rest:
@@ -140,40 +141,43 @@ def _parse_entrypoint_line(line):
140141
return None, None, None
141142

142143

144+
def _scan_one(root):
145+
# Scan d for dist-info directories with entry_points.txt
146+
dist_info = [d for d in root.glob("*.dist-info") if d.is_dir()]
147+
LOGGER.debug("Found %i dist-info directories in %s", len(dist_info), root)
148+
entrypoints = [f for f in [d / "entry_points.txt" for d in dist_info] if f.is_file()]
149+
LOGGER.debug("Found %i entry_points.txt files in %s", len(entrypoints), root)
150+
151+
# Filter down to [console_scripts] and [gui_scripts]
152+
for ep in entrypoints:
153+
try:
154+
f = open(ep, "r", encoding="utf-8", errors="strict")
155+
except OSError:
156+
LOGGER.debug("Failed to read %s", ep, exc_info=True)
157+
continue
158+
159+
with f:
160+
alias = None
161+
for line in f:
162+
if line.strip() == "[console_scripts]":
163+
alias = dict(windowed=0)
164+
elif line.strip() == "[gui_scripts]":
165+
alias = dict(windowed=1)
166+
elif line.lstrip().startswith("["):
167+
alias = None
168+
elif alias is not None:
169+
name, mod, func = _parse_entrypoint_line(line)
170+
if name and mod and func:
171+
yield (
172+
{**alias, "name": name},
173+
f"import sys; from {mod} import {func}; sys.exit({func}())",
174+
)
175+
176+
143177
def _scan(prefix, dirs):
144178
for dirname in dirs or ():
145179
root = prefix / dirname
146-
147-
# Scan d for dist-info directories with entry_points.txt
148-
dist_info = [d for d in root.listdir() if d.match("*.dist-info") and d.is_dir()]
149-
LOGGER.debug("Found %i dist-info directories in %s", len(dist_info), root)
150-
entrypoints = [f for f in [d / "entry_points.txt" for d in dist_info] if f.is_file()]
151-
LOGGER.debug("Found %i entry_points.txt files in %s", len(entrypoints), root)
152-
153-
# Filter down to [console_scripts] and [gui_scripts]
154-
for ep in entrypoints:
155-
try:
156-
f = open(ep, "r", encoding="utf-8", errors="strict")
157-
except OSError:
158-
LOGGER.debug("Failed to read %s", ep, exc_info=True)
159-
continue
160-
161-
with f:
162-
alias = None
163-
for line in f:
164-
if line.strip() == "[console_scripts]":
165-
alias = dict(windowed=0)
166-
elif line.strip() == "[gui_scripts]":
167-
alias = dict(windowed=1)
168-
elif line.lstrip().startswith("["):
169-
alias = None
170-
elif alias is not None:
171-
name, mod, func = _parse_entrypoint_line(line)
172-
if name and mod and func:
173-
yield (
174-
{**alias, "name": name},
175-
f"import sys; from {mod} import {func}; sys.exit({func}())",
176-
)
180+
yield from _scan_one(root)
177181

178182

179183
def scan_and_create_entrypoints(cmd, install, shortcut, _create_alias=create_alias):

tests/test_alias.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,42 @@ def fake_link(x, y):
216216
assert_log.end_of_log(),
217217
)
218218

219+
220+
def test_parse_entrypoint_line():
221+
for line, expect in [
222+
("", (None, None, None)),
223+
("# comment", (None, None, None)),
224+
("name-only", (None, None, None)),
225+
("name=value", (None, None, None)),
226+
("name=mod:func", ("name", "mod", "func")),
227+
("name=mod:func#comment", ("name", "mod", "func")),
228+
(" name = mod : func ", ("name", "mod", "func")),
229+
("name=mod:func[extra]", ("name", "mod", "func")),
230+
("name=mod:func [extra]", ("name", "mod", "func")),
231+
]:
232+
assert expect == AU._parse_entrypoint_line(line)
233+
234+
235+
def test_scan_entrypoints(tmp_path):
236+
site = tmp_path / "site"
237+
A = site / "a.dist-info"
238+
B = site / "b.dist-info"
239+
A.mkdir(exist_ok=True, parents=True)
240+
B.mkdir(exist_ok=True, parents=True)
241+
(A / "entry_points.txt").write_text("""# Test entries
242+
[console_scripts]
243+
a_cmd = a:main
244+
a2_cmd = a:main2 [spam]
245+
246+
[gui_scripts]
247+
aw_cmd = a:main
248+
""")
249+
actual = list(AU._scan_one(site))
250+
assert [a[0]["name"] for a in actual] == [
251+
"a_cmd", "a2_cmd", "aw_cmd"
252+
]
253+
assert [a[0]["windowed"] for a in actual] == [0, 0, 1]
254+
assert [a[1].rpartition("; ")[2] for a in actual] == [
255+
"sys.exit(main())", "sys.exit(main2())", "sys.exit(main())"
256+
]
257+

0 commit comments

Comments
 (0)