Skip to content

Commit

Permalink
Implement API to check if poetry environment is related to a folder (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartik Raj authored Mar 18, 2021
1 parent b0af142 commit 488aaa2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/client/common/installer/poetryEnvHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

import { isParentPath } from '../platform/fs-paths';
import { shellExec } from '../process/rawProcessApis';

/**
* Returns true if interpreter path belongs to a poetry environment which is associated with a particular folder,
* false otherwise.
* @param interpreterPath Absolute path to any python interpreter.
* @param folder Absolute path to the folder.
*/
export async function isPoetryEnvironmentRelatedToFolder(
interpreterPath: string,
folder: string,
poetryPath = 'poetry',
): Promise<boolean> {
try {
const result = await shellExec(`${poetryPath} env info -p`, { cwd: folder, timeout: 15000 }, undefined);
const pathToEnv = result.stdout.trim();
return isParentPath(interpreterPath, pathToEnv);
} catch {
return false; // No need to log error as this is expected if the project is not initialized for poetry.
}
}
9 changes: 9 additions & 0 deletions src/client/common/platform/fs-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,12 @@ export class FileSystemPathUtils implements IFileSystemPathUtils {
export function normCasePath(filePath: string): string {
return getOSType() === OSType.Windows ? nodepath.normalize(filePath).toUpperCase() : nodepath.normalize(filePath);
}

/**
* Returns true if given file path exists within the given parent directory, false otherwise.
* @param filePath File path to check for
* @param parentPath The potential parent path to check for
*/
export function isParentPath(filePath: string, parentPath: string): boolean {
return normCasePath(filePath).startsWith(normCasePath(parentPath));
}

0 comments on commit 488aaa2

Please sign in to comment.