|
7 | 7 | projects like CLSPV. |
8 | 8 | """ |
9 | 9 |
|
10 | | -from typing import Iterable, List |
| 10 | +import os |
| 11 | +from pathlib import Path |
| 12 | +from typing import Iterable, List, Optional |
11 | 13 |
|
12 | 14 | from pythainer.builders import PartialDockerBuilder, UbuntuDockerBuilder |
13 | 15 | from pythainer.builders.utils import cmake_build_install |
@@ -419,3 +421,56 @@ def qemu_builder( |
419 | 421 | builder.workdir(path="..") |
420 | 422 |
|
421 | 423 | return builder |
| 424 | + |
| 425 | +def pyvenv_builder( |
| 426 | + requirements_file_path:Optional[Path] = None, |
| 427 | + dependency_folders:Optional[List[Path]] = None, |
| 428 | + single_packages: Optional[List[str]] = None, |
| 429 | + lib_dir:Path = Path("/home/${USER_NAME}/workspace/libraries") |
| 430 | +) -> PartialDockerBuilder: |
| 431 | + |
| 432 | + """ |
| 433 | + Create a python virtual environment given the specified parameters |
| 434 | +
|
| 435 | + Parameters: |
| 436 | + requirements_file_path (Optional[Path] = None): |
| 437 | + Path to a file structured like a requirements.txt |
| 438 | + dependency_folders (Optional[List[Path]] = None): |
| 439 | + A list of paths that point towards folders with their own |
| 440 | + setup.py or pyproject.toml file, to be added to the venv |
| 441 | + single_packages: |
| 442 | + A list of names of packages such as |
| 443 | + "numpy" or "matplotlib" to be added to the venv. |
| 444 | + lib_dir: |
| 445 | + lib_dir (Path): Directory for libraries and tools. |
| 446 | +
|
| 447 | + Returns: |
| 448 | + PartialDockerBuilder: A builder that, when composed/applied, creates a venv in the container. |
| 449 | + """ |
| 450 | + |
| 451 | + venv_dir = lib_dir / "./python_venv/" |
| 452 | + builder = PartialDockerBuilder() |
| 453 | + |
| 454 | + builder.desc("install the python cvenv") |
| 455 | + builder.user() |
| 456 | + builder.run("mkdir {lib_dir}") |
| 457 | + builder.workdir(venv_dir) |
| 458 | + |
| 459 | + builder.run("python3 -m venv .cvenv") |
| 460 | + builder.run("./.cvenv/bin/python3 -m pip install --upgrade setuptools pip") |
| 461 | + |
| 462 | + if single_packages: |
| 463 | + packagelist = " ".join(single_packages) |
| 464 | + builder.run(f"./.cvenv/bin/python3 -m pip install --upgrade {packagelist}") |
| 465 | + |
| 466 | + if requirements_file_path: |
| 467 | + builder.copy(requirements_file_path, venv_dir / "./requirements_file/requirements.txt") |
| 468 | + builder.run(f"./.cvenv/bin/python3 -m pip install -r {venv_dir / "./requirements_file/requirements.txt"}") |
| 469 | + |
| 470 | + if dependency_folders: |
| 471 | + for path in dependency_folders: |
| 472 | + builder.copy(path,venv_dir / f"./dependencies/{os.path.basename(os.path.normpath(path))}") |
| 473 | + builder.run(f"./.cvenv/bin/python3 -m pip install -e ./dependencies/{os.path.basename(os.path.normpath(path))}/") |
| 474 | + |
| 475 | + builder.env(name="PATH", value=f"$PATH:{venv_dir / ".cvenv/bin/"}") |
| 476 | + return builder |
0 commit comments