customtabs: Don't crash when the client AIDL is incorrect.
[chromium-blink-merge.git] / net / test / python_utils.cc
blob7d137b7c4117054e20c153a028f6e71a1029e6ae
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.
5 #include "net/test/python_utils.h"
7 #include "base/base_paths.h"
8 #include "base/command_line.h"
9 #include "base/environment.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/lazy_instance.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/path_service.h"
16 #include "base/process/launch.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h"
20 #if defined(OS_MACOSX)
21 #include "base/mac/foundation_util.h"
22 #endif
24 const char kPythonPathEnv[] = "PYTHONPATH";
26 void AppendToPythonPath(const base::FilePath& dir) {
27 scoped_ptr<base::Environment> env(base::Environment::Create());
28 std::string old_path;
29 std::string dir_path;
30 #if defined(OS_WIN)
31 dir_path = base::WideToUTF8(dir.value());
32 #elif defined(OS_POSIX)
33 dir_path = dir.value();
34 #endif
35 if (!env->GetVar(kPythonPathEnv, &old_path)) {
36 env->SetVar(kPythonPathEnv, dir_path.c_str());
37 } else if (old_path.find(dir_path) == std::string::npos) {
38 std::string new_path(old_path);
39 #if defined(OS_WIN)
40 new_path.append(";");
41 #elif defined(OS_POSIX)
42 new_path.append(":");
43 #endif
44 new_path.append(dir_path.c_str());
45 env->SetVar(kPythonPathEnv, new_path);
49 bool GetPyProtoPath(base::FilePath* dir) {
50 // Locate the Python code generated by the protocol buffers compiler.
51 base::FilePath generated_code_dir;
52 if (!PathService::Get(base::DIR_EXE, &generated_code_dir)) {
53 LOG(ERROR) << "Can't find " << generated_code_dir.value();
54 return false;
57 #if defined(OS_MACOSX)
58 if (base::mac::AmIBundled())
59 generated_code_dir = generated_code_dir.DirName().DirName().DirName();
60 #endif
62 // Used for GYP. TODO(jam): remove after GN conversion.
63 const base::FilePath kPyProto(FILE_PATH_LITERAL("pyproto"));
64 if (base::DirectoryExists(generated_code_dir.Append(kPyProto))) {
65 *dir = generated_code_dir.Append(kPyProto);
66 return true;
69 // Used for GN.
70 const base::FilePath kGen(FILE_PATH_LITERAL("gen"));
71 if (base::DirectoryExists(generated_code_dir.Append(kGen))) {
72 *dir = generated_code_dir.Append(kGen);
73 return true;
76 return false;
79 #if defined(OS_WIN)
80 struct PythonExePath {
81 PythonExePath() {
82 // This is test-only code, so CHECK with a subprocess invocation is ok.
83 base::CommandLine command(base::FilePath(FILE_PATH_LITERAL("cmd")));
84 command.AppendArg("/c");
85 command.AppendArg("python");
86 command.AppendArg("-c");
87 command.AppendArg("import sys; print sys.executable");
88 std::string output;
89 CHECK(GetAppOutput(command, &output));
90 // This does only work if cmd.exe doesn't use a non-US codepage.
91 path_ = base::ASCIIToUTF16(output);
92 TrimWhitespace(path_, base::TRIM_ALL, &path_);
94 base::string16 path_;
96 static base::LazyInstance<PythonExePath>::Leaky g_python_path;
97 #endif
99 bool GetPythonCommand(base::CommandLine* python_cmd) {
100 DCHECK(python_cmd);
102 #if defined(OS_WIN)
103 // Most developers have depot_tools in their path, which only has a
104 // python.bat, not a python.exe. Go through cmd to find the path to
105 // the python executable.
106 // (Don't just return a a "cmd /c python" command line, because then tests
107 // that try to kill the python process will kill the cmd process instead,
108 // which can cause flakiness.)
109 python_cmd->SetProgram(base::FilePath(g_python_path.Get().path_));
110 #else
111 python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("python")));
112 #endif
114 // 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.
116 python_cmd->AppendArg("-u");
118 return true;