55from __future__ import annotations
66
77import os
8+ import subprocess
89from typing import TypedDict
910
1011
@@ -35,7 +36,12 @@ def get_available_deployments(base_path: str) -> DeploymentDict:
3536 continue
3637
3738 # Skip if ends with "deployments" or starts with "old"
38- if item .endswith ("deployments" ) or item .startswith ("old" ):
39+ if (
40+ item .endswith ("deployments" )
41+ or item .startswith ("old" )
42+ or item .startswith ("." )
43+ or item .startswith ("_" )
44+ ):
3945 continue
4046
4147 # If the item starts with "test_", add to test deployments
@@ -45,3 +51,56 @@ def get_available_deployments(base_path: str) -> DeploymentDict:
4551 out ["production" ].append (item )
4652
4753 return out
54+
55+
56+ def launch_deployment (deployment_path : str , cmd : str , activate_env : bool = True ) -> None :
57+ """
58+ Activate the BEC environment for the specified deployment
59+ and execute the given command. To this end, we open a new terminal window
60+ and run the activation command followed by the specified command.
61+
62+ Only macOS and Linux are supported.
63+
64+ Note that the current process will quit after launching the command.
65+ Args:
66+ deployment_path (str): The path to the deployment.
67+ cmd (str): The command to execute after activation.
68+ activate_env (bool): Whether to activate the BEC virtual environment.
69+ """
70+ activation_command = f"source { os .path .join (deployment_path , 'bec_venv' , 'bin' , 'activate' )} "
71+ if not activate_env :
72+ full_command = cmd
73+ else :
74+ full_command = f"{ activation_command } && { cmd } "
75+ platform = os .uname ().sysname
76+
77+ if platform == "Darwin" : # macOS
78+ iterm_check = subprocess .run (
79+ ["osascript" , "-e" , 'application "iTerm" is running' ],
80+ capture_output = True ,
81+ text = True ,
82+ check = True ,
83+ )
84+ if iterm_check .returncode == 0 :
85+ # iTerm is running
86+ apple_script = f"""
87+ tell application "iTerm"
88+ create window with default profile
89+ tell current session of current window
90+ write text "{ full_command } "
91+ end tell
92+ end tell
93+ """
94+ else :
95+ # iTerm is not running, use Terminal.app
96+ apple_script = f"""
97+ tell application "Terminal"
98+ do script "{ full_command } "
99+ activate
100+ end tell
101+ """
102+ subprocess .Popen (["osascript" , "-e" , apple_script ])
103+ elif platform == "Linux" :
104+ subprocess .Popen (["gnome-terminal" , "--" , "bash" , "-c" , full_command ])
105+ else :
106+ raise NotImplementedError ("This function only supports macOS and Linux." )
0 commit comments