Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}