7
7
from typing import Iterable
8
8
from typing import List
9
9
from typing import Optional
10
+ from typing import Text
10
11
from typing import Tuple
12
+ from typing import Union
11
13
12
14
from launch .some_substitutions_type import SomeSubstitutionsType
13
15
from launch .substitution import Substitution
16
+ from launch .substitutions .text_substitution import TextSubstitution
14
17
from launch .launch_context import LaunchContext
15
18
from launch .utilities import normalize_to_list_of_substitutions
16
19
from launch .utilities import perform_substitutions
17
20
18
- _global_process_counter_lock = threading .Lock ()
19
- _global_process_counter = 0 # in Python3, this number is unbounded (no rollover)
21
+ _executable_process_counter_lock = threading .Lock ()
22
+ _executable_process_counter = 0 # in Python3, this number is unbounded (no rollover)
23
+
20
24
21
25
class Executable :
22
- """Describes an executable (typically a single process) which may be run by the launch system."""
26
+ """Describes an executable (usually a single process) which may be run by the launch system."""
23
27
24
28
def __init__ (
25
29
self , * ,
26
- cmd : Iterable [SomeSubstitutionsType ],
30
+ cmd : Union [ SomeSubstitutionsType , Iterable [SomeSubstitutionsType ] ],
27
31
name : Optional [SomeSubstitutionsType ] = None ,
28
32
cwd : Optional [SomeSubstitutionsType ] = None ,
29
33
env : Optional [Dict [SomeSubstitutionsType , SomeSubstitutionsType ]] = None ,
@@ -44,7 +48,12 @@ def __init__(
44
48
None, they are added to the current environment. If not, env is updated with
45
49
additional_env.
46
50
"""
47
- self .__cmd = [normalize_to_list_of_substitutions (x ) for x in cmd ]
51
+ if (isinstance (cmd , Text )):
52
+ self .__cmd = [[TextSubstitution (text = cmd )]]
53
+ elif (isinstance (cmd , Substitution )):
54
+ self .__cmd = [[cmd ]]
55
+ else :
56
+ self .__cmd = [normalize_to_list_of_substitutions (x ) for x in cmd ]
48
57
self .__name = name if name is None else normalize_to_list_of_substitutions (name )
49
58
self .__cwd = cwd if cwd is None else normalize_to_list_of_substitutions (cwd )
50
59
self .__env = None # type: Optional[List[Tuple[List[Substitution], List[Substitution]]]]
@@ -88,9 +97,24 @@ def additional_env(self):
88
97
return self .__additional_env
89
98
90
99
@property
91
- def process_details (self ):
92
- """Getter for the substituted executable details, e.g. cmd, cwd, env, or None if substitutions have not been performed."""
93
- return self .__process_event_args
100
+ def final_name (self ):
101
+ """Getter for final_name."""
102
+ return self .__final_name
103
+
104
+ @property
105
+ def final_cmd (self ):
106
+ """Getter for final_cmd."""
107
+ return self .__final_cmd
108
+
109
+ @property
110
+ def final_cwd (self ):
111
+ """Getter for cwd."""
112
+ return self .__final_cwd
113
+
114
+ @property
115
+ def final_env (self ):
116
+ """Getter for final_env."""
117
+ return self .__final_env
94
118
95
119
def apply_context (self , context : LaunchContext ):
96
120
"""
@@ -100,22 +124,22 @@ def apply_context(self, context: LaunchContext):
100
124
- performs substitutions on various properties
101
125
"""
102
126
self .__expand_substitutions (context )
103
- process_event_args = self .__process_event_args
104
- if process_event_args is None :
105
- raise RuntimeError ('process_event_args unexpectedly None' )
106
127
107
128
def __expand_substitutions (self , context ):
108
129
# expand substitutions in arguments to async_execute_process()
109
- cmd = [perform_substitutions (context , x ) for x in self .__cmd ]
130
+ cmd = ' ' .join ([perform_substitutions (context , x ) for x in self .__cmd ])
131
+ cmd = shlex .split (cmd )
132
+ self .__final_cmd = cmd
110
133
name = os .path .basename (cmd [0 ]) if self .__name is None \
111
134
else perform_substitutions (context , self .__name )
112
- with _global_process_counter_lock :
113
- global _global_process_counter
114
- _global_process_counter += 1
115
- self .__name = '{ }-{}' . format ( name , _global_process_counter )
135
+ with _executable_process_counter_lock :
136
+ global _executable_process_counter
137
+ _executable_process_counter += 1
138
+ self .__final_name = f": { name } -{ _executable_process_counter } "
116
139
cwd = None
117
140
if self .__cwd is not None :
118
141
cwd = '' .join ([context .perform_substitution (x ) for x in self .__cwd ])
142
+ self .__final_cwd = cwd
119
143
env = None
120
144
if self .__env is not None :
121
145
env = {}
@@ -128,14 +152,4 @@ def __expand_substitutions(self, context):
128
152
for key , value in self .__additional_env :
129
153
env ['' .join ([context .perform_substitution (x ) for x in key ])] = \
130
154
'' .join ([context .perform_substitution (x ) for x in value ])
131
- # store packed kwargs for all ProcessEvent based events
132
- self .__process_event_args = {
133
- 'description' : self ,
134
- 'name' : self .__name ,
135
- 'cmd' : cmd ,
136
- 'cwd' : cwd ,
137
- 'env' : env ,
138
- # pid is added to the dictionary in the connection_made() method of the protocol.
139
- }
140
-
141
-
155
+ self .__final_env = env
0 commit comments