Skip to content

Commit

Permalink
Merge branch 'main' into noklam/track-command-usage-with-68
Browse files Browse the repository at this point in the history
Signed-off-by: Nok Lam Chan <[email protected]>
  • Loading branch information
noklam committed Sep 3, 2024
2 parents 4a5f140 + fc86ae1 commit 6f4fb4b
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 88 deletions.
42 changes: 42 additions & 0 deletions bundled/tool/install_dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import subprocess
import sys
from pathlib import Path

def install_dependencies(extension_root_dir):
"""
Install dependencies required for the Kedro extension.
Args:
extension_root_dir (str): The root directory of the extension.
Raises:
ImportError: If the required dependencies are not found.
"""
...
libs_path = Path(extension_root_dir) / "bundled" / "libs"
requirements_path = Path(extension_root_dir) / "kedro-viz-requirements.txt"

try:
import fastapi
import orjson
except ImportError:
subprocess.check_call(
[
sys.executable,
"-m",
"pip",
"install",
"-r",
Path(requirements_path),
"-t",
Path(libs_path),
"--no-cache-dir",
]
)


if __name__ == "__main__":
if len(sys.argv) > 1:
extension_root_dir = sys.argv[1]
else:
extension_root_dir = None
install_dependencies(extension_root_dir)
3 changes: 3 additions & 0 deletions kedro-viz-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fastapi>=0.100.0,<0.200.0
pydantic>=2.0.0 # In case of FastAPI installs pydantic==1
orjson>=3.9, <4.0
12 changes: 12 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ def _install_bundle(session: nox.Session) -> None:
"./requirements.txt",
)

# Installing kedro-viz to bundled/libs from here and not keeping it in requirements.in to avoid installing all its required packages
session.install(
"-t",
"./bundled/libs",
"--no-cache-dir",
"--implementation",
"py",
"--no-deps",
"--upgrade",
"kedro-viz",
)


def _check_files(names: List[str]) -> None:
root_dir = pathlib.Path(__file__).parent
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
# Required packages
pygls
packaging
networkx>=2.5
sqlalchemy>=1.4, <3

# TODO: Add your tool here
16 changes: 13 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
#
# This file is autogenerated by pip-compile with Python 3.12
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile ./requirements.in
#
attrs==23.2.0
attrs==24.2.0
# via
# cattrs
# lsprotocol
cattrs==23.2.3
cattrs==24.1.0
# via
# lsprotocol
# pygls
exceptiongroup==1.2.2
# via cattrs
lsprotocol==2023.0.1
# via pygls
networkx==3.3
# via -r ./requirements.in
packaging==24.1
# via -r ./requirements.in
pygls==1.3.1
# via -r ./requirements.in
sqlalchemy==2.0.32
# via -r ./requirements.in
typing-extensions==4.12.2
# via
# cattrs
# sqlalchemy
28 changes: 28 additions & 0 deletions src/common/callPythonScript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as cp from 'child_process';
import { getInterpreterDetails } from './python';

/**
* Calls a Python script with the specified arguments.
*
* @param pathToScript - The path to the Python script.
* @param scriptArgv - The arguments to pass to the script.
* @param context - The context object.
* @returns A promise that resolves when the script execution is complete.
*/
export async function callPythonScript(pathToScript: string, scriptArgv: string, context: any): Promise<string> {
return new Promise<string>(async (resolve, reject) => {
const script = context.asAbsolutePath(pathToScript);
const interpreterDetails = await getInterpreterDetails();
const pythonPath = interpreterDetails['path'] && interpreterDetails['path'][0];

cp.exec(`${pythonPath} ${script} ${scriptArgv}`, (error, stdout, stderr) => {
if (error) {
console.error(stderr);
reject(error);
} else {
console.log(stdout);
resolve(stdout);
}
});
});
}
26 changes: 25 additions & 1 deletion src/common/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

import * as fs from 'fs-extra';
import * as path from 'path';
import * as vscode from 'vscode';
import { LogLevel, Uri, WorkspaceFolder } from 'vscode';
import { Trace } from 'vscode-jsonrpc/node';
import { getWorkspaceFolders } from './vscodeapi';
import KedroVizPanel from '../webview/vizWebView';
import { callPythonScript } from './callPythonScript';
import { EXTENSION_ROOT_DIR } from './constants';
import { traceError, traceLog } from './log/logging';

function logLevelToTrace(logLevel: LogLevel): Trace {
switch (logLevel) {
Expand Down Expand Up @@ -66,3 +69,24 @@ export async function getProjectRoot(): Promise<WorkspaceFolder> {
return rootWorkspace;
}
}

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

if (!alreadyInstalled) {
const pathToScript = 'bundled/tool/install_dependencies.py';
try {
const stdout = await callPythonScript(pathToScript, EXTENSION_ROOT_DIR, context);

// Check if the script output contains the success message
if (stdout.includes('Successfully installed')) {
context.globalState.update('dependenciesInstalled', true);
traceLog(`Python dependencies installed!`);
console.log('Python dependencies installed!');
}
} catch (error) {
traceError(`Failed to install Python dependencies:: ${error}`);
console.error(`Failed to install Python dependencies:: ${error}`);
}
}
}
14 changes: 5 additions & 9 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ import { restartServer } from './common/server';
import { checkIfConfigurationChanged, getInterpreterFromSetting } from './common/settings';
import { loadServerDefaults } from './common/setup';
import { createStatusBar } from './common/status_bar';
import { getLSClientTraceLevel } from './common/utilities';
import { getLSClientTraceLevel, installDependenciesIfNeeded } from './common/utilities';
import { createOutputChannel, onDidChangeConfiguration, registerCommand } from './common/vscodeapi';
import KedroVizPanel from './webview/vizWebView';

let lsClient: LanguageClient | undefined;

export async function getlsClient() {
return lsClient;
}

export async function activate(context: vscode.ExtensionContext): Promise<void> {
await installDependenciesIfNeeded(context);

// 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 All @@ -46,6 +44,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
// List of commands
const CMD_RESTART_SERVER = `${serverId}.restart`;
const CMD_SELECT_ENV = `${serverId}.selectEnvironment`;
const CMD_RUN_KEDRO_VIZ = `${serverId}.runKedroViz`;

// Status Bar
const statusBarItem = await createStatusBar(CMD_SELECT_ENV, serverId);
Expand All @@ -70,7 +69,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
);

context.subscriptions.push(
vscode.commands.registerCommand('kedro.runKedroViz', async () => {
registerCommand(CMD_RUN_KEDRO_VIZ, async () => {
KedroVizPanel.createOrShow(context.extensionUri);
const projectData = await executeGetProjectDataCommand(lsClient);
KedroVizPanel.currentPanel?.updateData(projectData);
Expand Down Expand Up @@ -141,9 +140,6 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
registerCommand('kedro.sendDefinitionRequest', async (word) => {
await executeServerDefinitionCommand(lsClient, word);
}),
vscode.commands.registerCommand('kedro.runKedroViz', () => {
KedroVizPanel.createOrShow(context.extensionUri);
}),
);

setImmediate(async () => {
Expand Down
12 changes: 8 additions & 4 deletions src/test/python_tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
#
# This file is autogenerated by pip-compile with Python 3.12
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile ./src/test/python_tests/requirements.in
#
exceptiongroup==1.2.2
# via pytest
iniconfig==2.0.0
# via pytest
packaging==24.1
# via pytest
pluggy==1.5.0
# via pytest
pyhamcrest==2.1.0
# via -r requirements.in
# via -r ./src/test/python_tests/requirements.in
pytest==8.3.2
# via -r requirements.in
# via -r ./src/test/python_tests/requirements.in
python-jsonrpc-server==0.4.0
# via -r requirements.in
# via -r ./src/test/python_tests/requirements.in
tomli==2.0.1
# via pytest
ujson==5.10.0
# via python-jsonrpc-server
3 changes: 1 addition & 2 deletions src/webview/vizWebView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ export default class KedroVizPanel {
switch (message.command) {
case 'fromWebview':
if (message.node.type === 'data') {
// await executeServerDefinitionCommand(getlsClient(), message.node.text);
await vscode.commands.executeCommand('kedro.sendDefinitionRequest', message.node.text)
await vscode.commands.executeCommand('kedro.sendDefinitionRequest', message.node.text);
} else {
await goToDefinition(message.node);
}
Expand Down
Loading

0 comments on commit 6f4fb4b

Please sign in to comment.