python_utils.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "net/test/python_utils.h"
  5. #include <memory>
  6. #include "base/base_paths.h"
  7. #include "base/command_line.h"
  8. #include "base/environment.h"
  9. #include "base/files/file_path.h"
  10. #include "base/files/file_util.h"
  11. #include "base/logging.h"
  12. #include "base/path_service.h"
  13. #include "base/process/launch.h"
  14. #include "build/build_config.h"
  15. namespace {
  16. const base::FilePath::CharType kPythonPathEnv[] =
  17. FILE_PATH_LITERAL("PYTHONPATH");
  18. const base::FilePath::CharType kVPythonClearPathEnv[] =
  19. FILE_PATH_LITERAL("VPYTHON_CLEAR_PYTHONPATH");
  20. } // namespace
  21. void SetPythonPathInEnvironment(const std::vector<base::FilePath>& python_path,
  22. base::EnvironmentMap* map) {
  23. base::NativeEnvironmentString path_str;
  24. for (const auto& path : python_path) {
  25. if (!path_str.empty()) {
  26. #if BUILDFLAG(IS_WIN)
  27. path_str.push_back(';');
  28. #else
  29. path_str.push_back(':');
  30. #endif
  31. }
  32. path_str += path.value();
  33. }
  34. (*map)[kPythonPathEnv] = path_str;
  35. // vpython has instructions on BuildBot (not swarming or LUCI) to clear
  36. // PYTHONPATH on invocation. Since we are clearing and manipulating it
  37. // ourselves, we don't want vpython to throw out our hard work.
  38. (*map)[kVPythonClearPathEnv] = base::NativeEnvironmentString();
  39. }
  40. bool GetPythonCommand(base::CommandLine* python_cmd) {
  41. DCHECK(python_cmd);
  42. // Use vpython to pick up src.git's vpython VirtualEnv spec.
  43. #if BUILDFLAG(IS_WIN)
  44. python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("vpython.bat")));
  45. #else
  46. python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("vpython")));
  47. #endif
  48. // Launch python in unbuffered mode, so that python output doesn't mix with
  49. // gtest output in buildbot log files. See http://crbug.com/147368.
  50. python_cmd->AppendArg("-u");
  51. return true;
  52. }
  53. bool GetPython3Command(base::CommandLine* python_cmd) {
  54. DCHECK(python_cmd);
  55. // Use vpython3 to pick up src.git's vpython3 VirtualEnv spec.
  56. #if BUILDFLAG(IS_WIN)
  57. python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("vpython3.bat")));
  58. #else
  59. python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("vpython3")));
  60. #endif
  61. #if BUILDFLAG(IS_MAC)
  62. // Enable logging to help diagnose https://crbug.com/1254962. Remove this when
  63. // the bug is resolved.
  64. python_cmd->AppendArg("-vpython-log-level=info");
  65. #endif
  66. // Launch python in unbuffered mode, so that python output doesn't mix with
  67. // gtest output in buildbot log files. See http://crbug.com/147368.
  68. python_cmd->AppendArg("-u");
  69. return true;
  70. }