1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 """
37 Common ros script utilities, such as methods convenience methods for
38 creating master xmlrpc proxies and executing rospack. This library
39 is relatively immature and much of the functionality here will
40 likely be moved elsewhere as the API solidifies.
41 """
42
43 import itertools
44 import os
45 import re
46 import string
47 import subprocess
48 import sys
49
50 import roslib.exceptions
51 import roslib.launcher
52 import roslib.message
53 import roslib.msgs
54 import roslib.names
55 import roslib.network
56 import roslib.packages
57 import roslib.rosenv
58
59 PRODUCT = 'ros'
60
61
62 _GLOBAL_CALLER_ID = '/script'
63
64 _is_interactive = False
66 """
67 General API for a script specifying that it is being run in an
68 interactive environment. Many libraries may wish to change their
69 behavior based on being interactive (e.g. disabling signal
70 handlers on Ctrl-C).
71
72 @param interactive: True if current script is being run in an interactive shell
73 @type interactive: bool
74 """
75 global _is_interactive
76 _is_interactive = interactive
77
79 """
80 General API for a script specifying that it is being run in an
81 interactive environment. Many libraries may wish to change their
82 behavior based on being interactive (e.g. disabling signal
83 handlers on Ctrl-C).
84
85 @return: True if interactive flag has been set
86 @rtype: bool
87 """
88 return _is_interactive
89
91 """
92 Remove ROS remapping arguments from sys.argv arguments.
93 @return: copy of sys.argv with ROS remapping arguments removed
94 @rtype: [str]
95 """
96 if argv is None:
97 argv = sys.argv
98 return [a for a in argv if not roslib.names.REMAP in a]
99
121
123 """
124 Get an XMLRPC handle to the Master. It is recommended to use the
125 `rosgraph.masterapi` library instead, as it provides many
126 conveniences.
127
128 @return: XML-RPC proxy to ROS master
129 @rtype: xmlrpclib.ServerProxy
130 """
131 try:
132 import xmlrpc.client as xmlrpcclient
133 except ImportError:
134 import xmlrpclib as xmlrpcclient
135
136
137 uri = roslib.rosenv.get_master_uri()
138 try:
139 roslib.network.parse_http_host_and_port(uri)
140 except ValueError:
141 raise roslib.exceptions.ROSLibException("invalid master URI: %s"%uri)
142 return xmlrpcclient.ServerProxy(uri)
143
144
146 """
147 @return: ServerProxy XML-RPC proxy to ROS parameter server
148 @rtype: xmlrpclib.ServerProxy
149 """
150 return get_master()
151
153 """
154 Check whether or not master think subscriber_id subscribes to topic
155 @return: True if still register as a subscriber
156 @rtype: bool
157 @raise roslib.exceptions.ROSLibException: if communication with master fails
158 """
159 m = get_master()
160 code, msg, state = m.getSystemState(_GLOBAL_CALLER_ID)
161 if code != 1:
162 raise roslib.exceptions.ROSLibException("Unable to retrieve master state: %s"%msg)
163 _, subscribers, _ = state
164 for t, l in subscribers:
165 if t == topic:
166 return subscriber_id in l
167 else:
168 return False
169
171 """
172 Predicate to check whether or not master think publisher_id
173 publishes topic
174 @return: True if still register as a publisher
175 @rtype: bool
176 @raise roslib.exceptions.ROSLibException: if communication with master fails
177 """
178 m = get_master()
179 code, msg, state = m.getSystemState(_GLOBAL_CALLER_ID)
180 if code != 1:
181 raise roslib.exceptions.ROSLibException("Unable to retrieve master state: %s"%msg)
182 pubs, _, _ = state
183 for t, l in pubs:
184 if t == topic:
185 return publisher_id in l
186 else:
187 return False
188
190 """
191 Pretty print cmds, ask if they should be run, and if so, runs
192 them using subprocess.check_call.
193
194 @param cwd: (optional) set cwd of command that is executed
195 @type cwd: str
196 @return: True if cmds were run.
197 """
198
199 def quote(s):
200 return '"%s"'%s if ' ' in s else s
201 sys.stdout.write("Okay to execute:\n\n%s\n(y/n)?\n"%('\n'.join([' '.join([quote(s) for s in c]) for c in cmds])))
202 while 1:
203 input = sys.stdin.readline().strip().lower()
204 if input in ['y', 'n']:
205 break
206 accepted = input == 'y'
207 if accepted:
208 for c in cmds:
209 if cwd:
210 subprocess.check_call(c, cwd=cwd)
211 else:
212 subprocess.check_call(c)
213 return accepted
214