11import pathlib
22import shutil
33import subprocess
4+ import warnings
45from typing import List
56
67from 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 )
0 commit comments