 Chromium Code Reviews
 Chromium Code Reviews Issue 21537002:
  Remove hard dependency on bundled python_26 in tests  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@glyphcache_20130604
    
  
    Issue 21537002:
  Remove hard dependency on bundled python_26 in tests  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@glyphcache_20130604| OLD | NEW | 
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "net/test/spawned_test_server/local_test_server.h" | 5 #include "net/test/spawned_test_server/local_test_server.h" | 
| 6 | 6 | 
| 7 #include <windows.h> | 7 #include <windows.h> | 
| 8 #include <wincrypt.h> | 8 #include <wincrypt.h> | 
| 9 | 9 | 
| 10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" | 
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 78 return false; | 78 return false; | 
| 79 } | 79 } | 
| 80 | 80 | 
| 81 return true; | 81 return true; | 
| 82 } | 82 } | 
| 83 | 83 | 
| 84 } // namespace | 84 } // namespace | 
| 85 | 85 | 
| 86 namespace net { | 86 namespace net { | 
| 87 | 87 | 
| 88 class AddedPythonPath { | |
| 89 private: | |
| 90 bool path_modified_; | |
| 91 LPTSTR old_path_; | |
| 92 | |
| 93 public: | |
| 94 AddedPythonPath(); | |
| 95 ~AddedPythonPath(); | |
| 96 }; | |
| 97 | |
| 98 AddedPythonPath::AddedPythonPath() : path_modified_(false), old_path_(NULL) { | |
| 
Paweł Hajdan Jr.
2013/08/01 17:56:28
Could you reuse ScopedEnvironmentVariable from e.g
 
Daniel Bratell
2013/08/02 07:27:53
I took a look at it and it has slightly different
 
Paweł Hajdan Jr.
2013/08/19 19:16:55
Let's make the version you created for this purpos
 | |
| 99 // Retrieves the old path, adds third_party/python26 to the end of it and | |
| 100 // then restores the original path in the destructor. | |
| 101 | |
| 102 DWORD result = GetEnvironmentVariable(TEXT("PATH"), NULL, 0); | |
| 103 if (result == 0 && GetLastError() != ERROR_ENVVAR_NOT_FOUND) | |
| 104 return; | |
| 105 | |
| 106 if (result != 0) { | |
| 107 old_path_ = new TCHAR[result]; | |
| 
Paweł Hajdan Jr.
2013/08/01 17:56:28
We generally avoid using TCHAR, TEXT, and LPTSTR d
 | |
| 108 if (!old_path_) | |
| 109 return; | |
| 110 | |
| 111 result = GetEnvironmentVariable(TEXT("PATH"), old_path_, result); | |
| 112 if (result == 0) { | |
| 113 // PATH Variable has disappeared or something went wrong in Windows. | |
| 114 // We just do nothing. | |
| 115 return; | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 base::FilePath dir; | |
| 120 if (!PathService::Get(base::DIR_SOURCE_ROOT, &dir)) | |
| 121 return; | |
| 122 dir = dir.Append(FILE_PATH_LITERAL("third_party")) | |
| 123 .Append(FILE_PATH_LITERAL("python_26")); | |
| 124 | |
| 125 base::string16 new_path = dir.AsUTF16Unsafe(); | |
| 126 | |
| 127 size_t new_value_len = new_path.length() + 1; | |
| 128 if (old_path_) { | |
| 129 new_value_len += 1 + wcslen(old_path_); | |
| 130 } | |
| 131 | |
| 132 LPTSTR new_value = new TCHAR[new_value_len]; | |
| 133 if (!new_value) | |
| 134 return; | |
| 135 | |
| 136 if (old_path_) { | |
| 137 wcscpy(new_value, old_path_); | |
| 138 wcscat(new_value, TEXT(";")); // Add separator | |
| 139 } | |
| 140 else | |
| 141 new_value[0] = '\0'; | |
| 142 | |
| 143 // Add new path to the end so system pythons are used if available. | |
| 144 wcscat(new_value, new_path.c_str()); | |
| 145 result = SetEnvironmentVariable(TEXT("PATH"), new_value); | |
| 146 delete[] new_value; | |
| 147 if (result == 0) | |
| 148 return; | |
| 149 path_modified_ = true; | |
| 150 } | |
| 151 | |
| 152 AddedPythonPath::~AddedPythonPath() { | |
| 153 if (path_modified_) { | |
| 154 // old_path_ can be NULL in which case the variable will be | |
| 155 // removed. That is what we want. | |
| 156 (void)SetEnvironmentVariable(TEXT("PATH"), old_path_); | |
| 157 } | |
| 158 | |
| 159 delete[] old_path_; | |
| 160 } | |
| 161 | |
| 88 bool LocalTestServer::LaunchPython(const base::FilePath& testserver_path) { | 162 bool LocalTestServer::LaunchPython(const base::FilePath& testserver_path) { | 
| 89 CommandLine python_command(CommandLine::NO_PROGRAM); | 163 CommandLine python_command(CommandLine::NO_PROGRAM); | 
| 90 if (!GetPythonCommand(&python_command)) | 164 if (!GetPythonCommand(&python_command)) | 
| 91 return false; | 165 return false; | 
| 92 | 166 | 
| 93 python_command.AppendArgPath(testserver_path); | 167 python_command.AppendArgPath(testserver_path); | 
| 94 if (!AddCommandLineArguments(&python_command)) | 168 if (!AddCommandLineArguments(&python_command)) | 
| 95 return false; | 169 return false; | 
| 96 | 170 | 
| 97 HANDLE child_read = NULL; | 171 HANDLE child_read = NULL; | 
| (...skipping 29 matching lines...) Expand all Loading... | |
| 127 if (!job_handle_.IsValid()) { | 201 if (!job_handle_.IsValid()) { | 
| 128 LOG(ERROR) << "Could not create JobObject."; | 202 LOG(ERROR) << "Could not create JobObject."; | 
| 129 return false; | 203 return false; | 
| 130 } | 204 } | 
| 131 | 205 | 
| 132 if (!base::SetJobObjectAsKillOnJobClose(job_handle_.Get())) { | 206 if (!base::SetJobObjectAsKillOnJobClose(job_handle_.Get())) { | 
| 133 LOG(ERROR) << "Could not SetInformationJobObject."; | 207 LOG(ERROR) << "Could not SetInformationJobObject."; | 
| 134 return false; | 208 return false; | 
| 135 } | 209 } | 
| 136 | 210 | 
| 211 AddedPythonPath python_path; | |
| 137 base::LaunchOptions launch_options; | 212 base::LaunchOptions launch_options; | 
| 138 launch_options.inherit_handles = true; | 213 launch_options.inherit_handles = true; | 
| 139 launch_options.job_handle = job_handle_.Get(); | 214 launch_options.job_handle = job_handle_.Get(); | 
| 140 if (!base::LaunchProcess(python_command, launch_options, &process_handle_)) { | 215 if (!base::LaunchProcess(python_command, launch_options, &process_handle_)) { | 
| 141 LOG(ERROR) << "Failed to launch " << python_command.GetCommandLineString(); | 216 LOG(ERROR) << "Failed to launch " << python_command.GetCommandLineString(); | 
| 142 return false; | 217 return false; | 
| 143 } | 218 } | 
| 144 | 219 | 
| 145 return true; | 220 return true; | 
| 146 } | 221 } | 
| (...skipping 19 matching lines...) Expand all Loading... | |
| 166 if (!ParseServerData(server_data)) { | 241 if (!ParseServerData(server_data)) { | 
| 167 LOG(ERROR) << "Could not parse server_data: " << server_data; | 242 LOG(ERROR) << "Could not parse server_data: " << server_data; | 
| 168 return false; | 243 return false; | 
| 169 } | 244 } | 
| 170 | 245 | 
| 171 return true; | 246 return true; | 
| 172 } | 247 } | 
| 173 | 248 | 
| 174 } // namespace net | 249 } // namespace net | 
| 175 | 250 | 
| OLD | NEW |