-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathPythonLibrarySession.cpp
More file actions
353 lines (292 loc) · 10.4 KB
/
Copy pathPythonLibrarySession.cpp
File metadata and controls
353 lines (292 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//*************************************************************************************************
// Copyright (C) Microsoft Corporation.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
//
// @File: PythonLibrarySession.cpp
//
// Purpose:
// Class encapsulating operations performed in a library management session
//
//*************************************************************************************************
#include <fstream>
#include <regex>
#include <unordered_map>
#include "Logger.h"
#include "PythonExtensionUtils.h"
#include "PythonLibrarySession.h"
#include "PythonNamespace.h"
using namespace std;
namespace bp = boost::python;
#ifdef _WIN64
#include <filesystem>
namespace fs = std::filesystem;
#else
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif
//-------------------------------------------------------------------------------------------------
// Name: PythonLibrarySession::Init
//
// Description:
// Initializes the Python library session, initialize the main namespace.
//
void PythonLibrarySession::Init(
const SQLGUID *sessionId)
{
LOG("PythonLibrarySession::Init");
m_mainNamespace = PythonNamespace::MainNamespace();
}
//-------------------------------------------------------------------------------------------------
// Name: PythonLibrarySession::InstallLibrary
//
// Description:
// Install the specified library.
//
// Returns:
// The result of installation
//
SQLRETURN PythonLibrarySession::InstallLibrary(
string tempFolder,
SQLCHAR *libraryName,
SQLINTEGER libraryNameLength,
SQLCHAR *libraryFile,
SQLINTEGER libraryFileLength,
SQLCHAR *libraryInstallDirectory,
SQLINTEGER libraryInstallDirectoryLength)
{
LOG("PythonLibrarySession::InstallExternalLibrary");
SQLRETURN result = SQL_ERROR;
string errorString;
string libName = string(reinterpret_cast<char *>(libraryName), libraryNameLength);
string installDir = string(reinterpret_cast<char *>(libraryInstallDirectory), libraryInstallDirectoryLength);
installDir = PythonExtensionUtils::NormalizePathString(installDir);
string libFilePath = string(reinterpret_cast<char *>(libraryFile), libraryFileLength);
libFilePath = PythonExtensionUtils::NormalizePathString(libFilePath);
string extractScript = "import zipfile\n"
"with zipfile.ZipFile('" + libFilePath + "') as zip:\n"
" zip.extractall('" + tempFolder + "')";
bp::exec(extractScript.c_str(), m_mainNamespace);
string installPath = "";
// Find the python package inside the zip to use for installation.
//
for (const fs::directory_entry &entry : fs::directory_iterator(tempFolder))
{
string type = entry.path().extension().generic_string();
if (type.compare(".whl") == 0 ||
type.compare(".zip") == 0 ||
type.compare(".gz") == 0)
{
installPath = entry.path().generic_string();
break;
}
}
if (installPath.empty())
{
throw runtime_error("Could not find the package inside the zip - "
"external library must be a python package inside a zip.");
}
string pathToPython = PythonExtensionUtils::GetPathToPython();
// Set the TMPDIR so that pip uses our destination as temp. This allows us to use a
// non-default instance.
// Without this, TMPDIR will have MSSQL##$INSTANCE in the path, and the $ causes problems
// with pip because they interpret $INSTANCE as a variable, not part of the path.
//
string setTemp = "import os;oldtemp = os.environ['TMPDIR'] if 'TMPDIR' in os.environ else None;"
"os.environ['TMPDIR'] = '" + tempFolder + "'";
bp::exec(setTemp.c_str(), m_mainNamespace);
string installScript =
"import subprocess;pipresult = subprocess.run(['" + pathToPython +
"', '-m', 'pip', 'install', '" + installPath +
"', '--no-deps', '--ignore-installed', '--no-cache-dir'"
", '-t', '" + installDir + "']).returncode";
bp::exec(installScript.c_str(), m_mainNamespace);
int pipResult = bp::extract<int>(m_mainNamespace["pipresult"]);
string resetTemp = "if oldtemp: \n"
" os.environ['TMPDIR'] = oldtemp\n"
"else:\n"
" del os.environ['TMPDIR']";
bp::exec(resetTemp.c_str(), m_mainNamespace);
if (pipResult != 0)
{
throw runtime_error("Pip failed to install the package with exit code " +
to_string(pipResult));
}
result = SQL_SUCCESS;
return result;
}
//-------------------------------------------------------------------------------------------------
// Name: PythonLibrarySession::UninstallLibrary
//
// Description:
// Uninstall the specified library from the specified library directory
//
// Returns:
// The result of uninstallation
//
SQLRETURN PythonLibrarySession::UninstallLibrary(
SQLCHAR *libraryName,
SQLINTEGER libraryNameLength,
SQLCHAR *libraryInstallDirectory,
SQLINTEGER libraryInstallDirectoryLength
)
{
LOG("PythonLibrarySession::UninstallExternalLibrary");
SQLRETURN result = SQL_ERROR;
string errorString;
vector<fs::directory_entry> artifacts;
string libName = string(reinterpret_cast<char *>(libraryName), libraryNameLength);
string installDir = string(reinterpret_cast<char *>(libraryInstallDirectory),
libraryInstallDirectoryLength);
installDir = PythonExtensionUtils::NormalizePathString(installDir);
try
{
// Save the top_level items so we can delete them if the pip uninstall fails.
// If pip uninstall succeeds, we won't need this.
//
artifacts = GetTopLevel(libName, installDir);
string pathToPython = PythonExtensionUtils::GetPathToPython();
string uninstallScript =
"newPath = ['" + installDir + "'] + _originalpath\n"
"os.environ['PYTHONPATH'] = os.pathsep.join(newPath)\n"
"import subprocess\n"
"pipresult = subprocess.run(['" + pathToPython +
"', '-m', 'pip', 'uninstall', '" + libName + "', '-y']).returncode\n";
bp::exec(uninstallScript.c_str(), m_mainNamespace);
int pipResult = bp::extract<int>(m_mainNamespace["pipresult"]);
if (pipResult == 0)
{
result = SQL_SUCCESS;
}
else
{
throw runtime_error("Pip failed to fully uninstall the package with exit code " +
to_string(pipResult));
}
}
catch (const exception & ex)
{
result = SQL_ERROR;
errorString = string(ex.what());
}
catch (const bp::error_already_set &)
{
result = SQL_ERROR;
errorString = PythonExtensionUtils::ParsePythonException();
}
catch (...)
{
result = SQL_ERROR;
errorString = "Unexpected exception occurred in function UninstallExternalLibrary()";
}
// If pip fails for some reason, we try to manually uninstall the package by deleting the
// top level package folder as well as any dist/egg/.py files that were left behind.
//
if (result != SQL_SUCCESS && fs::exists(installDir))
{
LOG("Failed to fully uninstall " + libName + " with pip, deleting files manually");
for (fs::directory_entry entry : artifacts)
{
fs::remove_all(entry);
}
vector<fs::directory_entry> newArtifacts = GetAllArtifacts(libName, installDir);
for (fs::directory_entry entry : newArtifacts)
{
fs::remove_all(entry);
}
// If we successfully removed all the files, then we have a SUCCESS result.
//
result = SQL_SUCCESS;
}
return result;
}
//-------------------------------------------------------------------------------------------------
// Name: PythonLibrarySession::GetTopLevel
//
// Description:
// Get top level directory/ies for a package
//
// Returns:
// A vector of directory_entries of the top level artifacts of the package
//
vector<fs::directory_entry> PythonLibrarySession::GetTopLevel(string libName, string installDir)
{
vector<fs::directory_entry> artifacts;
regex_constants::syntax_option_type caseInsensitive = regex_constants::icase;
// Normalize library names by replacing all dashes and underscores with regex for either
//
string regexLibName = regex_replace(libName, regex("(-|_)"), "(-|_)");
if (fs::exists(installDir))
{
for (const fs::directory_entry &entry : fs::directory_iterator(installDir))
{
string pathFilename = entry.path().filename().string();
// The top_level.txt file is in the egg-info or dist-info folder
//
regex egg("^" + regexLibName + "-(.*)egg(.*)", caseInsensitive);
regex distinfo("^" + regexLibName + "-(.*)dist-info", caseInsensitive);
if (regex_match(pathFilename, egg) ||
regex_match(pathFilename, distinfo))
{
artifacts.push_back(entry);
// The top_level.txt file tells us what items this package put into the
// installation directory that we will need to delete to uninstall.
//
fs::path topLevelPath = entry.path();
topLevelPath = topLevelPath.append("top_level.txt");
if (fs::exists(topLevelPath))
{
// Read in the top_level file to find what the top_level folders and files are
//
ifstream topLevelFile(topLevelPath);
string str;
while (getline(topLevelFile, str))
{
if (str.size() > 0)
{
fs::path path(installDir);
artifacts.push_back(fs::directory_entry(path.append(str)));
}
}
topLevelFile.close();
break;
}
break;
}
}
}
return artifacts;
}
//-------------------------------------------------------------------------------------------------
// Name: PythonLibrarySession::GetAllArtifacts
//
// Description:
// Get all the artifacts we can find of a package that are in the path
//
// Returns:
// A vector of directory_entries of the artifacts
//
vector<fs::directory_entry> PythonLibrarySession::GetAllArtifacts(string libName, string path)
{
vector<fs::directory_entry> artifacts;
regex_constants::syntax_option_type caseInsensitive = regex_constants::icase;
// Normalize library names by replacing all dashes with underscores
//
string regexLibName = regex_replace(libName, regex("(-|_)"), "(-|_)");
if (fs::exists(path))
{
for (const fs::directory_entry &entry : fs::directory_iterator(path))
{
string pathFilename = entry.path().filename().string();
regex pth("^" + regexLibName + "-(.*).pth", caseInsensitive);
regex pyFile("^" + regexLibName + ".py", caseInsensitive);
if (regex_match(pathFilename, pyFile) ||
regex_match(pathFilename, pth))
{
artifacts.push_back(entry);
}
}
}
return artifacts;
}