Skip to content

Commit ad3ef24

Browse files
committed
Add windows support
1 parent 32a6c2a commit ad3ef24

File tree

7 files changed

+87
-46
lines changed

7 files changed

+87
-46
lines changed

.github/workflows/conda.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ jobs:
1010
matrix:
1111
os:
1212
- Ubuntu
13+
- Windows
14+
- MacOs
1315
py:
14-
- "3.8"
16+
- 3.8
1517

1618
steps:
1719
- name: Setup python for test ${{ matrix.py }}
@@ -21,11 +23,12 @@ jobs:
2123
- uses: conda-incubator/setup-miniconda@v2
2224
with:
2325
auto-update-conda: true
26+
auto-activate-base: false
2427
python-version: ${{ matrix.python-version}}
2528
- uses: actions/checkout@v3
2629
- name: Upgrade pip
27-
run: python -m pip install -U pip
30+
run: python3 -m pip install -U pip
2831
- name: Install dependencies
29-
run: python -m pip install -e '.[test]'
32+
run: python3 -m pip install -e '.[test]'
3033
- name: Run pytest
31-
run: pytest
34+
run: python3 -m pytest

.github/workflows/pyenv.yml

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,27 @@ on: [push, pull_request]
33

44
jobs:
55
job:
6-
name: "Pytest with pyenv"
7-
runs-on: ubuntu-latest
6+
name: test ${{ matrix.py }} - ${{ matrix.os }}
7+
runs-on: ${{ matrix.os }}-latest
88
strategy:
99
matrix:
1010
python:
1111
- 3.9
12+
os:
13+
- Ubuntu
14+
- Windows
15+
- MacOs
1216

1317
steps:
14-
- uses: actions/checkout@v3
15-
- name: Install python version
16-
uses: gabrielfalcao/pyenv-action@v13
17-
with:
18-
default: "${{ matrix.python }}"
19-
command: pip install -U pip # upgrade pip after installing python
20-
- name: Install pyenv-virtualenv
21-
run: git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
22-
- name: Install dependencies
23-
run: pip install -e '.[test]'
24-
- name: Run tests
25-
run: pytest
18+
- uses: actions/checkout@v3
19+
- name: Install python version
20+
uses: gabrielfalcao/pyenv-action@v13
21+
with:
22+
default: "${{ matrix.python }}"
23+
command: pip3 install -U pip # upgrade pip after installing python
24+
- name: Install pyenv-virtualenv
25+
run: git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
26+
- name: Install dependencies
27+
run: pip3 install -e '.[test]'
28+
- name: Run tests
29+
run: python3 -m pytest

.github/workflows/venv.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
matrix:
1111
os:
1212
- Ubuntu
13-
# - Windows
13+
- Windows
1414
- MacOs
1515
py:
1616
- "3.11"
@@ -25,8 +25,8 @@ jobs:
2525
python-version: ${{ matrix.py }}
2626
- uses: actions/checkout@v3
2727
- name: Upgrade pip
28-
run: python -m pip install -U pip
28+
run: python3 -m pip install -U pip
2929
- name: Install dependencies
30-
run: python -m pip install -e '.[test]'
30+
run: python3 -m pip install -e '.[test]'
3131
- name: Run pytest
32-
run: pytest
32+
run: python3 -m pytest

python_compose/unit/conda.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,32 @@ def __init__(
3232

3333
def create(self) -> None:
3434
"""Function for creating a virtual environment."""
35-
envs = [
36-
row.split()[0]
37-
for row in subprocess.check_output(["conda", "env", "list"]).decode().split("\n")[2:]
38-
if row
39-
]
35+
conda_env_list = subprocess.check_output(
36+
["conda", "env", "list"]).decode().splitlines()
37+
envs = []
38+
print(conda_env_list)
39+
if len(conda_env_list) >= 2:
40+
envs = [
41+
row.split()[0]
42+
for row in conda_env_list[2:]
43+
if row
44+
]
4045
if self.name in envs:
41-
warnings.warn(f"Skipping pyenv venv creation for {self.name}. Venv already exists.")
46+
warnings.warn(
47+
f"Skipping pyenv venv creation for {self.name}. Venv already exists.")
4248
else:
4349
subprocess.check_call(["conda", "create", "-n", self.name, "-y"])
4450

4551
def install_requirements(self) -> None:
4652
"""Function to install any and all requirements for running a script in the virtual
4753
environment."""
4854
if isinstance(self.requirements, list) and self.requirements:
49-
subprocess.check_call(["conda", "install", "-n", self.name] + self.requirements)
55+
subprocess.check_call(
56+
["conda", "install", "-n", self.name] + self.requirements)
5057
elif isinstance(self.requirements, pathlib.Path):
5158
subprocess.check_call(
52-
["conda", "install", "-n", self.name, "-f", str(self.requirements)]
59+
["conda", "install", "-n", self.name,
60+
"-f", str(self.requirements)]
5361
)
5462

5563
def start(self) -> None:

python_compose/unit/pyenv_virtualenv.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import pathlib
3+
import platform
34
import shutil
45
import subprocess
56
import warnings
@@ -37,32 +38,44 @@ def __init__(
3738

3839
def create(self) -> None:
3940
"""Function for creating a virtual environment."""
40-
self.pyenv_root = pathlib.Path(subprocess.check_output(["pyenv", "root"]).decode().strip())
41+
self.pyenv_root = pathlib.Path(
42+
subprocess.check_output(["pyenv", "root"]).decode().strip())
4143
self.env_path = self.pyenv_root / "versions" / self.name
42-
self.python_path = self.pyenv_root / "versions" / self.name / "bin" / "python"
44+
if platform.system == "Windows":
45+
self.python_path = self.pyenv_root / "versions" / \
46+
self.name / "Scripts" / "python.exe"
47+
else:
48+
self.python_path = self.pyenv_root / "versions" / self.name / "bin" / "python"
4349

44-
subprocess.check_call(["pyenv", "install", self.py_version, "--skip-existing"])
50+
subprocess.check_call(
51+
["pyenv", "install", self.py_version, "--skip-existing"])
4552
if os.path.exists(self.env_path):
46-
warnings.warn(f"Skipping pyenv venv creation for {self.name}. Venv already exists.")
53+
warnings.warn(
54+
f"Skipping pyenv venv creation for {self.name}. Venv already exists.")
4755
else:
48-
subprocess.check_call(["pyenv", "virtualenv", self.py_version, self.name])
56+
subprocess.check_call(
57+
["pyenv", "virtualenv", self.py_version, self.name])
4958

5059
def install_requirements(self) -> None:
5160
"""Function to install any and all requirements for running a script in the virtual
5261
environment."""
53-
subprocess.check_call([str(self.python_path), "-m", "pip", "install", "-U", "pip"])
62+
subprocess.check_call(
63+
[str(self.python_path), "-m", "pip", "install", "-U", "pip"])
5464
if isinstance(self.requirements, list) and self.requirements:
5565
subprocess.check_call(
56-
[str(self.python_path), "-m", "pip", "install"] + self.requirements
66+
[str(self.python_path), "-m", "pip",
67+
"install"] + self.requirements
5768
)
5869
elif isinstance(self.requirements, pathlib.Path):
5970
subprocess.check_call(
60-
[str(self.python_path), "-m", "pip", "install", "-r", str(self.requirements)]
71+
[str(self.python_path), "-m", "pip",
72+
"install", "-r", str(self.requirements)]
6173
)
6274

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

6881
def clean(self) -> None:

python_compose/unit/venv.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import os
12
import pathlib
3+
import platform
24
import shutil
35
import subprocess
46
from typing import List, Union
@@ -31,31 +33,40 @@ def __init__(
3133
self.name = name
3234
self.env_dir = env_dir
3335
self.env_path = self.env_dir / self.name
34-
self.python_path = self.env_path / "bin" / "python"
36+
if platform.system() == "Windows":
37+
self.python_path = self.env_path / "Scripts" / "python.exe"
38+
else:
39+
self.python_path = self.env_path / "bin" / "python"
3540
self.requirements = requirements
3641
self.script_path = script_path
3742
self.script_args = script_args
3843

3944
def create(self) -> None:
4045
"""Function for creating a virtual environment."""
41-
self.env = EnvBuilder(system_site_packages=True, clear=False, with_pip=True)
46+
self.env = EnvBuilder(system_site_packages=True,
47+
clear=False, with_pip=True)
48+
if platform.system() == "Windows":
49+
os.makedirs(self.env_path)
4250
self.env.create(self.env_path)
4351

4452
def install_requirements(self) -> None:
4553
"""Function to install any and all requirements for running a script in the virtual
4654
environment."""
4755
if isinstance(self.requirements, list) and self.requirements:
4856
subprocess.check_call(
49-
[str(self.python_path), "-m", "pip", "install"] + self.requirements
57+
[str(self.python_path), "-m", "pip",
58+
"install"] + self.requirements
5059
)
5160
elif isinstance(self.requirements, pathlib.Path):
5261
subprocess.check_call(
53-
[str(self.python_path), "-m", "pip", "install", "-r", str(self.requirements)]
62+
[str(self.python_path), "-m", "pip",
63+
"install", "-r", str(self.requirements)]
5464
)
5565

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

6172
def clean(self) -> None:

tests/test_compose.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import pathlib
3+
import platform
34
import random
45
import shutil
56
import string
@@ -62,7 +63,7 @@ def test_conda(tmp_path: pathlib.Path, random_string: str) -> None:
6263
"test",
6364
[],
6465
[
65-
"python",
66+
"python3",
6667
os.path.join("tests", "create_file.py"),
6768
str(output_file),
6869
random_string,
@@ -84,7 +85,8 @@ def test_yaml_deserialization() -> None:
8485

8586

8687
def test_pydantic_to_compose_unit() -> None:
87-
units = compose.pydantic_to_units(compose.from_yaml(pathlib.Path("tests") / "config.yaml"))
88+
units = compose.pydantic_to_units(
89+
compose.from_yaml(pathlib.Path("tests") / "config.yaml"))
8890
assert len(units) == 3
8991
counter: Counter[str] = Counter()
9092
for unit in units:

0 commit comments

Comments
 (0)