OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/process/kill.h" | 8 #include "base/process/kill.h" |
9 #include "base/process/launch.h" | 9 #include "base/process/launch.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 const char kExecScript_Help[] = | 235 const char kExecScript_Help[] = |
236 "exec_script: Synchronously run a script and return the output.\n" | 236 "exec_script: Synchronously run a script and return the output.\n" |
237 "\n" | 237 "\n" |
238 " exec_script(filename, arguments, input_conversion,\n" | 238 " exec_script(filename, arguments, input_conversion,\n" |
239 " [file_dependencies])\n" | 239 " [file_dependencies])\n" |
240 "\n" | 240 "\n" |
241 " Runs the given script, returning the stdout of the script. The build\n" | 241 " Runs the given script, returning the stdout of the script. The build\n" |
242 " generation will fail if the script does not exist or returns a nonzero\n" | 242 " generation will fail if the script does not exist or returns a nonzero\n" |
243 " exit code.\n" | 243 " exit code.\n" |
244 "\n" | 244 "\n" |
| 245 " The current directory when executing the script will be the root\n" |
| 246 " build directory. If you are passing file names, you will want to use\n" |
| 247 " the to_build_dir() function to make file names relative to this\n" |
| 248 " path (see \"gn help to_build_dir\").\n" |
| 249 "\n" |
245 "Arguments:\n" | 250 "Arguments:\n" |
246 "\n" | 251 "\n" |
247 " filename:\n" | 252 " filename:\n" |
248 " File name of python script to execute, relative to the build file.\n" | 253 " File name of python script to execute. Non-absolute names will\n" |
| 254 " be treated as relative to the current build file.\n" |
249 "\n" | 255 "\n" |
250 " arguments:\n" | 256 " arguments:\n" |
251 " A list of strings to be passed to the script as arguments.\n" | 257 " A list of strings to be passed to the script as arguments.\n" |
252 "\n" | 258 "\n" |
253 " input_conversion:\n" | 259 " input_conversion:\n" |
254 " Controls how the file is read and parsed.\n" | 260 " Controls how the file is read and parsed.\n" |
255 " See \"gn help input_conversion\".\n" | 261 " See \"gn help input_conversion\".\n" |
256 "\n" | 262 "\n" |
257 " dependencies:\n" | 263 " dependencies:\n" |
258 " (Optional) A list of files that this script reads or otherwise\n" | 264 " (Optional) A list of files that this script reads or otherwise\n" |
259 " depends on. These dependencies will be added to the build result\n" | 265 " depends on. These dependencies will be added to the build result\n" |
260 " such that if any of them change, the build will be regenerated and\n" | 266 " such that if any of them change, the build will be regenerated and\n" |
261 " the script will be re-run.\n" | 267 " the script will be re-run.\n" |
262 "\n" | 268 "\n" |
263 " The script itself will be an implicit dependency so you do not\n" | 269 " The script itself will be an implicit dependency so you do not\n" |
264 " need to list it.\n" | 270 " need to list it.\n" |
265 "\n" | 271 "\n" |
266 "Example:\n" | 272 "Example:\n" |
267 "\n" | 273 "\n" |
268 " all_lines = exec_script(\"myscript.py\", [some_input], \"list lines\",\n" | 274 " all_lines = exec_script(\"myscript.py\", [some_input], \"list lines\",\n" |
269 " [\"data_file.txt\"])\n"; | 275 " [ to_build_dir(\"data_file.txt\") ])\n"; |
270 | 276 |
271 Value RunExecScript(Scope* scope, | 277 Value RunExecScript(Scope* scope, |
272 const FunctionCallNode* function, | 278 const FunctionCallNode* function, |
273 const std::vector<Value>& args, | 279 const std::vector<Value>& args, |
274 Err* err) { | 280 Err* err) { |
275 if (args.size() != 3 && args.size() != 4) { | 281 if (args.size() != 3 && args.size() != 4) { |
276 *err = Err(function->function(), "Wrong number of args to write_file", | 282 *err = Err(function->function(), "Wrong number of args to write_file", |
277 "I expected three or four arguments."); | 283 "I expected three or four arguments."); |
278 return Value(); | 284 return Value(); |
279 } | 285 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 base::TimeTicks begin_exec; | 335 base::TimeTicks begin_exec; |
330 if (g_scheduler->verbose_logging()) { | 336 if (g_scheduler->verbose_logging()) { |
331 #if defined(OS_WIN) | 337 #if defined(OS_WIN) |
332 g_scheduler->Log("Pythoning", UTF16ToUTF8(cmdline.GetCommandLineString())); | 338 g_scheduler->Log("Pythoning", UTF16ToUTF8(cmdline.GetCommandLineString())); |
333 #else | 339 #else |
334 g_scheduler->Log("Pythoning", cmdline.GetCommandLineString()); | 340 g_scheduler->Log("Pythoning", cmdline.GetCommandLineString()); |
335 #endif | 341 #endif |
336 begin_exec = base::TimeTicks::Now(); | 342 begin_exec = base::TimeTicks::Now(); |
337 } | 343 } |
338 | 344 |
| 345 base::FilePath startup_dir = |
| 346 build_settings->GetFullPath(build_settings->build_dir()); |
| 347 |
339 // Execute the process. | 348 // Execute the process. |
340 // TODO(brettw) set the environment block. | 349 // TODO(brettw) set the environment block. |
341 std::string output; | 350 std::string output; |
342 std::string stderr_output; // TODO(brettw) not hooked up, see above. | 351 std::string stderr_output; // TODO(brettw) not hooked up, see above. |
343 int exit_code = 0; | 352 int exit_code = 0; |
344 if (!ExecProcess(cmdline, build_settings->GetFullPath(cur_dir), | 353 if (!ExecProcess(cmdline, startup_dir, |
345 &output, &stderr_output, &exit_code)) { | 354 &output, &stderr_output, &exit_code)) { |
346 *err = Err(function->function(), "Could not execute python.", | 355 *err = Err(function->function(), "Could not execute python.", |
347 "I was trying to execute \"" + FilePathToUTF8(python_path) + "\"."); | 356 "I was trying to execute \"" + FilePathToUTF8(python_path) + "\"."); |
348 return Value(); | 357 return Value(); |
349 } | 358 } |
350 if (g_scheduler->verbose_logging()) { | 359 if (g_scheduler->verbose_logging()) { |
351 g_scheduler->Log("Pythoning", script_source.value() + " took " + | 360 g_scheduler->Log("Pythoning", script_source.value() + " took " + |
352 base::Int64ToString( | 361 base::Int64ToString( |
353 (base::TimeTicks::Now() - begin_exec).InMilliseconds()) + | 362 (base::TimeTicks::Now() - begin_exec).InMilliseconds()) + |
354 "ms"); | 363 "ms"); |
355 } | 364 } |
356 | 365 |
357 // TODO(brettw) maybe we need stderr also for reasonable stack dumps. | 366 // TODO(brettw) maybe we need stderr also for reasonable stack dumps. |
358 if (exit_code != 0) { | 367 if (exit_code != 0) { |
359 std::string msg = "Current dir: " + | 368 std::string msg = "Current dir: " + FilePathToUTF8(startup_dir) + |
360 FilePathToUTF8(build_settings->GetFullPath(cur_dir)) + "\nCommand: " + | 369 "\nCommand: " + cmdline.GetCommandLineString() + |
361 cmdline.GetCommandLineString() + | |
362 "\nReturned " + base::IntToString(exit_code); | 370 "\nReturned " + base::IntToString(exit_code); |
363 if (!output.empty()) | 371 if (!output.empty()) |
364 msg += " and printed out:\n\n" + output; | 372 msg += " and printed out:\n\n" + output; |
365 else | 373 else |
366 msg += "."; | 374 msg += "."; |
367 *err = Err(function->function(), "Script returned non-zero exit code.", | 375 *err = Err(function->function(), "Script returned non-zero exit code.", |
368 msg); | 376 msg); |
369 return Value(); | 377 return Value(); |
370 } | 378 } |
371 | 379 |
372 return ConvertInputToValue(output, function, args[2], err); | 380 return ConvertInputToValue(output, function, args[2], err); |
373 } | 381 } |
374 | 382 |
375 } // namespace functions | 383 } // namespace functions |
OLD | NEW |