Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne committed Nov 28, 2017
2 parents eff4792 + d324979 commit 4553c28
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 26 deletions.
22 changes: 10 additions & 12 deletions src/client/interpreter/locators/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import { PythonInterpreter } from "../contracts";
import { IS_WINDOWS, fsReaddirAsync } from "../../common/utils";
import * as path from 'path';
import { getArchitectureDislayName } from "../../common/registry";
import { getArchitectureDislayName } from '../../common/registry';
import { fsReaddirAsync, IS_WINDOWS } from '../../common/utils';
import { PythonInterpreter } from '../contracts';

const CheckPythonInterpreterRegEx = IS_WINDOWS ? /^python(\d+(.\d+)?)?\.exe$/ : /^python(\d+(.\d+)?)?$/;

export function lookForInterpretersInDirectory(pathToCheck: string): Promise<string[]> {
return fsReaddirAsync(pathToCheck)
.then(subDirs => subDirs.filter(fileName => CheckPythonInterpreterRegEx.test(path.basename(fileName))));
.then(subDirs => subDirs.filter(fileName => CheckPythonInterpreterRegEx.test(path.basename(fileName))))
.catch(err => {
console.error('Python Extension (lookForInterpretersInDirectory.fsReaddirAsync):', err);
return [] as string[];
});
}

export function fixInterpreterDisplayName(item: PythonInterpreter) {
if (!item.displayName) {
const arch = getArchitectureDislayName(item.architecture);
const version = item.version || '';
item.displayName = ['Python', version, arch].filter(item => item.length > 0).join(' ').trim();
const version = typeof item.version === 'string' ? item.version : '';
item.displayName = ['Python', version, arch].filter(namePart => namePart.length > 0).join(' ').trim();
}
return item;
}
export function fixInterpreterPath(item: PythonInterpreter) {
// For some reason anaconda seems to use \\ in the registry path.
item.path = IS_WINDOWS ? item.path.replace(/\\\\/g, "\\") : item.path;
item.path = IS_WINDOWS ? item.path.replace(/\//g, "\\") : item.path;
return item;
}
5 changes: 3 additions & 2 deletions src/client/interpreter/locators/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';
import * as _ from 'lodash';
import * as path from 'path';
import { Disposable, Uri, workspace } from 'vscode';
import { RegistryImplementation } from '../../common/registry';
import { arePathsSame, Is_64Bit, IS_WINDOWS } from '../../common/utils';
import { IInterpreterLocatorService, PythonInterpreter } from '../contracts';
import { InterpreterVersionService } from '../interpreterVersion';
import { VirtualEnvironmentManager } from '../virtualEnvs';
import { fixInterpreterDisplayName, fixInterpreterPath } from './helpers';
import { fixInterpreterDisplayName } from './helpers';
import { CondaEnvFileService, getEnvironmentsFile as getCondaEnvFile } from './services/condaEnvFileService';
import { CondaEnvService } from './services/condaEnvService';
import { CondaLocatorService } from './services/condaLocator';
Expand Down Expand Up @@ -53,7 +54,7 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
// tslint:disable-next-line:underscore-consistent-invocation
return _.flatten(listOfInterpreters)
.map(fixInterpreterDisplayName)
.map(fixInterpreterPath)
.map(item => { item.path = path.normalize(item.path); return item; })
.reduce<PythonInterpreter[]>((accumulator, current) => {
if (accumulator.findIndex(item => arePathsSame(item.path, current.path)) === -1) {
accumulator.push(current);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ export class CondaEnvFileService implements IInterpreterLocatorService {
.then(promises => Promise.all(promises))
.then(interpreterPaths => interpreterPaths.filter(item => item.length > 0))
.then(interpreterPaths => interpreterPaths.map(item => this.getInterpreterDetails(item)))
.then(promises => Promise.all(promises));
.then(promises => Promise.all(promises))
.catch((err) => {
console.error('Python Extension (getEnvironmentsFromFile.readFile):', err);
// Ignore errors in reading the file.
return [] as PythonInterpreter[];
});
}
private async getInterpreterDetails(interpreter: string) {
return this.versionService.getVersion(interpreter, path.basename(interpreter))
Expand Down
3 changes: 3 additions & 0 deletions src/client/interpreter/locators/services/condaEnvService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export class CondaEnvService implements IInterpreterLocatorService {
resolve([]);
}
});
}).catch((err) => {
console.error('Python Extension (getSuggestionsFromConda):', err);
return [];
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class CurrentPathService implements IInterpreterLocatorService {
resolve(getFirstNonEmptyLineFromMultilineString(stdout));
});
})
.then(value => value.length === 0 ? defaultValue : value);
.then(value => value.length === 0 ? defaultValue : value)
.catch(() => defaultValue); // Ignore exceptions in getting the executable.
}
}
28 changes: 20 additions & 8 deletions src/client/interpreter/locators/services/virtualEnvService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,29 @@ export class VirtualEnvService implements IInterpreterLocatorService {
.then(dirs => Promise.all(dirs.map(lookForInterpretersInDirectory)))
// tslint:disable-next-line:underscore-consistent-invocation
.then(pathsWithInterpreters => _.flatten(pathsWithInterpreters))
.then(interpreters => Promise.all(interpreters.map(interpreter => this.getVirtualEnvDetails(interpreter))));
.then(interpreters => Promise.all(interpreters.map(interpreter => this.getVirtualEnvDetails(interpreter))))
.catch((err) => {
console.error('Python Extension (lookForInterpretersInVenvs):', err);
// Ignore exceptions.
return [] as PythonInterpreter[];
});
}
private getProspectiveDirectoriesForLookup(subDirs: string[]) {
const dirToLookFor = IS_WINDOWS ? 'SCRIPTS' : 'BIN';
return subDirs.map(subDir => fsReaddirAsync(subDir).then(dirs => {
const scriptOrBinDirs = dirs.filter(dir => {
const folderName = path.basename(dir);
return folderName.toUpperCase() === dirToLookFor;
});
return scriptOrBinDirs.length === 1 ? scriptOrBinDirs[0] : '';
}));
return subDirs.map(subDir => fsReaddirAsync(subDir)
.then(dirs => {
const scriptOrBinDirs = dirs.filter(dir => {
const folderName = path.basename(dir);
return folderName.toUpperCase() === dirToLookFor;
});
return scriptOrBinDirs.length === 1 ? scriptOrBinDirs[0] : '';
})
.catch((err) => {
console.error('Python Extension (getProspectiveDirectoriesForLookup):', err);
// Ignore exceptions.
return '';
}));

}
private async getVirtualEnvDetails(interpreter: string): Promise<PythonInterpreter> {
return Promise.all([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class WindowsRegistryService implements IInterpreterLocatorService {
companyDisplayName: interpreterInfo.companyDisplayName
} as PythonInterpreter;
})
.then(interpreter => interpreter ? fs.pathExists(interpreter.path).then(exists => exists ? interpreter : null) : null)
.then(interpreter => interpreter ? fs.pathExists(interpreter.path).catch(() => false).then(exists => exists ? interpreter : null) : null)
.catch(error => {
console.error(`Failed to retrieve interpreter details for company ${companyKey},tag: ${tagKey}, hive: ${hive}, arch: ${arch}`);
console.error(error);
Expand Down
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@
"no-backbone-get-set-outside-model": false,
"underscore-consistent-invocation": false,
"no-void-expression": false,
"no-non-null-assertion": false,
"no-non-null-assertion": false,
"prefer-type-cast": false,
"promise-function-async": false,
"function-name": false,
"variable-name": false
}
Expand Down

0 comments on commit 4553c28

Please sign in to comment.