Skip to content

Commit

Permalink
Some cleanup and switching some file io to Mojo::File
Browse files Browse the repository at this point in the history
  • Loading branch information
pstaabp committed Nov 27, 2023
1 parent 768cfb7 commit 56a55b2
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 70 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::Zip
Archive::Zip::SimpleZip
Array::Utils
Benchmark
Expand Down
1 change: 1 addition & 0 deletions conf/site.conf.dist
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ $webwork_server_admin_email = '';
# system utilities
####################################################
$externalPrograms{git} = "/usr/bin/git";
$externalPrograms{tar} = "/bin/tar";

####################################################
# equation rendering/hardcopy utiltiies
Expand Down
9 changes: 6 additions & 3 deletions lib/HardcopyRenderedProblem.pm
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,12 @@ sub generate_hardcopy_tex {
push(@$errors, "Failed to generate error log file: $@") if $@;

# Create a zip archive of the bundle directory
my $zip = Archive::Zip::SimpleZip->new($working_dir->dirname->child('hardcopy.zip')->to_string);
$zip->add($working_dir->dirname->to_string, storelinks => 1);

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);

return;
Expand Down
77 changes: 37 additions & 40 deletions lib/WeBWorK/ContentGenerator/Hardcopy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ problem sets.
=cut

use File::Path;
use File::Copy qw(move copy);
use File::Temp qw/tempdir/;
use Mojo::File;
use String::ShellQuote;
Expand Down Expand Up @@ -500,8 +498,8 @@ async sub generate_hardcopy ($c, $format, $userIDsRef, $setIDsRef) {
my $userID = $c->param('user');

# Create the temporary directory. Use mkpath to ensure it exists (mkpath is pretty much `mkdir -p`).
my $temp_dir_parent_path = "$ce->{webworkDirs}{tmp}/$courseID/hardcopy/$userID";
eval { mkpath($temp_dir_parent_path) };
my $temp_dir_parent_path = Mojo::File->new("$ce->{webworkDirs}{tmp}/$courseID/hardcopy/$userID");
eval { $temp_dir_parent_path->make_path };
if ($@) {
$c->add_error("Couldn't create hardcopy directory $temp_dir_parent_path: ", $c->tag('code', $@));
return;
Expand Down Expand Up @@ -598,14 +596,14 @@ async sub generate_hardcopy ($c, $format, $userIDsRef, $setIDsRef) {

# Try to move the hardcopy file out of the temp directory.
my $final_file_final_path = "$temp_dir_parent_path/$final_file_name";
my $mv_ok = move($final_file_path, $final_file_final_path);
unless ($mv_ok) {
eval { Mojo::File->new($final_file_path)->move_to($final_file_final_path) };
if ($@) {
$c->add_error(
'Failed to move hardcopy file "',
$c->tag('code', $final_file_name),
'" from "', $c->tag('code', $temp_dir_path),
'" to "', $c->tag('code', $temp_dir_parent_path),
'":', $c->tag('br'), $c->tag('pre', $!)
'":', $c->tag('br'), $c->tag('pre', $@)
);
$final_file_final_path = "$temp_dir_rel_path/$final_file_name";
}
Expand All @@ -630,11 +628,10 @@ async sub generate_hardcopy ($c, $format, $userIDsRef, $setIDsRef) {

# helper function to remove temp dirs
sub delete_temp_dir ($c, $temp_dir_path) {
Mojo::File->new($temp_dir_path)->remove_tree;

if ($!) {
eval { Mojo::File->new($temp_dir_path)->remove_tree };
if ($@) {
$c->add_error('Failed to remove temporary directory "',
$c->tag('code', $file, '":', $c->tag('br'), $c->tag('pre', $!)));
$c->tag('code', $temp_dir_path, '":', $c->tag('br'), $c->tag('pre', $@)));
}
return;
}
Expand All @@ -649,55 +646,55 @@ sub delete_temp_dir ($c, $temp_dir_path) {

sub generate_hardcopy_tex ($c, $temp_dir_path, $final_file_basename) {
my $src_name = "hardcopy.tex";
my $bundle_path = "$temp_dir_path/$final_file_basename";
my $bundle_path = Mojo::File->new("$temp_dir_path/$final_file_basename");

# Create directory for the tex bundle
if (!mkdir $bundle_path) {
eval { $bundle_path->mkdir };
if ($@) {
$c->add_error(
'Failed to create directory "',
$c->tag('code', $bundle_path),
'": ', $c->tag('br'), $c->tag('pre', $!)
'": ', $c->tag('br'), $c->tag('pre', $@)
);
return $src_name;
}

# Move the tex file into the bundle directory
my $mv_ok = move("$temp_dir_path/$src_name", $bundle_path);

unless ($mv_ok) {
eval { Mojo::File->new("$temp_dir_path/$src_name")->move_to($bundle_path) };
if ($@) {
$c->add_error(
'Failed to move "',
$c->tag('code', $src_name),
'" into directory "',
$c->tag('code', $bundle_path),
'":', $c->tag('br'), $c->tag('pre', $!)
'":', $c->tag('br'), $c->tag('pre', $@)
);
return $src_name;
}

# Copy the common tex files into the bundle directory
my $ce = $c->ce;
for (qw{webwork2.sty webwork_logo.png}) {
my $copy_ok = copy("$ce->{webworkDirs}{assetsTex}/$_", $bundle_path);
unless ($copy_ok) {
eval { Mojo::File("$ce->{webworkDirs}{assetsTex}/$_")->copy_to($bundle_path) };
if ($@) {
$c->add_error(
'Failed to copy "',
$c->tag('code', "$ce->{webworkDirs}{assetsTex}/$_"),
'" into directory "',
$c->tag('code', $bundle_path),
'":', $c->tag('br'), $c->tag('pre', $!)
'":', $c->tag('br'), $c->tag('pre', $@)
);
}
}
for (qw{pg.sty PGML.tex CAPA.tex}) {
my $copy_ok = copy("$ce->{pg}{directories}{assetsTex}/$_", $bundle_path);
unless ($copy_ok) {
eval { Mojo::File->new("$ce->{pg}{directories}{assetsTex}/$_")->copy_to($bundle_path) };
if ($@) {
$c->add_error(
'Failed to copy "',
$c->tag('code', "$ce->{pg}{directories}{assetsTex}/$_"),
'" into directory "',
$c->tag('code', $bundle_path),
'":', $c->tag('br'), $c->tag('pre', $!)
'":', $c->tag('br'), $c->tag('pre', $@)
);
}
}
Expand All @@ -715,14 +712,15 @@ sub generate_hardcopy_tex ($c, $temp_dir_path, $final_file_basename) {
$data =~ s{$resource}{$basename}g;

# Copy the image file into the bundle directory.
my $copy_ok = copy($resource, $bundle_path);
unless ($copy_ok) {
eval { Mojo::File->new($resource)->copy_to($bundle_path) };

if ($@) {
$c->add_error(
'Failed to copy image "',
$c->tag('code', $resource),
'" into directory "',
$c->tag('code', $bundle_path),
'":', $c->tag('br'), $c->tag('pre', $!)
'":', $c->tag('br'), $c->tag('pre', $@)
);
}
}
Expand All @@ -739,17 +737,16 @@ 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 = Archive::Zip::SimpleZip->new($zip_file);
$zip->add($temp_dir_path, Name => "hardcopy_files", StoreLink => 1);
my $zip;
eval {
$zip = Archive::Zip::SimpleZip->new($zip_file);
$zip->add($temp_dir_path, Name => "hardcopy_files", StoreLink => 1);
};

unless ($zip->close) {
$c->add_error(
'Failed to create zip archive of directory "',
$c->tag('code', $bundle_path),
': $SimpleZipError"'
);
return "$bundle_path/$src_name";
}
$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 || $@);

return $zip_file;
}
Expand Down Expand Up @@ -813,8 +810,8 @@ sub generate_hardcopy_pdf ($c, $temp_dir_path, $final_file_basename) {
my $src_name = "hardcopy.pdf";
my $dest_name = "$final_file_basename.pdf";

my $mv_ok = move("$temp_dir_path/$src_name", "$temp_dir_path/$dest_name");
unless ($mv_ok) {
eval { Mojo::File->new("$temp_dir_path/$src_name")->move_to("$temp_dir_path/$dest_name") };
if ($@) {
$c->add_error(
'Failed to rename "',
$c->tag('code', $src_name),
Expand All @@ -824,7 +821,7 @@ sub generate_hardcopy_pdf ($c, $temp_dir_path, $final_file_basename) {
$c->tag('code', $temp_dir_path),
'":',
$c->tag('br'),
$c->tag('pre', $!)
$c->tag('pre', $@)
);
$final_file_name = $src_name;
} else {
Expand Down
55 changes: 28 additions & 27 deletions lib/WeBWorK/Utils/CourseManagement.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ use String::ShellQuote;
use UUID::Tiny qw(create_uuid_as_string);

use WeBWorK::Debug;
use File::Path qw(rmtree);
use File::Copy qw(move);
use File::Copy::Recursive qw(dircopy);
use File::Spec;
use Mojo::File;
Expand Down Expand Up @@ -256,8 +254,8 @@ sub addCourse {
or croak
"Can't create the course '$courseID' because the courses directory '$rootParent' is not writeable.";
# try to create it
mkdir $root
or croak "Can't create the course '$courseID' becasue the root directory '$root' could not be created: $!.";
eval { Mojo::File->new($root)->make_path };
croak "Can't create the course '$courseID' becasue the root directory '$root' could not be created: $@." if $@;
}

# deal with the rest of the directories
Expand All @@ -284,9 +282,10 @@ sub addCourse {
}

# try to create it
mkdir $courseDir
or warn
"Failed to create $courseDirName directory '$courseDir': $!. You will have to create this directory manually.\n";
eval { Mojo::File($courseDir)->make_path };
warn "Failed to create $courseDirName directory '$courseDir': $!. "
. "You will have to create this directory manually."
if $@;
}

##### step 2: create course database #####
Expand Down Expand Up @@ -339,8 +338,7 @@ sub addCourse {
## copy templates ##
if (-d $sourceDir) {
my $destDir = $ce->{courseDirs}{templates};
my $copy_ok = dircopy("$sourceDir", $destDir);
warn "Failed to copy templates from course '$sourceCourse': $! " if $!;
warn "Failed to copy templates from course '$sourceCourse': $! " unless dircopy("$sourceDir", $destDir);
} else {
warn
"Failed to copy templates from course '$sourceCourse': templates directory '$sourceDir' does not exist.\n";
Expand All @@ -349,9 +347,8 @@ sub addCourse {
## this copies the html/tmp directory as well which is not optimal
$sourceDir = $sourceCE->{courseDirs}->{html};
if (-d $sourceDir) {
my $destDir = $ce->{courseDirs}{html};
dircopy($sourceDir, $destDir);
warn "Failed to copy html from course '$sourceCourse': $!" if ($!);
warn "Failed to copy html from course '$sourceCourse': $!"
unless dircopy($sourceDir, $ce->{courseDirs}{html});
} else {
warn "Failed to copy html from course '$sourceCourse': html directory '$sourceDir' does not exist.\n";
}
Expand Down Expand Up @@ -446,10 +443,10 @@ sub renameCourse {
##### step 1: move course directory #####

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

die "Failed to move course directory: $!\n" unless ($mv_ok);
die "Failed to move course directory: $@" if ($@);

# get new course environment
my $newCE = $oldCE->new({ courseName => $newCourseID });
Expand Down Expand Up @@ -504,8 +501,8 @@ sub renameCourse {

# try to move the directory
debug("Going to move $oldDir to $newDir...\n");
my $mv_ok = move($oldDir, $newDir);
warn "Failed to move directory from $oldDir to $newDir" unless $mv_ok;
eval { Mojo::File->new($oldDir)->move_to($newDir) };
warn "Failed to move directory from $oldDir to $newDir with error: $@" if $@;
} else {
debug("oldDir $oldDir was already moved.\n");
}
Expand Down Expand Up @@ -663,7 +660,8 @@ sub deleteCourse {

# try to delete the directory
debug("Going to delete $courseDir...\n");
rmtree($courseDir, 0, 1);
eval { Mojo::File->new($courseDir)->remove_tree };
warn "A error occurred when deleting $courseDir" if $@;
} else {
debug("courseDir $courseDir was already deleted.\n");
}
Expand Down Expand Up @@ -747,7 +745,8 @@ sub archiveCourse {
#### step 1: dump tables #####

unless (-e $dump_dir) {
mkdir $dump_dir or croak "Failed to create course database dump directory '$dump_dir': $!";
eval { Mojo::File->new($dump_dir)->make_path };
croak "Failed to create course database dump directory '$dump_dir': $@" if $@;
}

my $db = new WeBWorK::DB($ce->{dbLayout});
Expand Down Expand Up @@ -776,12 +775,13 @@ sub archiveCourse {
##### step 3: cleanup -- remove database dump files from course directory #####

unless (-e $archive_path) {
unless (move($tmp_archive_path, $archive_path)) {
unlink($tmp_archive_path); #clean up
croak "Failed to rename archived file to '$archive_path': $!";
eval { Mojo::File->new($tmp_archive_path)->move_to($archive_path) };
if ($@) {
eval { Mojo::File->new($tmp_archive_path)->remove };
croak "Failed to rename archived file to '$archive_path': $@";
}
} else {
unlink($tmp_archive_path); #clean up
eval { Mojo::File->new($tmp_archive_path)->remove };
croak "Failed to create archived file at '$archive_path'. File already exists.";
}
_archiveCourse_remove_dump_dir($ce, $dump_dir);
Expand All @@ -791,7 +791,7 @@ sub archiveCourse {

sub _archiveCourse_remove_dump_dir {
my ($ce, $dump_dir) = @_;
rmtree($dump_dir, { error => \my $err });
Mojo::File->new($dump_dir)->remove_tree({ error => \my $err });

if ($err && @$err) {
for my $diag (@$err) {
Expand Down Expand Up @@ -912,16 +912,17 @@ sub unarchiveCourse {
_archiveCourse_remove_dump_dir($ce, $dump_dir);
}
if (-e $old_dump_file) {
unlink $old_dump_file or carp "Failed to unlink course database dump file '$old_dump_file: $_\n";
eval { Mojo::File->new($old_dump_file)->remove };
warn "Failed to unlink course database dump file '$old_dump_file: $@" if $@;
}

# Create the html_temp folder (since it isn't included in the
# tarball
my $tmpDir = $ce2->{courseDirs}->{html_temp};
if (!-e $tmpDir) {
mkdir $tmpDir
or warn
"Failed to create html_temp directory '$tmpDir': $!. You will have to create this directory manually.\n";
eval { Mojo::File->new($tmpDir)->make_path };
warn "Failed to create html_temp directory '$tmpDir': $@. You will have to create this directory manually."
if $@;
}

##### step 6: rename course #####
Expand Down

0 comments on commit 56a55b2

Please sign in to comment.