-
Notifications
You must be signed in to change notification settings - Fork 0
/
bb_extract
executable file
·180 lines (149 loc) · 5.03 KB
/
bb_extract
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
# Blackboard cleaner
# Author: Richard Torenvliet
# Github-alias: icyrizard
#
# This bash script is used to unpack the zipfile blackboard generates when
# you download all the files in the "full grade center" -> Assignment Download
# File -> select all students -> download zipfile. Use help functionality to
# guide you to use this bash script.
#
# The MIT License (MIT)
#
# Copyright (c) 2014 Richard Torenvliet #
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# default extract directory
BB_EXTRACT_DIR=/tmp/blackboard
#Initialize variables to default values.
OUTPUT_DIR=/tmp/bb_cleaned
#Set Script Name variable
SCRIPT=`basename ${BASH_SOURCE[0]}`
#Set fonts for Help.
NORM=`tput sgr0`
BOLD=`tput bold`
REV=`tput smso`
unpack_result=""
# using auto compression detection of tar. This is different for
# GNU and bsdtar, hence this check
if tar --version | grep -q GNU; then
auto_unpack_tar_command="tar -xaf";
else
auto_unpack_tar_command="tar -xf";
fi
function extract() {
ARCHIVE=`printf "%q" $1`
OUTPUT=`printf "%q" $2`
unpack_result="success"
default_unpack_command="$auto_unpack_tar_command ARCHIVE $OUTPUT"
if [ -f $ARCHIVE ] ; then
case $ARCHIVE in
*.rar) unrar x $ARCHIVE $OUTPUT;;
*.zip) unzip $ARCHIVE -d $OUTPUT 1> /dev/null;;
*) bash -c $default_unpack_command;;
esac
else
unpack_result="fail"
fi
}
# forloop delimiter
IFS=$'\n'
# find_and_unpack:
# Find all .txt files, remove .txt postfix, and use it as a prefix to
# search for archives. If a pdf found, put it in the output directory.
# The output directories name is generated from the first line of the .txt.
# Blackboard uses that to state on which behalf the assignment was submitted.
function find_and_unpack() {
INPUT_DIR=$1
OUTPUT_DIR=$2
for i in `ls $INPUT_DIR/*.txt`
do
PREFIX=`echo $i | awk '{split($0, prefix,".txt"); print prefix[1]}'`
SUBMIT_NAME=$OUTPUT_DIR/`head -1 $PREFIX.txt| cut -d " " -f2,3,4,5,6 | \
sed -e "s/\ /_/g;s/(//g;s/)//g;" | awk '{print tolower($0)}'`
mkdir -p $SUBMIT_NAME
# cp txt to submitters DIR
cp $i $SUBMIT_NAME
FIND_PREFIX=`basename $PREFIX`
ARCHIVE_LIST=`find $INPUT_DIR -name "$FIND_PREFIX\_*.gz" \
-o -name "$FIND_PREFIX\_*.tar"\
-o -name "$FIND_PREFIX\_*.zip"\
-o -name "$FIND_PREFIX\_*.rar"\
-o -name "$FIND_PREFIX\_*.[bz2tg]*"`
FILE_LIST=`find "$INPUT_DIR" -name "$FIND_PREFIX\_*.[A-Za-z0-9]*"`
for FILE in $FILE_LIST; do
echo $FILE
cp $FILE_LIST $SUBMIT_NAME
done
if [ -z $ARCHIVE_LIST ]; then echo $SUBMIT_NAME >> "error.txt"
else
for ARCHIVE in $ARCHIVE_LIST; do
extract $ARCHIVE $SUBMIT_NAME
done
fi
done
}
function echo_original_filecount() {
FILE_COUNT=`ls $DIR -I *.txt -I *.pdf | grep -o -E \
"\.([[:alpha:]])*(\.[[:alpha:]]*)*$" | sort | uniq -c`
echo "archive files in orig_folder: \n" $FILE_COUNT
}
# delete old output directory, create new empty one
function create_directory() {
rm -rf $1
mkdir -p $1
}
# Help function
function HELP {
echo -e \\n"Help documentation for ${SCRIPT}."\\n
echo -e "Basic usage: $SCRIPT gradebook.zip"\\n
echo "Command line switches are optional. The following switches are recognized."
echo "-d --Set output directory. ${BOLD}$OUTPUT_DIR${NORM}."
echo -e "-h --Displays this help message. No further functions are performed."\\n
echo -e "Example: ${BOLD}$SCRIPT -d assignment1 gradebook.zip${NORM}"\\n
exit 1
}
#Check the number of arguments. If none are passed, print help and exit.
NUMARGS=$#
if [ $NUMARGS -eq 0 ]; then
HELP
exit 1
fi
while getopts :d:h FLAG; do
case $FLAG in
d)
OUTPUT_DIR=$OPTARG
;;
h) #show help
HELP
;;
\?)
echo -e \\n"Option -${BOLD}$OPTARG${NORM} not allowed."
HELP
;;
esac
done
# shift ops, all optional args are now removed $1 will have to be the filename
shift $((OPTIND-1))
while [ $# -ne 0 ]; do
ZIPFILE=$1
echo "creating dir $BB_EXTRACT_DIR ..."
create_directory $BB_EXTRACT_DIR
create_directory $OUTPUT_DIR
echo "extracting $ZIPFILE to $BB_EXTRACT_DIR"
extract $ZIPFILE $BB_EXTRACT_DIR
if [ $unpack_result != "fail" ]; then
echo "find and unpack all submitted assignments"
find_and_unpack $BB_EXTRACT_DIR $OUTPUT_DIR
else
echo "continueing"
fi
shift
done
exit 0