дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-03 16:51:16 +02:00
feat(python): install packages from version control or a local directory (#6601)
A + button in the Python Packages tool window offers Install from Version Control… and Install from Disk… (with Browse), both with Install as editable (-e). A plain https://, ssh://, git:// or git@host:path URL gets pip's git+ form; git+… and other vcs+ URLs pass as they are. sdk_python_pip takes an editable flag that adds -e (pip and uv pip); text starting with - is still refused, so no other pip option can be passed. Closes #6531 Co-authored-by: wildcatDownstairs <[email protected]>
Цей коміт міститься в:
1 джерело
3747a8a63f
коміт
ddea7e5ec5
15 files changed
+237
-14
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
|
||||
import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { ProjectInfo } from "@/platform/ipc";
|
||||
|
||||
@@ -11,10 +11,18 @@ const mocks = vi.hoisted(() => ({
|
||||
container: vi.fn(),
|
||||
index: vi.fn(),
|
||||
cancel: vi.fn(),
|
||||
menu: vi.fn(),
|
||||
openUrl: vi.fn(),
|
||||
active: null as string | null,
|
||||
}));
|
||||
vi.mock("@/platform/openUrl", () => ({ openExternalUrl: mocks.openUrl }));
|
||||
// The context menu host is not mounted here: pick menu items by label instead.
|
||||
vi.mock("@/ui", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("@/ui")>()),
|
||||
showContextMenu: mocks.menu,
|
||||
}));
|
||||
const pick = (label: string) =>
|
||||
(mocks.menu.mock.lastCall![1] as { label: string; run: () => void }[]).find((i) => i.label === label)!.run();
|
||||
vi.mock("@tauri-apps/api/core", () => ({ Channel: class {} }));
|
||||
vi.mock("@/platform/ipc", async (importOriginal) => {
|
||||
const real = await importOriginal<typeof import("@/platform/ipc")>();
|
||||
@@ -110,6 +118,7 @@ describe("Python Packages", () => {
|
||||
"six",
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
expect.anything(),
|
||||
expect.anything(),
|
||||
);
|
||||
@@ -117,6 +126,46 @@ describe("Python Packages", () => {
|
||||
await waitFor(() => expect(mocks.packages.mock.calls.filter((c) => c[1] === false)).toHaveLength(2));
|
||||
});
|
||||
|
||||
it("installs a repository URL or a local directory, in editable mode when asked", async () => {
|
||||
open();
|
||||
await screen.findByText("urllib3");
|
||||
fireEvent.click(screen.getByRole("button", { name: "Install from Version Control or Disk" }));
|
||||
act(() => pick("Install from Version Control…"));
|
||||
const url = await screen.findByRole("textbox", { name: "Repository URL" });
|
||||
fireEvent.change(url, { target: { value: "https://github.com/benjaminp/six.git" } });
|
||||
fireEvent.click(screen.getByRole("button", { name: "Install" }));
|
||||
expect(mocks.pip).toHaveBeenLastCalledWith(
|
||||
"/p/.venv/bin/python",
|
||||
"git+https://github.com/benjaminp/six.git",
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
expect.anything(),
|
||||
expect.anything(),
|
||||
);
|
||||
await waitFor(() =>
|
||||
expect(screen.getByRole("button", { name: "Install from Version Control or Disk" })).toBeEnabled(),
|
||||
);
|
||||
|
||||
fireEvent.click(screen.getByRole("button", { name: "Install from Version Control or Disk" }));
|
||||
act(() => pick("Install from Disk…"));
|
||||
const path = await screen.findByRole("textbox", { name: "Path" });
|
||||
fireEvent.change(path, { target: { value: "--index-url=https://evil.example" } });
|
||||
expect(screen.getByRole("button", { name: "Install" })).toBeDisabled();
|
||||
fireEvent.change(path, { target: { value: "./mylib" } });
|
||||
fireEvent.click(screen.getByRole("checkbox", { name: "Install as editable (-e)" }));
|
||||
fireEvent.click(screen.getByRole("button", { name: "Install" }));
|
||||
expect(mocks.pip).toHaveBeenLastCalledWith(
|
||||
"/p/.venv/bin/python",
|
||||
"./mylib",
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
expect.anything(),
|
||||
expect.anything(),
|
||||
);
|
||||
});
|
||||
|
||||
it("shows a PyPI project's links and its Markdown description, opening links in the browser", async () => {
|
||||
open();
|
||||
await screen.findByText("urllib3");
|
||||
@@ -246,6 +295,7 @@ describe("Python Packages", () => {
|
||||
"requests",
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
expect.anything(),
|
||||
expect.anything(),
|
||||
);
|
||||
@@ -256,6 +306,7 @@ describe("Python Packages", () => {
|
||||
"requests",
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
expect.anything(),
|
||||
expect.anything(),
|
||||
);
|
||||
|
||||
@@ -8,11 +8,14 @@ import { useMarkupHtml } from "@/editor/lsp/markdown";
|
||||
import { executeAction } from "@/platform/actions";
|
||||
import { useTx } from "@/platform/i18n";
|
||||
import { call, commands, isCancelled, nextRequestId, type PypiPackage, type PythonPackage } from "@/platform/ipc";
|
||||
import { choosePath } from "@/platform/choosePath";
|
||||
import { openExternalUrl } from "@/platform/openUrl";
|
||||
import { dirname, useProject } from "@/platform/project";
|
||||
import { usePythonPackages } from "@/platform/pythonPackages";
|
||||
import {
|
||||
Button,
|
||||
Checkbox,
|
||||
Dialog,
|
||||
EmptyState,
|
||||
Icon,
|
||||
IconButton,
|
||||
@@ -23,10 +26,11 @@ import {
|
||||
SplitGroup,
|
||||
SplitHandle,
|
||||
SplitPanel,
|
||||
showContextMenu,
|
||||
T,
|
||||
} from "@/ui";
|
||||
import { editorService } from "@/workbench/contributions";
|
||||
import { isPackageName, listRows, normalize, requirementOf, rowKey, type Row } from "./model";
|
||||
import { isPackageName, listRows, normalize, requirementOf, rowKey, vcsRequirement, type Row } from "./model";
|
||||
import styles from "./PythonPackages.module.css";
|
||||
|
||||
type Op = "install" | "upgrade" | "uninstall";
|
||||
@@ -227,6 +231,7 @@ export function PackagesView() {
|
||||
const [job, setJob] = useState<Job | null>(null);
|
||||
const [log, setLog] = useState<string[]>([]);
|
||||
const [jobError, setJobError] = useState<string | null>(null);
|
||||
const [installFrom, setInstallFrom] = useState<Source | null>(null);
|
||||
|
||||
const name = query.trim();
|
||||
const searchable = !container && isPackageName(name);
|
||||
@@ -249,7 +254,7 @@ export function PackagesView() {
|
||||
const details = usePypi(picked && !container && !fromSearch && wanted ? picked.name : null, 200);
|
||||
const info = fromSearch ? search : details;
|
||||
|
||||
const run = (op: Op, requirement: string) => {
|
||||
const run = (op: Op, requirement: string, editable = false) => {
|
||||
if (!python || job) return;
|
||||
const request = nextRequestId();
|
||||
const channel = new Channel<string>();
|
||||
@@ -257,7 +262,7 @@ export function PackagesView() {
|
||||
setLog([]);
|
||||
setJobError(null);
|
||||
setJob({ op, target: requirement, cancel: () => void commands.cancelRequest(request) });
|
||||
call(commands.sdkPythonPip(python, requirement, op === "uninstall", op === "upgrade", request, channel))
|
||||
call(commands.sdkPythonPip(python, requirement, op === "uninstall", op === "upgrade", editable, request, channel))
|
||||
.catch((e: unknown) => setJobError(isCancelled(e) ? null : message(e)))
|
||||
.finally(() => {
|
||||
setJob(null);
|
||||
@@ -306,6 +311,21 @@ export function PackagesView() {
|
||||
disabled={loading || !!job}
|
||||
onClick={container ? retry : local.reload}
|
||||
/>
|
||||
{!container && (
|
||||
<IconButton
|
||||
icon="plus"
|
||||
size="small"
|
||||
title="Install from Version Control or Disk"
|
||||
disabled={!python || !!job}
|
||||
onClick={(e) => {
|
||||
const r = e.currentTarget.getBoundingClientRect();
|
||||
showContextMenu({ x: r.left, y: r.bottom + 2 }, [
|
||||
{ label: "Install from Version Control…", run: () => setInstallFrom("vcs") },
|
||||
{ label: "Install from Disk…", run: () => setInstallFrom("disk") },
|
||||
]);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
{python && (
|
||||
<span className={styles.interpreter} title={python}>
|
||||
{relativeTo(root, python)}
|
||||
@@ -456,10 +476,85 @@ export function PackagesView() {
|
||||
</pre>
|
||||
)}
|
||||
</SplitPanel>
|
||||
{installFrom && (
|
||||
<InstallFromDialog
|
||||
source={installFrom}
|
||||
onClose={() => setInstallFrom(null)}
|
||||
onInstall={(requirement, editable) => {
|
||||
setInstallFrom(null);
|
||||
run("install", requirement, editable);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</SplitGroup>
|
||||
);
|
||||
}
|
||||
|
||||
type Source = "vcs" | "disk";
|
||||
|
||||
/** A repository URL or a local directory or archive to install, as PyCharm's Advanced Package Install. */
|
||||
function InstallFromDialog({
|
||||
source,
|
||||
onClose,
|
||||
onInstall,
|
||||
}: {
|
||||
source: Source;
|
||||
onClose: () => void;
|
||||
onInstall: (requirement: string, editable: boolean) => void;
|
||||
}) {
|
||||
const tx = useTx();
|
||||
const [value, setValue] = useState("");
|
||||
const [editable, setEditable] = useState(false);
|
||||
const text = value.trim();
|
||||
// pip would read a leading `-` as one of its options.
|
||||
const valid = !!text && !text.startsWith("-");
|
||||
const install = () => valid && onInstall(source === "vcs" ? vcsRequirement(text) : text, editable);
|
||||
return (
|
||||
<Dialog
|
||||
open
|
||||
title={source === "vcs" ? "Install from Version Control" : "Install from Disk"}
|
||||
onClose={onClose}
|
||||
width={520}
|
||||
buttons={[
|
||||
{ label: tx("Cancel"), role: "cancel" },
|
||||
{ label: tx("Install"), role: "default", onClick: install, disabled: !valid },
|
||||
]}
|
||||
>
|
||||
<div className={styles.installFrom}>
|
||||
<div className={styles.field}>
|
||||
<Input
|
||||
autoFocus
|
||||
value={value}
|
||||
placeholder={
|
||||
source === "vcs" ? "git+https://github.com/owner/project.git" : tx("Project directory or archive")
|
||||
}
|
||||
aria-label={source === "vcs" ? "Repository URL" : "Path"}
|
||||
onChange={(e) => setValue(e.target.value)}
|
||||
onKeyDown={(e) => e.key === "Enter" && install()}
|
||||
/>
|
||||
{source === "disk" && (
|
||||
<Button
|
||||
onClick={() =>
|
||||
void choosePath({ directory: true, title: tx("Project Directory") }).then(
|
||||
(path) => path && setValue(path),
|
||||
)
|
||||
}
|
||||
>
|
||||
<T>Browse</T>
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
<Checkbox checked={editable} onChange={setEditable} label="Install as editable (-e)" />
|
||||
{source === "vcs" && editable && (
|
||||
<div className={styles.note}>
|
||||
<T>An editable install from version control needs the package name at the end of the URL: #egg=name.</T>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
/** `path` relative to `root` when it is inside it. */
|
||||
function relativeTo(root: string | null, path: string) {
|
||||
const base = root?.replace(/[\\/]+$/, "");
|
||||
|
||||
@@ -226,3 +226,17 @@
|
||||
user-select: text;
|
||||
-webkit-user-select: text;
|
||||
}
|
||||
|
||||
.installFrom {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--space-2);
|
||||
}
|
||||
.field {
|
||||
display: flex;
|
||||
gap: var(--space-1-5);
|
||||
}
|
||||
.field > :first-child {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { isPackageName, listRows, normalize, requirementOf, rowKey } from "./model";
|
||||
import { isPackageName, listRows, normalize, requirementOf, rowKey, vcsRequirement } from "./model";
|
||||
|
||||
const INSTALLED = [
|
||||
{ name: "requests", version: "2.31.0" },
|
||||
@@ -72,3 +72,14 @@ describe("names and requirements", () => {
|
||||
expect(requirementOf("six", null)).toBe("six");
|
||||
});
|
||||
});
|
||||
|
||||
describe("vcsRequirement", () => {
|
||||
it("installs a repository URL with Git unless it names a VCS", () => {
|
||||
expect(vcsRequirement(" https://github.com/psf/requests.git ")).toBe("git+https://github.com/psf/requests.git");
|
||||
expect(vcsRequirement("[email protected]:psf/requests.git")).toBe("git+ssh://[email protected]/psf/requests.git");
|
||||
expect(vcsRequirement("git+https://github.com/psf/[email protected]#egg=requests")).toBe(
|
||||
"git+https://github.com/psf/[email protected]#egg=requests",
|
||||
);
|
||||
expect(vcsRequirement("hg+https://hg.example.org/demo")).toBe("hg+https://hg.example.org/demo");
|
||||
});
|
||||
});
|
||||
@@ -56,3 +56,14 @@ export function listRows(
|
||||
|
||||
/** `name==version`, or the bare name for the newest release. */
|
||||
export const requirementOf = (name: string, version: string | null) => (version ? `${name}==${version}` : name);
|
||||
|
||||
/**
|
||||
* What pip installs for a repository URL: `git+https://…` and other `vcs+` forms as they are, a
|
||||
* plain `https://`, `ssh://` or `git://` URL and scp-like `git@host:path` as Git.
|
||||
*/
|
||||
export function vcsRequirement(url: string) {
|
||||
const text = url.trim();
|
||||
if (/^(https?|ssh|git):\/\//i.test(text)) return `git+${text}`;
|
||||
const scp = /^([\w.-]+@[\w.-]+):(?!\/\/)(.+)$/.exec(text);
|
||||
return scp ? `git+ssh://${scp[1]}/${scp[2]}` : text;
|
||||
}
|
||||
Reference in new issue
Заблокувати користувача