1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen// Use of this source code is governed by a BSD-style license that can be
3a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen// found in the LICENSE file.
4a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen
5a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen#include "net/test/python_utils.h"
6a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen
7a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen#include "base/base_paths.h"
8a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen#include "base/environment.h"
9a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen#include "base/file_path.h"
104a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#include "base/file_util.h"
113f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen#include "base/logging.h"
12ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/memory/scoped_ptr.h"
13a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen#include "base/path_service.h"
14a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen#include "base/utf_string_conversions.h"
15a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen
16a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsenconst char kPythonPathEnv[] = "PYTHONPATH";
17a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen
18a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsenvoid AppendToPythonPath(const FilePath& dir) {
19a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen  scoped_ptr<base::Environment> env(base::Environment::Create());
20a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen  std::string old_path;
21a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen  std::string dir_path;
22a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen#if defined(OS_WIN)
23a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen  dir_path = WideToUTF8(dir.value());
24a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen#elif defined(OS_POSIX)
25a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen  dir_path = dir.value();
26a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen#endif
27a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen  if (!env->GetVar(kPythonPathEnv, &old_path)) {
28a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen    env->SetVar(kPythonPathEnv, dir_path.c_str());
29a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen  } else if (old_path.find(dir_path) == std::string::npos) {
30a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen    std::string new_path(old_path);
31a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen#if defined(OS_WIN)
32a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen    new_path.append(";");
33a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen#elif defined(OS_POSIX)
34a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen    new_path.append(":");
35a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen#endif
36a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen    new_path.append(dir_path.c_str());
37a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen    env->SetVar(kPythonPathEnv, new_path);
38a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen  }
39a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen}
40a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen
413f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsennamespace {
423f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
433f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen// Search for |to_try|, rolling up the directory tree from
443f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen// |start_dir|.  If found, return true and put the path to |to_try| in
453f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen// |out_dir|.  If not, return false and leave |out_dir| untouched.
463f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsenbool TryRelativeToDir(const FilePath& start_dir,
473f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen                      const FilePath& to_try,
483f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen                      FilePath* out_dir) {
493f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  FilePath dir(start_dir);
503f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  while (!file_util::DirectoryExists(dir.Append(to_try))) {
513f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    FilePath parent = dir.DirName();
523f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    if (parent == dir) {
533f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen      // We hit the root directory.
543f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen      return false;
553f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    }
563f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    dir = parent;
573f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  }
583f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  *out_dir = dir;
593f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  return true;
603f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen}
613f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
623f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen}  // namespace
633f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
644a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdochbool GetPyProtoPath(FilePath* dir) {
654a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  // Locate the Python code generated by the protocol buffers compiler.
664a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  FilePath generated_code_dir;
674a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  if (!PathService::Get(base::DIR_EXE, &generated_code_dir)) {
683f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    LOG(ERROR) << "Can't find " << generated_code_dir.value();
694a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    return false;
704a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  }
714a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
724a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  const FilePath kPyProto(FILE_PATH_LITERAL("pyproto"));
734a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
744a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#if defined(OS_MACOSX) || defined(OS_CHROMEOS)
753f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  FilePath source_dir;
763f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  if (!PathService::Get(base::DIR_SOURCE_ROOT, &source_dir)) {
773f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    LOG(ERROR) << "Can't find " << source_dir.value();
783f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    return false;
793f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  }
803f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // On Mac, and possibly Chrome OS, DIR_EXE might be pointing deep
813f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // into the Release/ (or Debug/) directory and we can't depend on
823f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // how far down it goes. So we walk upwards from DIR_EXE until we
833f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  // find a likely looking spot.
843f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  if (!TryRelativeToDir(generated_code_dir, kPyProto, dir)) {
853f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    LOG(WARNING) << "Can't find " << kPyProto.value()
863f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen                 << " next to " << generated_code_dir.value();
873f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    // On Chrome OS, we may have installed the test binaries and support tools
883f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    // in a wholly separate location, relative to DIR_SOURCE_ROOT.  We'll want
893f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    // to do a similar investigation from that point as well.
903f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    generated_code_dir = source_dir
913f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen        .Append(FILE_PATH_LITERAL("out"))
923f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen        .Append(FILE_PATH_LITERAL("Release"));
933f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen    if (!TryRelativeToDir(generated_code_dir, kPyProto, dir)) {
943f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen      LOG(WARNING) << "Can't find " << kPyProto.value()
953f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen                   << " next to " << generated_code_dir.value();
964a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch      return false;
974a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    }
984a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  }
993f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  generated_code_dir = *dir;
1004a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#endif
1014a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  *dir = generated_code_dir.Append(kPyProto);
1023f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  VLOG(2) << "Found " << kPyProto.value() << " in " << dir->value();
1034a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  return true;
1044a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch}
1054a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
106a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsenbool GetPythonRunTime(FilePath* dir) {
107a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen#if defined(OS_WIN)
108a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen  if (!PathService::Get(base::DIR_SOURCE_ROOT, dir))
109a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen    return false;
110a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen  *dir = dir->Append(FILE_PATH_LITERAL("third_party"))
111201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch      .Append(FILE_PATH_LITERAL("python_26"))
112a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen      .Append(FILE_PATH_LITERAL("python.exe"));
113a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen#elif defined(OS_POSIX)
114a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen  *dir = FilePath("python");
115a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen#endif
116a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen  return true;
117a8d393aaf9718c42d29078685fc0b94add05b863Kristian Monsen}
118