Skip to content

Commit fd85c1d

Browse files
committed
Added a poetry unit
1 parent ef257fc commit fd85c1d

8 files changed

Lines changed: 28 additions & 8 deletions

File tree

.github/workflows/conda.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ jobs:
2222
with:
2323
auto-update-conda: true
2424
python-version: ${{ matrix.python-version}}
25+
- name: Install and configure Poetry
26+
uses: snok/install-poetry@v1
2527
- uses: actions/checkout@v3
2628
- name: Upgrade pip
2729
run: python -m pip install -U pip

.github/workflows/formatting.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ jobs:
1818
uses: actions/setup-python@v3
1919
with:
2020
python-version: ${{ matrix.py }}
21+
- name: Install and configure Poetry
22+
uses: snok/install-poetry@v1
2123
- uses: actions/checkout@v3
2224
- name: Upgrade pip
2325
run: python -m pip install -U pip

.github/workflows/mamba.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ jobs:
2222
with:
2323
auto-update-conda: true
2424
python-version: ${{ matrix.python-version}}
25+
- name: Install and configure Poetry
26+
uses: snok/install-poetry@v1
2527
- name: Install mamba
2628
run: conda install mamba -n base -c conda-forge -y
2729
- uses: actions/checkout@v3

.github/workflows/pyenv.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ jobs:
1717
with:
1818
default: "${{ matrix.python }}"
1919
command: pip install -U pip # upgrade pip after installing python
20+
- name: Install and configure Poetry
21+
uses: snok/install-poetry@v1
2022
- name: Install pyenv-virtualenv
2123
run: git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
2224
- name: Install dependencies

.github/workflows/venv.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ jobs:
2323
uses: actions/setup-python@v3
2424
with:
2525
python-version: ${{ matrix.py }}
26+
- name: Install and configure Poetry
27+
uses: snok/install-poetry@v1
2628
- uses: actions/checkout@v3
2729
- name: Upgrade pip
2830
run: python -m pip install -U pip
29-
- name: Install poetry
30-
run: python -m pip install poetry
3131
- name: Install dependencies
3232
run: python -m pip install -e '.[test]'
3333
- name: Run pytest

examples/poetry/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ readme = "README.md"
66
authors = ["A <[email protected]>"]
77

88
[tool.poetry.dependencies]
9-
python = "^3.11"
9+
python = "^3.8"
1010

1111

1212
[build-system]

python_compose/unit/poetry.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pathlib
22
import shutil
33
import subprocess
4+
import warnings
45
from typing import List
56

67
from python_compose.unit.compose_unit import ComposeUnit
@@ -25,19 +26,29 @@ def __init__(
2526
self.source_dir = source_dir
2627
self.script_path = script_path
2728
self.script_args = script_args
28-
self.env_dir = subprocess.check_output(
29+
self.env_dir = ""
30+
try:
31+
self.env_dir = self.env()
32+
except subprocess.CalledProcessError:
33+
warnings.warn(f"No environment has been created for {self.source_dir}")
34+
35+
def env(self) -> str:
36+
"""Get the virtual environment path for this unit."""
37+
return subprocess.check_output(
2938
["poetry", "env", "info", "--path"], cwd=self.source_dir
30-
)
39+
).decode()
3140

3241
def create(self) -> None:
3342
"""Function for creating a virtual environment."""
34-
# Poetry creates the virtual environments on initialization so we don't have to here.
43+
# Poetry creates the virtual environments on installation so we don't have to here.
3544
pass
3645

3746
def install_requirements(self) -> None:
3847
"""Function to install any and all requirements for running a script in the virtual
3948
environment."""
4049
subprocess.check_call(["poetry", "install"], cwd=self.source_dir)
50+
if not self.env_dir:
51+
self.env_dir = self.env()
4152

4253
def start(self) -> None:
4354
"""Function to start a script in the previously created virtual environment."""
@@ -49,4 +60,5 @@ def start(self) -> None:
4960

5061
def clean(self) -> None:
5162
"""Function to erase any pre-existing files to: be recreated by a Python Compose Unit."""
52-
shutil.rmtree(self.env_dir)
63+
if self.env_dir:
64+
shutil.rmtree(self.env_dir)

tests/poetry_test/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ readme = "README.md"
77
packages = [{include = "poetry_test"}]
88

99
[tool.poetry.dependencies]
10-
python = "^3.11"
10+
python = "^3.8"
1111

1212

1313
[build-system]

0 commit comments

Comments
 (0)