|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { _ISOLATED as ISOLATED } from './scripts'; |
| 5 | + |
| 6 | +// "python" contains functions corresponding to the various ways that |
| 7 | +// the extension invokes a Python executable internally. Each function |
| 8 | +// takes arguments relevant to the specific use case. However, each |
| 9 | +// always *returns* a list of strings for the commandline arguments that |
| 10 | +// should be used when invoking the Python executable for the specific |
| 11 | +// use case, whether through spawn/exec or a terminal. |
| 12 | +// |
| 13 | +// Where relevant (nearly always), the function also returns a "parse" |
| 14 | +// function that may be used to deserialize the stdout of the command |
| 15 | +// into the corresponding object or objects. "parse()" takes a single |
| 16 | +// string as the stdout text and returns the relevant data. |
| 17 | + |
| 18 | +export function execCode(code: string, isolated = true): string[] { |
| 19 | + const args = ['-c', code]; |
| 20 | + if (isolated) { |
| 21 | + args.splice(0, 0, ISOLATED); |
| 22 | + } |
| 23 | + // "code" isn't specific enough to know how to parse it, |
| 24 | + // so we only return the args. |
| 25 | + return args; |
| 26 | +} |
| 27 | + |
| 28 | +export function execModule(name: string, moduleArgs: string[], isolated = true): string[] { |
| 29 | + const args = ['-m', name, ...moduleArgs]; |
| 30 | + if (isolated) { |
| 31 | + args[0] = ISOLATED; // replace |
| 32 | + } |
| 33 | + // "code" isn't specific enough to know how to parse it, |
| 34 | + // so we only return the args. |
| 35 | + return args; |
| 36 | +} |
| 37 | + |
| 38 | +export function getVersion(): [string[], (out: string) => string] { |
| 39 | + // There is no need to isolate this. |
| 40 | + const args = ['--version']; |
| 41 | + |
| 42 | + function parse(out: string): string { |
| 43 | + return out.trim(); |
| 44 | + } |
| 45 | + |
| 46 | + return [args, parse]; |
| 47 | +} |
| 48 | + |
| 49 | +export function getSysPrefix(): [string[], (out: string) => string] { |
| 50 | + const args = [ISOLATED, '-c', 'import sys;print(sys.prefix)']; |
| 51 | + |
| 52 | + function parse(out: string): string { |
| 53 | + return out.trim(); |
| 54 | + } |
| 55 | + |
| 56 | + return [args, parse]; |
| 57 | +} |
| 58 | + |
| 59 | +export function getExecutable(): [string[], (out: string) => string] { |
| 60 | + const args = [ISOLATED, '-c', 'import sys;print(sys.executable)']; |
| 61 | + |
| 62 | + function parse(out: string): string { |
| 63 | + return out.trim(); |
| 64 | + } |
| 65 | + |
| 66 | + return [args, parse]; |
| 67 | +} |
| 68 | + |
| 69 | +export function getSitePackages(): [string[], (out: string) => string] { |
| 70 | + const args = [ |
| 71 | + ISOLATED, |
| 72 | + '-c', |
| 73 | + // On windows we also need the libs path (second item will |
| 74 | + // return c:\xxx\lib\site-packages). This is returned by |
| 75 | + // the following: |
| 76 | + 'from distutils.sysconfig import get_python_lib; print(get_python_lib())' |
| 77 | + ]; |
| 78 | + |
| 79 | + function parse(out: string): string { |
| 80 | + return out.trim(); |
| 81 | + } |
| 82 | + |
| 83 | + return [args, parse]; |
| 84 | +} |
| 85 | + |
| 86 | +export function getUserSitePackages(): [string[], (out: string) => string] { |
| 87 | + const args = [ISOLATED, 'site', '--user-site']; |
| 88 | + |
| 89 | + function parse(out: string): string { |
| 90 | + return out.trim(); |
| 91 | + } |
| 92 | + |
| 93 | + return [args, parse]; |
| 94 | +} |
| 95 | + |
| 96 | +export function isValid(): [string[], (out: string) => boolean] { |
| 97 | + // There is no need to isolate this. |
| 98 | + const args = ['-c', 'print(1234)']; |
| 99 | + |
| 100 | + function parse(out: string): boolean { |
| 101 | + return out.startsWith('1234'); |
| 102 | + } |
| 103 | + |
| 104 | + return [args, parse]; |
| 105 | +} |
| 106 | + |
| 107 | +export function isModuleInstalled(name: string): [string[], (out: string) => boolean] { |
| 108 | + const args = [ISOLATED, '-c', `import ${name}`]; |
| 109 | + |
| 110 | + function parse(_out: string): boolean { |
| 111 | + // If the command did not fail then the module is installed. |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + return [args, parse]; |
| 116 | +} |
| 117 | + |
| 118 | +export function getModuleVersion(name: string): [string[], (out: string) => string] { |
| 119 | + const args = [ISOLATED, name, '--version']; |
| 120 | + |
| 121 | + function parse(out: string): string { |
| 122 | + return out.trim(); |
| 123 | + } |
| 124 | + |
| 125 | + return [args, parse]; |
| 126 | +} |
0 commit comments