-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-zip
executable file
·197 lines (153 loc) · 4.39 KB
/
make-zip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env perl
# make-zip test-file other-files
# POD documentation at end.
# +--------------+---------------------------------------------------
# | Instructions |
# +--------------+
# Most of the instructions can be found in the POD documentation at
# the end. Or see the README file for more details.
# +-----------------+------------------------------------------------
# | Developer Notes |
# +-----------------+
# This should be a relatively simple program. I've just added most
# of the tooling that I like to see in programs (command-line flags,
# input checking, some tracing, etc.)
# +-------+----------------------------------------------------------
# | To Do |
# +-------+
# [ ] Finish the isGradeFile code.
# +---------+--------------------------------------------------------
# | Modules |
# +---------+
use strict;
use warnings;
use File::Basename;
use File::Copy;
use Pod::Usage;
use Getopt::Long qw(GetOptions);
Getopt::Long::Configure qw(gnu_getopt);
# +---------+--------------------------------------------------------
# | Globals |
# +---------+
my $dir = dirname((-l __FILE__) ? readlink(__FILE__) : __FILE__);
my @PRIMARY_FILES = ("$dir/files/setup.sh", "$dir/files/lib-grade.rkt");
my $help = 0;
my $docs = 0;
my $verbose = 0;
# +-----------+------------------------------------------------------
# | Utilities |
# +-----------+
# LOG($msg)
# Print a tracing message when tracing is on.
sub LOG($) {
if ($verbose) {
my ($msg) = @_;
print STDERR "*** ", $msg, "\n";
}
} # LOG
# badfile($msg)
# Print the message and return 0;
sub badfile($) {
my ($msg) = @_;
print STDERR "Error: $msg\n";
return 0;
} # badfile
# isGradeFile($fname)
# Checks to see if fname appears to be a grade file.
sub isGradeFile($) {
my ($fname) = @_;
0 == system("grep -q '^#lang racket' '$fname'")
or return badfile("File needs to start with `#lang racket`");
return 1;
} # isGradeFile
# sampleGradeFile()
# Print a sample grade file
sub sampleGradeFile() {
print STDERR <<"CONTENTS";
#lang racket
(require rackunit)
(require "lib-grade.rkt")
(current-directory "/autograder/submission")
(define-var NAME from "FILE.rkt")
(define-test-suite tests
(test-equal? "silly" silly "Silly")
(test-equal? "Same Directory"
(first-line "silly.txt")
"Silly")
(test-equal? "Different directory, using full path"
(first-line "/autograder/source/sample.txt")
"Sample"))
(generate-results tests)
CONTENTS
} # sampleGradeFile
# +------+-----------------------------------------------------------
# | Main |
# +------+
# Get the initial options
GetOptions(
'verbose|v' => \$verbose,
'help|?' => \$help,
'docs' => \$docs
);
LOG("Verbose mode is ON");
pod2usage(1) if $help;
pod2usage(-verbose => 2) if $docs;
if ($#ARGV < 0) {
print STDERR "Error: You must supply a test file\n";
pod2usage(1);
}
my $gradeFilePath = $ARGV[0];
my $gradeFile = basename($gradeFilePath);
LOG("Grade file path is $gradeFilePath, file is $gradeFile");
if ($gradeFilePath !~ m/\.rkt$/) {
print STDERR "Error: Grade file must be a Racket file\n";
pod2usage(1);
}
if (!isGradeFile($gradeFilePath)) {
print STDERR "Error: Invalid grade file. Example follows.\n\n";
sampleGradeFile();
print STDERR "\n";
pod2usage(1);
}
my $tmpdir = "/tmp/make-zip-$$";
LOG("Using directory $tmpdir\n");
mkdir $tmpdir;
foreach my $file (@ARGV) {
LOG("Copying $file");
copy($file,$tmpdir);
} # foreach
foreach my $file (@PRIMARY_FILES) {
LOG("Copying $file");
copy($file,$tmpdir);
} # foreach
LOG("Creating autograder");
my $autograderFile = "$tmpdir/run_autograder";
open my $autograder , "> $autograderFile";
print $autograder <<"AUTOGRADER";
#!/usr/bin/env bash
# raco pkg update --all
racket /autograder/source/$gradeFile;
AUTOGRADER
close $autograder;
chmod 0755, $autograderFile;
my $zipfile = $gradeFile;
$zipfile =~ s/\.rkt$/.zip/g;
$zipfile =~ s/^/autograder-/g;
LOG("Creating zipfile $zipfile");
system("zip -j $zipfile '$tmpdir'/*");
if (!$verbose) {
system("rm -rf $tmpdir");
} else {
LOG("Please don't forget to remove $tmpdir!");
}
__END__
=head1 NAME
make-zip - Create a zipfile for uploading to Gradescope
=head1 SYNOPSIS
make-zip [options] tests.rkt [other-files]
=head1 OPTIONS
=head1 DESCRIPTION=
=head1 AUTHOR
Samuel A. Rebelsky
=head1 LICENSE
=cut