- add sources.
[platform/framework/web/crosswalk.git] / src / net / test / python_utils.cc
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.
4
5 #include "net/test/python_utils.h"
6
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"
16
17 const char kPythonPathEnv[] = "PYTHONPATH";
18
19 void AppendToPythonPath(const base::FilePath& dir) {
20   scoped_ptr<base::Environment> env(base::Environment::Create());
21   std::string old_path;
22   std::string dir_path;
23 #if defined(OS_WIN)
24   dir_path = WideToUTF8(dir.value());
25 #elif defined(OS_POSIX)
26   dir_path = dir.value();
27 #endif
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);
32 #if defined(OS_WIN)
33     new_path.append(";");
34 #elif defined(OS_POSIX)
35     new_path.append(":");
36 #endif
37     new_path.append(dir_path.c_str());
38     env->SetVar(kPythonPathEnv, new_path);
39   }
40 }
41
42 namespace {
43
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 (!base::DirectoryExists(dir.Append(to_try))) {
52     base::FilePath parent = dir.DirName();
53     if (parent == dir) {
54       // We hit the root directory.
55       return false;
56     }
57     dir = parent;
58   }
59   *out_dir = dir;
60   return true;
61 }
62
63 }  // namespace
64
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();
70     return false;
71   }
72
73   const base::FilePath kPyProto(FILE_PATH_LITERAL("pyproto"));
74
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();
79     return false;
80   }
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();
97       return false;
98     }
99   }
100   generated_code_dir = *dir;
101 #endif
102   *dir = generated_code_dir.Append(kPyProto);
103   VLOG(2) << "Found " << kPyProto.value() << " in " << dir->value();
104   return true;
105 }
106
107 bool GetPythonCommand(CommandLine* python_cmd) {
108   DCHECK(python_cmd);
109
110   python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("python")));
111
112   // Launch python in unbuffered mode, so that python output doesn't mix with
113   // gtest output in buildbot log files. See http://crbug.com/147368.
114   python_cmd->AppendArg("-u");
115
116   return true;
117 }