Chromium Code Reviews| Index: net/test/spawned_test_server/local_test_server_win.cc |
| diff --git a/net/test/spawned_test_server/local_test_server_win.cc b/net/test/spawned_test_server/local_test_server_win.cc |
| index fd26483ed477b37698538f1f2b609e7d79b343f2..4958d40ec3bc558c286ce6c66b89141817833793 100644 |
| --- a/net/test/spawned_test_server/local_test_server_win.cc |
| +++ b/net/test/spawned_test_server/local_test_server_win.cc |
| @@ -85,6 +85,80 @@ bool ReadData(HANDLE read_fd, HANDLE write_fd, |
| namespace net { |
| +class AddedPythonPath { |
| +private: |
| + bool path_modified_; |
| + LPTSTR old_path_; |
| + |
| +public: |
| + AddedPythonPath(); |
| + ~AddedPythonPath(); |
| +}; |
| + |
| +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
|
| + // Retrieves the old path, adds third_party/python26 to the end of it and |
| + // then restores the original path in the destructor. |
| + |
| + DWORD result = GetEnvironmentVariable(TEXT("PATH"), NULL, 0); |
| + if (result == 0 && GetLastError() != ERROR_ENVVAR_NOT_FOUND) |
| + return; |
| + |
| + if (result != 0) { |
| + old_path_ = new TCHAR[result]; |
|
Paweł Hajdan Jr.
2013/08/01 17:56:28
We generally avoid using TCHAR, TEXT, and LPTSTR d
|
| + if (!old_path_) |
| + return; |
| + |
| + result = GetEnvironmentVariable(TEXT("PATH"), old_path_, result); |
| + if (result == 0) { |
| + // PATH Variable has disappeared or something went wrong in Windows. |
| + // We just do nothing. |
| + return; |
| + } |
| + } |
| + |
| + base::FilePath dir; |
| + if (!PathService::Get(base::DIR_SOURCE_ROOT, &dir)) |
| + return; |
| + dir = dir.Append(FILE_PATH_LITERAL("third_party")) |
| + .Append(FILE_PATH_LITERAL("python_26")); |
| + |
| + base::string16 new_path = dir.AsUTF16Unsafe(); |
| + |
| + size_t new_value_len = new_path.length() + 1; |
| + if (old_path_) { |
| + new_value_len += 1 + wcslen(old_path_); |
| + } |
| + |
| + LPTSTR new_value = new TCHAR[new_value_len]; |
| + if (!new_value) |
| + return; |
| + |
| + if (old_path_) { |
| + wcscpy(new_value, old_path_); |
| + wcscat(new_value, TEXT(";")); // Add separator |
| + } |
| + else |
| + new_value[0] = '\0'; |
| + |
| + // Add new path to the end so system pythons are used if available. |
| + wcscat(new_value, new_path.c_str()); |
| + result = SetEnvironmentVariable(TEXT("PATH"), new_value); |
| + delete[] new_value; |
| + if (result == 0) |
| + return; |
| + path_modified_ = true; |
| +} |
| + |
| +AddedPythonPath::~AddedPythonPath() { |
| + if (path_modified_) { |
| + // old_path_ can be NULL in which case the variable will be |
| + // removed. That is what we want. |
| + (void)SetEnvironmentVariable(TEXT("PATH"), old_path_); |
| + } |
| + |
| + delete[] old_path_; |
| +} |
| + |
| bool LocalTestServer::LaunchPython(const base::FilePath& testserver_path) { |
| CommandLine python_command(CommandLine::NO_PROGRAM); |
| if (!GetPythonCommand(&python_command)) |
| @@ -134,6 +208,7 @@ bool LocalTestServer::LaunchPython(const base::FilePath& testserver_path) { |
| return false; |
| } |
| + AddedPythonPath python_path; |
| base::LaunchOptions launch_options; |
| launch_options.inherit_handles = true; |
| launch_options.job_handle = job_handle_.Get(); |