Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions .github/workflows/conda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ jobs:
matrix:
os:
- Ubuntu
- Windows
- MacOs
py:
- "3.8"
- 3.8

steps:
- name: Setup python for test ${{ matrix.py }}
Expand All @@ -21,11 +23,12 @@ jobs:
- uses: conda-incubator/setup-miniconda@v2
with:
auto-update-conda: true
auto-activate-base: false
python-version: ${{ matrix.python-version}}
- uses: actions/checkout@v3
- name: Upgrade pip
run: python -m pip install -U pip
run: python3 -m pip install -U pip
- name: Install dependencies
run: python -m pip install -e '.[test]'
run: python3 -m pip install -e '.[test]'
- name: Run pytest
run: pytest
run: python3 -m pytest
32 changes: 18 additions & 14 deletions .github/workflows/pyenv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@ on: [push, pull_request]

jobs:
job:
name: "Pytest with pyenv"
runs-on: ubuntu-latest
name: test ${{ matrix.py }} - ${{ matrix.os }}
runs-on: ${{ matrix.os }}-latest
strategy:
matrix:
python:
- 3.9
os:
- Ubuntu
- Windows
- MacOs

steps:
- uses: actions/checkout@v3
- name: Install python version
uses: gabrielfalcao/pyenv-action@v13
with:
default: "${{ matrix.python }}"
command: pip install -U pip # upgrade pip after installing python
- name: Install pyenv-virtualenv
run: git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
- name: Install dependencies
run: pip install -e '.[test]'
- name: Run tests
run: pytest
- uses: actions/checkout@v3
- name: Install python version
uses: gabrielfalcao/pyenv-action@v13
with:
default: "${{ matrix.python }}"
command: pip3 install -U pip # upgrade pip after installing python
- name: Install pyenv-virtualenv
run: git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
- name: Install dependencies
run: pip3 install -e '.[test]'
- name: Run tests
run: python3 -m pytest
8 changes: 4 additions & 4 deletions .github/workflows/venv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
matrix:
os:
- Ubuntu
# - Windows
- Windows
- MacOs
py:
- "3.11"
Expand All @@ -25,8 +25,8 @@ jobs:
python-version: ${{ matrix.py }}
- uses: actions/checkout@v3
- name: Upgrade pip
run: python -m pip install -U pip
run: python3 -m pip install -U pip
- name: Install dependencies
run: python -m pip install -e '.[test]'
run: python3 -m pip install -e '.[test]'
- name: Run pytest
run: pytest
run: python3 -m pytest
24 changes: 16 additions & 8 deletions python_compose/unit/conda.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,32 @@ def __init__(

def create(self) -> None:
"""Function for creating a virtual environment."""
envs = [
row.split()[0]
for row in subprocess.check_output(["conda", "env", "list"]).decode().split("\n")[2:]
if row
]
conda_env_list = subprocess.check_output(
["conda", "env", "list"]).decode().splitlines()
envs = []
print(conda_env_list)
if len(conda_env_list) >= 2:
envs = [
row.split()[0]
for row in conda_env_list[2:]
if row
]
if self.name in envs:
warnings.warn(f"Skipping pyenv venv creation for {self.name}. Venv already exists.")
warnings.warn(
f"Skipping pyenv venv creation for {self.name}. Venv already exists.")
else:
subprocess.check_call(["conda", "create", "-n", self.name, "-y"])

def install_requirements(self) -> None:
"""Function to install any and all requirements for running a script in the virtual
environment."""
if isinstance(self.requirements, list) and self.requirements:
subprocess.check_call(["conda", "install", "-n", self.name] + self.requirements)
subprocess.check_call(
["conda", "install", "-n", self.name] + self.requirements)
elif isinstance(self.requirements, pathlib.Path):
subprocess.check_call(
["conda", "install", "-n", self.name, "-f", str(self.requirements)]
["conda", "install", "-n", self.name,
"-f", str(self.requirements)]
)

def start(self) -> None:
Expand Down
31 changes: 22 additions & 9 deletions python_compose/unit/pyenv_virtualenv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import pathlib
import platform
import shutil
import subprocess
import warnings
Expand Down Expand Up @@ -37,32 +38,44 @@ def __init__(

def create(self) -> None:
"""Function for creating a virtual environment."""
self.pyenv_root = pathlib.Path(subprocess.check_output(["pyenv", "root"]).decode().strip())
self.pyenv_root = pathlib.Path(
subprocess.check_output(["pyenv", "root"]).decode().strip())
self.env_path = self.pyenv_root / "versions" / self.name
self.python_path = self.pyenv_root / "versions" / self.name / "bin" / "python"
if platform.system == "Windows":
self.python_path = self.pyenv_root / "versions" / \
self.name / "Scripts" / "python.exe"
else:
self.python_path = self.pyenv_root / "versions" / self.name / "bin" / "python"

subprocess.check_call(["pyenv", "install", self.py_version, "--skip-existing"])
subprocess.check_call(
["pyenv", "install", self.py_version, "--skip-existing"])
if os.path.exists(self.env_path):
warnings.warn(f"Skipping pyenv venv creation for {self.name}. Venv already exists.")
warnings.warn(
f"Skipping pyenv venv creation for {self.name}. Venv already exists.")
else:
subprocess.check_call(["pyenv", "virtualenv", self.py_version, self.name])
subprocess.check_call(
["pyenv", "virtualenv", self.py_version, self.name])

def install_requirements(self) -> None:
"""Function to install any and all requirements for running a script in the virtual
environment."""
subprocess.check_call([str(self.python_path), "-m", "pip", "install", "-U", "pip"])
subprocess.check_call(
[str(self.python_path), "-m", "pip", "install", "-U", "pip"])
if isinstance(self.requirements, list) and self.requirements:
subprocess.check_call(
[str(self.python_path), "-m", "pip", "install"] + self.requirements
[str(self.python_path), "-m", "pip",
"install"] + self.requirements
)
elif isinstance(self.requirements, pathlib.Path):
subprocess.check_call(
[str(self.python_path), "-m", "pip", "install", "-r", str(self.requirements)]
[str(self.python_path), "-m", "pip",
"install", "-r", str(self.requirements)]
)

def start(self) -> None:
"""Function to start a script in the previously created virtual environment"""
p = subprocess.Popen([str(self.python_path), str(self.script_path)] + self.script_args)
p = subprocess.Popen([str(self.python_path), str(
self.script_path)] + self.script_args)
p.communicate()

def clean(self) -> None:
Expand Down
22 changes: 15 additions & 7 deletions python_compose/unit/venv.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
import pathlib
import platform
import shutil
import subprocess
import venv
from typing import List, Union
from venv import EnvBuilder

from python_compose.unit.compose_unit import ComposeUnit

Expand Down Expand Up @@ -31,31 +33,37 @@ def __init__(
self.name = name
self.env_dir = env_dir
self.env_path = self.env_dir / self.name
self.python_path = self.env_path / "bin" / "python"
if platform.system() == "Windows":
self.python_path = self.env_path / "Scripts" / "python.exe"
else:
self.python_path = self.env_path / "bin" / "python"
self.requirements = requirements
self.script_path = script_path
self.script_args = script_args

def create(self) -> None:
"""Function for creating a virtual environment."""
self.env = EnvBuilder(system_site_packages=True, clear=False, with_pip=True)
self.env.create(self.env_path)
venv.create(str(self.env_path), system_site_packages=True,
clear=False, with_pip=True)

def install_requirements(self) -> None:
"""Function to install any and all requirements for running a script in the virtual
environment."""
if isinstance(self.requirements, list) and self.requirements:
subprocess.check_call(
[str(self.python_path), "-m", "pip", "install"] + self.requirements
[str(self.python_path), "-m", "pip",
"install"] + self.requirements
)
elif isinstance(self.requirements, pathlib.Path):
subprocess.check_call(
[str(self.python_path), "-m", "pip", "install", "-r", str(self.requirements)]
[str(self.python_path), "-m", "pip",
"install", "-r", str(self.requirements)]
)

def start(self) -> None:
"""Function to start a script in the previously created virtual environment."""
p = subprocess.Popen([str(self.python_path), str(self.script_path)] + self.script_args)
p = subprocess.Popen([str(self.python_path), str(
self.script_path)] + self.script_args)
p.communicate()

def clean(self) -> None:
Expand Down
6 changes: 4 additions & 2 deletions tests/test_compose.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import pathlib
import platform
import random
import shutil
import string
Expand Down Expand Up @@ -62,7 +63,7 @@ def test_conda(tmp_path: pathlib.Path, random_string: str) -> None:
"test",
[],
[
"python",
"python3",
os.path.join("tests", "create_file.py"),
str(output_file),
random_string,
Expand All @@ -84,7 +85,8 @@ def test_yaml_deserialization() -> None:


def test_pydantic_to_compose_unit() -> None:
units = compose.pydantic_to_units(compose.from_yaml(pathlib.Path("tests") / "config.yaml"))
units = compose.pydantic_to_units(
compose.from_yaml(pathlib.Path("tests") / "config.yaml"))
assert len(units) == 3
counter: Counter[str] = Counter()
for unit in units:
Expand Down