Skip to content

Commit 3321a2a

Browse files
authored
refactor(vscode): share is python model present code (#4200)
1 parent 5a8e230 commit 3321a2a

2 files changed

Lines changed: 42 additions & 33 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { getInterpreterDetails } from "./common/python"
2+
import { err, ok, Result } from "./functional/result"
3+
import { traceInfo } from "./common/log"
4+
import { promisify } from "util"
5+
import { execFile } from "child_process"
6+
7+
/** isPythonModuleInstallled returns true if the given python module is installed.
8+
*
9+
* @param moduleName - The name of the python module to check.
10+
* @returns True if the module is installed, false otherwise.
11+
*/
12+
export const isPythonModuleInstalled = async (moduleName: string): Promise<Result<boolean, string>> => {
13+
const interpreterDetails = await getInterpreterDetails()
14+
if (!interpreterDetails.path) {
15+
return err("No Python interpreter found")
16+
}
17+
const pythonPath = interpreterDetails.path[0]
18+
const checkScript = `
19+
import sys
20+
if sys.version_info >= (3, 12):
21+
from importlib import metadata
22+
else:
23+
import importlib_metadata as metadata
24+
25+
try:
26+
metadata.version('${moduleName}')
27+
print("true")
28+
except metadata.PackageNotFoundError:
29+
print("false")
30+
`
31+
try {
32+
const execFileAsync = promisify(execFile)
33+
const { stdout } = await execFileAsync(pythonPath, ["-c", checkScript])
34+
const isInstalled = stdout.trim() === "true"
35+
traceInfo(`${moduleName} is installed: ${isInstalled}`)
36+
37+
return ok(stdout.trim() === "true")
38+
} catch (error) {
39+
return err(`Failed to check tcloud installation: ${error}`)
40+
}}

vscode/extension/src/utilities/sqlmesh/sqlmesh.ts

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Result, err, isErr, ok } from "../functional/result"
55
import { getProjectRoot } from "../common/utilities"
66
import { execFile } from "child_process"
77
import { promisify } from "util"
8+
import { isPythonModuleInstalled } from "../python"
89

910

1011
export type sqlmesh_exec = {
@@ -22,39 +23,7 @@ export type sqlmesh_exec = {
2223
export const is_tcloud_installed = async (): Promise<
2324
Result<boolean, string>
2425
> => {
25-
const interpreterDetails = await getInterpreterDetails()
26-
if (!interpreterDetails.path) {
27-
return err("No Python interpreter found")
28-
}
29-
traceVerbose(
30-
`Using interpreter from Python extension: ${interpreterDetails.path.join(
31-
" "
32-
)}`
33-
)
34-
35-
const pythonPath = interpreterDetails.path[0]
36-
const checkScript = `
37-
import sys
38-
if sys.version_info >= (3, 12):
39-
from importlib import metadata
40-
else:
41-
import importlib_metadata as metadata
42-
43-
try:
44-
metadata.version('tcloud')
45-
print("true")
46-
except metadata.PackageNotFoundError:
47-
print("false")
48-
`
49-
try {
50-
const execFileAsync = promisify(execFile)
51-
traceVerbose(`Checking tcloud installation with script: ${checkScript}`)
52-
const { stdout } = await execFileAsync(pythonPath, ["-c", checkScript])
53-
traceVerbose(`tcloud installation check result: ${stdout.trim()}`)
54-
return ok(stdout.trim() === "true")
55-
} catch (error) {
56-
return err(`Failed to check tcloud installation: ${error}`)
57-
}
26+
return isPythonModuleInstalled("tcloud")
5827
}
5928

6029
/**

0 commit comments

Comments
 (0)