Skip to content

Commit

Permalink
Install pydantic and orjson with pip
Browse files Browse the repository at this point in the history
Signed-off-by: Jitendra Gundaniya <jitendra_gundaniya@mckinsey.com>
  • Loading branch information
jitu5 committed Aug 27, 2024
1 parent 9945df6 commit 5beaca2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
23 changes: 23 additions & 0 deletions bundled/tool/install_dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import subprocess
import sys
import shutil
import glob
from pathlib import Path

def install_dependencies(libsPath):
# Delete folders that start with "pydantic" which was installed by FASTAPI during the extension build process
for folder in glob.glob(f"{libsPath}/pydantic*"):
shutil.rmtree(folder, ignore_errors=True)

try:
import pydantic
import orjson
except ImportError:
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'pydantic', 'orjson', '-t', Path(libsPath), '--no-cache-dir'])

if __name__ == "__main__":
if len(sys.argv) > 1:
libsPath = sys.argv[1]
else:
libsPath = None
install_dependencies(libsPath)
4 changes: 2 additions & 2 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
# Required packages
pygls
packaging
aiofiles>=22.1.0
networkx>=2.5
pydantic>=2.0.0
sqlalchemy>=1.4, <3
fastapi>=0.100.0,<0.200.0

# TODO: Add your tool here
21 changes: 21 additions & 0 deletions src/common/installPythonDependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as cp from 'child_process';
import { getInterpreterDetails } from './python';

export async function installPythonDependencies(context: any): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
const script = context.asAbsolutePath('bundled/tool/install_dependencies.py');
const interpreterDetails = await getInterpreterDetails();
const pythonPath = interpreterDetails['path'] && interpreterDetails['path'][0];
const libsPath = context.asAbsolutePath('bundled/libs');

cp.exec(`${pythonPath} ${script} ${libsPath}`, (error, stdout, stderr) => {
if (error) {
console.error(stderr);
reject(error);
} else {
console.log(stdout);
resolve();
}
});
});
}
22 changes: 18 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,29 @@ import { loadServerDefaults } from './common/setup';
import { getLSClientTraceLevel, getProjectRoot } from './common/utilities';
import { createOutputChannel, onDidChangeConfiguration, registerCommand } from './common/vscodeapi';
import KedroVizPanel from './webview/vizWebView';
import { installPythonDependencies } from './common/installPythonDependencies';

let lsClient: LanguageClient | undefined;

export async function getlsClient() {
return lsClient;
}


export async function activate(context: vscode.ExtensionContext): Promise<void> {
const alreadyInstalled = context.globalState.get('dependenciesInstalled', false);

if (!alreadyInstalled) {
try {
await installPythonDependencies(context);
context.globalState.update('dependenciesInstalled', true);
vscode.window.showInformationMessage('Dependencies installed!');
} catch (error) {
vscode.window.showErrorMessage('Failed to install dependencies: ' + error);
}
} else {
vscode.window.showInformationMessage('Dependencies already installed.');
}

// This is required to get server name and module. This should be
// the first thing that we do in this extension.
const serverInfo = loadServerDefaults();
Expand Down Expand Up @@ -106,9 +120,9 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>

traceError(
'Python interpreter missing:\r\n' +
'[Option 1] Select python interpreter using the ms-python.python.\r\n' +
`[Option 2] Set an interpreter using "${serverId}.interpreter" setting.\r\n` +
'Please use Python 3.8 or greater.',
'[Option 1] Select python interpreter using the ms-python.python.\r\n' +
`[Option 2] Set an interpreter using "${serverId}.interpreter" setting.\r\n` +
'Please use Python 3.8 or greater.',
);
};

Expand Down

0 comments on commit 5beaca2

Please sign in to comment.