Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 807b201

Browse files
committed
add uninstall cmd
1 parent e141891 commit 807b201

3 files changed

Lines changed: 32 additions & 42 deletions

File tree

engine/extensions/python-engines/python_utils.cc

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,21 @@ std::filesystem::path GetUvPath() {
2020
const auto bin_name = system_info->os == kWindowsOs ? "uv.exe" : "uv";
2121
return GetPythonEnginesPath() / "bin" / bin_name;
2222
}
23+
bool UvCleanCache() {
24+
auto cmd = UvBuildCommand("cache");
25+
cmd.push_back("clean");
26+
auto result = cortex::process::SpawnProcess(cmd);
27+
if (result.has_error()) {
28+
CTL_INF(result.error());
29+
return false;
30+
}
31+
return cortex::process::WaitProcess(result.value());
32+
}
2333

24-
bool IsUvInstalled() {
34+
bool UvIsInstalled() {
2535
return std::filesystem::exists(GetUvPath());
2636
}
27-
cpp::result<void, std::string> InstallUv() {
37+
cpp::result<void, std::string> UvInstall() {
2838
const auto py_bin_path = GetPythonEnginesPath() / "bin";
2939
std::filesystem::create_directories(py_bin_path);
3040

@@ -75,7 +85,7 @@ cpp::result<void, std::string> InstallUv() {
7585
// this Python installation.
7686
// we can add this once we allow passing custom env var to SpawnProcess().
7787
// https://docs.astral.sh/uv/reference/cli/#uv-python-install
78-
std::vector<std::string> command = BuildUvCommand("python");
88+
std::vector<std::string> command = UvBuildCommand("python");
7989
command.push_back("install");
8090
command.push_back("3.10");
8191

@@ -92,7 +102,7 @@ cpp::result<void, std::string> InstallUv() {
92102
return {};
93103
}
94104

95-
std::vector<std::string> BuildUvCommand(const std::string& action,
105+
std::vector<std::string> UvBuildCommand(const std::string& action,
96106
const std::string& directory) {
97107
// use our own cache dir so that when users delete cortexcpp/, everything is deleted.
98108
const auto cache_dir = GetPythonEnginesPath() / "cache" / "uv";
@@ -106,31 +116,4 @@ std::vector<std::string> BuildUvCommand(const std::string& action,
106116
return command;
107117
}
108118

109-
// cpp::result<void, std::string> UvDownloadDeps(
110-
// const std::filesystem::path& model_dir) {
111-
// if (!IsUvInstalled())
112-
// return cpp::fail(
113-
// "uv is not installed. Please run `cortex engines install python`.");
114-
115-
// std::vector<std::string> command = BuildUvCommand("sync", model_dir.string());
116-
117-
// // script mode. 1st argument is path to .py script
118-
// if (!std::filesystem::exists(model_dir / "pyproject.toml")) {
119-
// config::PythonModelConfig py_cfg;
120-
// py_cfg.ReadFromYaml((model_dir / "model.yml").string());
121-
// command.push_back("--script");
122-
// command.push_back(py_cfg.entrypoint[0]);
123-
// }
124-
125-
// auto result = cortex::process::SpawnProcess(command);
126-
// if (result.has_error())
127-
// return cpp::fail("Fail to install Python dependencies. " + result.error());
128-
129-
// if (!cortex::process::WaitProcess(result.value())) {
130-
// return cpp::fail("Fail to install Python dependencies.");
131-
// }
132-
133-
// return {};
134-
// }
135-
136119
} // namespace python_utils

engine/extensions/python-engines/python_utils.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ std::filesystem::path GetEnvsPath();
1414
std::filesystem::path GetUvPath();
1515

1616
// UV-related functions
17-
bool IsUvInstalled();
18-
cpp::result<void, std::string> InstallUv();
19-
std::vector<std::string> BuildUvCommand(const std::string& action,
17+
bool UvIsInstalled();
18+
cpp::result<void, std::string> UvInstall();
19+
std::vector<std::string> UvBuildCommand(const std::string& action,
2020
const std::string& directory = "");
21-
// cpp::result<void, std::string> UvDownloadDeps(
22-
// const std::filesystem::path& yaml_path);
21+
bool UvCleanCache();
2322

2423
struct PythonSubprocess {
2524
cortex::process::ProcessInfo proc_info;

engine/services/engine_service.cc

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,16 @@ cpp::result<bool, std::string> EngineService::UninstallEngineVariant(
201201
} else {
202202
return cpp::fail("No variant provided");
203203
}
204+
} else if (ne == kVllmEngine) {
205+
// variant is ignored for vLLM
206+
if (version == std::nullopt) {
207+
path_to_remove = python_utils::GetEnvsPath() / "vllm";
208+
209+
// we only clean uv cache when all vLLM versions are deleted
210+
python_utils::UvCleanCache();
211+
} else {
212+
path_to_remove = python_utils::GetEnvsPath() / "vllm" / version.value();
213+
}
204214
} else {
205215
return cpp::fail("Not implemented for engine " + ne);
206216
}
@@ -394,8 +404,8 @@ cpp::result<void, std::string> EngineService::DownloadVllm(
394404
// NOTE: everything below is not async
395405
// to make it async, we have to run everything in a thread (spawning and waiting
396406
// for subprocesses)
397-
if (!python_utils::IsUvInstalled()) {
398-
auto result = python_utils::InstallUv();
407+
if (!python_utils::UvIsInstalled()) {
408+
auto result = python_utils::UvInstall();
399409
if (result.has_error())
400410
return result;
401411
}
@@ -421,21 +431,20 @@ cpp::result<void, std::string> EngineService::DownloadVllm(
421431
// initialize venv
422432
if (!fs::exists(vllm_path / ".venv")) {
423433
std::vector<std::string> cmd =
424-
python_utils::BuildUvCommand("venv", vllm_path.string());
434+
python_utils::UvBuildCommand("venv", vllm_path.string());
425435
cmd.push_back("--relocatable");
426436
auto result = cortex::process::SpawnProcess(cmd);
427437
if (result.has_error())
428438
return cpp::fail(result.error());
429439

430440
// TODO: check return code
431-
// NOTE: these are not async
432441
cortex::process::WaitProcess(result.value());
433442
}
434443

435444
// install vLLM
436445
{
437446
std::vector<std::string> cmd =
438-
python_utils::BuildUvCommand("pip", vllm_path.string());
447+
python_utils::UvBuildCommand("pip", vllm_path.string());
439448
cmd.push_back("install");
440449
cmd.push_back("vllm==" + concrete_version);
441450
auto result = cortex::process::SpawnProcess(cmd);
@@ -444,7 +453,6 @@ cpp::result<void, std::string> EngineService::DownloadVllm(
444453

445454
// TODO: check return code
446455
// one reason this may fail is that the requested version does not exist
447-
// NOTE: these are not async
448456
cortex::process::WaitProcess(result.value());
449457
}
450458

0 commit comments

Comments
 (0)