Skip to content

Commit 488aaa2

Browse files
author
Kartik Raj
authored
Implement API to check if poetry environment is related to a folder (#15686)
1 parent b0af142 commit 488aaa2

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
'use strict';
5+
6+
import { isParentPath } from '../platform/fs-paths';
7+
import { shellExec } from '../process/rawProcessApis';
8+
9+
/**
10+
* Returns true if interpreter path belongs to a poetry environment which is associated with a particular folder,
11+
* false otherwise.
12+
* @param interpreterPath Absolute path to any python interpreter.
13+
* @param folder Absolute path to the folder.
14+
*/
15+
export async function isPoetryEnvironmentRelatedToFolder(
16+
interpreterPath: string,
17+
folder: string,
18+
poetryPath = 'poetry',
19+
): Promise<boolean> {
20+
try {
21+
const result = await shellExec(`${poetryPath} env info -p`, { cwd: folder, timeout: 15000 }, undefined);
22+
const pathToEnv = result.stdout.trim();
23+
return isParentPath(interpreterPath, pathToEnv);
24+
} catch {
25+
return false; // No need to log error as this is expected if the project is not initialized for poetry.
26+
}
27+
}

src/client/common/platform/fs-paths.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,12 @@ export class FileSystemPathUtils implements IFileSystemPathUtils {
147147
export function normCasePath(filePath: string): string {
148148
return getOSType() === OSType.Windows ? nodepath.normalize(filePath).toUpperCase() : nodepath.normalize(filePath);
149149
}
150+
151+
/**
152+
* Returns true if given file path exists within the given parent directory, false otherwise.
153+
* @param filePath File path to check for
154+
* @param parentPath The potential parent path to check for
155+
*/
156+
export function isParentPath(filePath: string, parentPath: string): boolean {
157+
return normCasePath(filePath).startsWith(normCasePath(parentPath));
158+
}

0 commit comments

Comments
 (0)