3
3
import logging
4
4
import os
5
5
import pathlib
6
+ import subprocess
6
7
import typing as t
7
8
8
9
logger = logging .getLogger (__name__ )
@@ -16,9 +17,10 @@ def optional_windows_and_pane(
16
17
The function evaluates the 'if' condition specified in `workspace_dict` to determine inclusion:
17
18
- If 'if' key is not present, it defaults to True.
18
19
- If 'if' is a string or boolean, it's treated as a shell variable.
19
- - 'if' can be a dictionary containing 'shell' or 'python' keys with valid expressions.
20
- - 'shell' expressions are expanded and checked against true values ('y', 'yes', '1', 'on', 'true', 't').
21
- - 'python' expressions are evaluated using `eval()`
20
+ - 'if' can be a dictionary containing 'shell', 'shell_var' or 'python' keys with valid expressions.
21
+ - 'shell_var' expressions are expanded and checked against true values ('y', 'yes', '1', 'on', 'true', 't').
22
+ - 'shell' expressions are evaluated using subprocess
23
+ - 'python' expressions are evaluated using `exec()`
22
24
23
25
Parameters
24
26
----------
@@ -35,18 +37,22 @@ def optional_windows_and_pane(
35
37
if_cond = workspace_dict ["if" ]
36
38
if isinstance (if_cond , (str , bool )):
37
39
# treat this as shell variable
38
- if_cond = {"shell " : if_cond }
39
- if not isinstance (if_cond , dict ) or not ( "shell" in if_cond or "python" in if_cond ):
40
+ if_cond = {"shell_var " : if_cond }
41
+ if not isinstance (if_cond , dict ) or not any ( predicate in if_cond for predicate in ( "python" , "shell" , "shell_var" ) ):
40
42
raise ValueError (f"if conditions does not contains valid expression: { if_cond } " )
43
+ if "shell_var" in if_cond :
44
+ if expandshell (str (if_cond ["shell_var" ])).lower () not in ("y" , "yes" , "1" , "on" , "true" , "t" ):
45
+ return False
41
46
if "shell" in if_cond :
42
- if isinstance (if_cond ["shell" ], str ):
43
- if expandshell (if_cond ["shell" ]).lower () not in ("y" , "yes" , "1" , "on" , "true" , "t" ):
44
- return False
45
- elif isinstance (if_cond ["shell" ], bool ):
46
- if not if_cond ["shell" ]:
47
- return False
47
+ if subprocess .run (if_cond ["shell" ], shell = True ).returncode != 0 :
48
+ return False
48
49
if "python" in if_cond :
49
- if if_cond ["python" ] and not eval (if_cond ["python" ]): # dangerous
50
+ # assign the result of the last statement from the python snippet
51
+ py_statements = if_cond ["python" ].split (";" )
52
+ py_statements [- 1 ] = f"ret={ py_statements [- 1 ]} "
53
+ locals = {}
54
+ exec (";" .join (py_statements ), {}, locals )
55
+ if not locals ['ret' ]:
50
56
return False
51
57
return True
52
58
0 commit comments