Skip to content

Commit

Permalink
Abstract away some code repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
mtorpey committed Sep 9, 2024
1 parent 89616bd commit e4324af
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 40 deletions.
2 changes: 2 additions & 0 deletions gap/archive.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
#! <K>true</K> or <K>false</K>
DeclareGlobalFunction("InstallPackageFromArchive");

DeclareGlobalFunction("PKGMAN_TarTopDirectory");

PKGMAN_ArchiveFormats := [".tar.gz", ".tar.bz2"];
65 changes: 37 additions & 28 deletions gap/archive.gi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
InstallGlobalFunction(InstallPackageFromArchive,
function(url)
local get, user_pkg_dir, url_parts, filename, path, tar, options, exec,
files, topdir, dir, movedname;
local get, user_pkg_dir, url_parts, filename, path, exec, topdir, dir,
movedname;

# Download archive
Info(InfoPackageManager, 3, "Downloading archive from URL ", url, " ...");
Expand All @@ -18,27 +18,11 @@ function(url)
FileString(path, get.result);
Info(InfoPackageManager, 2, "Saved archive to ", path);

# Check which version of tar we are using
tar := PKGMAN_Exec(".", "tar", "--version");
if StartsWith(tar.output, "tar (GNU tar)") then
options := "--warning=none";
else
options := "";
fi;

# Check contents
exec := PKGMAN_Exec(".", "tar", options, "-tf", path);
if exec.code <> 0 then
Info(InfoPackageManager, 1, "Could not inspect tarball contents");
# Find the name of the directory in the archive
topdir := PKGMAN_TarTopDirectory(path);
if topdir = fail then
return false;
fi;
files := SplitString(exec.output, "", "\n");
topdir := Set(files, f -> SplitString(f, "/")[1]);
if Length(topdir) <> 1 then
Info(InfoPackageManager, 1, "Archive should contain 1 directory (not ", Length(topdir), ")");
return false;
fi;
topdir := topdir[1];

# Check availability of target location
dir := Filename(Directory(user_pkg_dir), topdir);
Expand Down Expand Up @@ -70,20 +54,45 @@ function(url)
# Install dependencies
if PKGMAN_InstallDependencies(dir) <> true then
Info(InfoPackageManager, 1, "Dependencies not satisfied for ", topdir);
if ValueOption("keepDirectory") <> true then
PKGMAN_RemoveDir(dir);
fi;
PKGMAN_RemoveDirOptional(dir);
return false;
fi;

# Check validity
if PKGMAN_CheckPackage(dir) = false then
if ValueOption("keepDirectory") <> true then
PKGMAN_RemoveDir(dir);
fi;
PKGMAN_RemoveDirOptional(dir);
return false;
fi;
PKGMAN_RefreshPackageInfo();

PKGMAN_RefreshPackageInfo();
return true;
end);

InstallGlobalFunction(PKGMAN_TarTopDirectory,
function(path)
local tar, options, exec, files, topdir;
# Check which version of tar we are using
tar := PKGMAN_Exec(".", "tar", "--version");
if StartsWith(tar.output, "tar (GNU tar)") then
options := "--warning=none";
else
options := "";
fi;

# Check contents
exec := PKGMAN_Exec(".", "tar", options, "-tf", path);
if exec.code <> 0 then
Info(InfoPackageManager, 1, "Could not inspect tarball contents");
return fail;
fi;

# Expect to find a single directory and nothing else
files := SplitString(exec.output, "", "\n");
topdir := Set(files, f -> SplitString(f, "/")[1]);
if Length(topdir) <> 1 then
Info(InfoPackageManager, 1, "Archive should contain 1 directory (not ", Length(topdir), ")");
return fail;
fi;

return topdir[1];
end);
1 change: 1 addition & 0 deletions gap/directories.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ DeclareGlobalFunction("PKGMAN_SetCustomPackageDir");
DeclareGlobalFunction("PKGMAN_CreateDirRecursively");
DeclareGlobalFunction("PKGMAN_InsertPackageDirectory");
DeclareGlobalFunction("PKGMAN_IsValidTargetDir");
DeclareGlobalFunction("PKGMAN_RemoveDirOptional");
DeclareGlobalFunction("PKGMAN_RemoveDir");

PKGMAN_CustomPackageDir := "";
Expand Down
7 changes: 7 additions & 0 deletions gap/directories.gi
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ function(dir)
return true;
end);

InstallGlobalFunction(PKGMAN_RemoveDirOptional,
function(dir)
if ValueOption("keepDirectory") <> true then
PKGMAN_RemoveDir(dir);
fi;
end);

InstallGlobalFunction(PKGMAN_RemoveDir,
function(dir)
# this 'if' statement is a paranoid check - it should always be true
Expand Down
8 changes: 2 additions & 6 deletions gap/git.gi
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,14 @@ function(url, args...)
info := Filename(Directory(dir), "PackageInfo.g");
if not IsReadableFile(info) then
Info(InfoPackageManager, 1, "Could not find PackageInfo.g");
if ValueOption("keepDirectory") <> true then
PKGMAN_RemoveDir(dir);
fi;
PKGMAN_RemoveDirOptional(dir);
return false;
fi;

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

Expand Down
8 changes: 2 additions & 6 deletions gap/hg.gi
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,14 @@ function(url, args...)
info := Filename(Directory(dir), "PackageInfo.g");
if not IsReadableFile(info) then
Info(InfoPackageManager, 1, "Could not find PackageInfo.g");
if ValueOption("keepDirectory") <> true then
PKGMAN_RemoveDir(dir);
fi;
PKGMAN_RemoveDirOptional(dir);
return false;
fi;

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

Expand Down

0 comments on commit e4324af

Please sign in to comment.