fractal-server 2.2.0a0__py3-none-any.whl → 2.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fractal_server/__init__.py +1 -1
- fractal_server/app/db/__init__.py +1 -1
- fractal_server/app/models/v1/state.py +1 -2
- fractal_server/app/routes/admin/v1.py +2 -2
- fractal_server/app/routes/admin/v2.py +2 -2
- fractal_server/app/routes/api/v1/job.py +2 -2
- fractal_server/app/routes/api/v1/task_collection.py +4 -4
- fractal_server/app/routes/api/v2/__init__.py +23 -3
- fractal_server/app/routes/api/v2/job.py +2 -2
- fractal_server/app/routes/api/v2/submit.py +6 -0
- fractal_server/app/routes/api/v2/task_collection.py +74 -34
- fractal_server/app/routes/api/v2/task_collection_custom.py +170 -0
- fractal_server/app/routes/api/v2/task_collection_ssh.py +125 -0
- fractal_server/app/routes/aux/_runner.py +10 -2
- fractal_server/app/runner/compress_folder.py +120 -0
- fractal_server/app/runner/executors/slurm/__init__.py +0 -3
- fractal_server/app/runner/executors/slurm/_batching.py +0 -1
- fractal_server/app/runner/executors/slurm/_slurm_config.py +9 -9
- fractal_server/app/runner/executors/slurm/ssh/__init__.py +3 -0
- fractal_server/app/runner/executors/slurm/ssh/_executor_wait_thread.py +112 -0
- fractal_server/app/runner/executors/slurm/ssh/_slurm_job.py +120 -0
- fractal_server/app/runner/executors/slurm/ssh/executor.py +1488 -0
- fractal_server/app/runner/executors/slurm/sudo/__init__.py +3 -0
- fractal_server/app/runner/executors/slurm/{_check_jobs_status.py → sudo/_check_jobs_status.py} +1 -1
- fractal_server/app/runner/executors/slurm/{_executor_wait_thread.py → sudo/_executor_wait_thread.py} +1 -1
- fractal_server/app/runner/executors/slurm/{_subprocess_run_as_user.py → sudo/_subprocess_run_as_user.py} +1 -1
- fractal_server/app/runner/executors/slurm/{executor.py → sudo/executor.py} +12 -12
- fractal_server/app/runner/extract_archive.py +38 -0
- fractal_server/app/runner/v1/__init__.py +78 -40
- fractal_server/app/runner/v1/_slurm/__init__.py +1 -1
- fractal_server/app/runner/v2/__init__.py +183 -82
- fractal_server/app/runner/v2/_local_experimental/__init__.py +22 -12
- fractal_server/app/runner/v2/_local_experimental/executor.py +12 -8
- fractal_server/app/runner/v2/_slurm/__init__.py +1 -6
- fractal_server/app/runner/v2/_slurm_ssh/__init__.py +125 -0
- fractal_server/app/runner/v2/_slurm_ssh/_submit_setup.py +83 -0
- fractal_server/app/runner/v2/_slurm_ssh/get_slurm_config.py +182 -0
- fractal_server/app/runner/v2/runner_functions_low_level.py +9 -11
- fractal_server/app/runner/versions.py +30 -0
- fractal_server/app/schemas/v1/__init__.py +1 -0
- fractal_server/app/schemas/{state.py → v1/state.py} +4 -21
- fractal_server/app/schemas/v2/__init__.py +4 -1
- fractal_server/app/schemas/v2/task_collection.py +101 -30
- fractal_server/config.py +222 -21
- fractal_server/main.py +27 -1
- fractal_server/migrations/env.py +1 -1
- fractal_server/ssh/__init__.py +4 -0
- fractal_server/ssh/_fabric.py +245 -0
- fractal_server/tasks/utils.py +12 -64
- fractal_server/tasks/v1/background_operations.py +2 -2
- fractal_server/tasks/{endpoint_operations.py → v1/endpoint_operations.py} +7 -12
- fractal_server/tasks/v1/utils.py +67 -0
- fractal_server/tasks/v2/_TaskCollectPip.py +61 -32
- fractal_server/tasks/v2/_venv_pip.py +195 -0
- fractal_server/tasks/v2/background_operations.py +257 -295
- fractal_server/tasks/v2/background_operations_ssh.py +317 -0
- fractal_server/tasks/v2/endpoint_operations.py +136 -0
- fractal_server/tasks/v2/templates/_1_create_venv.sh +46 -0
- fractal_server/tasks/v2/templates/_2_upgrade_pip.sh +30 -0
- fractal_server/tasks/v2/templates/_3_pip_install.sh +32 -0
- fractal_server/tasks/v2/templates/_4_pip_freeze.sh +21 -0
- fractal_server/tasks/v2/templates/_5_pip_show.sh +59 -0
- fractal_server/tasks/v2/utils.py +54 -0
- {fractal_server-2.2.0a0.dist-info → fractal_server-2.3.0.dist-info}/METADATA +6 -2
- {fractal_server-2.2.0a0.dist-info → fractal_server-2.3.0.dist-info}/RECORD +68 -44
- fractal_server/tasks/v2/get_collection_data.py +0 -14
- {fractal_server-2.2.0a0.dist-info → fractal_server-2.3.0.dist-info}/LICENSE +0 -0
- {fractal_server-2.2.0a0.dist-info → fractal_server-2.3.0.dist-info}/WHEEL +0 -0
- {fractal_server-2.2.0a0.dist-info → fractal_server-2.3.0.dist-info}/entry_points.txt +0 -0
fractal_server/tasks/utils.py
CHANGED
@@ -1,42 +1,12 @@
|
|
1
1
|
import re
|
2
|
-
import shutil
|
3
|
-
import sys
|
4
2
|
from pathlib import Path
|
5
|
-
from typing import Optional
|
6
3
|
|
7
4
|
from fractal_server.config import get_settings
|
8
|
-
from fractal_server.logger import get_logger
|
9
5
|
from fractal_server.syringe import Inject
|
10
|
-
from fractal_server.utils import execute_command
|
11
6
|
|
12
7
|
COLLECTION_FILENAME = "collection.json"
|
13
8
|
COLLECTION_LOG_FILENAME = "collection.log"
|
14
|
-
|
15
|
-
|
16
|
-
def get_python_interpreter(version: Optional[str] = None) -> str:
|
17
|
-
"""
|
18
|
-
Return the path to the python interpreter
|
19
|
-
|
20
|
-
Args:
|
21
|
-
version: Python version
|
22
|
-
|
23
|
-
Raises:
|
24
|
-
ValueError: If the python version requested is not available on the
|
25
|
-
host.
|
26
|
-
|
27
|
-
Returns:
|
28
|
-
interpreter: string representing the python executable or its path
|
29
|
-
"""
|
30
|
-
if version:
|
31
|
-
interpreter = shutil.which(f"python{version}")
|
32
|
-
if not interpreter:
|
33
|
-
raise ValueError(
|
34
|
-
f"Python version {version} not available on host."
|
35
|
-
)
|
36
|
-
else:
|
37
|
-
interpreter = sys.executable
|
38
|
-
|
39
|
-
return interpreter
|
9
|
+
COLLECTION_FREEZE_FILENAME = "collection_freeze.txt"
|
40
10
|
|
41
11
|
|
42
12
|
def slugify_task_name(task_name: str) -> str:
|
@@ -63,6 +33,10 @@ def get_log_path(base: Path) -> Path:
|
|
63
33
|
return base / COLLECTION_LOG_FILENAME
|
64
34
|
|
65
35
|
|
36
|
+
def get_freeze_path(base: Path) -> Path:
|
37
|
+
return base / COLLECTION_FREEZE_FILENAME
|
38
|
+
|
39
|
+
|
66
40
|
def get_collection_log(venv_path: Path) -> str:
|
67
41
|
package_path = get_absolute_venv_path(venv_path)
|
68
42
|
log_path = get_log_path(package_path)
|
@@ -70,6 +44,13 @@ def get_collection_log(venv_path: Path) -> str:
|
|
70
44
|
return log
|
71
45
|
|
72
46
|
|
47
|
+
def get_collection_freeze(venv_path: Path) -> str:
|
48
|
+
package_path = get_absolute_venv_path(venv_path)
|
49
|
+
freeze_path = get_freeze_path(package_path)
|
50
|
+
freeze = freeze_path.open().read()
|
51
|
+
return freeze
|
52
|
+
|
53
|
+
|
73
54
|
def _normalize_package_name(name: str) -> str:
|
74
55
|
"""
|
75
56
|
Implement PyPa specifications for package-name normalization
|
@@ -86,36 +67,3 @@ def _normalize_package_name(name: str) -> str:
|
|
86
67
|
The normalized package name.
|
87
68
|
"""
|
88
69
|
return re.sub(r"[-_.]+", "-", name).lower()
|
89
|
-
|
90
|
-
|
91
|
-
async def _init_venv(
|
92
|
-
*,
|
93
|
-
path: Path,
|
94
|
-
python_version: Optional[str] = None,
|
95
|
-
logger_name: str,
|
96
|
-
) -> Path:
|
97
|
-
"""
|
98
|
-
Set a virtual environment at `path/venv`
|
99
|
-
|
100
|
-
Args:
|
101
|
-
path : Path
|
102
|
-
path to directory in which to set up the virtual environment
|
103
|
-
python_version : default=None
|
104
|
-
Python version the virtual environment will be based upon
|
105
|
-
|
106
|
-
Returns:
|
107
|
-
python_bin : Path
|
108
|
-
path to python interpreter
|
109
|
-
"""
|
110
|
-
logger = get_logger(logger_name)
|
111
|
-
logger.debug(f"[_init_venv] {path=}")
|
112
|
-
interpreter = get_python_interpreter(version=python_version)
|
113
|
-
logger.debug(f"[_init_venv] {interpreter=}")
|
114
|
-
await execute_command(
|
115
|
-
cwd=path,
|
116
|
-
command=f"{interpreter} -m venv venv",
|
117
|
-
logger_name=logger_name,
|
118
|
-
)
|
119
|
-
python_bin = path / "venv/bin/python"
|
120
|
-
logger.debug(f"[_init_venv] {python_bin=}")
|
121
|
-
return python_bin
|
@@ -6,13 +6,13 @@ import json
|
|
6
6
|
from pathlib import Path
|
7
7
|
from shutil import rmtree as shell_rmtree
|
8
8
|
|
9
|
-
from ..utils import _init_venv
|
10
9
|
from ..utils import _normalize_package_name
|
11
10
|
from ..utils import get_collection_log
|
12
11
|
from ..utils import get_collection_path
|
13
12
|
from ..utils import get_log_path
|
14
13
|
from ..utils import slugify_task_name
|
15
14
|
from ._TaskCollectPip import _TaskCollectPip
|
15
|
+
from .utils import _init_venv_v1
|
16
16
|
from fractal_server.app.db import DBSyncSession
|
17
17
|
from fractal_server.app.db import get_sync_db
|
18
18
|
from fractal_server.app.models.v1 import State
|
@@ -168,7 +168,7 @@ async def _create_venv_install_package(
|
|
168
168
|
task_pkg.package_name = _normalize_package_name(task_pkg.package_name)
|
169
169
|
task_pkg.package = _normalize_package_name(task_pkg.package)
|
170
170
|
|
171
|
-
python_bin = await
|
171
|
+
python_bin = await _init_venv_v1(
|
172
172
|
path=path,
|
173
173
|
python_version=task_pkg.python_version,
|
174
174
|
logger_name=logger_name,
|
@@ -4,12 +4,10 @@ from typing import Optional
|
|
4
4
|
from typing import Union
|
5
5
|
from zipfile import ZipFile
|
6
6
|
|
7
|
-
from
|
8
|
-
from .
|
9
|
-
from .
|
10
|
-
from .v2._TaskCollectPip import _TaskCollectPip as _TaskCollectPipV2
|
7
|
+
from ..utils import _normalize_package_name
|
8
|
+
from ._TaskCollectPip import _TaskCollectPip as _TaskCollectPipV1
|
9
|
+
from .utils import get_python_interpreter_v1
|
11
10
|
from fractal_server.app.schemas.v1 import ManifestV1
|
12
|
-
from fractal_server.app.schemas.v2 import ManifestV2
|
13
11
|
from fractal_server.config import get_settings
|
14
12
|
from fractal_server.logger import get_logger
|
15
13
|
from fractal_server.syringe import Inject
|
@@ -21,13 +19,13 @@ FRACTAL_PUBLIC_TASK_SUBDIR = ".fractal"
|
|
21
19
|
|
22
20
|
async def download_package(
|
23
21
|
*,
|
24
|
-
task_pkg:
|
22
|
+
task_pkg: _TaskCollectPipV1,
|
25
23
|
dest: Union[str, Path],
|
26
24
|
) -> Path:
|
27
25
|
"""
|
28
26
|
Download package to destination
|
29
27
|
"""
|
30
|
-
interpreter =
|
28
|
+
interpreter = get_python_interpreter_v1(version=task_pkg.python_version)
|
31
29
|
pip = f"{interpreter} -m pip"
|
32
30
|
version = (
|
33
31
|
f"=={task_pkg.package_version}" if task_pkg.package_version else ""
|
@@ -43,7 +41,7 @@ async def download_package(
|
|
43
41
|
|
44
42
|
def _load_manifest_from_wheel(
|
45
43
|
path: Path, wheel: ZipFile, logger_name: Optional[str] = None
|
46
|
-
) ->
|
44
|
+
) -> ManifestV1:
|
47
45
|
logger = get_logger(logger_name)
|
48
46
|
namelist = wheel.namelist()
|
49
47
|
try:
|
@@ -60,9 +58,6 @@ def _load_manifest_from_wheel(
|
|
60
58
|
if manifest_version == "1":
|
61
59
|
pkg_manifest = ManifestV1(**manifest_dict)
|
62
60
|
return pkg_manifest
|
63
|
-
elif manifest_version == "2":
|
64
|
-
pkg_manifest = ManifestV2(**manifest_dict)
|
65
|
-
return pkg_manifest
|
66
61
|
else:
|
67
62
|
msg = f"Manifest version {manifest_version=} not supported"
|
68
63
|
logger.error(msg)
|
@@ -140,7 +135,7 @@ def inspect_package(path: Path, logger_name: Optional[str] = None) -> dict:
|
|
140
135
|
|
141
136
|
def create_package_dir_pip(
|
142
137
|
*,
|
143
|
-
task_pkg:
|
138
|
+
task_pkg: _TaskCollectPipV1,
|
144
139
|
create: bool = True,
|
145
140
|
) -> Path:
|
146
141
|
"""
|
@@ -0,0 +1,67 @@
|
|
1
|
+
from pathlib import Path
|
2
|
+
from typing import Optional
|
3
|
+
|
4
|
+
from fractal_server.logger import get_logger
|
5
|
+
from fractal_server.utils import execute_command
|
6
|
+
|
7
|
+
|
8
|
+
def get_python_interpreter_v1(version: Optional[str] = None) -> str:
|
9
|
+
"""
|
10
|
+
Return the path to the python interpreter
|
11
|
+
|
12
|
+
Args:
|
13
|
+
version: Python version
|
14
|
+
|
15
|
+
Raises:
|
16
|
+
ValueError: If the python version requested is not available on the
|
17
|
+
host.
|
18
|
+
|
19
|
+
Returns:
|
20
|
+
interpreter: string representing the python executable or its path
|
21
|
+
"""
|
22
|
+
import shutil
|
23
|
+
import sys
|
24
|
+
|
25
|
+
if version:
|
26
|
+
interpreter = shutil.which(f"python{version}")
|
27
|
+
if not interpreter:
|
28
|
+
raise ValueError(
|
29
|
+
f"Python version {version} not available on host."
|
30
|
+
)
|
31
|
+
else:
|
32
|
+
interpreter = sys.executable
|
33
|
+
|
34
|
+
return interpreter
|
35
|
+
|
36
|
+
|
37
|
+
async def _init_venv_v1(
|
38
|
+
*,
|
39
|
+
path: Path,
|
40
|
+
python_version: Optional[str] = None,
|
41
|
+
logger_name: str,
|
42
|
+
) -> Path:
|
43
|
+
"""
|
44
|
+
Set a virtual environment at `path/venv`
|
45
|
+
|
46
|
+
Args:
|
47
|
+
path : Path
|
48
|
+
path to directory in which to set up the virtual environment
|
49
|
+
python_version : default=None
|
50
|
+
Python version the virtual environment will be based upon
|
51
|
+
|
52
|
+
Returns:
|
53
|
+
python_bin : Path
|
54
|
+
path to python interpreter
|
55
|
+
"""
|
56
|
+
logger = get_logger(logger_name)
|
57
|
+
logger.debug(f"[_init_venv] {path=}")
|
58
|
+
interpreter = get_python_interpreter_v1(version=python_version)
|
59
|
+
logger.debug(f"[_init_venv] {interpreter=}")
|
60
|
+
await execute_command(
|
61
|
+
cwd=path,
|
62
|
+
command=f"{interpreter} -m venv venv",
|
63
|
+
logger_name=logger_name,
|
64
|
+
)
|
65
|
+
python_bin = path / "venv/bin/python"
|
66
|
+
logger.debug(f"[_init_venv] {python_bin=}")
|
67
|
+
return python_bin
|
@@ -1,56 +1,90 @@
|
|
1
|
+
import logging
|
1
2
|
from pathlib import Path
|
2
3
|
from typing import Optional
|
3
4
|
|
5
|
+
from pydantic import BaseModel
|
6
|
+
from pydantic import Extra
|
7
|
+
from pydantic import Field
|
4
8
|
from pydantic import root_validator
|
9
|
+
from pydantic import validator
|
5
10
|
|
11
|
+
from fractal_server.app.schemas._validators import valdictkeys
|
12
|
+
from fractal_server.app.schemas._validators import valstr
|
6
13
|
from fractal_server.app.schemas.v2 import ManifestV2
|
7
|
-
from fractal_server.
|
14
|
+
from fractal_server.tasks.utils import _normalize_package_name
|
15
|
+
from fractal_server.tasks.v2.utils import _parse_wheel_filename
|
8
16
|
|
9
17
|
|
10
|
-
class _TaskCollectPip(
|
18
|
+
class _TaskCollectPip(BaseModel, extra=Extra.forbid):
|
11
19
|
"""
|
12
|
-
Internal
|
20
|
+
Internal task-collection model.
|
13
21
|
|
14
|
-
|
22
|
+
This model is similar to the API request-body model (`TaskCollectPip`), but
|
23
|
+
with enough differences that we keep them separated (and they do not have a
|
24
|
+
common base).
|
15
25
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
26
|
+
Attributes:
|
27
|
+
package: Either a PyPI package name or the absolute path to a wheel
|
28
|
+
file.
|
29
|
+
package_name: The actual normalized name of the package, which is set
|
30
|
+
internally through a validator.
|
31
|
+
package_version: Package version. For local packages, it is set
|
32
|
+
internally through a validator.
|
22
33
|
"""
|
23
34
|
|
24
|
-
|
35
|
+
package: str
|
36
|
+
package_name: str
|
37
|
+
python_version: str
|
38
|
+
package_extras: Optional[str] = None
|
39
|
+
pinned_package_versions: dict[str, str] = Field(default_factory=dict)
|
40
|
+
package_version: Optional[str] = None
|
25
41
|
package_path: Optional[Path] = None
|
26
42
|
package_manifest: Optional[ManifestV2] = None
|
27
43
|
|
44
|
+
_pinned_package_versions = validator(
|
45
|
+
"pinned_package_versions", allow_reuse=True
|
46
|
+
)(valdictkeys("pinned_package_versions"))
|
47
|
+
_package_extras = validator("package_extras", allow_reuse=True)(
|
48
|
+
valstr("package_extras")
|
49
|
+
)
|
50
|
+
_python_version = validator("python_version", allow_reuse=True)(
|
51
|
+
valstr("python_version")
|
52
|
+
)
|
53
|
+
|
28
54
|
@property
|
29
55
|
def is_local_package(self) -> bool:
|
30
56
|
return bool(self.package_path)
|
31
57
|
|
32
58
|
@root_validator(pre=True)
|
33
|
-
def
|
59
|
+
def set_package_info(cls, values):
|
34
60
|
"""
|
35
|
-
|
36
|
-
|
37
|
-
In this case, the user is providing directly a package file, rather
|
38
|
-
than a remote one from PyPI. We set the `package_path` attribute and
|
39
|
-
get the actual package name and version from the package file name.
|
61
|
+
Depending on whether the package is a local wheel file or a PyPI
|
62
|
+
package, set some of its metadata.
|
40
63
|
"""
|
41
64
|
if "/" in values["package"]:
|
65
|
+
# Local package: parse wheel filename
|
42
66
|
package_path = Path(values["package"])
|
43
67
|
if not package_path.is_absolute():
|
44
68
|
raise ValueError("Package path must be absolute")
|
45
|
-
if package_path.exists():
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
69
|
+
if not package_path.exists():
|
70
|
+
logging.warning(
|
71
|
+
f"Package {package_path} does not exist locally."
|
72
|
+
)
|
73
|
+
values["package_path"] = package_path
|
74
|
+
wheel_metadata = _parse_wheel_filename(package_path.name)
|
75
|
+
values["package_name"] = _normalize_package_name(
|
76
|
+
wheel_metadata["distribution"]
|
77
|
+
)
|
78
|
+
values["package_version"] = wheel_metadata["version"]
|
79
|
+
else:
|
80
|
+
# Remote package: use `package` as `package_name`
|
81
|
+
_package = values["package"]
|
82
|
+
if _package.endswith(".whl"):
|
83
|
+
raise ValueError(
|
84
|
+
f"ERROR: package={_package} ends with '.whl' "
|
85
|
+
"but it is not the absolute path to a wheel file."
|
86
|
+
)
|
87
|
+
values["package_name"] = _normalize_package_name(values["package"])
|
54
88
|
return values
|
55
89
|
|
56
90
|
@property
|
@@ -74,10 +108,7 @@ class _TaskCollectPip(TaskCollectPipV2):
|
|
74
108
|
collection_type = "pip_remote"
|
75
109
|
|
76
110
|
package_extras = self.package_extras or ""
|
77
|
-
|
78
|
-
python_version = f"py{self.python_version}"
|
79
|
-
else:
|
80
|
-
python_version = "" # FIXME: can we allow this?
|
111
|
+
python_version = f"py{self.python_version}"
|
81
112
|
|
82
113
|
source = ":".join(
|
83
114
|
(
|
@@ -95,8 +126,6 @@ class _TaskCollectPip(TaskCollectPipV2):
|
|
95
126
|
Verify that the package has all attributes that are needed to continue
|
96
127
|
with task collection
|
97
128
|
"""
|
98
|
-
if not self.package_name:
|
99
|
-
raise ValueError("`package_name` attribute is not set")
|
100
129
|
if not self.package_version:
|
101
130
|
raise ValueError("`package_version` attribute is not set")
|
102
131
|
if not self.package_manifest:
|
@@ -0,0 +1,195 @@
|
|
1
|
+
from pathlib import Path
|
2
|
+
from typing import Optional
|
3
|
+
|
4
|
+
from ..utils import COLLECTION_FREEZE_FILENAME
|
5
|
+
from fractal_server.logger import get_logger
|
6
|
+
from fractal_server.tasks.v2._TaskCollectPip import _TaskCollectPip
|
7
|
+
from fractal_server.tasks.v2.utils import get_python_interpreter_v2
|
8
|
+
from fractal_server.utils import execute_command
|
9
|
+
|
10
|
+
|
11
|
+
async def _pip_install(
|
12
|
+
venv_path: Path,
|
13
|
+
task_pkg: _TaskCollectPip,
|
14
|
+
logger_name: str,
|
15
|
+
) -> Path:
|
16
|
+
"""
|
17
|
+
Install package in venv
|
18
|
+
|
19
|
+
Args:
|
20
|
+
venv_path:
|
21
|
+
task_pkg:
|
22
|
+
logger_name:
|
23
|
+
|
24
|
+
Returns:
|
25
|
+
The location of the package.
|
26
|
+
"""
|
27
|
+
|
28
|
+
logger = get_logger(logger_name)
|
29
|
+
|
30
|
+
pip = venv_path / "venv/bin/pip"
|
31
|
+
|
32
|
+
extras = f"[{task_pkg.package_extras}]" if task_pkg.package_extras else ""
|
33
|
+
|
34
|
+
if task_pkg.is_local_package:
|
35
|
+
pip_install_str = f"{task_pkg.package_path.as_posix()}{extras}"
|
36
|
+
else:
|
37
|
+
version_string = (
|
38
|
+
f"=={task_pkg.package_version}" if task_pkg.package_version else ""
|
39
|
+
)
|
40
|
+
pip_install_str = f"{task_pkg.package_name}{extras}{version_string}"
|
41
|
+
|
42
|
+
await execute_command(
|
43
|
+
cwd=venv_path,
|
44
|
+
command=f"{pip} install --upgrade pip",
|
45
|
+
logger_name=logger_name,
|
46
|
+
)
|
47
|
+
await execute_command(
|
48
|
+
cwd=venv_path,
|
49
|
+
command=f"{pip} install {pip_install_str}",
|
50
|
+
logger_name=logger_name,
|
51
|
+
)
|
52
|
+
if task_pkg.pinned_package_versions:
|
53
|
+
for (
|
54
|
+
pinned_pkg_name,
|
55
|
+
pinned_pkg_version,
|
56
|
+
) in task_pkg.pinned_package_versions.items():
|
57
|
+
|
58
|
+
logger.debug(
|
59
|
+
"Specific version required: "
|
60
|
+
f"{pinned_pkg_name}=={pinned_pkg_version}"
|
61
|
+
)
|
62
|
+
logger.debug(
|
63
|
+
"Preliminary check: verify that "
|
64
|
+
f"{pinned_pkg_version} is already installed"
|
65
|
+
)
|
66
|
+
stdout_show = await execute_command(
|
67
|
+
cwd=venv_path,
|
68
|
+
command=f"{pip} show {pinned_pkg_name}",
|
69
|
+
logger_name=logger_name,
|
70
|
+
)
|
71
|
+
current_version = next(
|
72
|
+
line.split()[-1]
|
73
|
+
for line in stdout_show.split("\n")
|
74
|
+
if line.startswith("Version:")
|
75
|
+
)
|
76
|
+
if current_version != pinned_pkg_version:
|
77
|
+
logger.debug(
|
78
|
+
f"Currently installed version of {pinned_pkg_name} "
|
79
|
+
f"({current_version}) differs from pinned version "
|
80
|
+
f"({pinned_pkg_version}); "
|
81
|
+
f"install version {pinned_pkg_version}."
|
82
|
+
)
|
83
|
+
await execute_command(
|
84
|
+
cwd=venv_path,
|
85
|
+
command=(
|
86
|
+
f"{pip} install "
|
87
|
+
f"{pinned_pkg_name}=={pinned_pkg_version}"
|
88
|
+
),
|
89
|
+
logger_name=logger_name,
|
90
|
+
)
|
91
|
+
else:
|
92
|
+
logger.debug(
|
93
|
+
f"Currently installed version of {pinned_pkg_name} "
|
94
|
+
f"({current_version}) already matches the pinned version."
|
95
|
+
)
|
96
|
+
|
97
|
+
# Extract package installation path from `pip show`
|
98
|
+
stdout_show = await execute_command(
|
99
|
+
cwd=venv_path,
|
100
|
+
command=f"{pip} show {task_pkg.package_name}",
|
101
|
+
logger_name=logger_name,
|
102
|
+
)
|
103
|
+
|
104
|
+
location = Path(
|
105
|
+
next(
|
106
|
+
line.split()[-1]
|
107
|
+
for line in stdout_show.split("\n")
|
108
|
+
if line.startswith("Location:")
|
109
|
+
)
|
110
|
+
)
|
111
|
+
|
112
|
+
# NOTE
|
113
|
+
# https://packaging.python.org/en/latest/specifications/recording-installed-packages/
|
114
|
+
# This directory is named as {name}-{version}.dist-info, with name and
|
115
|
+
# version fields corresponding to Core metadata specifications. Both
|
116
|
+
# fields must be normalized (see the name normalization specification and
|
117
|
+
# the version normalization specification), and replace dash (-)
|
118
|
+
# characters with underscore (_) characters, so the .dist-info directory
|
119
|
+
# always has exactly one dash (-) character in its stem, separating the
|
120
|
+
# name and version fields.
|
121
|
+
package_root = location / (task_pkg.package_name.replace("-", "_"))
|
122
|
+
logger.debug(f"[_pip install] {location=}")
|
123
|
+
logger.debug(f"[_pip install] {task_pkg.package_name=}")
|
124
|
+
logger.debug(f"[_pip install] {package_root=}")
|
125
|
+
|
126
|
+
# Run `pip freeze --all` and store its output
|
127
|
+
stdout_freeze = await execute_command(
|
128
|
+
cwd=venv_path, command=f"{pip} freeze --all", logger_name=logger_name
|
129
|
+
)
|
130
|
+
with (venv_path / COLLECTION_FREEZE_FILENAME).open("w") as f:
|
131
|
+
f.write(stdout_freeze)
|
132
|
+
|
133
|
+
return package_root
|
134
|
+
|
135
|
+
|
136
|
+
async def _init_venv_v2(
|
137
|
+
*,
|
138
|
+
path: Path,
|
139
|
+
python_version: Optional[str] = None,
|
140
|
+
logger_name: str,
|
141
|
+
) -> Path:
|
142
|
+
"""
|
143
|
+
Set a virtual environment at `path/venv`
|
144
|
+
|
145
|
+
Args:
|
146
|
+
path : Path
|
147
|
+
path to directory in which to set up the virtual environment
|
148
|
+
python_version : default=None
|
149
|
+
Python version the virtual environment will be based upon
|
150
|
+
|
151
|
+
Returns:
|
152
|
+
python_bin : Path
|
153
|
+
path to python interpreter
|
154
|
+
"""
|
155
|
+
logger = get_logger(logger_name)
|
156
|
+
logger.debug(f"[_init_venv] {path=}")
|
157
|
+
interpreter = get_python_interpreter_v2(python_version=python_version)
|
158
|
+
logger.debug(f"[_init_venv] {interpreter=}")
|
159
|
+
await execute_command(
|
160
|
+
cwd=path,
|
161
|
+
command=f"{interpreter} -m venv venv",
|
162
|
+
logger_name=logger_name,
|
163
|
+
)
|
164
|
+
python_bin = path / "venv/bin/python"
|
165
|
+
logger.debug(f"[_init_venv] {python_bin=}")
|
166
|
+
return python_bin
|
167
|
+
|
168
|
+
|
169
|
+
async def _create_venv_install_package_pip(
|
170
|
+
*,
|
171
|
+
task_pkg: _TaskCollectPip,
|
172
|
+
path: Path,
|
173
|
+
logger_name: str,
|
174
|
+
) -> tuple[Path, Path]:
|
175
|
+
"""
|
176
|
+
Create venv and install package
|
177
|
+
|
178
|
+
Args:
|
179
|
+
path: the directory in which to create the environment
|
180
|
+
task_pkg: object containing the different metadata required to install
|
181
|
+
the package
|
182
|
+
|
183
|
+
Returns:
|
184
|
+
python_bin: path to venv's python interpreter
|
185
|
+
package_root: the location of the package manifest
|
186
|
+
"""
|
187
|
+
python_bin = await _init_venv_v2(
|
188
|
+
path=path,
|
189
|
+
python_version=task_pkg.python_version,
|
190
|
+
logger_name=logger_name,
|
191
|
+
)
|
192
|
+
package_root = await _pip_install(
|
193
|
+
venv_path=path, task_pkg=task_pkg, logger_name=logger_name
|
194
|
+
)
|
195
|
+
return python_bin, package_root
|