Chromium Code Reviews| 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 "base/base_paths.h" | 7 #include "base/base_paths.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/environment.h" | 9 #include "base/environment.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/lazy_instance.h" | |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 16 #include "base/process/launch.h" | |
| 17 #include "base/strings/string_util.h" | |
| 15 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 16 | 19 |
| 17 const char kPythonPathEnv[] = "PYTHONPATH"; | 20 const char kPythonPathEnv[] = "PYTHONPATH"; |
| 18 | 21 |
| 19 void AppendToPythonPath(const base::FilePath& dir) { | 22 void AppendToPythonPath(const base::FilePath& dir) { |
| 20 scoped_ptr<base::Environment> env(base::Environment::Create()); | 23 scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 21 std::string old_path; | 24 std::string old_path; |
| 22 std::string dir_path; | 25 std::string dir_path; |
| 23 #if defined(OS_WIN) | 26 #if defined(OS_WIN) |
| 24 dir_path = base::WideToUTF8(dir.value()); | 27 dir_path = base::WideToUTF8(dir.value()); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 return false; | 102 return false; |
| 100 } | 103 } |
| 101 } | 104 } |
| 102 generated_code_dir = *dir; | 105 generated_code_dir = *dir; |
| 103 #endif | 106 #endif |
| 104 *dir = generated_code_dir.Append(kPyProto); | 107 *dir = generated_code_dir.Append(kPyProto); |
| 105 VLOG(2) << "Found " << kPyProto.value() << " in " << dir->value(); | 108 VLOG(2) << "Found " << kPyProto.value() << " in " << dir->value(); |
| 106 return true; | 109 return true; |
| 107 } | 110 } |
| 108 | 111 |
| 112 #if defined(OS_WIN) | |
| 113 struct PythonExePath { | |
| 114 PythonExePath() { | |
| 115 // This is test-only code, so CHECK with a subprocess invocation is ok. | |
| 116 base::CommandLine command(base::FilePath(FILE_PATH_LITERAL("cmd"))); | |
| 117 command.AppendArg("/c"); | |
| 118 command.AppendArg("python"); | |
| 119 command.AppendArg("-c"); | |
| 120 command.AppendArg("import sys; print sys.executable"); | |
| 121 std::string output; | |
| 122 CHECK(GetAppOutput(command, &output)); | |
| 123 // This does only work if cmd.exe doesn't use a non-US codepage. | |
|
scottmg
2015/01/30 19:46:00
It think it'd only fail if depot_tools src is in a
| |
| 124 path_ = base::ASCIIToUTF16(output); | |
| 125 TrimWhitespace(path_, base::TRIM_ALL, &path_); | |
| 126 } | |
| 127 base::string16 path_; | |
| 128 }; | |
| 129 static base::LazyInstance<PythonExePath>::Leaky g_python_path; | |
| 130 #endif | |
| 131 | |
| 109 bool GetPythonCommand(base::CommandLine* python_cmd) { | 132 bool GetPythonCommand(base::CommandLine* python_cmd) { |
| 110 DCHECK(python_cmd); | 133 DCHECK(python_cmd); |
| 111 | 134 |
| 135 #if defined(OS_WIN) | |
| 136 // Most developers have depot_tools in their path, which only has a | |
| 137 // python.bat, not a python.exe. Go through cmd to find the path to | |
| 138 // the python executable. | |
| 139 // (Don't just return a a "cmd /c python" command line, because then tests | |
| 140 // that try to kill the python process will kill the cmd process instead, | |
| 141 // which can cause flakiness.) | |
| 142 python_cmd->SetProgram(base::FilePath(g_python_path.Get().path_)); | |
| 143 #else | |
| 112 python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("python"))); | 144 python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("python"))); |
| 145 #endif | |
| 113 | 146 |
| 114 // Launch python in unbuffered mode, so that python output doesn't mix with | 147 // Launch python in unbuffered mode, so that python output doesn't mix with |
| 115 // gtest output in buildbot log files. See http://crbug.com/147368. | 148 // gtest output in buildbot log files. See http://crbug.com/147368. |
| 116 python_cmd->AppendArg("-u"); | 149 python_cmd->AppendArg("-u"); |
| 117 | 150 |
| 118 return true; | 151 return true; |
| 119 } | 152 } |
| OLD | NEW |