ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / net / test / python_utils.cc
blob9de1e7de8646b4c3cc30706932cd398a92e2b521
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 const char kPythonPathEnv[] = "PYTHONPATH";
22 void AppendToPythonPath(const base::FilePath& dir) {
23 scoped_ptr<base::Environment> env(base::Environment::Create());
24 std::string old_path;
25 std::string dir_path;
26 #if defined(OS_WIN)
27 dir_path = base::WideToUTF8(dir.value());
28 #elif defined(OS_POSIX)
29 dir_path = dir.value();
30 #endif
31 if (!env->GetVar(kPythonPathEnv, &old_path)) {
32 env->SetVar(kPythonPathEnv, dir_path.c_str());
33 } else if (old_path.find(dir_path) == std::string::npos) {
34 std::string new_path(old_path);
35 #if defined(OS_WIN)
36 new_path.append(";");
37 #elif defined(OS_POSIX)
38 new_path.append(":");
39 #endif
40 new_path.append(dir_path.c_str());
41 env->SetVar(kPythonPathEnv, new_path);
45 bool GetPyProtoPath(base::FilePath* dir) {
46 // Locate the Python code generated by the protocol buffers compiler.
47 base::FilePath generated_code_dir;
48 if (!PathService::Get(base::DIR_EXE, &generated_code_dir)) {
49 LOG(ERROR) << "Can't find " << generated_code_dir.value();
50 return false;
53 const base::FilePath kPyProto(FILE_PATH_LITERAL("pyproto"));
55 #if defined(OS_MACOSX)
56 // On Mac, DIR_EXE might be pointing deep into the Release/ (or Debug/)
57 // directory and we can't depend on how far down it goes. So we walk upwards
58 // from DIR_EXE until we find a likely looking spot.
59 while (!base::DirectoryExists(generated_code_dir.Append(kPyProto))) {
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;
66 generated_code_dir = parent;
68 #endif
69 *dir = generated_code_dir.Append(kPyProto);
70 VLOG(2) << "Found " << kPyProto.value() << " in " << dir->value();
71 return true;
74 #if defined(OS_WIN)
75 struct PythonExePath {
76 PythonExePath() {
77 // This is test-only code, so CHECK with a subprocess invocation is ok.
78 base::CommandLine command(base::FilePath(FILE_PATH_LITERAL("cmd")));
79 command.AppendArg("/c");
80 command.AppendArg("python");
81 command.AppendArg("-c");
82 command.AppendArg("import sys; print sys.executable");
83 std::string output;
84 CHECK(GetAppOutput(command, &output));
85 // This does only work if cmd.exe doesn't use a non-US codepage.
86 path_ = base::ASCIIToUTF16(output);
87 TrimWhitespace(path_, base::TRIM_ALL, &path_);
89 base::string16 path_;
91 static base::LazyInstance<PythonExePath>::Leaky g_python_path;
92 #endif
94 bool GetPythonCommand(base::CommandLine* python_cmd) {
95 DCHECK(python_cmd);
97 #if defined(OS_WIN)
98 // Most developers have depot_tools in their path, which only has a
99 // python.bat, not a python.exe. Go through cmd to find the path to
100 // the python executable.
101 // (Don't just return a a "cmd /c python" command line, because then tests
102 // that try to kill the python process will kill the cmd process instead,
103 // which can cause flakiness.)
104 python_cmd->SetProgram(base::FilePath(g_python_path.Get().path_));
105 #else
106 python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("python")));
107 #endif
109 // 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.
111 python_cmd->AppendArg("-u");
113 return true;