language-extensions/python/src/PythonExtension.cpp
//-------------------------------------------------------------------------------------------------
// Name: InstallExternalLibrary
//
// Description:
//  Installs an external library to the specified directory
//  The library file is expected to be a zip containing the python package inside.
//  We unzip the file then install the python package inside using a subprocess call to pip.
//
// Returns:
//  SQL_SUCCESS on success, else SQL_ERROR
//

InstallExternalLibrary
Description:
Installs an external library to the specified directory
The library file is expected to be a zip containing the python package inside.
We unzip the file then install the python package inside using a subprocess call to pip.


InstallExternalLibrary Description : Installs an external library to the specified directory The library file is expected to be a zip containing the python package inside . We unzip the file then install the python package inside using a subprocess call to pip .
SQLRETURN InstallExternalLibrary(
	SQLGUID    setupSessionId,
	SQLCHAR    *libraryName,
	SQLINTEGER libraryNameLength,
	SQLCHAR    *libraryFile,
	SQLINTEGER libraryFileLength,
	SQLCHAR    *libraryInstallDirectory,
	SQLINTEGER libraryInstallDirectoryLength,
	SQLCHAR    **libraryError,
	SQLINTEGER *libraryErrorLength)
{
	LOG("InstallExternalLibrary");
	SQLRETURN result = SQL_ERROR;
	string errorString;
	string installDir = string(reinterpret_cast<char *>(libraryInstallDirectory), 
		libraryInstallDirectoryLength);
	installDir = PythonExtensionUtils::NormalizePathString(installDir);
	string tempFolder = PythonExtensionUtils::NormalizePathString(
		fs::path(installDir).append("tmp").string());
	try
	{
		PythonLibrarySession librarySession = PythonLibrarySession();
		librarySession.Init(&setupSessionId);
		result = librarySession.InstallLibrary(
			tempFolder,
			libraryName,
			libraryNameLength,
			libraryFile,
			libraryFileLength,
			libraryInstallDirectory,
			libraryInstallDirectoryLength);
	}
	catch (const exception & ex)
	{
		result = SQL_ERROR;
		errorString = string(ex.what());
		LOG_ERROR(errorString);
	}
	catch (const bp::error_already_set &)
	{
		result = SQL_ERROR;
		errorString = PythonExtensionUtils::ParsePythonException();
		LOG_ERROR("Python error: " + errorString);
	}
	catch (...)
	{
		result = SQL_ERROR;
		errorString = "Unexpected exception occurred in function InstallExternalLibrary()";
		LOG_ERROR(errorString);
	}
	if (fs::exists(tempFolder))
	{
		fs::remove_all(tempFolder);
	}
	if (!errorString.empty())
	{
		*libraryErrorLength = errorString.length();
		string *pError = new string(errorString);
		SQLCHAR *error = const_cast<SQLCHAR*>(reinterpret_cast<const SQLCHAR *>(pError->c_str()));
		*libraryError = error;
	}
	return result;
}
SQLRETURN InstallExternalLibrary ( SQLGUID setupSessionId , SQLCHAR * libraryName , SQLINTEGER libraryNameLength , SQLCHAR * libraryFile , SQLINTEGER libraryFileLength , SQLCHAR * libraryInstallDirectory , SQLINTEGER libraryInstallDirectoryLength , SQLCHAR * * libraryError , SQLINTEGER * libraryErrorLength ) { LOG ( " " ) ; SQLRETURN result = SQL_ERROR ; string errorString ; string installDir = string ( reinterpret_cast < char * > ( libraryInstallDirectory ) , libraryInstallDirectoryLength ) ; installDir = PythonExtensionUtils :: NormalizePathString ( installDir ) ; string tempFolder = PythonExtensionUtils :: NormalizePathString ( fs :: path ( installDir ) . append ( " " ) . string ( ) ) ; try { PythonLibrarySession librarySession = PythonLibrarySession ( ) ; librarySession . Init ( & setupSessionId ) ; result = librarySession . InstallLibrary ( tempFolder , libraryName , libraryNameLength , libraryFile , libraryFileLength , libraryInstallDirectory , libraryInstallDirectoryLength ) ; } catch ( const exception & ex ) { result = SQL_ERROR ; errorString = string ( ex . what ( ) ) ; LOG_ERROR ( errorString ) ; } catch ( const bp :: error_already_set & ) { result = SQL_ERROR ; errorString = PythonExtensionUtils :: ParsePythonException ( ) ; LOG_ERROR ( " " + errorString ) ; } catch ( ... ) { result = SQL_ERROR ; errorString = " " ; LOG_ERROR ( errorString ) ; } if ( fs :: exists ( tempFolder ) ) { fs :: remove_all ( tempFolder ) ; } if ( ! errorString . empty ( ) ) { * libraryErrorLength = errorString . length ( ) ; string * pError = new string ( errorString ) ; SQLCHAR * error = const_cast < SQLCHAR * > ( reinterpret_cast < const SQLCHAR * > ( pError -> c_str ( ) ) ) ; * libraryError = error ; } return result ; }
Name: InstallExternalLibrary
Description:
Installs an external library to the specified directory
The library file is expected to be a zip containing the python package inside.
Name : InstallExternalLibrary Description : Installs an external library to the specified directory The library file is expected to be a zip containing the python package inside .
// Clean up the temp installation folder
 //
