Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: net/test/python_utils.cc

Issue 969313002: Simplify GetPyProtoPath. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 24 matching lines...) Expand all
35 #if defined(OS_WIN) 35 #if defined(OS_WIN)
36 new_path.append(";"); 36 new_path.append(";");
37 #elif defined(OS_POSIX) 37 #elif defined(OS_POSIX)
38 new_path.append(":"); 38 new_path.append(":");
39 #endif 39 #endif
40 new_path.append(dir_path.c_str()); 40 new_path.append(dir_path.c_str());
41 env->SetVar(kPythonPathEnv, new_path); 41 env->SetVar(kPythonPathEnv, new_path);
42 } 42 }
43 } 43 }
44 44
45 namespace {
46
47 #if defined(OS_MACOSX) || defined(OS_CHROMEOS)
48 // Search for |to_try|, rolling up the directory tree from
49 // |start_dir|. If found, return true and put the path to |to_try| in
50 // |out_dir|. If not, return false and leave |out_dir| untouched.
51 bool TryRelativeToDir(const base::FilePath& start_dir,
52 const base::FilePath& to_try,
53 base::FilePath* out_dir) {
54 base::FilePath dir(start_dir);
55 while (!base::DirectoryExists(dir.Append(to_try))) {
56 base::FilePath parent = dir.DirName();
57 if (parent == dir) {
58 // We hit the root directory.
59 return false;
60 }
61 dir = parent;
62 }
63 *out_dir = dir;
64 return true;
65 }
66 #endif // defined(OS_MACOSX) || defined(OS_CHROMEOS)
67
68 } // namespace
69
70 bool GetPyProtoPath(base::FilePath* dir) { 45 bool GetPyProtoPath(base::FilePath* dir) {
71 // Locate the Python code generated by the protocol buffers compiler. 46 // Locate the Python code generated by the protocol buffers compiler.
72 base::FilePath generated_code_dir; 47 base::FilePath generated_code_dir;
73 if (!PathService::Get(base::DIR_EXE, &generated_code_dir)) { 48 if (!PathService::Get(base::DIR_EXE, &generated_code_dir)) {
74 LOG(ERROR) << "Can't find " << generated_code_dir.value(); 49 LOG(ERROR) << "Can't find " << generated_code_dir.value();
75 return false; 50 return false;
76 } 51 }
77 52
78 const base::FilePath kPyProto(FILE_PATH_LITERAL("pyproto")); 53 const base::FilePath kPyProto(FILE_PATH_LITERAL("pyproto"));
79 54
80 #if defined(OS_MACOSX) || defined(OS_CHROMEOS) 55 #if defined(OS_MACOSX)
81 base::FilePath source_dir; 56 // On Mac, DIR_EXE might be pointing deep into the Release/ (or Debug/)
82 if (!PathService::Get(base::DIR_SOURCE_ROOT, &source_dir)) { 57 // directory and we can't depend on how far down it goes. So we walk upwards
83 LOG(ERROR) << "Can't find " << source_dir.value(); 58 // from DIR_EXE until we find a likely looking spot.
84 return false; 59 while (!base::DirectoryExists(generated_code_dir.Append(kPyProto))) {
85 } 60 base::FilePath parent = generated_code_dir.DirName();
86 // On Mac, and possibly Chrome OS, DIR_EXE might be pointing deep 61 if (parent == generated_code_dir) {
87 // into the Release/ (or Debug/) directory and we can't depend on 62 // We hit the root directory. Maybe we didn't build any targets which
88 // how far down it goes. So we walk upwards from DIR_EXE until we 63 // produced Python protocol buffers.
89 // find a likely looking spot.
90 if (!TryRelativeToDir(generated_code_dir, kPyProto, dir)) {
91 LOG(WARNING) << "Can't find " << kPyProto.value()
92 << " next to " << generated_code_dir.value();
93 // On Chrome OS, we may have installed the test binaries and support tools
94 // in a wholly separate location, relative to DIR_SOURCE_ROOT. We'll want
95 // to do a similar investigation from that point as well.
96 generated_code_dir = source_dir
97 .Append(FILE_PATH_LITERAL("out"))
98 .Append(FILE_PATH_LITERAL("Release"));
99 if (!TryRelativeToDir(generated_code_dir, kPyProto, dir)) {
100 LOG(WARNING) << "Can't find " << kPyProto.value()
101 << " next to " << generated_code_dir.value();
102 return false; 64 return false;
103 } 65 }
66 generated_code_dir = parent;
104 } 67 }
105 generated_code_dir = *dir;
106 #endif 68 #endif
107 *dir = generated_code_dir.Append(kPyProto); 69 *dir = generated_code_dir.Append(kPyProto);
108 VLOG(2) << "Found " << kPyProto.value() << " in " << dir->value(); 70 VLOG(2) << "Found " << kPyProto.value() << " in " << dir->value();
109 return true; 71 return true;
110 } 72 }
111 73
112 #if defined(OS_WIN) 74 #if defined(OS_WIN)
113 struct PythonExePath { 75 struct PythonExePath {
114 PythonExePath() { 76 PythonExePath() {
115 // This is test-only code, so CHECK with a subprocess invocation is ok. 77 // This is test-only code, so CHECK with a subprocess invocation is ok.
(...skipping 27 matching lines...) Expand all
143 #else 105 #else
144 python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("python"))); 106 python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("python")));
145 #endif 107 #endif
146 108
147 // Launch python in unbuffered mode, so that python output doesn't mix with 109 // Launch python in unbuffered mode, so that python output doesn't mix with
148 // gtest output in buildbot log files. See http://crbug.com/147368. 110 // gtest output in buildbot log files. See http://crbug.com/147368.
149 python_cmd->AppendArg("-u"); 111 python_cmd->AppendArg("-u");
150 112
151 return true; 113 return true;
152 } 114 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698