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 import xmlrpclib
132
133 uri = roslib.rosenv.get_master_uri()
134 try:
135 roslib.network.parse_http_host_and_port(uri)
136 except ValueError:
137 raise roslib.exceptions.ROSLibException("invalid master URI: %s"%uri)
138 return xmlrpclib.ServerProxy(uri)
139
140
142 """
143 @return: ServerProxy XML-RPC proxy to ROS parameter server
144 @rtype: xmlrpclib.ServerProxy
145 """
146 return get_master()
147
149 """
150 Check whether or not master think subscriber_id subscribes to topic
151 @return: True if still register as a subscriber
152 @rtype: bool
153 @raise roslib.exceptions.ROSLibException: if communication with master fails
154 """
155 m = get_master()
156 code, msg, state = m.getSystemState(_GLOBAL_CALLER_ID)
157 if code != 1:
158 raise roslib.exceptions.ROSLibException("Unable to retrieve master state: %s"%msg)
159 _, subscribers, _ = state
160 for t, l in subscribers:
161 if t == topic:
162 return subscriber_id in l
163 else:
164 return False
165
167 """
168 Predicate to check whether or not master think publisher_id
169 publishes topic
170 @return: True if still register as a publisher
171 @rtype: bool
172 @raise roslib.exceptions.ROSLibException: if communication with master fails
173 """
174 m = get_master()
175 code, msg, state = m.getSystemState(_GLOBAL_CALLER_ID)
176 if code != 1:
177 raise roslib.exceptions.ROSLibException("Unable to retrieve master state: %s"%msg)
178 pubs, _, _ = state
179 for t, l in pubs:
180 if t == topic:
181 return publisher_id in l
182 else:
183 return False
184
186 """
187 Pretty print cmds, ask if they should be run, and if so, runs
188 them using subprocess.check_call.
189
190 @param cwd: (optional) set cwd of command that is executed
191 @type cwd: str
192 @return: True if cmds were run.
193 """
194
195 def quote(s):
196 return '"%s"'%s if ' ' in s else s
197 print "Okay to execute:\n\n%s\n(y/n)?"%('\n'.join([' '.join([quote(s) for s in c]) for c in cmds]))
198 while 1:
199 input = sys.stdin.readline().strip()
200 if input in ['y', 'n']:
201 break
202 accepted = input == 'y'
203 import subprocess
204 if accepted:
205 for c in cmds:
206 if cwd:
207 subprocess.check_call(c, cwd=cwd)
208 else:
209 subprocess.check_call(c)
210 return accepted
211