Home | History | Annotate | Download | only in test
      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 #if defined(OS_MACOSX) || defined(OS_CHROMEOS)
     45 // Search for |to_try|, rolling up the directory tree from
     46 // |start_dir|.  If found, return true and put the path to |to_try| in
     47 // |out_dir|.  If not, return false and leave |out_dir| untouched.
     48 bool TryRelativeToDir(const base::FilePath& start_dir,
     49                       const base::FilePath& to_try,
     50                       base::FilePath* out_dir) {
     51   base::FilePath dir(start_dir);
     52   while (!base::DirectoryExists(dir.Append(to_try))) {
     53     base::FilePath parent = dir.DirName();
     54     if (parent == dir) {
     55       // We hit the root directory.
     56       return false;
     57     }
     58     dir = parent;
     59   }
     60   *out_dir = dir;
     61   return true;
     62 }
     63 #endif  // defined(OS_MACOSX) || defined(OS_CHROMEOS)
     64 
     65 }  // namespace
     66 
     67 bool GetPyProtoPath(base::FilePath* dir) {
     68   // Locate the Python code generated by the protocol buffers compiler.
     69   base::FilePath generated_code_dir;
     70   if (!PathService::Get(base::DIR_EXE, &generated_code_dir)) {
     71     LOG(ERROR) << "Can't find " << generated_code_dir.value();
     72     return false;
     73   }
     74 
     75   const base::FilePath kPyProto(FILE_PATH_LITERAL("pyproto"));
     76 
     77 #if defined(OS_MACOSX) || defined(OS_CHROMEOS)
     78   base::FilePath source_dir;
     79   if (!PathService::Get(base::DIR_SOURCE_ROOT, &source_dir)) {
     80     LOG(ERROR) << "Can't find " << source_dir.value();
     81     return false;
     82   }
     83   // On Mac, and possibly Chrome OS, DIR_EXE might be pointing deep
     84   // into the Release/ (or Debug/) directory and we can't depend on
     85   // how far down it goes. So we walk upwards from DIR_EXE until we
     86   // find a likely looking spot.
     87   if (!TryRelativeToDir(generated_code_dir, kPyProto, dir)) {
     88     LOG(WARNING) << "Can't find " << kPyProto.value()
     89                  << " next to " << generated_code_dir.value();
     90     // On Chrome OS, we may have installed the test binaries and support tools
     91     // in a wholly separate location, relative to DIR_SOURCE_ROOT.  We'll want
     92     // to do a similar investigation from that point as well.
     93     generated_code_dir = source_dir
     94         .Append(FILE_PATH_LITERAL("out"))
     95         .Append(FILE_PATH_LITERAL("Release"));
     96     if (!TryRelativeToDir(generated_code_dir, kPyProto, dir)) {
     97       LOG(WARNING) << "Can't find " << kPyProto.value()
     98                    << " next to " << generated_code_dir.value();
     99       return false;
    100     }
    101   }
    102   generated_code_dir = *dir;
    103 #endif
    104   *dir = generated_code_dir.Append(kPyProto);
    105   VLOG(2) << "Found " << kPyProto.value() << " in " << dir->value();
    106   return true;
    107 }
    108 
    109 bool GetPythonCommand(CommandLine* python_cmd) {
    110   DCHECK(python_cmd);
    111 
    112 #if defined(OS_WIN)
    113   // This permits finding the proper python in path even if it is a .bat file.
    114   python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("cmd.exe")));
    115   python_cmd->AppendArg("/c");
    116   python_cmd->AppendArg("python");
    117 #else
    118   python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("python")));
    119 #endif  // defined(OS_WIN)
    120   // Launch python in unbuffered mode, so that python output doesn't mix with
    121   // gtest output in buildbot log files. See http://crbug.com/147368.
    122   python_cmd->AppendArg("-u");
    123 
    124   return true;
    125 }
    126