64 help=('Run doxygen to generate html documentation from source comments'), |
63 help=('Run doxygen to generate html documentation from source comments'), |
65 action="store_true", default=False, |
64 action="store_true", default=False, |
66 dest='doxygen') |
65 dest='doxygen') |
67 |
66 |
68 opt.add_option('--run', |
67 opt.add_option('--run', |
69 help=('Run a locally built program'), |
68 help=('Run a locally built program; argument can be a program name,' |
|
69 ' or a command starting with the program name.'), |
70 type="string", default='', dest='run') |
70 type="string", default='', dest='run') |
|
71 opt.add_option('--command-template', |
|
72 help=('Template of the command used to run the program given by --run;' |
|
73 ' It should be a shell command string containing %s inside,' |
|
74 ' which will be replaced by the actual program.'), |
|
75 type="string", default=None, dest='command_template') |
71 |
76 |
72 opt.add_option('--shell', |
77 opt.add_option('--shell', |
73 help=('Run a shell with an environment suitably modified to run locally built programs'), |
78 help=('Run a shell with an environment suitably modified to run locally built programs'), |
74 action="store_true", default=False, |
79 action="store_true", default=False, |
75 dest='shell') |
80 dest='shell') |
161 |
166 |
162 if Params.g_options.doxygen: |
167 if Params.g_options.doxygen: |
163 doxygen() |
168 doxygen() |
164 |
169 |
165 if Params.g_options.run: |
170 if Params.g_options.run: |
166 run_program(Params.g_options.run) |
171 run_program(Params.g_options.run, Params.g_options.command_template) |
|
172 raise SystemExit(0) |
|
173 |
|
174 if Params.g_options.command_template: |
|
175 Params.fatal("Option --command-template requires the option --run to be given") |
167 |
176 |
168 def _find_program(program_name, env): |
177 def _find_program(program_name, env): |
169 launch_dir = os.path.abspath(Params.g_cwd_launch) |
178 launch_dir = os.path.abspath(Params.g_cwd_launch) |
170 found_programs = [] |
179 found_programs = [] |
171 for obj in Object.g_allobjs: |
180 for obj in Object.g_allobjs: |
210 else: |
219 else: |
211 os_env[pathvar] = pathsep.join(list(env['NS3_MODULE_PATH'])) |
220 os_env[pathvar] = pathsep.join(list(env['NS3_MODULE_PATH'])) |
212 |
221 |
213 retval = subprocess.Popen(argv, env=os_env).wait() |
222 retval = subprocess.Popen(argv, env=os_env).wait() |
214 if retval: |
223 if retval: |
215 raise SystemExit(retval) |
224 Params.fatal("Command %s exited with code %i" % (argv, retval)) |
216 |
225 |
217 |
226 |
218 def run_program(program_string): |
227 def run_program(program_string, command_template=None): |
|
228 """ |
|
229 if command_template is not None, then program_string == program |
|
230 name and argv is given by command_template with %s replaced by the |
|
231 full path to the program. Else, program_string is interpreted as |
|
232 a shell command with first name being the program name. |
|
233 """ |
219 env = Params.g_build.env_of_name('default') |
234 env = Params.g_build.env_of_name('default') |
220 argv = shlex.split(program_string) |
235 |
221 program_name = argv[0] |
236 if command_template is None: |
222 |
237 argv = shlex.split(program_string) |
223 try: |
238 program_name = argv[0] |
224 program_obj = _find_program(program_name, env) |
239 |
225 except ValueError, ex: |
240 try: |
226 Params.fatal(str(ex)) |
241 program_obj = _find_program(program_name, env) |
227 |
242 except ValueError, ex: |
228 try: |
243 Params.fatal(str(ex)) |
229 program_node, = program_obj.m_linktask.m_outputs |
244 |
230 except AttributeError: |
245 try: |
231 Params.fatal("%s does not appear to be a program" % (program_name,)) |
246 program_node, = program_obj.m_linktask.m_outputs |
232 |
247 except AttributeError: |
233 execvec = [program_node.abspath(env)] + argv[1:] |
248 Params.fatal("%s does not appear to be a program" % (program_name,)) |
|
249 |
|
250 execvec = [program_node.abspath(env)] + argv[1:] |
|
251 |
|
252 else: |
|
253 |
|
254 program_name = program_string |
|
255 try: |
|
256 program_obj = _find_program(program_name, env) |
|
257 except ValueError, ex: |
|
258 Params.fatal(str(ex)) |
|
259 try: |
|
260 program_node, = program_obj.m_linktask.m_outputs |
|
261 except AttributeError: |
|
262 Params.fatal("%s does not appear to be a program" % (program_name,)) |
|
263 |
|
264 execvec = shlex.split(command_template % (program_node.abspath(env),)) |
|
265 |
234 |
266 |
235 former_cwd = os.getcwd() |
267 former_cwd = os.getcwd() |
236 os.chdir(Params.g_cwd_launch) |
268 os.chdir(Params.g_cwd_launch) |
237 try: |
269 try: |
238 return _run_argv(execvec) |
270 retval = _run_argv(execvec) |
239 finally: |
271 finally: |
240 os.chdir(former_cwd) |
272 os.chdir(former_cwd) |
|
273 |
|
274 return retval |
241 |
275 |
242 |
276 |
243 def run_shell(): |
277 def run_shell(): |
244 if sys.platform == 'win32': |
278 if sys.platform == 'win32': |
245 shell = os.environ.get("COMSPEC", "cmd.exe") |
279 shell = os.environ.get("COMSPEC", "cmd.exe") |