This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathinterpreterService.ts
292 lines (269 loc) · 13 KB
/
interpreterService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { inject, injectable } from 'inversify'
import md5 from 'md5'
import path from 'path'
import { workspace, Disposable, events, Event, Emitter, Uri } from 'coc.nvim'
import { IDocumentManager, IWorkspaceService } from '../common/application/types'
import { getArchitectureDisplayName } from '../common/platform/registry'
import { IFileSystem } from '../common/platform/types'
import { IPythonExecutionFactory } from '../common/process/types'
import { IConfigurationService, IDisposableRegistry, IPersistentState, IPersistentStateFactory, Resource } from '../common/types'
import { sleep } from '../common/utils/async'
import { IServiceContainer } from '../ioc/types'
import {
IInterpreterDisplay, IInterpreterHelper, IInterpreterLocatorService,
IInterpreterService, INTERPRETER_LOCATOR_SERVICE,
InterpreterType, PythonInterpreter
} from './contracts'
import { IVirtualEnvironmentManager } from './virtualEnvs/types'
import { emptyFn } from '../common/function'
const EXPITY_DURATION = 24 * 60 * 60 * 1000
@injectable()
export class InterpreterService implements Disposable, IInterpreterService {
private readonly locator: IInterpreterLocatorService
private readonly fs: IFileSystem
private readonly persistentStateFactory: IPersistentStateFactory
private readonly configService: IConfigurationService
private readonly didChangeInterpreterEmitter = new Emitter<void>()
private readonly didChangeInterpreterInformation = new Emitter<PythonInterpreter>()
private readonly inMemoryCacheOfDisplayNames = new Map<string, string>()
private readonly updatedInterpreters = new Set<string>()
private pythonPathSetting = ''
constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer) {
this.locator = serviceContainer.get<IInterpreterLocatorService>(IInterpreterLocatorService, INTERPRETER_LOCATOR_SERVICE)
this.fs = this.serviceContainer.get<IFileSystem>(IFileSystem)
this.persistentStateFactory = this.serviceContainer.get<IPersistentStateFactory>(IPersistentStateFactory)
this.configService = this.serviceContainer.get<IConfigurationService>(IConfigurationService)
}
public get hasInterpreters(): Promise<boolean> {
return this.locator.hasInterpreters
}
public async refresh(resource?: Uri) {
const interpreterDisplay = this.serviceContainer.get<IInterpreterDisplay>(IInterpreterDisplay)
return interpreterDisplay.refresh(resource)
}
public initialize() {
const disposables = this.serviceContainer.get<Disposable[]>(IDisposableRegistry)
// const documentManager = this.serviceContainer.get<IDocumentManager>(IDocumentManager)
events.on('BufEnter', async bufnr => {
let doc = workspace.getDocument(bufnr)
if (doc && doc.filetype == 'python') {
await this.refresh(Uri.parse(doc.uri))
}
}, null, disposables)
// disposables.push(documentManager.onDidChangeActiveTextEditor(e => e ? this.refresh(e.document.uri) : undefined))
const workspaceService = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService)
const pySettings = this.configService.getSettings()
this.pythonPathSetting = pySettings.pythonPath
const disposable = workspaceService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('python.pythonPath', undefined)) {
this.onConfigChanged()
}
})
disposables.push(disposable)
}
public async getInterpreters(resource?: Uri): Promise<PythonInterpreter[]> {
const interpreters = await this.locator.getInterpreters(resource)
await Promise.all(interpreters
.filter(item => !item.displayName)
.map(async item => {
item.displayName = await this.getDisplayName(item, resource)
// Keep information up to date with latest details.
if (!item.cachedEntry) {
this.updateCachedInterpreterInformation(item, resource).catch(emptyFn)
}
}))
return interpreters
}
public dispose(): void {
this.locator.dispose()
this.didChangeInterpreterEmitter.dispose()
this.didChangeInterpreterInformation.dispose()
}
public get onDidChangeInterpreter(): Event<void> {
return this.didChangeInterpreterEmitter.event
}
public get onDidChangeInterpreterInformation(): Event<PythonInterpreter> {
return this.didChangeInterpreterInformation.event
}
public async getActiveInterpreter(resource?: Uri): Promise<PythonInterpreter | undefined> {
const pythonExecutionFactory = this.serviceContainer.get<IPythonExecutionFactory>(IPythonExecutionFactory)
const pythonExecutionService = await pythonExecutionFactory.create({ resource })
const fullyQualifiedPath = await pythonExecutionService.getExecutablePath().catch(() => undefined)
// Python path is invalid or python isn't installed.
if (!fullyQualifiedPath) {
return
}
return this.getInterpreterDetails(fullyQualifiedPath, resource)
}
public async getInterpreterDetails(pythonPath: string, resource?: Uri): Promise<PythonInterpreter | undefined> {
// If we don't have the fully qualified path, then get it.
if (path.basename(pythonPath) === pythonPath) {
const pythonExecutionFactory = this.serviceContainer.get<IPythonExecutionFactory>(IPythonExecutionFactory)
const pythonExecutionService = await pythonExecutionFactory.create({ resource })
pythonPath = await pythonExecutionService.getExecutablePath().catch(() => '')
// Python path is invalid or python isn't installed.
if (!pythonPath) {
return
}
}
const store = await this.getInterpreterCache(pythonPath)
if (store.value && store.value.info) {
return store.value.info
}
const fs = this.serviceContainer.get<IFileSystem>(IFileSystem)
// Don't want for all interpreters are collected.
// Try to collect the infromation manually, that's faster.
// Get from which ever comes first.
const option1 = (async () => {
const result = this.collectInterpreterDetails(pythonPath, resource)
await sleep(1000) // let the other option complete within 1s if possible.
return result
})()
// This is the preferred approach, hence the delay in option 1.
const option2 = (async () => {
const interpreters = await this.getInterpreters(resource)
const found = interpreters.find(i => fs.arePathsSame(i.path, pythonPath))
if (found) {
// Cache the interpreter info, only if we get the data from interpretr list.
// tslint:disable-next-line:no-any
(found as any).__store = true
return found
}
// Use option1 as a fallback.
// tslint:disable-next-line:no-any
return option1 as any as PythonInterpreter
})()
const interpreterInfo = await Promise.race([option2, option1]) as PythonInterpreter
// tslint:disable-next-line:no-any
if (interpreterInfo && (interpreterInfo as any).__store) {
await this.updateCachedInterpreterInformation(interpreterInfo, resource)
} else {
// If we got information from option1, then when option2 finishes cache it for later use (ignoring erors)
option2.then(async info => {
// tslint:disable-next-line:no-any
if (info && (info as any).__store) {
await this.updateCachedInterpreterInformation(info, resource)
}
}).catch(emptyFn)
}
return interpreterInfo
}
/**
* Gets the display name of an interpreter.
* The format is `Python <Version> <bitness> (<env name>: <env type>)`
* E.g. `Python 3.5.1 32-bit (myenv2: virtualenv)`
* @param {Partial<PythonInterpreter>} info
* @returns {string}
* @memberof InterpreterService
*/
public async getDisplayName(info: Partial<PythonInterpreter>, resource?: Uri): Promise<string> {
// faster than calculating file has agian and again, only when deailing with cached items.
if (!info.cachedEntry && info.path && this.inMemoryCacheOfDisplayNames.has(info.path)) {
return this.inMemoryCacheOfDisplayNames.get(info.path)!
}
const fileHash = (info.path ? await this.fs.getFileHash(info.path).catch(() => '') : '') || ''
// Do not include dipslay name into hash as that changes.
const interpreterHash = `${fileHash}-${md5(JSON.stringify({ ...info, displayName: '' }))}`
const store = this.persistentStateFactory.createGlobalPersistentState<{ hash: string; displayName: string }>(`${info.path}.interpreter.displayName.v7`, undefined, EXPITY_DURATION)
if (store.value && store.value.hash === interpreterHash && store.value.displayName) {
this.inMemoryCacheOfDisplayNames.set(info.path!, store.value.displayName)
return store.value.displayName
}
const displayName = await this.buildInterpreterDisplayName(info, resource)
// If dealing with cached entry, then do not store the display name in cache.
if (!info.cachedEntry) {
await store.updateValue({ displayName, hash: interpreterHash })
this.inMemoryCacheOfDisplayNames.set(info.path!, displayName)
}
return displayName
}
public async getInterpreterCache(pythonPath: string): Promise<IPersistentState<{ fileHash: string; info?: PythonInterpreter }>> {
const fileHash = (pythonPath ? await this.fs.getFileHash(pythonPath).catch(() => '') : '') || ''
const store = this.persistentStateFactory.createGlobalPersistentState<{ fileHash: string; info?: PythonInterpreter }>(`${pythonPath}.interpreter.Details.v7`, undefined, EXPITY_DURATION)
if (!store.value || store.value.fileHash !== fileHash) {
await store.updateValue({ fileHash })
}
return store
}
protected async updateCachedInterpreterInformation(info: PythonInterpreter, resource: Resource): Promise<void> {
const key = JSON.stringify(info)
if (this.updatedInterpreters.has(key)) {
return
}
this.updatedInterpreters.add(key)
const state = await this.getInterpreterCache(info.path)
info.displayName = await this.getDisplayName(info, resource)
// Check if info has indeed changed.
if (state.value && state.value.info &&
JSON.stringify(info) === JSON.stringify(state.value.info)) {
return
}
this.inMemoryCacheOfDisplayNames.delete(info.path)
await state.updateValue({ fileHash: state.value.fileHash, info })
this.didChangeInterpreterInformation.fire(info)
}
protected async buildInterpreterDisplayName(info: Partial<PythonInterpreter>, resource?: Uri): Promise<string> {
const displayNameParts: string[] = ['Python']
const envSuffixParts: string[] = []
if (info.version) {
displayNameParts.push(`${info.version.major}.${info.version.minor}.${info.version.patch}`)
}
if (info.architecture) {
displayNameParts.push(getArchitectureDisplayName(info.architecture))
}
if (!info.envName && info.path && info.type && info.type === InterpreterType.Pipenv) {
// If we do not have the name of the environment, then try to get it again.
// This can happen based on the context (i.e. resource).
// I.e. we can determine if an environment is PipEnv only when giving it the right workspacec path (i.e. resource).
const virtualEnvMgr = this.serviceContainer.get<IVirtualEnvironmentManager>(IVirtualEnvironmentManager)
info.envName = await virtualEnvMgr.getEnvironmentName(info.path, resource)
}
if (info.envName && info.envName.length > 0) {
envSuffixParts.push(`'${info.envName}'`)
}
if (info.type) {
const interpreterHelper = this.serviceContainer.get<IInterpreterHelper>(IInterpreterHelper)
const name = interpreterHelper.getInterpreterTypeDisplayName(info.type)
if (name) {
envSuffixParts.push(name)
}
}
const envSuffix = envSuffixParts.length === 0 ? '' :
`(${envSuffixParts.join(': ')})`
return `${displayNameParts.join(' ')} ${envSuffix}`.trim()
}
private onConfigChanged = () => {
// Check if we actually changed our python path
const pySettings = this.configService.getSettings()
if (this.pythonPathSetting !== pySettings.pythonPath) {
this.pythonPathSetting = pySettings.pythonPath
this.didChangeInterpreterEmitter.fire()
const interpreterDisplay = this.serviceContainer.get<IInterpreterDisplay>(IInterpreterDisplay)
// tslint:disable-next-line: no-console
interpreterDisplay.refresh().catch(ex => console.error('Python Extension: display.refresh', ex))
}
}
private async collectInterpreterDetails(pythonPath: string, resource: Uri | undefined) {
const interpreterHelper = this.serviceContainer.get<IInterpreterHelper>(IInterpreterHelper)
const virtualEnvManager = this.serviceContainer.get<IVirtualEnvironmentManager>(IVirtualEnvironmentManager)
const [info, type] = await Promise.all([
interpreterHelper.getInterpreterInformation(pythonPath),
virtualEnvManager.getEnvironmentType(pythonPath)
])
if (!info) {
return
}
const details: Partial<PythonInterpreter> = {
...(info as PythonInterpreter),
path: pythonPath,
type
}
const envName = type === InterpreterType.Unknown ? undefined : await virtualEnvManager.getEnvironmentName(pythonPath, resource)
const pthonInfo = {
...(details as PythonInterpreter),
envName
}
pthonInfo.displayName = await this.getDisplayName(pthonInfo, resource)
return pthonInfo
}
}