Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(deducer): parse index URLs from requirements.txt #298

Merged
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
7 changes: 7 additions & 0 deletions .changeset/smart-pants-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@plutolang/pyright-deducer": patch
---

feat(deducer): parse index URLs from requirements.txt

The deducer now parses index URLs specified in the project's root requirements.txt file. These URLs are utilized for dependency installation and are also included in the generated requirements.txt file.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as SlimUtils from "./slim";
import * as AwsUtils from "./aws-utils";
import * as CmdUtils from "./command-utils";
import * as MetadataUtils from "./metadata";
import { getIndexUrls, IndexUrl } from "./pyproject";
import { getIndexUrls, IndexUrl } from "./index-url";
import { Architecture, InstalledModule, Module, ModuleType, Runtime } from "./types";

export interface BundleModulesOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ export async function getIndexUrls(): Promise<IndexUrl[]> {
// to find a way to get the root of the workspace.
const rootpath = process.cwd();

let indexUrls: IndexUrl[] = [];
const pyprojectToml = await loadPyprojectToml(rootpath);
if (!pyprojectToml || !isManagedByPoetry(pyprojectToml)) {
return [];
if (pyprojectToml && isManagedByPoetry(pyprojectToml)) {
indexUrls = getPoetrySources(pyprojectToml);
} else if (await fs.exists(path.join(rootpath, "requirements.txt"))) {
const requirements = await fs.readFile(path.join(rootpath, "requirements.txt"), "utf-8");
indexUrls = getIndexUrlsFromRequirements(requirements);
}

const sources = getPoetrySources(pyprojectToml);

// ensure there is only one primary source
let hasPrimary = false;
for (const source of sources) {
for (const source of indexUrls) {
if (!source.primary) continue;

if (hasPrimary) {
Expand All @@ -41,7 +43,7 @@ export async function getIndexUrls(): Promise<IndexUrl[]> {
}
}

return sources;
return indexUrls;
}

async function loadPyprojectToml(rootpath: string) {
Expand Down Expand Up @@ -75,3 +77,18 @@ function getPoetrySources(pyprojectToml: any): IndexUrl[] {
return { url: s.url, primary: s.priority === "primary" || s.priority === "default" };
});
}

function getIndexUrlsFromRequirements(requirements: string): IndexUrl[] {
const lines = requirements.split("\n");
const indexUrls: IndexUrl[] = [];
for (const line of lines) {
const match = line.match(/--(extra-)?index-url\s+(.+)/);
if (!match) continue;

const primary = !match[1];
const url = match[2];
indexUrls.push({ url, primary });
}

return indexUrls;
}
Loading