Skip to content

Commit

Permalink
Refactor InstallPackageFromArchive
Browse files Browse the repository at this point in the history
  • Loading branch information
mtorpey committed Oct 2, 2024
1 parent 3416ea1 commit 4408df1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 36 deletions.
1 change: 1 addition & 0 deletions gap/archive.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
DeclareGlobalFunction("InstallPackageFromArchive");

DeclareGlobalFunction("PKGMAN_ExtractArchive");
DeclareGlobalFunction("PKGMAN_TarTopDirectory");

PKGMAN_ArchiveFormats := [".tar.gz", ".tar.bz2"];
70 changes: 34 additions & 36 deletions gap/archive.gi
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
InstallGlobalFunction(InstallPackageFromArchive,
function(url)
local get, user_pkg_dir, url_parts, filename, path, exec, topdir, dir,
movedname;
local archive_path, dir;

# Download the archive
archive_path := PKGMAN_DownloadUrlToTempFile(url);
if archive_path = fail then return false; fi;

# Extract the archive
dir := PKGMAN_ExtractArchive(archive_path, PKGMAN_PackageDir());
if dir = fail then return false; fi;

# Download archive
Info(InfoPackageManager, 3, "Downloading archive from URL ", url, " ...");
get := PKGMAN_DownloadURL(url);
if get.success <> true then
Info(InfoPackageManager, 1, "Could not download from ", url);
# Install dependencies
if PKGMAN_InstallDependencies(dir) <> true then
Info(InfoPackageManager, 1, "Dependencies not satisfied for ", PKGMAN_TarTopDirectory(archive_path));
PKGMAN_RemoveDirOptional(dir);
return false;
fi;

# Check validity
if PKGMAN_CheckPackage(dir) = false then
PKGMAN_RemoveDirOptional(dir);
return false;
fi;
user_pkg_dir := PKGMAN_PackageDir();
url_parts := SplitString(url, "/");
filename := url_parts[Length(url_parts)];
path := Filename(DirectoryTemporary(), filename);
path := Concatenation(path, ".pkgman"); # TEMP: hack till GAP #4110 is merged
FileString(path, get.result);
Info(InfoPackageManager, 2, "Saved archive to ", path);

PKGMAN_RefreshPackageInfo();
return true;
end);

InstallGlobalFunction(PKGMAN_ExtractArchive,
function(archive_path, target_path)
local topdir, dir, movedname, exec;
# Find the name of the directory in the archive
topdir := PKGMAN_TarTopDirectory(path);
topdir := PKGMAN_TarTopDirectory(archive_path);
if topdir = fail then
return false;
return fail;
fi;

# Check availability of target location
dir := Filename(Directory(user_pkg_dir), topdir);
dir := Filename(Directory(target_path), topdir);
if not PKGMAN_IsValidTargetDir(dir) then
if IsDirectoryPath(dir) and IsWritableFile(dir) and IsReadableFile(dir) then
# old version installed with the same name: change dir name
Expand All @@ -35,37 +47,23 @@ function(url)
PKGMAN_RefreshPackageInfo();
if exec.code <> 0 then
Info(InfoPackageManager, 1, "Could not rename old package directory");
return false;
return fail;
fi;
else
return false;
return fail;
fi;
fi;

# Extract package
Info(InfoPackageManager, 2, "Extracting to ", dir, " ...");
exec := PKGMAN_Exec(".", "tar", "xf", path, "-C", user_pkg_dir);
exec := PKGMAN_Exec(".", "tar", "xf", archive_path, "-C", target_path);
if exec.code <> 0 then
Info(InfoPackageManager, 1, "Extraction unsuccessful");
return false;
return fail;
fi;
Info(InfoPackageManager, 4, "Extracted successfully");

# Install dependencies
if PKGMAN_InstallDependencies(dir) <> true then
Info(InfoPackageManager, 1, "Dependencies not satisfied for ", topdir);
PKGMAN_RemoveDirOptional(dir);
return false;
fi;

# Check validity
if PKGMAN_CheckPackage(dir) = false then
PKGMAN_RemoveDirOptional(dir);
return false;
fi;

PKGMAN_RefreshPackageInfo();
return true;
return dir;
end);

InstallGlobalFunction(PKGMAN_TarTopDirectory,
Expand Down
1 change: 1 addition & 0 deletions gap/download.gd
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DeclareGlobalFunction("PKGMAN_DownloadUrlToTempFile");
DeclareGlobalFunction("PKGMAN_DownloadURL");
DeclareGlobalFunction("PKGMAN_DownloadPackageInfo");

Expand Down
18 changes: 18 additions & 0 deletions gap/download.gi
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
InstallGlobalFunction(PKGMAN_DownloadUrlToTempFile,
function(url)
local get, url_parts, filename, path;
Info(InfoPackageManager, 3, "Downloading archive from URL ", url, " ...");
get := PKGMAN_DownloadURL(url);
if get.success <> true then
Info(InfoPackageManager, 1, "Could not download from ", url);
return fail;
fi;
url_parts := SplitString(url, "/");
filename := url_parts[Length(url_parts)];
path := Filename(DirectoryTemporary(), filename);
path := Concatenation(path, ".pkgman"); # TEMP: hack till GAP #4110 is merged
FileString(path, get.result);
Info(InfoPackageManager, 2, "Saved archive to ", path);
return path;
end);

InstallGlobalFunction(PKGMAN_DownloadURL,
function(url)
local tool, exec;
Expand Down

0 comments on commit 4408df1

Please sign in to comment.