-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheinstein
executable file
·936 lines (807 loc) · 23.5 KB
/
einstein
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
#!/usr/bin/env zsh
# Script to upload a file to Einstein.
#
# Usage... run like this to see the usage massage:
#
# $ einstein -h
#
# You might have to provide your password (but you should only have to do that once).
#
# Installing this script locally
# ==============================
#
# 1) Install zsh, on many Debian/Ubuntu-like systems, that's
#
# $ sudo apt-get install zsh
#
# What is the command on MacOS? If you know, then tell me and I'll put it here.
#
# 2) Download the script:
#
# $ wget "https://einstein.computing.dcu.ie/res/einstein"
#
# 3) This script needs to know your username on Einstein. On SoC servers, that's trivial,
# because it's just $( whoami ). If you're running this elsewhere, though, your local
# username probably won't match your SoC username.
#
# In, that case...
#
# either:
# 1) set the EINSTEIN_USERNAME variable in your ~/.bashrc,
# (SOC_USERNAME works too),
#
# export EINSTEIN_USERNAME="bloggsj123"
#
# ...and restart your shell:
#
# $ exec bash
#
# or:
# 2) or just hard wire your SoC username below, like this:
# username=bloggsj
#
# 4) Install the file and make it executable; something like:
#
# $ sudo install -v -m 0755 einstein /usr/local/bin
#
if [[ -n $EINSTEIN_USERNAME ]]
then
username=$EINSTEIN_USERNAME
elif [[ -n $SOC_USERNAME ]]
then
username=$SOC_USERNAME
else
username=$( whoami )
# If necessary, uncomment and edit the following line to hard wire you username:
# username=bloggsj
fi
#
# Requirements: zsh, curl and sha1sum.
#
# Lecturers only...
# For installing expected standard output: nodejs and CoffeeScript:
#
# $ sudo npm install -g coffee-script
#
# "sha1sum" is apparently known as "shasum" on MacOS.
#
sha1sum="sha1sum"
if ! command which $sha1sum
then
command which shasum && sha1sum="shasum"
fi > /dev/null
zmodload -F zsh/stat b:zstat
zmodload zsh/datetime
# ########################################################################
# Help.
#
if [[ $argv[1] == "-h" ]] || [[ $argv[1] == "--help" ]]
then
pager ()
{
if [[ -t 1 ]]
then
less
else
cat
fi
}
#
cat <<EOF | pager
Uploading to einstein:
$ $0:t FILE|DIRECTORY [FILES|DIRECTORIES...]
For files, upload the file to einstein.
For directories (and primarily for staff)... if the directory appears to be
a marker directory and the directory contains an exemplar, then upload the
exemplar.
For directories (otherwise)... upload the most recently-edited task file
in the current working directory.
$ $0:t
The same as "$0:t \$PWD".
Other usage (likely useful for staff only):
$ $0:t -i
Upload exemplar, but run "make install" in the parent directory first.
This is useful for installing and testing markers (if you use "make install"
to install your markers). Possibly of interest to SB only.
$ $0:t -a
Upload exemplar, then install the resulting standard output in the local
"stdout.txt" files.
This is useful for having einstein itself generate the expected standard
output (because running tests locally can be fiddly and error prone).
$ $0:t -p
Generate "publish.txt" file to publish a model solution.
Also... if this is a git repo, then "git add publish.txt"
Also... if a suitable makefile and target exist in the parent directory,
then run "make -C .. install".
$ $0:t -u
Remove "publish.txt" file to unpublish a model solution.
Also... if this is a git repo, then "git rm publish.txt".
Also... if a suitable makefile and target exist in the parent directory,
then run "make -C .. install".
This actually removes "**/publish.txt", so it can be used to unpublish *all*
model solutions at the start of the academic year.
EOF
exit 0
fi
# ########################################################################
# Arguments.
#
# These are primarily for SB's usage. They are for installing and testing
# Einstein markers.
#
publish=
unpublish=
install_markers=
install_outputs=
while getopts 'puia' opt
do
case $opt in
p ) publish="yes" ;;
u ) unpublish="yes" ;;
i ) install_markers="yes" ;;
a ) install_outputs="yes" ;;
\? ) print "invalid option: -$opt" >&2; exit 1 ;;
esac
done
shift $((OPTIND-1))
# ########################################################################
# Utilities.
# The output is tweaked somewhat for the case where we're uploading multiple files
# at the same time.
#
reduced_output=''
unless_reduced_output ()
{
[[ -z $reduced_output ]] && $argv
}
contains ()
{
local thing=$argv[1]; shift
[[ ${argv[(ie)$thing]} -le $#argv ]]
}
{
if [[ -t 1 ]]
then
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
else
RED=""
GREEN=""
BLUE=""
NC=""
fi
coloured ()
{
local colour=$argv[1]; shift
print -- "$colour$argv$NC"
}
}
print $SSH_CLIENT none | read ssh_client junk
http ()
{
curl --header "x-ssh-client: $ssh_client" --silent --config - $argv <<< "user = \"$username:$password\""
}
is-git-repo ()
{
local directory=$argv[1]
(
cd $directory && git -C . rev-parse 2> /dev/null
)
}
relative-path ()
{
realpath --relative-to=$PWD $argv[1]
}
have-makefile ()
{
local directory=$argv[1]
[[ -f $directory/Makefile ]] || [[ -f $directory/makefile ]]
}
have-make-target ()
{
local directory=$argv[1]
local target=$argv[2]
make -n -s -C $directory $target > /dev/null 2>&1
}
run-make-install ()
{
local directory=$argv[1]:a
# Never install if $directory is already an Einstein marker directory.
#
if [[ $( hostname ) == "einstein" ]] && [[ $directory == ~/markers/* ]]
then
print "Skipping 'make install' in Einstein marker directory."
print
return 0
fi
# This is probably very SB specific but, ...
# If there is a make file with a target "install" in the parent directory,
# then run that target.
#
set -e
if have-makefile $directory && have-make-target $directory install
then
print "install via makefile in" $( relative-path $directory )
make -C $directory install
else
print "error: cannot find 'make install' target in '$directory'" >&2
false
fi
set +e
}
# ########################################################################
# Check for required commands.
#
{
have ()
{
command which $argv > /dev/null
}
# This is not a comprehensive list; it's just the commands which are most likely to be missing.
#
required_commands=( curl $sha1sum)
[[ -n $publish ]] && required_commands+=( git )
[[ -n $unpublish ]] && required_commands+=( git )
[[ -n $install_markers ]] && required_commands+=( make )
[[ -n $install_outputs ]] && required_commands+=( node coffee )
for command in $required_commands
do
if ! have $command
then
print "error: cannot find command '$command' (which is required), please install it" >&2
if [[ $command == "coffee" ]]
then
print " install 'coffee' with 'sudo npm install -g coffee-script'" >&2
fi
exit 1
fi
done
unset command required_commands
unset -f have
}
# ########################################################################
# Publish/unpublish model solutions.
#
publish-model-solutions ()
{
set -e
local dir directory task
[[ $#argv == 0 ]] && set -- "."
for dir in $argv
do
[[ -f $dir ]] && dir=$dir:h
directory=$dir:a
task=$directory:t
if ! [[ -f $directory/$task ]]
then
print "error: no exemplar to publish in" $( relative-path $directory/$task ) >&2
exit 1
fi
print "touch" $( relative-path $dir/publish.txt )
touch $directory/publish.txt
is-git-repo $directory && ( cd $directory && git add --verbose publish.txt )
have-makefile $directory:h \
&& have-make-target $directory:h install \
&& run-make-install $directory:h \
|| true
done
exit 0
}
unpublish-model-solutions ()
{
set -e
local dir directory task
[[ $#argv == 0 ]] && set -- **/publish.txt(.N)
for dir in $argv
do
[[ -f $dir ]] && dir=$dir:h
directory=$dir:a
[[ -f $directory/publish.txt ]] || continue
if is-git-repo $directory
then
( cd $directory && git rm -f publish.txt )
else
rm -v $directory/publish.txt
fi
have-makefile $directory:h \
&& have-make-target $directory:h install \
&& run-make-install $directory:h \
|| true
done
exit 0
}
[[ -n $publish ]] && publish-model-solutions $argv
[[ -n $unpublish ]] && unpublish-model-solutions $argv
# ########################################################################
# Install via make, if requested.
#
if [[ -n $install_markers ]]
then
run-make-install ".."
fi
# ########################################################################
# Ensure we have a working username and password.
#
# The secret salt here isn't to make the encryption secure; it's rather just to
# reduce the likelihood that somebody stumbles across an unencrytpted password
# in one of the off-site backups.
pw_file=$HOME/.einstein-passwd-v2.txt
secret="Eey6ahchDoho5yu5"
have_openssl_pbkdf2 ()
{
date | openssl enc -pbkdf2 -aes256 -base64 -pass pass:$secret > /dev/null 2>&1
}
if have_openssl_pbkdf2
then
enc=( openssl enc -pbkdf2 -aes256 -base64 -pass pass:$secret -out $pw_file )
dec=( openssl enc -d -pbkdf2 -aes256 -base64 -pass pass:$secret -in $pw_file )
else
enc=( openssl enc -aes256 -base64 -pass pass:$secret -out $pw_file )
dec=( openssl enc -d -aes256 -base64 -pass pass:$secret -in $pw_file )
fi
{
# Migration v1 -> v2.
# Version 1 was using a depricated form of encryption.
#
pw_file_v1=$HOME/.einstein-passwd.txt
if [[ -s $pw_file_v1 ]] && ! [[ -f $pw_file ]]
then
if openssl enc -d -aes256 -base64 -pass pass:$secret -in $pw_file_v1 2> /dev/null | $enc
then
chmod 0600 $pw_file
rm $pw_file_v1
fi
fi
unset pw_file_v1
}
if [[ -f $pw_file ]] && ! $dec -out /dev/null
then
rm -v $pw_file
fi
if ! [[ -f $pw_file ]]
then
print "this-is-not-the-correct-password" | $enc
chmod 0600 $pw_file
fi
password=$( $dec )
ask_user_for_password ()
{
print "user:" $(coloured $BLUE $username)
read -s password"?enter your password: "
print
print $password | $enc
chmod 0600 $pw_file
}
while ! http --output /dev/null --write-out "%{http_code}\n" "https://ca000.computing.dcu.ie/einstein/now" | grep -q -w 200
do
ask_user_for_password
done
# ########################################################################
# Fetch the Einstein task list (and, hence, the module list too).
#
# These are lines of the form:
#
# 75d16fa158ba3c795051fa3b6a4a3a10b580c595 ca114 ca116 ca117
#
# where the hash is the SHA1 hash of the task name, and the rest of the tokens are the modules on which that
# task is available.
#
# For the vast majority of tasks, the task name is unique to the module.
#
# For a number of tasks (e.g. "hello.py"), the same task name is available on several modules.
#
fetch_tasks ()
{
# This really shouldn't fail but, if it does, then we want to know about it.
#
set -e
print -- $argv
print
#
[[ -f ~/.einstein-tasks.txt.tmp ]] && chmod u+w ~/.einstein-tasks.txt.tmp
[[ -f ~/.einstein-tasks.txt ]] && chmod u+w ~/.einstein-tasks.txt
[[ -f ~/.einstein-modules.txt.tmp ]] && chmod u+w ~/.einstein-modules.txt.tmp
[[ -f ~/.einstein-modules.txt ]] && chmod u+w ~/.einstein-modules.txt
#
# If we're running on the Einstein server, then just copy the file. Otherwise, fetch
# it over the network.
#
if [[ -f "/var/www/termcast/tasks.txt" ]]
then
cp "/var/www/termcast/tasks.txt" ~/.einstein-tasks.txt.tmp
else
http "https://einstein.computing.dcu.ie/termcast/tasks.txt" > ~/.einstein-tasks.txt.tmp
fi
#
cut -d ' ' -f 2- ~/.einstein-tasks.txt.tmp | tr " " "\n" | sort | uniq > ~/.einstein-modules.txt.tmp
mv ~/.einstein-modules.txt.tmp ~/.einstein-modules.txt
mv ~/.einstein-tasks.txt.tmp ~/.einstein-tasks.txt
set +e
}
if ! [[ -s ~/.einstein-tasks.txt ]]
then
fetch_tasks "Fetching the Einstein task list..."
fi
last_task_list_update_time=$( zstat +mtime ~/.einstein-tasks.txt )
if (( 60 * 15 < $EPOCHSECONDS - last_task_list_update_time ))
then
fetch_tasks "Refreshing the Einstein task list (it's more than 15 minutes old)..."
fi
# ########################################################################
# Work out the module at hand (for a particular task file).
# Test whether a string is a known Einstein module name.
#
is_module ()
{
fgrep -w $argv[1] < ~/.einstein-modules.txt
}
# If the path is something like /home/blott/CA116/week-01/hello.py, then the module is "ca116".
#
get_module_by_path ()
{
local file=$argv[1]
local realpath=$file:a:l
# Below, "z" means split on IFS/whitespace, "s:/:" means split on directory separators.
# It would be better to split on "-", "_" and the like too, but I don't know how to do that
# concisely.
#
for component in ${(zs:/:)realpath:h}
do
is_module $component:l && return
done
false
}
# Like above, but also accept junk like ~/myCA116directory.
#
get_module_by_sloppy_path ()
{
local mod file=$argv[1]:a:l
for mod in $( < ~/.einstein-modules.txt )
do
if [[ $file == *$mod* ]]
then
print $mod
return
fi
done
false
}
# Get a list of modules for which the task has a marker for the upload file name.
# (The intended module can only one of these, because other modules do not have markers!)
#
get_module_by_task ()
{
local sha1 junk file=$argv[1] task=$file:t
local -a mods
print -n $task | $sha1sum | read sha1 junk
mods=( $( fgrep $sha1 ~/.einstein-tasks.txt ) )
if [[ $#mods == 0 ]] || [[ $#mods == 1 ]]
then
false
else
# Shift off the hash (leaving just the modules).
shift mods
print -l -- $mods
fi
}
# This outputs a list of candidate modules for the upload file (and succeeds only if at
# least one module is found).
#
get_modules ()
{
get_module_by_path $argv || get_module_by_sloppy_path $argv || get_module_by_task $argv
}
# ########################################################################
# Remember a previous module choice.
#
get_previous_module ()
{
[[ -s ~/.einstein-previous-module.txt ]] && print -- $( < ~/.einstein-previous-module.txt )
}
remember_previous_module ()
{
[[ $#argv == 1 ]] && print -- $argv[1] > ~/.einstein-previous-module.txt
}
# ########################################################################
# Handle the actual uploads.
#
handle_upload ()
{
local module
local -a mods selections
mods=( $( get_modules $argv ) )
case $#mods in
0 )
print -- "error:" $argv[1]:t "is not a valid task name in any Einstein module" >&2
false; return $? ;;
1 )
# There's only one module, so we're done.
true ;;
* )
# There is more than one candidate module. We'll have to ask the user.
#
# First, ask if they want to re-use a previous choice (if there is one).
#
if module=$( get_previous_module ) && contains $module $mods
then
print
print "You previously uploaded for module" $( coloured $BLUE $module )"."
print "Type 'y' to choose" $( coloured $BLUE $module ) "again (or anything else to pick a different module)."
if read -q answer"?? "
then
mods=( $module )
print
fi
fi
#
# If there are still multiple choices, then pick from a list.
#
selections=( $mods )
while [[ $#mods != 1 ]]
do
PS3="Enter one of the number choices above, or the module code... "
print -l -- "" "'${BLUE}$argv[1]:t$NC' is a task on the following modules, pick a module...\n"
#
select module in $selections
do
if [[ -z $module ]] && [[ -n $REPLY ]] && contains $REPLY $selections
then
mods=( $REPLY )
elif [[ -n $module ]]
then
mods=( $module )
else
print
print $( coloured $RED "Computer says 'no'; enter either the module code or the corresponding number..." )
fi
break
done
done
#
remember_previous_module $mods[1] ;;
esac
module=$mods[1]
upload_file $module $argv
}
upload_file ()
{
local module=$argv[1]
local file=$argv[2]
local task=$file:t
local web_page="https://$module.computing.dcu.ie/"
print "file:" $( coloured $BLUE $( realpath --relative-to=$PWD $file) )
print "task:" $( coloured $BLUE $module/$task )
print "web :" $( coloured $BLUE $web_page )
#
if [[ $file:a == */markers/* ]]
then
# This is an Einstein marker directory.
#
reduced_output="yes"
#
if [[ -f "$file:a:h/task-description.html" ]]
then
bytes=$( wc -c < "$file:a:h/task-description.html" )
sha=$( $sha1sum "$file:a:h/task-description.html" | cut -d ' ' -f 1 )
print "note:" $( coloured $BLUE "task-description.html detected" )
print " " $( coloured $BLUE "task-description.html: $bytes byte(s), $sha" )
else
print "note:" $( coloured $BLUE "task-description.html not detected" )
fi
fi
#
print
print "Uploading" $( coloured $BLUE $task ) "to Einstein (this may take a few seconds)..."
mkdir -p ~/.einstein
http --form "file=@$file" "${web_page}einstein/upload" | tee ~/.einstein/$module-$task.json | format_report $file $module $task
}
format_report ()
{
local -i passed=0 failed=0 tests=0
local report module einstein task test_name result
local call_file=$argv[1]
local call_module=$argv[2]
local call_task=$argv[3]
print
while read report module einstein task test_name result
do
[[ $report == "#test-report" ]] || continue
tests+=1
[[ $result == "passed" ]] && print $( coloured $GREEN "*** test $tests: $result" ) && passed+=1
[[ $result == "failed" ]] && print $( coloured $RED "*** test $tests: $result" ) && failed+=1
done
if (( tests == 0 ))
then
print $( coloured $RED "*** error: invalid task name" )
print $( coloured $RED "*** error: ... $call_task is not a task on $call_module" )
print
return 1
fi
unless_reduced_output print
if (( passed < tests ))
then
print $( coloured $RED "*** failed: $failed of $tests" )
fi
if (( 0 < passed ))
then
print $( coloured $GREEN "*** passed: $passed of $tests" )
fi
if (( passed == tests ))
then
print $( coloured $GREEN "*** overall: correct" )
else
print $( coloured $RED "*** overall: incorrect" )
fi
unless_reduced_output print
print $( coloured $BLUE "*** report: https://$call_module.computing.dcu.ie/einstein/report.html" )
# Use with caution!
# Only lecturers are ever likely to want to do this.
#
# This is used to install the *actual* outputs from the Einstein uploads
# as the expected outputs for the task.
#
if [[ -n $install_outputs ]]
then
install_actual_outputs $call_file $call_module $call_task
fi
(( passed == tests ))
}
# ########################################################################
# Primarily for SB, to support writing Einstein markers.
#
# Assume that the outputs are correct, and install them in the relevant stdout.txt files.
#
# Here, we embed a CoffeeScript script inside this shell script.
# Yuck!
#
# We do it like this in order to avoid users having to install a second script.
# Also, while it would be possible to parse the JSON with jq, the Javascript
# solution is just cleaner and likely more robust.
#
install_marker_output ()
{
if ! which coffee > /dev/null
then
print
print "error: installation of actual outputs requires CoffeeScript"
print
print " sudo npm install -g coffee-script"
print
print " (which in turn requires nodejs and npm)"
exit 1
fi >&2
local installation_script=$( mktemp /tmp/install-einstein-marker-output-$USER.XXXXXXXX )
cat > $installation_script <<EOF
if true # This gives us more readable indentation, that's all.
fs = require "fs"
# The JSON report is on stdin.
fs.readFile 0, (err, data) ->
if err
console.error "error: failed to read JSON #{err}"
process.exit 1
else
for test in JSON.parse(data.toString()).results
name = test.test
name = "./" if name == "test0"
unless 0 <= name.indexOf "/../"
console.log " ...installing #{name}stdout.txt"
fs.writeFile "#{name}stdout.txt", test.stdout, (err) ->
if err
console.error "error: failed to write output #{err}"
process.exit 1
EOF
coffee $installation_script
result=$?
rm $installation_script
return $result
}
install_actual_outputs ()
{
local file=$argv[1]
local module=$argv[2]
local task=$argv[3]
local json=$HOME/.einstein/$module-$task.json
# The JSON is embedded in a rather strange way in the response from the
# HTTP POST, above.
#
# Tidy it up, so that we're left just with the JSON.
#
sed -i '/^#test/ d; s/^#json //;' $json
print -l "" "installing the expected standard output for $task..."
(
cd $file:a:h
install_marker_output < $json
) || exit 1
}
# ########################################################################
# Work out the most recently-updated task file in a directory.
#
get_recent_file () {
local file sha1 junk answer directory=$argv[1]
for file in $directory/*(.Nom)
do
print -n $file:t | $sha1sum | read sha1 junk
if fgrep -q $sha1 ~/.einstein-tasks.txt
then
{
print
print "Picking your most recently-edited task file:" $( coloured $BLUE $( realpath --relative-to=$PWD $file ) )
} >&2
print $file
return
fi
done
print $directory
}
# ########################################################################
# Tweak the command-line arguments:
#
# - empty -> "."
# - /foobar/hello.py/ -> /foorbar/hello.py/hello.py
# - /ca116/ -> most recently-edited task file
# - and expand MVTs
#
# Yikes!
# A recursive shell script!
#
# This will loop for ever if it encounters a malformed MVT task (an MVT task
# which links to itself).
[[ $#argv == 0 ]] && set -- "."
resolve_argument ()
{
local file=$argv[1]
local candidate_task_name=$file:a:t
if [[ -d $file ]] && [[ -f "$file/multi-variant-task.txt" ]]
then
file="$file/multi-variant-task.txt"
reduced_output="yes"
elif [[ -d $file ]] && [[ -f $file/$candidate_task_name ]]
then
file="$file/$candidate_task_name"
reduced_output="yes"
elif [[ -d $file ]]
then
# Offer the most-recently-updated task file.
#
file=$( get_recent_file $file )
fi
if [[ -f $file ]] && [[ $file:t == "multi-variant-task.txt" ]]
then
variants=( $( < $file ) )
print "MVT detected; selecting $#variants variant(s):" >&2
#
for variant in $variants
do
print " $file:h/../$variant" >&2
resolve_argument "$file:h/../$variant"
done
else
print $file
fi
}
args=()
for arg in $argv
do
args+=( $( resolve_argument $arg ) )
done
set -- $args
(( 1 < $#argv )) && reduced_output="yes"
[[ $PWD == */markers/* ]] && reduced_output="yes"
# ########################################################################
# Run all of that...
integer errors=0
for file in $argv
do
if [[ -f $file ]]
then
[[ $#argv == 1 ]] || print -l -- "" "**************************************************************"
handle_upload $file || errors+=1
else
print "error, invalid file: $file" >&2
errors+=1
fi
done
# Dummy commit 4: 12/12/2021 @ 10:48
(( errors == 0 ))