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/file_util.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/path_service.h"
15 #include "base/strings/utf_string_conversions.h"
17 const char kPythonPathEnv
[] = "PYTHONPATH";
19 void AppendToPythonPath(const base::FilePath
& dir
) {
20 scoped_ptr
<base::Environment
> env(base::Environment::Create());
24 dir_path
= WideToUTF8(dir
.value());
25 #elif defined(OS_POSIX)
26 dir_path
= dir
.value();
28 if (!env
->GetVar(kPythonPathEnv
, &old_path
)) {
29 env
->SetVar(kPythonPathEnv
, dir_path
.c_str());
30 } else if (old_path
.find(dir_path
) == std::string::npos
) {
31 std::string
new_path(old_path
);
34 #elif defined(OS_POSIX)
37 new_path
.append(dir_path
.c_str());
38 env
->SetVar(kPythonPathEnv
, new_path
);
44 // Search for |to_try|, rolling up the directory tree from
45 // |start_dir|. If found, return true and put the path to |to_try| in
46 // |out_dir|. If not, return false and leave |out_dir| untouched.
47 bool TryRelativeToDir(const base::FilePath
& start_dir
,
48 const base::FilePath
& to_try
,
49 base::FilePath
* out_dir
) {
50 base::FilePath
dir(start_dir
);
51 while (!file_util::DirectoryExists(dir
.Append(to_try
))) {
52 base::FilePath parent
= dir
.DirName();
54 // We hit the root directory.
65 bool GetPyProtoPath(base::FilePath
* dir
) {
66 // Locate the Python code generated by the protocol buffers compiler.
67 base::FilePath generated_code_dir
;
68 if (!PathService::Get(base::DIR_EXE
, &generated_code_dir
)) {
69 LOG(ERROR
) << "Can't find " << generated_code_dir
.value();
73 const base::FilePath
kPyProto(FILE_PATH_LITERAL("pyproto"));
75 #if defined(OS_MACOSX) || defined(OS_CHROMEOS)
76 base::FilePath source_dir
;
77 if (!PathService::Get(base::DIR_SOURCE_ROOT
, &source_dir
)) {
78 LOG(ERROR
) << "Can't find " << source_dir
.value();
81 // On Mac, and possibly Chrome OS, DIR_EXE might be pointing deep
82 // into the Release/ (or Debug/) directory and we can't depend on
83 // how far down it goes. So we walk upwards from DIR_EXE until we
84 // find a likely looking spot.
85 if (!TryRelativeToDir(generated_code_dir
, kPyProto
, dir
)) {
86 LOG(WARNING
) << "Can't find " << kPyProto
.value()
87 << " next to " << generated_code_dir
.value();
88 // On Chrome OS, we may have installed the test binaries and support tools
89 // in a wholly separate location, relative to DIR_SOURCE_ROOT. We'll want
90 // to do a similar investigation from that point as well.
91 generated_code_dir
= source_dir
92 .Append(FILE_PATH_LITERAL("out"))
93 .Append(FILE_PATH_LITERAL("Release"));
94 if (!TryRelativeToDir(generated_code_dir
, kPyProto
, dir
)) {
95 LOG(WARNING
) << "Can't find " << kPyProto
.value()
96 << " next to " << generated_code_dir
.value();
100 generated_code_dir
= *dir
;
102 *dir
= generated_code_dir
.Append(kPyProto
);
103 VLOG(2) << "Found " << kPyProto
.value() << " in " << dir
->value();
107 bool GetPythonCommand(CommandLine
* python_cmd
) {
111 if (!PathService::Get(base::DIR_SOURCE_ROOT
, &dir
))
113 dir
= dir
.Append(FILE_PATH_LITERAL("third_party"))
114 .Append(FILE_PATH_LITERAL("python_26"))
115 .Append(FILE_PATH_LITERAL("python.exe"));
116 #elif defined(OS_POSIX)
117 dir
= base::FilePath("python");
120 python_cmd
->SetProgram(dir
);
122 // Launch python in unbuffered mode, so that python output doesn't mix with
123 // gtest output in buildbot log files. See http://crbug.com/147368.
124 python_cmd
->AppendArg("-u");