Skip to content

Commit

Permalink
Separate directories.* files
Browse files Browse the repository at this point in the history
  • Loading branch information
mtorpey committed Sep 4, 2024
1 parent 3a54900 commit 51ddaf1
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 152 deletions.
8 changes: 0 additions & 8 deletions gap/PackageManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,12 @@ DeclareGlobalFunction("RemovePackage");
# Hidden functions
DeclareGlobalFunction("PKGMAN_CheckPackage");
DeclareGlobalFunction("PKGMAN_Exec");
DeclareGlobalFunction("PKGMAN_PackageDir");
DeclareGlobalFunction("PKGMAN_CreateDirRecursively");
DeclareGlobalFunction("PKGMAN_IsValidTargetDir");
DeclareGlobalFunction("PKGMAN_RefreshPackageInfo");
DeclareGlobalFunction("PKGMAN_InsertPackageDirectory");
DeclareGlobalFunction("PKGMAN_SetCustomPackageDir");
DeclareGlobalFunction("PKGMAN_RemoveDir");
DeclareGlobalFunction("PKGMAN_ValidatePackageInfo");
DeclareGlobalFunction("PKGMAN_InfoWithIndent");

# Hidden variables
PKGMAN_CustomPackageDir := "";
PKGMAN_ArchiveFormats := [".tar.gz", ".tar.bz2"];
PKGMAN_Sysinfo := Filename(DirectoriesLibrary(""), "sysinfo.gap");

# PackageInfo must at least contain the following to pass:
PKGMAN_RequiredPackageInfoFields := ["PackageName",
Expand Down
113 changes: 0 additions & 113 deletions gap/PackageManager.gi
Original file line number Diff line number Diff line change
Expand Up @@ -521,119 +521,6 @@ function()
Info(InfoPackageManager, 4, "Reloaded all package info records");
end);

InstallGlobalFunction(PKGMAN_PackageDir,
function()
local dir;
if PKGMAN_CustomPackageDir <> "" then
dir := PKGMAN_CustomPackageDir;
else
if GAPInfo.UserGapRoot = fail then
ErrorNoReturn("UserGapRoot not set. Cannot determine package directory");
fi;
dir := Concatenation(GAPInfo.UserGapRoot, "/pkg");
fi;
if not IsDirectoryPath(dir) then
PKGMAN_CreateDirRecursively(dir);
PKGMAN_InsertPackageDirectory(dir);
fi;
return dir;
end);

InstallGlobalFunction(PKGMAN_CreateDirRecursively,
function(dir)
local path, newdir, i, res;
path := SplitString(dir, "/");
newdir := "";
for i in [1 .. Length(path)] do
Append(newdir, path[i]);
Append(newdir, "/");
if not IsDirectoryPath(newdir) then
res := CreateDir(newdir);
if res <> true then
Info(InfoPackageManager, 1, "Failed to create required directory");
Info(InfoPackageManager, 2, "at ", newdir);
return fail;
fi;
Info(InfoPackageManager, 2, "Created directory ", newdir);
fi;
od;
return true;
end);

InstallGlobalFunction(PKGMAN_InsertPackageDirectory,
function(pkgpath)
local parent;
# Locate the parent directory
if EndsWith(pkgpath, "/pkg") then
parent := pkgpath{[1 .. Length(pkgpath) - 3]};
elif EndsWith(pkgpath, "/pkg/") then
parent := pkgpath{[1 .. Length(pkgpath) - 4]};
else
return fail;
fi;
if not parent in GAPInfo.RootPaths then
# Append the new root paths.
GAPInfo.RootPaths := Immutable(Concatenation([parent], GAPInfo.RootPaths));
fi;
# Clear the cache.
GAPInfo.DirectoriesLibrary := AtomicRecord(rec());
# Reread the package information.
if IsBound(GAPInfo.PackagesInfoInitialized) and
GAPInfo.PackagesInfoInitialized = true then
GAPInfo.PackagesInfoInitialized := false;
InitializePackagesInfoRecords();
fi;
return true;
end);

InstallGlobalFunction(PKGMAN_IsValidTargetDir,
function(dir)
if not IsDirectoryPath(dir) then
return true; # Assume parent directory is PKGMAN_PackageDir()
fi;
if not IsWritableFile(dir) then
Info(InfoPackageManager, 1, "Target location not writable");
Info(InfoPackageManager, 2, "(check ", dir, ")");
return false;
elif not IsReadableFile(dir) then
Info(InfoPackageManager, 1, "Target location not readable");
Info(InfoPackageManager, 2, "(check ", dir, ")");
return false;
elif Length(DirectoryContents(dir)) > 2 then
Info(InfoPackageManager, 1, "Package already installed at target location");
Info(InfoPackageManager, 2,
"Target directory ", dir, " exists and is non-empty");
return false;
fi;
return true;
end);

InstallGlobalFunction(PKGMAN_SetCustomPackageDir,
function(dir)
if not (EndsWith(dir, "/pkg") or EndsWith(dir, "/pkg/")) then
return fail;
fi;
# Set the variable
PKGMAN_CustomPackageDir := dir;
# Create the directory if necessary
PKGMAN_PackageDir();
# Register as a pkg directory (with top priority)
PKGMAN_InsertPackageDirectory(dir);
# Get any packages already present there
PKGMAN_RefreshPackageInfo();
# No return value
end);

InstallGlobalFunction(PKGMAN_RemoveDir,
function(dir)
# this 'if' statement is a paranoid check - it should always be true
if StartsWith(dir, PKGMAN_PackageDir()) and dir <> PKGMAN_PackageDir() then
RemoveDirectoryRecursively(dir);
Info(InfoPackageManager, 2, "Removed directory ", dir);
PKGMAN_RefreshPackageInfo();
fi;
end);

InstallGlobalFunction(PKGMAN_ValidatePackageInfo,
function(info)
local quiet;
Expand Down
9 changes: 9 additions & 0 deletions gap/directories.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DeclareGlobalFunction("PKGMAN_PackageDir");
DeclareGlobalFunction("PKGMAN_SetCustomPackageDir");
DeclareGlobalFunction("PKGMAN_CreateDirRecursively");
DeclareGlobalFunction("PKGMAN_InsertPackageDirectory");
DeclareGlobalFunction("PKGMAN_IsValidTargetDir");
DeclareGlobalFunction("PKGMAN_RemoveDir");

PKGMAN_CustomPackageDir := "";
PKGMAN_Sysinfo := Filename(DirectoriesLibrary(""), "sysinfo.gap");
112 changes: 112 additions & 0 deletions gap/directories.gi
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
InstallGlobalFunction(PKGMAN_PackageDir,
function()
local dir;
if PKGMAN_CustomPackageDir <> "" then
dir := PKGMAN_CustomPackageDir;
else
if GAPInfo.UserGapRoot = fail then
ErrorNoReturn("UserGapRoot not set. Cannot determine package directory");
fi;
dir := Concatenation(GAPInfo.UserGapRoot, "/pkg");
fi;
if not IsDirectoryPath(dir) then
PKGMAN_CreateDirRecursively(dir);
PKGMAN_InsertPackageDirectory(dir);
fi;
return dir;
end);

InstallGlobalFunction(PKGMAN_SetCustomPackageDir,
function(dir)
if not (EndsWith(dir, "/pkg") or EndsWith(dir, "/pkg/")) then
return fail;
fi;
# Set the variable
PKGMAN_CustomPackageDir := dir;
# Create the directory if necessary
PKGMAN_PackageDir();
# Register as a pkg directory (with top priority)
PKGMAN_InsertPackageDirectory(dir);
# Get any packages already present there
PKGMAN_RefreshPackageInfo();
# No return value
end);

InstallGlobalFunction(PKGMAN_CreateDirRecursively,
function(dir)
local path, newdir, i, res;
path := SplitString(dir, "/");
newdir := "";
for i in [1 .. Length(path)] do
Append(newdir, path[i]);
Append(newdir, "/");
if not IsDirectoryPath(newdir) then
res := CreateDir(newdir);
if res <> true then
Info(InfoPackageManager, 1, "Failed to create required directory");
Info(InfoPackageManager, 2, "at ", newdir);
return fail;
fi;
Info(InfoPackageManager, 2, "Created directory ", newdir);
fi;
od;
return true;
end);

InstallGlobalFunction(PKGMAN_InsertPackageDirectory,
function(pkgpath)
local parent;
# Locate the parent directory
if EndsWith(pkgpath, "/pkg") then
parent := pkgpath{[1 .. Length(pkgpath) - 3]};
elif EndsWith(pkgpath, "/pkg/") then
parent := pkgpath{[1 .. Length(pkgpath) - 4]};
else
return fail;
fi;
if not parent in GAPInfo.RootPaths then
# Append the new root paths.
GAPInfo.RootPaths := Immutable(Concatenation([parent], GAPInfo.RootPaths));
fi;
# Clear the cache.
GAPInfo.DirectoriesLibrary := AtomicRecord(rec());
# Reread the package information.
if IsBound(GAPInfo.PackagesInfoInitialized) and
GAPInfo.PackagesInfoInitialized = true then
GAPInfo.PackagesInfoInitialized := false;
InitializePackagesInfoRecords();
fi;
return true;
end);

InstallGlobalFunction(PKGMAN_IsValidTargetDir,
function(dir)
if not IsDirectoryPath(dir) then
return true; # Assume parent directory is PKGMAN_PackageDir()
fi;
if not IsWritableFile(dir) then
Info(InfoPackageManager, 1, "Target location not writable");
Info(InfoPackageManager, 2, "(check ", dir, ")");
return false;
elif not IsReadableFile(dir) then
Info(InfoPackageManager, 1, "Target location not readable");
Info(InfoPackageManager, 2, "(check ", dir, ")");
return false;
elif Length(DirectoryContents(dir)) > 2 then
Info(InfoPackageManager, 1, "Package already installed at target location");
Info(InfoPackageManager, 2,
"Target directory ", dir, " exists and is non-empty");
return false;
fi;
return true;
end);

InstallGlobalFunction(PKGMAN_RemoveDir,
function(dir)
# this 'if' statement is a paranoid check - it should always be true
if StartsWith(dir, PKGMAN_PackageDir()) and dir <> PKGMAN_PackageDir() then
RemoveDirectoryRecursively(dir);
Info(InfoPackageManager, 2, "Removed directory ", dir);
PKGMAN_RefreshPackageInfo();
fi;
end);
1 change: 1 addition & 0 deletions init.g
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

ReadPackage("PackageManager", "gap/PackageManager.gd");
ReadPackage("PackageManager", "gap/compile.gd");
ReadPackage("PackageManager", "gap/directories.gd");
ReadPackage("PackageManager", "gap/distro.gd");
ReadPackage("PackageManager", "gap/doc.gd");
ReadPackage("PackageManager", "gap/download.gd");
Expand Down
1 change: 1 addition & 0 deletions read.g
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

ReadPackage("PackageManager", "gap/PackageManager.gi");
ReadPackage("PackageManager", "gap/compile.gi");
ReadPackage("PackageManager", "gap/directories.gi");
ReadPackage("PackageManager", "gap/distro.gi");
ReadPackage("PackageManager", "gap/doc.gi");
ReadPackage("PackageManager", "gap/download.gi");
Expand Down
31 changes: 0 additions & 31 deletions tst/PackageManager.tst
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,6 @@ gap> InstallPackage("https://gap-packages.github.io/PackageManager/dummy/badpack
#I PackageInfo.g lacks PackageName field
false

# Fail to extract due to permissions
gap> dir := Filename(Directory(PKGMAN_PackageDir()), "badpackage");;
gap> CreateDir(dir);
true
gap> PKGMAN_Exec(".", "chmod", "000", dir);
rec( code := 0, output := "" )
gap> PKGMAN_CreateDirRecursively(Filename(Directory(dir), "subfolder"));
#I Failed to create required directory
fail
gap> InstallPackage("https://gap-packages.github.io/PackageManager/dummy/badpackage.tar.gz");
#I Target location not writable
false
gap> PKGMAN_Exec(".", "chmod", "222", dir);
rec( code := 0, output := "" )
gap> InstallPackage("https://gap-packages.github.io/PackageManager/dummy/badpackage.tar.gz");
#I Target location not readable
false
gap> PKGMAN_Exec(".", "chmod", "777", dir);
rec( code := 0, output := "" )

# Check a bad package directory
gap> baddir := Filename(Directory(PKGMAN_PackageDir()), "badpkg");;
gap> CreateDir(baddir);;
Expand All @@ -133,17 +113,6 @@ Error, <cmd> should be a string
gap> PKGMAN_Exec(".", "xyzabc");
fail

# PKGMAN_CustomPackageDir
gap> olddir := PKGMAN_CustomPackageDir;;
gap> PKGMAN_CustomPackageDir := "";;
gap> EndsWith(PKGMAN_PackageDir(), "/.gap/pkg");
true
gap> PKGMAN_CustomPackageDir := olddir;;
gap> PKGMAN_SetCustomPackageDir("/home"); # not ending in pkg
fail
gap> PKGMAN_InsertPackageDirectory("/home"); # not ending in pkg
fail

# Install to existing empty directory
gap> CreateDir(Filename(Directory(PKGMAN_PackageDir()), "Toric-1.9.5"));
true
Expand Down
30 changes: 30 additions & 0 deletions tst/directories.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Fail to extract due to permissions
gap> dir := Filename(Directory(PKGMAN_PackageDir()), "badpackage");;
gap> CreateDir(dir);
true
gap> PKGMAN_Exec(".", "chmod", "000", dir);
rec( code := 0, output := "" )
gap> PKGMAN_CreateDirRecursively(Filename(Directory(dir), "subfolder"));
#I Failed to create required directory
fail
gap> InstallPackage("https://gap-packages.github.io/PackageManager/dummy/badpackage.tar.gz");
#I Target location not writable
false
gap> PKGMAN_Exec(".", "chmod", "222", dir);
rec( code := 0, output := "" )
gap> InstallPackage("https://gap-packages.github.io/PackageManager/dummy/badpackage.tar.gz");
#I Target location not readable
false
gap> PKGMAN_Exec(".", "chmod", "777", dir);
rec( code := 0, output := "" )

# PKGMAN_CustomPackageDir
gap> olddir := PKGMAN_CustomPackageDir;;
gap> PKGMAN_CustomPackageDir := "";;
gap> EndsWith(PKGMAN_PackageDir(), "/.gap/pkg");
true
gap> PKGMAN_CustomPackageDir := olddir;;
gap> PKGMAN_SetCustomPackageDir("/home"); # not ending in pkg
fail
gap> PKGMAN_InsertPackageDirectory("/home"); # not ending in pkg
fail

0 comments on commit 51ddaf1

Please sign in to comment.