66from python_compose .unit .compose_unit import ComposeUnit
77
88
9- class CondaUnit (ComposeUnit ):
10- """A Compose Unit for creating Conda environments and running scripts in them."""
9+ class AnacondaUnit (ComposeUnit ):
10+ """A base Compose Unit for creating anaconda-compatible environments and
11+ running scripts in them."""
12+
13+ EXECUTABLE_NAME = ""
1114
1215 def __init__ (
1316 self ,
@@ -19,10 +22,10 @@ def __init__(
1922 """
2023
2124 Args:
22- name: Name of the conda environment to create.
25+ name: Name of the environment to create.
2326 requirements: Either a path to a requirements.txt file or a list of requirements to
2427 install.
25- command: The command to run in the conda environment
28+ command: The command to run in the environment
2629 working_dir: The optional working directory for the command being run.
2730 """
2831 self .name = name
@@ -34,33 +37,49 @@ def create(self) -> None:
3437 """Function for creating a virtual environment."""
3538 envs = [
3639 row .split ()[0 ]
37- for row in subprocess .check_output (["conda" , "env" , "list" ]).decode ().split ("\n " )[2 :]
40+ for row in subprocess .check_output ([self .EXECUTABLE_NAME , "env" , "list" ])
41+ .decode ()
42+ .split ("\n " )[2 :]
3843 if row
3944 ]
4045 if self .name in envs :
4146 warnings .warn (f"Skipping pyenv venv creation for { self .name } . Venv already exists." )
4247 else :
43- subprocess .check_call (["conda" , "create" , "-n" , self .name , "-y" ])
48+ subprocess .check_call ([self . EXECUTABLE_NAME , "create" , "-n" , self .name , "-y" ])
4449
4550 def install_requirements (self ) -> None :
4651 """Function to install any and all requirements for running a script in the virtual
4752 environment."""
4853 if isinstance (self .requirements , list ) and self .requirements :
49- subprocess .check_call (["conda" , "install" , "-n" , self .name ] + self .requirements )
54+ subprocess .check_call (
55+ [self .EXECUTABLE_NAME , "install" , "-n" , self .name ] + self .requirements
56+ )
5057 elif isinstance (self .requirements , pathlib .Path ):
5158 subprocess .check_call (
52- ["conda" , "install" , "-n" , self .name , "-f" , str (self .requirements )]
59+ [self . EXECUTABLE_NAME , "install" , "-n" , self .name , "-f" , str (self .requirements )]
5360 )
5461
5562 def start (self ) -> None :
5663 """Function to start a script in the previously created virtual environment."""
5764 p = subprocess .Popen (
58- ["conda" , "run" , "-n" , self .name , "--no-capture-output" ]
65+ [self . EXECUTABLE_NAME , "run" , "-n" , self .name , "--no-capture-output" ]
5966 + (["--cwd" , str (self .working_dir )] if self .working_dir else [])
6067 + self .command
6168 )
6269 p .communicate ()
6370
6471 def clean (self ) -> None :
6572 """Function to erase any pre-existing files to be recreated by a Python Compose Unit."""
66- subprocess .check_call (["conda" , "remove" , "-n" , self .name , "--all" ])
73+ subprocess .check_call ([self .EXECUTABLE_NAME , "remove" , "-n" , self .name , "--all" ])
74+
75+
76+ class CondaUnit (AnacondaUnit ):
77+ """A Compose Unit for creating conda environments and running scripts in them."""
78+
79+ EXECUTABLE_NAME = "conda"
80+
81+
82+ class MambaUnit (AnacondaUnit ):
83+ """A Compose Unit for creating mamba environments and running scripts in them."""
84+
85+ EXECUTABLE_NAME = "mamba"
0 commit comments