| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "net/test/python_utils.h" | 5 #include "net/test/python_utils.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/environment.h" | 11 #include "base/environment.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/process/launch.h" | 17 #include "base/process/launch.h" |
| 18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 20 | 20 |
| 21 #if defined(OS_MACOSX) | 21 #if defined(OS_MACOSX) |
| 22 #include "base/mac/foundation_util.h" | 22 #include "base/mac/foundation_util.h" |
| 23 #endif | 23 #endif |
| 24 | 24 |
| 25 const char kPythonPathEnv[] = "PYTHONPATH"; | 25 const char kPythonPathEnv[] = "PYTHONPATH"; |
| 26 const char kPythonVirtualEnv[] = "VIRTUAL_ENV"; |
| 26 | 27 |
| 27 void ClearPythonPath() { | 28 void ClearPythonPath() { |
| 28 std::unique_ptr<base::Environment> env(base::Environment::Create()); | 29 std::unique_ptr<base::Environment> env(base::Environment::Create()); |
| 29 env->UnSetVar(kPythonPathEnv); | 30 env->UnSetVar(kPythonPathEnv); |
| 30 } | 31 } |
| 31 | 32 |
| 32 void AppendToPythonPath(const base::FilePath& dir) { | 33 void AppendToPythonPath(const base::FilePath& dir) { |
| 33 std::unique_ptr<base::Environment> env(base::Environment::Create()); | 34 std::unique_ptr<base::Environment> env(base::Environment::Create()); |
| 34 std::string old_path; | 35 std::string old_path; |
| 35 std::string dir_path; | 36 std::string dir_path; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 CHECK(GetAppOutput(command, &output)); | 96 CHECK(GetAppOutput(command, &output)); |
| 96 // This does only work if cmd.exe doesn't use a non-US codepage. | 97 // This does only work if cmd.exe doesn't use a non-US codepage. |
| 97 path_ = base::ASCIIToUTF16(output); | 98 path_ = base::ASCIIToUTF16(output); |
| 98 TrimWhitespace(path_, base::TRIM_ALL, &path_); | 99 TrimWhitespace(path_, base::TRIM_ALL, &path_); |
| 99 } | 100 } |
| 100 base::string16 path_; | 101 base::string16 path_; |
| 101 }; | 102 }; |
| 102 static base::LazyInstance<PythonExePath>::Leaky g_python_path; | 103 static base::LazyInstance<PythonExePath>::Leaky g_python_path; |
| 103 #endif | 104 #endif |
| 104 | 105 |
| 106 bool IsInPythonVirtualEnv() { |
| 107 return base::Environment::Create()->HasVar(kPythonVirtualEnv); |
| 108 } |
| 109 |
| 105 bool GetPythonCommand(base::CommandLine* python_cmd) { | 110 bool GetPythonCommand(base::CommandLine* python_cmd) { |
| 106 DCHECK(python_cmd); | 111 DCHECK(python_cmd); |
| 107 | 112 |
| 108 #if defined(OS_WIN) | 113 #if defined(OS_WIN) |
| 109 // Most developers have depot_tools in their path, which only has a | 114 // Most developers have depot_tools in their path, which only has a |
| 110 // python.bat, not a python.exe. Go through cmd to find the path to | 115 // python.bat, not a python.exe. Go through cmd to find the path to |
| 111 // the python executable. | 116 // the python executable. |
| 112 // (Don't just return a a "cmd /c python" command line, because then tests | 117 // (Don't just return a a "cmd /c python" command line, because then tests |
| 113 // that try to kill the python process will kill the cmd process instead, | 118 // that try to kill the python process will kill the cmd process instead, |
| 114 // which can cause flakiness.) | 119 // which can cause flakiness.) |
| 115 python_cmd->SetProgram(base::FilePath(g_python_path.Get().path_)); | 120 python_cmd->SetProgram(base::FilePath(g_python_path.Get().path_)); |
| 116 #else | 121 #else |
| 117 python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("python"))); | 122 python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("python"))); |
| 118 #endif | 123 #endif |
| 119 | 124 |
| 120 // Launch python in unbuffered mode, so that python output doesn't mix with | 125 // Launch python in unbuffered mode, so that python output doesn't mix with |
| 121 // gtest output in buildbot log files. See http://crbug.com/147368. | 126 // gtest output in buildbot log files. See http://crbug.com/147368. |
| 122 python_cmd->AppendArg("-u"); | 127 python_cmd->AppendArg("-u"); |
| 123 | 128 |
| 124 // Prevent using system-installed libraries. Use hermetic versioned copies. | 129 if (!IsInPythonVirtualEnv()) { |
| 125 python_cmd->AppendArg("-S"); | 130 // Prevent using system-installed libraries. Use hermetic versioned copies. |
| 131 python_cmd->AppendArg("-S"); |
| 132 } |
| 126 | 133 |
| 127 return true; | 134 return true; |
| 128 } | 135 } |
| OLD | NEW |