-
Notifications
You must be signed in to change notification settings - Fork 46
/
python.py
72 lines (60 loc) · 2.38 KB
/
python.py
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
from collections.abc import Iterator
from algokit.core.proc import run
from algokit.core.utils import extract_version_triple, find_valid_pipx_command
def find_valid_puyapy_command(version: str | None) -> list[str]:
return _find_puyapy_command_at_version(version) if version is not None else _find_puyapy_command()
def _find_puyapy_command_at_version(version: str) -> list[str]:
"""
Find puyapy command with a specific version.
If the puyapy version isn't installed, install it with pipx run.
"""
for puyapy_command in _get_candidate_puyapy_commands():
try:
puyapy_version_result = run([*puyapy_command, "--version"])
except OSError:
pass # in case of path/permission issues, go to next candidate
else:
if puyapy_version_result.exit_code == 0 and (
extract_version_triple(version) == extract_version_triple(puyapy_version_result.output)
):
return puyapy_command
pipx_command = find_valid_pipx_command(
"Unable to find pipx install so that the `PuyaPy` compiler can be run; "
"please install pipx via https://pypa.github.io/pipx/ "
"and then try `algokit compile python ...` again."
)
return [
*pipx_command,
"run",
f"--spec=puyapy=={version}",
"puyapy",
]
def _find_puyapy_command() -> list[str]:
"""
Find puyapy command.
If puyapy isn't installed, install the latest version with pipx.
"""
for puyapy_command in _get_candidate_puyapy_commands():
try:
puyapy_help_result = run([*puyapy_command, "--version"])
except OSError:
pass # in case of path/permission issues, go to next candidate
else:
if puyapy_help_result.exit_code == 0:
return puyapy_command
pipx_command = find_valid_pipx_command(
"Unable to find pipx install so that the `PuyaPy` compiler can be run; "
"please install pipx via https://pypa.github.io/pipx/ "
"and then try `algokit compile python ...` again."
)
return [
*pipx_command,
"run",
"--spec=puyapy",
"puyapy",
]
def _get_candidate_puyapy_commands() -> Iterator[list[str]]:
# when puyapy is installed at the project level
yield ["poetry", "run", "puyapy"]
# when puyapy is installed at the global level
yield ["puyapy"]