xref: /aosp_15_r20/external/cronet/net/test/python_utils.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 The Chromium Authors
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 <memory>
8 
9 #include "base/command_line.h"
10 #include "base/environment.h"
11 #include "base/files/file_path.h"
12 #include "build/build_config.h"
13 
14 namespace {
15 const base::FilePath::CharType kPythonPathEnv[] =
16     FILE_PATH_LITERAL("PYTHONPATH");
17 const base::FilePath::CharType kVPythonClearPathEnv[] =
18     FILE_PATH_LITERAL("VPYTHON_CLEAR_PYTHONPATH");
19 }  // namespace
20 
SetPythonPathInEnvironment(const std::vector<base::FilePath> & python_path,base::EnvironmentMap * map)21 void SetPythonPathInEnvironment(const std::vector<base::FilePath>& python_path,
22                                 base::EnvironmentMap* map) {
23   base::NativeEnvironmentString path_str;
24   for (const auto& path : python_path) {
25     if (!path_str.empty()) {
26 #if BUILDFLAG(IS_WIN)
27       path_str.push_back(';');
28 #else
29       path_str.push_back(':');
30 #endif
31     }
32     path_str += path.value();
33   }
34 
35   (*map)[kPythonPathEnv] = path_str;
36 
37   // vpython has instructions on BuildBot (not swarming or LUCI) to clear
38   // PYTHONPATH on invocation. Since we are clearing and manipulating it
39   // ourselves, we don't want vpython to throw out our hard work.
40   (*map)[kVPythonClearPathEnv] = base::NativeEnvironmentString();
41 }
42 
GetPython3Command(base::CommandLine * python_cmd)43 bool GetPython3Command(base::CommandLine* python_cmd) {
44   DCHECK(python_cmd);
45 
46 // Use vpython3 to pick up src.git's vpython3 VirtualEnv spec.
47 #if BUILDFLAG(IS_WIN)
48   python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("vpython3.bat")));
49 #else
50   python_cmd->SetProgram(base::FilePath(FILE_PATH_LITERAL("vpython3")));
51 #endif
52 
53 #if BUILDFLAG(IS_MAC)
54   // Enable logging to help diagnose https://crbug.com/1254962. Remove this when
55   // the bug is resolved.
56   python_cmd->AppendArg("-vpython-log-level=info");
57 #endif
58 
59   // Launch python in unbuffered mode, so that python output doesn't mix with
60   // gtest output in buildbot log files. See http://crbug.com/147368.
61   python_cmd->AppendArg("-u");
62 
63   return true;
64 }
65