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/lazy_instance.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
16 #include "base/process/launch.h" | 16 #include "base/process/launch.h" |
17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
19 | 19 |
| 20 #if defined(OS_MACOSX) |
| 21 #include "base/mac/foundation_util.h" |
| 22 #endif |
| 23 |
20 const char kPythonPathEnv[] = "PYTHONPATH"; | 24 const char kPythonPathEnv[] = "PYTHONPATH"; |
21 | 25 |
22 void AppendToPythonPath(const base::FilePath& dir) { | 26 void AppendToPythonPath(const base::FilePath& dir) { |
23 scoped_ptr<base::Environment> env(base::Environment::Create()); | 27 scoped_ptr<base::Environment> env(base::Environment::Create()); |
24 std::string old_path; | 28 std::string old_path; |
25 std::string dir_path; | 29 std::string dir_path; |
26 #if defined(OS_WIN) | 30 #if defined(OS_WIN) |
27 dir_path = base::WideToUTF8(dir.value()); | 31 dir_path = base::WideToUTF8(dir.value()); |
28 #elif defined(OS_POSIX) | 32 #elif defined(OS_POSIX) |
29 dir_path = dir.value(); | 33 dir_path = dir.value(); |
(...skipping 13 matching lines...) Expand all Loading... |
43 } | 47 } |
44 | 48 |
45 bool GetPyProtoPath(base::FilePath* dir) { | 49 bool GetPyProtoPath(base::FilePath* dir) { |
46 // Locate the Python code generated by the protocol buffers compiler. | 50 // Locate the Python code generated by the protocol buffers compiler. |
47 base::FilePath generated_code_dir; | 51 base::FilePath generated_code_dir; |
48 if (!PathService::Get(base::DIR_EXE, &generated_code_dir)) { | 52 if (!PathService::Get(base::DIR_EXE, &generated_code_dir)) { |
49 LOG(ERROR) << "Can't find " << generated_code_dir.value(); | 53 LOG(ERROR) << "Can't find " << generated_code_dir.value(); |
50 return false; | 54 return false; |
51 } | 55 } |
52 | 56 |
| 57 #if defined(OS_MACOSX) |
| 58 if (base::mac::AmIBundled()) |
| 59 generated_code_dir = generated_code_dir.DirName().DirName().DirName(); |
| 60 #endif |
| 61 |
| 62 // Used for GYP. TODO(jam): remove after GN conversion. |
53 const base::FilePath kPyProto(FILE_PATH_LITERAL("pyproto")); | 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; |
| 67 } |
54 | 68 |
55 #if defined(OS_MACOSX) | 69 // Used for GN. |
56 // On Mac, DIR_EXE might be pointing deep into the Release/ (or Debug/) | 70 const base::FilePath kGen(FILE_PATH_LITERAL("gen")); |
57 // directory and we can't depend on how far down it goes. So we walk upwards | 71 if (base::DirectoryExists(generated_code_dir.Append(kGen))) { |
58 // from DIR_EXE until we find a likely looking spot. | 72 *dir = generated_code_dir.Append(kGen); |
59 while (!base::DirectoryExists(generated_code_dir.Append(kPyProto))) { | 73 return true; |
60 base::FilePath parent = generated_code_dir.DirName(); | |
61 if (parent == generated_code_dir) { | |
62 // We hit the root directory. Maybe we didn't build any targets which | |
63 // produced Python protocol buffers. | |
64 return false; | |
65 } | |
66 generated_code_dir = parent; | |
67 } | 74 } |
68 #endif | 75 |
69 *dir = generated_code_dir.Append(kPyProto); | 76 return false; |
70 VLOG(2) << "Found " << kPyProto.value() << " in " << dir->value(); | |
71 return true; | |
72 } | 77 } |
73 | 78 |
74 #if defined(OS_WIN) | 79 #if defined(OS_WIN) |
75 struct PythonExePath { | 80 struct PythonExePath { |
76 PythonExePath() { | 81 PythonExePath() { |
77 // This is test-only code, so CHECK with a subprocess invocation is ok. | 82 // This is test-only code, so CHECK with a subprocess invocation is ok. |
78 base::CommandLine command(base::FilePath(FILE_PATH_LITERAL("cmd"))); | 83 base::CommandLine command(base::FilePath(FILE_PATH_LITERAL("cmd"))); |
79 command.AppendArg("/c"); | 84 command.AppendArg("/c"); |
80 command.AppendArg("python"); | 85 command.AppendArg("python"); |
81 command.AppendArg("-c"); | 86 command.AppendArg("-c"); |
(...skipping 23 matching lines...) Expand all Loading... |
105 #else | 110 #else |
106 python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("python"))); | 111 python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("python"))); |
107 #endif | 112 #endif |
108 | 113 |
109 // Launch python in unbuffered mode, so that python output doesn't mix with | 114 // Launch python in unbuffered mode, so that python output doesn't mix with |
110 // gtest output in buildbot log files. See http://crbug.com/147368. | 115 // gtest output in buildbot log files. See http://crbug.com/147368. |
111 python_cmd->AppendArg("-u"); | 116 python_cmd->AppendArg("-u"); |
112 | 117 |
113 return true; | 118 return true; |
114 } | 119 } |
OLD | NEW |