Skip to content

Commit

Permalink
Eliminated Archive::Extract in favor of Archive::Tar and some other c…
Browse files Browse the repository at this point in the history
…leanup.
  • Loading branch information
pstaabp committed Nov 10, 2023
1 parent 825bffc commit 2166fe6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
1 change: 1 addition & 0 deletions bin/check_modules.pl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ =head1 DESCRIPTION
);

my @modulesList = qw(
Archive::Tar
Archive::Zip
Archive::Zip::SimpleZip
Array::Utils
Expand Down
10 changes: 6 additions & 4 deletions bin/download-OPL-metadata-release.pl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use File::Fetch;
use File::Copy;
use File::Path;
use Archive::Extract;
use Archive::Tar;
use Mojo::File;
use JSON;

Expand Down Expand Up @@ -54,9 +54,11 @@ BEGIN
my $releaseFile = $releaseDownloadFF->fetch(to => $ce->{webworkDirs}{tmp}) or die $releaseDownloadFF->error;
say 'Downloaded release archive, now extracting.';

my $arch = Archive::Extract->new(archive => "$releaseFile");
my $ok = $arch->extract(to => $ce->{webworkDirs}{tmp});
die "There was an error extracting the release: $arch->error" unless $ok;
my $arch = Archive::Tar->new($releaseFile);
die "An error occurred while creating the tar file: $releaseFile" unless $arch;
$arch->setcwd($path);
$arch->extract();
die "There was an error extracting the metadata release: $arch->error" if $arch->error;

# Copy the json files into htdocs.
for (glob("$ce->{webworkDirs}{tmp}/webwork-open-problem-library/JSON-SAVED/*.json")) {
Expand Down
18 changes: 11 additions & 7 deletions lib/HardcopyRenderedProblem.pm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use warnings;

use File::Path;
use String::ShellQuote;
use Archive::Zip;
use Archive::Zip::SimpleZip qw($SimpleZipError);
use Mojo::File qw(path tempdir);
use XML::LibXML;
Expand Down Expand Up @@ -172,14 +173,17 @@ sub generate_hardcopy_tex {
push(@$errors, "Failed to generate error log file: $@") if $@;

# Create a zip archive of the bundle directory
my $zip;
eval {
$zip = Archive::Zip::SimpleZip->new($working_dir->dirname->child('hardcopy.zip')->to_string);
$zip->add($working_dir->dirname->to_string, storelinks => 1);
};
push(@$errors, $@) if $@;
push(@$errors, qq{Failed to create zip archive of directory "$working_dir": $SimpleZipError}) unless ($zip->close);
my $zip = Archive::Zip::SimpleZip->new($working_dir->child('hardcopy.zip')->to_string);
unless ($zip) {
push(@$errors, "Failed to make a zip file at " . $working_dir->child('hardcopy.zip')->to_string);
return;
}

$zip->add($working_dir->dirname->to_string, storelinks => 1);
unless ($zip->close) {
push(@$errors, "Failed to create zip archive of directory '$working_dir': $SimpleZipError");
return;
}
return;
}

Expand Down
19 changes: 10 additions & 9 deletions lib/WeBWorK/ContentGenerator/Hardcopy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -736,16 +736,17 @@ sub generate_hardcopy_tex ($c, $temp_dir_path, $final_file_basename) {

# Create a zip archive of the bundle directory
my $zip_file = "$temp_dir_path/$final_file_basename.zip";
my $zip;
eval {
$zip = Archive::Zip::SimpleZip->new($zip_file);
$zip->add($temp_dir_path, Name => "hardcopy_files", StoreLink => 1);
};
$zip = Archive::Zip::SimpleZip->new($zip_file);
unless ($zip) {
$c->add_error('Failed to create zip archive of directory "', $c->tag('code', $bundle_path), '": $SimpleZipError"');
return;
}

$c->add_error('Failed to create zip archive of directory "', $c->tag('code', $bundle_path), '": $SimpleZipError"')
unless $zip->close;
$c->add_error('Failed to create zip archive of directory "', $c->tag('code', $bundle_path), '": $@') if $@;
return "$bundle_path/$src_name" if (!$zip->close || $@);
my $ok = $zip->add($temp_dir_path, Name => "hardcopy_files", StoreLink => 1);
unless ($ok) {
$c->add_error('Failed to create zip archive of directory "', $c->tag('code', $bundle_path), '": $SimpleZipError"');
return;
}

return $zip_file;
}
Expand Down
19 changes: 10 additions & 9 deletions lib/WeBWorK/Utils/CourseManagement.pm
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use File::Copy::Recursive qw(dircopy);
use File::Spec;
use Mojo::File;
use Archive::Tar;
use Archive::Extract;
use WeBWorK::CourseEnvironment;
use WeBWorK::DB;
use WeBWorK::Debug;
Expand Down Expand Up @@ -357,8 +356,8 @@ sub addCourse {
if (exists $options{copySimpleConfig}) {
my $sourceFile = $sourceCE->{courseFiles}->{simpleConfig};
if (-e $sourceFile) {
my $copy_ok = copy($sourceFile, $ce->{courseFiles}{simpleConfig});
warn "Failed to copy simple.conf from course '$sourceCourse': $!" unless $copy_ok;
eval { Mojo::File->new($sourceFile)->copy_to($ce->{courseFiles}) };
warn "Failed to copy simple.conf from course '$sourceCourse': $@" if $@;
}
}

Expand Down Expand Up @@ -443,9 +442,8 @@ sub renameCourse {
##### step 1: move course directory #####

# move top-level course directory
eval { Mojo::File->new($oldCourseDir)->move_to($newCourseDir) };
debug("moving course dir from $oldCourseDir to $newCourseDir");

eval { Mojo::File->new($oldCourseDir)->move_to($newCourseDir) };
die "Failed to move course directory: $@" if ($@);

# get new course environment
Expand Down Expand Up @@ -857,11 +855,14 @@ sub unarchiveCourse {

##### step 2: crack open the tarball #####

my $arch = Archive::Extract->new(archive => $archivePath);
my $extr_ok = $arch->extract(to => $coursesDir);
unless ($extr_ok) {
my $arch = Archive::Tar->new($archivePath);
die "The tar file $archivePath is not valid." unless $arch;
$arch->setcwd($coursesDir);
$arch->extract();

if ($arch->error) {
_unarchiveCourse_move_back($restoreCourseData);
die "Failed to unarchive course directory for course $newCourseID: $!";
die "Failed to unarchive course directory for course $newCourseID: $arch->error";
}

##### step 3: read the course environment for this course #####
Expand Down

0 comments on commit 2166fe6

Please sign in to comment.