Skip to content

Commit

Permalink
use pip.PackageCacheInfo in pip.installCachedPackage
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal committed Feb 13, 2023
1 parent 4c4a122 commit 52ce1cb
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 52 deletions.
58 changes: 36 additions & 22 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,36 +696,50 @@ async function uninstallPackage(packageName) {
}
exports.uninstallPackage = uninstallPackage;
async function installCachedPackage(packageName) {
let pkgInfo = await (0, info_1.showPackageInfo)(packageName);
if (pkgInfo === null) {
packageName = validatePackageName(packageName);
pkgInfo = await log_1.default.group(`Installing ${log_1.default.emph(packageName)} package...`, async () => {
const pkgInfo = await (0, info_1.showPackageInfo)(packageName);
if (pkgInfo !== null)
return;
packageName = validatePackageName(packageName);
await log_1.default.group(`Installing ${log_1.default.emph(packageName)} package...`, async () => {
log_1.default.info("Checking for cache...");
const cacheInfo = new cache_1.PackageCacheInfo(packageName);
const contentInfo = await cacheInfo.restoreContentInfo();
if (contentInfo !== undefined) {
log_1.default.info("Restoring package from cache...");
if (await (0, cache_1.restorePackage)(packageName)) {
const key = await contentInfo.restore();
if (key !== undefined) {
log_1.default.info("Validating package...");
const pkgInfo = await (0, info_1.showPackageInfo)(packageName);
if (pkgInfo !== null) {
log_1.default.info("Package is valid");
return pkgInfo;
return;
}
log_1.default.warning("Invalid package. Cache probably is corrupted!");
log_1.default.warning("Invalid package! Cache probably is corrupted");
}
log_1.default.info("Installing package using pip...");
installPackage(packageName);
log_1.default.info("Saving package to cache...");
await (0, cache_1.cachePackage)(packageName);
log_1.default.info("Validating package...");
const pkgInfo = await (0, info_1.showPackageInfo)(packageName);
if (pkgInfo === null) {
throw new Error("Invalid package. Installation probably is corrupted!");
else {
log_1.default.warning("Could not restore package from cache!");
}
log_1.default.info("Package is valid");
return pkgInfo;
});
}
for (const dependency of pkgInfo.dependencies) {
await installCachedPackage(dependency);
}
}
log_1.default.info("Installing package using pip...");
await installPackage(packageName);
log_1.default.info("Saving package to cache...");
try {
const contentInfo = await cacheInfo.accumulateContentInfo();
await contentInfo.save();
await cacheInfo.saveContentInfo();
}
catch (err) {
const errMsg = err instanceof Error ? err.message : "unknown error";
log_1.default.warning(`Could not save package to cache! ${errMsg}`);
}
log_1.default.info("Validating package...");
const pkgInfo = await (0, info_1.showPackageInfo)(packageName);
if (pkgInfo === null) {
log_1.default.error("Invalid package! Installation probably is corrupted");
throw new Error("Invalid package");
}
log_1.default.info("Package is valid");
});
}
exports.installCachedPackage = installCachedPackage;

Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

67 changes: 38 additions & 29 deletions src/deps/pip/install.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as exec from "../../exec";
import log from "../../log";
import { PackageInfo, showPackageInfo } from "./info";
import { cachePackage, restorePackage } from "./cache";
import { showPackageInfo } from "./info";
import { PackageCacheInfo } from "./cache";

function validatePackageName(packageName: string): string {
switch (packageName.toLowerCase()) {
Expand All @@ -22,39 +22,48 @@ export async function uninstallPackage(packageName: string) {
}

export async function installCachedPackage(packageName: string) {
let pkgInfo = await showPackageInfo(packageName);
if (pkgInfo === null) {
packageName = validatePackageName(packageName);
pkgInfo = await log.group(
`Installing ${log.emph(packageName)} package...`,
async (): Promise<PackageInfo> => {
const pkgInfo = await showPackageInfo(packageName);
if (pkgInfo !== null) return;
packageName = validatePackageName(packageName);
await log.group(
`Installing ${log.emph(packageName)} package...`,
async () => {
log.info("Checking for cache...");
const cacheInfo = new PackageCacheInfo(packageName);
const contentInfo = await cacheInfo.restoreContentInfo();
if (contentInfo !== undefined) {
log.info("Restoring package from cache...");
if (await restorePackage(packageName)) {
const key = await contentInfo.restore();
if (key !== undefined) {
log.info("Validating package...");
const pkgInfo = await showPackageInfo(packageName);
if (pkgInfo !== null) {
log.info("Package is valid");
return pkgInfo;
return;
}
log.warning("Invalid package. Cache probably is corrupted!");
log.warning("Invalid package! Cache probably is corrupted");
} else {
log.warning("Could not restore package from cache!");
}
log.info("Installing package using pip...");
installPackage(packageName);
log.info("Saving package to cache...");
await cachePackage(packageName);
log.info("Validating package...");
const pkgInfo = await showPackageInfo(packageName);
if (pkgInfo === null) {
throw new Error(
"Invalid package. Installation probably is corrupted!"
);
}
log.info("Package is valid");
return pkgInfo;
}
);
}
for (const dependency of pkgInfo.dependencies) {
await installCachedPackage(dependency);
}
log.info("Installing package using pip...");
await installPackage(packageName);
log.info("Saving package to cache...");
try {
const contentInfo = await cacheInfo.accumulateContentInfo();
await contentInfo.save();
await cacheInfo.saveContentInfo();
} catch (err) {
const errMsg = err instanceof Error ? err.message : "unknown error";
log.warning(`Could not save package to cache! ${errMsg}`);
}
log.info("Validating package...");
const pkgInfo = await showPackageInfo(packageName);
if (pkgInfo === null) {
log.error("Invalid package! Installation probably is corrupted");
throw new Error("Invalid package");
}
log.info("Package is valid");
}
);
}

0 comments on commit 52ce1cb

Please sign in to comment.