workflow.sh 10.9 KB
Newer Older
Betz Katrin's avatar
Betz Katrin committed
1
2
#!/bin/bash

thorsten.vitt's avatar
thorsten.vitt committed
3
setup() {
Betz Katrin's avatar
Betz Katrin committed
4

thorsten.vitt's avatar
thorsten.vitt committed
5
6
7
8
9
    configdir="`dirname $0`"
    if [ "x$configdir" = "x" ]
    then
        configdir="."
    fi
Betz Katrin's avatar
Betz Katrin committed
10

thorsten.vitt's avatar
thorsten.vitt committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    # Read the config file
    echo "reading config from $configdir"
        if [ -r workflow.rc ]
        then 
            . "$configdir/workflow.rc"
        else
            die "ERROR: Configuration file $configdir/workflow.rc not found."
        fi

        # Validate the configuration
        [ -d "$ssdir" ] || die "Stylesheet directory $ssdir not found or not a directory"
        [ -d "$idir" ] || die "Input directory $idir not found or not a directory"
        [ "$odir" = "" ] && die "Output directory \$odir has not been configured!"
}


# Usage: pipeline filename
#
# Runs most processing steps for one individual encyklopaedia.
Betz Katrin's avatar
Betz Katrin committed
30
pipeline() {
thorsten.vitt's avatar
thorsten.vitt committed
31
    path="$1"
thorsten.vitt's avatar
thorsten.vitt committed
32
    mode=$2
33
    # filename with extension
thorsten.vitt's avatar
thorsten.vitt committed
34
    filename="`basename \"$path\"`"
35
    # filename without extension
thorsten.vitt's avatar
thorsten.vitt committed
36
    subdir="`basename \"$path\" .xml`"
37
    
38
39
40
41
42
43
    if [ -r skip.txt ] && echo "$filename" | grep -q -F -f skip.txt
    then
       echo "$filename is in skip list -- skipping."
       return
    fi

44
    echo "Processing $filename..."
Betz Katrin's avatar
Betz Katrin committed
45
46
47
48
 
    echo "   1.1 Adding ids to $filename ..."
 
    saxon \
thorsten.vitt's avatar
thorsten.vitt committed
49
        -s:"$corpusdir/$filename" \
Betz Katrin's avatar
Betz Katrin committed
50
        -xsl:"$ssdir/1_add_data/add_tgID.xsl" \
51
        -o:"$odir/01_1_addID/$filename" || return
Betz Katrin's avatar
Betz Katrin committed
52
53
54
   
    echo "   1.2 Adding work information to $filename ..."
     saxon \
55
        -s:"$odir/01_1_addID/$filename" \
Betz Katrin's avatar
Betz Katrin committed
56
        -xsl:"$ssdir/1_add_data/add_Work_add_pname-location.xsl" \
57
        -o:"$odir/01_2_addWorkInfo/$filename" || return
Betz Katrin's avatar
Betz Katrin committed
58
59
60
  
    echo "   2. Creating TEI-Structure from $filename ... " 
    saxon \
61
        -s:"$odir/01_2_addWorkInfo/$filename" \
Betz Katrin's avatar
Betz Katrin committed
62
63
64
65
66
        -xsl:"$ssdir/2_generateTEI/generateTEI.xsl"\
        -o:"$odir/2-TEI-Structure/$filename"

    echo "   3. Linking $filename... " 
    saxon \
67
        -s:"$odir/2-TEI-Structure/$filename" \
68
        -xsl:"$ssdir/3_linking/linking.xsl" \
69
        -o:"$odir/3-Linking/$filename" || return
Betz Katrin's avatar
Betz Katrin committed
70
71
72
73

		
	echo "   4. structureMetadata ... " 
    saxon \
74
        -s:"$odir/3-Linking/$filename" \
Betz Katrin's avatar
Betz Katrin committed
75
        -xsl:"$ssdir/4_structureMetadata/structureMetadata.xsl" \
76
        -o:"$odir/4-structureMetadata/$filename" || return
Betz Katrin's avatar
Betz Katrin committed
77

78
79
80
81
82
83
    case "$filename" in
       *-000*)
          echo "--- Stopping workflow early for $filename"
          return
          ;;
    esac
Betz Katrin's avatar
Betz Katrin committed
84
85
86
  
    echo "   5. Genres ... " 
    saxon \
87
        -s:"$odir/4-structureMetadata/$filename" \
Betz Katrin's avatar
Betz Katrin committed
88
        -xsl:"$ssdir/5_genres/genres.xsl" \
89
        -o:"$odir/5-genres/$filename" || return
90
91
92
        
    echo "   6. Corrections ... " 
    saxon \
93
        -s:"$odir/5-genres/$filename" \
Betz Katrin's avatar
Betz Katrin committed
94
        -xsl:"$ssdir/6_corrections/corrections.xsl" \
95
        -o:"$odir/6-corrections/$filename" || return
Betz Katrin's avatar
Betz Katrin committed
96
97

    echo "Done with $filename."	
98

99
    echo "   7pre. Clearing split output directory"
thorsten.vitt's avatar
thorsten.vitt committed
100
    rm -rf -- "$odir/7-split/$subdir"
101
102
103
    
    echo "   7. Splitting $filename ... " 
    saxon \
104
        -s:"$odir/6-corrections/$filename" \
Betz Katrin's avatar
Betz Katrin committed
105
        -xsl:"$ssdir/7_split/split.xsl" splitDir="${subdir}" crtFilename="${filename}" \
106
        -o:"$odir/7-split/main.xml"     || return
107
108
109
110
111
112
113
114
115
116

    echo "   8. Postprocessing ..."
    if [ \! -L "$odir/final" ]
    then
       old_dir=`pwd`
       cd "$odir"
       ln -s 7-split final
       cd "$old_dir"
    fi

117
    
Betz Katrin's avatar
Betz Katrin committed
118
119
120
121
122
123
124
125
126
127
128
    #echo "   8. Extract targets from $filename ... " 
    #saxon \
    #    -s:"$odir/7-splitEnzyklops/$filename" \
    #    -xsl:"$ssdir/8_extractLinks/extractLinks.xsl" \
    #    -o:"$odir/8-extractLinks/$filename" || return

    #if [ "$mode" == "partial" ]
   #then
   #    echo "   (Partial application -- skipping postprocessing steps)"
   #else
   #    rewrite-links   "$subdir"
129
130
   collect-images  "$subdir"
   process-aggregations    "$subdir"
Betz Katrin's avatar
Betz Katrin committed
131
132
133
   # fi

   # echo "Done with $filename." 
Betz Katrin's avatar
Betz Katrin committed
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
# Prepare, i.e. clear the target directory
prepare-output-directory() {
    if [ "$1" == "" ]
    then
        outputdir="$odir"

        echo "Preparation: Removing old directories below $outputdir ..."
        [ "$outputdir" != "" -a -d "$outputdir" ] && rm -rf "$outputdir"/*

        cat > "$odir/README.html" <<EOF
        <ol>
        <li><a href='http://dev.digital-humanities.de/ci/job/digibib-enzyklops/'><img src='http://dev.digital-humanities.de/ci/buildStatus/icon?job=digibib-enzyklops'/> Transformation</a></li>
        <li><a href='http://dev.digital-humanities.de/ci/job/digibib-enzyklops-validate/'><img src='http://dev.digital-humanities.de/ci/buildStatus/icon?job=digibib-enzyklops-validate'/> XML-Validierung</a></li>
        <li><a href='http://dev.digital-humanities.de/ci/job/digibib-enzyklops-text/'><img src='http://dev.digital-humanities.de/ci/buildStatus/icon?job=digibib-enzyklops-text'/> Text-Validierung</a></li>
        <li><a href='http://dev.digital-humanities.de/ci/job/digibib-enzyklops-html/'><img src='http://dev.digital-humanities.de/ci/buildStatus/icon?job=digibib-enzyklops-html'/> HTML-Vorschau</a></li>
        </ol>
EOF
    else
        rm -rfv "$odir/splitEnzyklops/$1"
        rm -rfv "$odir/Link-rewriting/$1"
        rm -rfv "$odir/html/$1"
    fi
}
Betz Katrin's avatar
Betz Katrin committed
160
161


162
163
164
165
166
167
168
# Processes all files generated in the last (splitEnzyklops) step of the
# individual pipeline calls and fix the links within such that they contain the
# paths to the correct files. This only deals with *.item.xml files.
rewrite-links() {
    if [ "$1" == "" ]
    then
        echo "Link rewriting for all enzyclopaediae ..."
169
        rewritesource="$odir/7-split"
170
171
172
        rewritetarget="$odir/Link-rewriting"
    else
        echo "   Link rewriting for $1 ..."
173
        rewritesource="$odir/7-split/$1"
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
        rewritetarget="$odir/Link-rewriting/$1"
    fi
    # create the directory ...
    mkdir -p "$rewritetarget"
    # ... and make an absolute, canonical path for it
    rewritetarget=$(readlink -f "$rewritetarget")

    # First copy the unaffected files: 
    # Go into the split directory, 
    # find all _f_iles that are not (\!) named '*.item.xml',
    # copy them to $rewritetarget, preserving directory structure
    echo "      1. Copying unaffected files ..."
    ( cd "$rewritesource" && \
        find * -type f \! -name '*.item.xml' -print0 \
        | xargs -0 cp --parents -t "$rewritetarget" )

    n=0
    step=50
    # Now process all items:
    echo "      2. Processing potentially interlinked items ..."
    find "$rewritesource" -name '*.item.xml' | while read item
    do
        # remove the $odir/splitEnzyklops prefix to get a relative path
        relitem=${item#$rewritesource/}
        saxon \
            -s:"$item" \
            -xsl:"$ssdir/Link-rewriting/link-rewriting.xsl" \
            -o:"$rewritetarget/$relitem"
        n=$(( $n + 1 ))
        echo "     ... $n. $relitem ..."
    done

    # link the final folder
    ( cd $odir && ln -sfT Link-rewriting final )
    echo "Link rewriting done"
}

# Links the images from the source directory to the final directory.
# The stylesheets should create metadata files of the form <filename>.meta in
# the place where the images should be. This function looks for those metadata
# files, tries to find a matching image file in the source folder, and creates
# a symbolic link to it at the right place. If no image could be found for a
# specific .meta file, an ERROR message is printed.
collect-all-images() {
    echo "Collecting all images ..."
    for dir in $odir/final/*
    do
        if [ -d "$dir" ]
        then
            enz=$(basename $dir)
            collect-images "$enz"
        fi
    done
}

collect-images() {
    if [ "$1" == "" ]
    then
        collect-all-images
    else
        enz="$1"
        dir="$odir/final/$enz"

        echo "   Collecting images for $enz ..."

        find $dir -name '*.png.meta' -or -name '*.jpg.meta' | while read meta
        do
            img_name=$(basename $meta .meta)
242
243
            img_source="$idir/Literatur/Images/$img_name"
            img_source_orig="$idir/Literatur/Images original/$img_name"
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
            if [ -r "$img_source_orig" ]
            then
                ln -s "$img_source_orig" "${meta%.meta}"
            elif [ -r "$img_source" ]
            then
                ln -s "$img_source" "${meta%.meta}"
            else
                echo "ERROR collecting images: For $meta, neither $img_source_orig nor $img_source were found."
            fi
        done
    fi
}

# Recursively process the generated aggregations from the final folder.
# (1) Validates that all the links in the aggregation files work
# (2) Validates that the generated files are all linked somewhere, except for 
#       - .lst and .html files, which won't be imported
#       - image files, already checked by the script above
# (3) Generates a fancy HEADER.html index linking each individual file.
# Note that the HEADER file also links to html preview versions of each
# item -- these must however be created separately using the html.sh script!
process-all-aggregations() {
    echo
    echo "Validating filenames and generating indexes ..."

    for dir in $odir/final/*
    do
        if [ -d $dir ]
        then
            process-aggregations "$(basename $dir)"
        fi
    done
}

process-aggregations() {
    if [ "$1" == "" ]
    then
        process-all-aggregations
    else
        enz="$1"
        dir="$odir/final/$enz"

        echo "Looking through $enz ..."
        filelist="$dir/files.lst"
        echo '<filelist xmlns="http://www.textgrid.de/ns/filelist">' > "$filelist"
        find "$dir" -type f -and -not -name '*.lst' -and -not -name '*.html' \
            -printf '  <file modified="%TY-%Tm-%TdT%TH:%TM">%P</file>\n' >> "$filelist"
        echo '</filelist>' >> "$filelist"

293
        saxon -s:"$dir/collection/collection.collection" \
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
            -xsl:dbsplitindex.xsl \
            -o:"$dir/HEADER.html" \
            "base=$dir/HEADER.html" "filelist=$filelist" "html=$odir/html/`basename $dir`"

    fi
}

##################### Helper functions ###########################

# print an error message and exit.
die() {
    if [ $# -gt 0 ]
    then
        echo "ERROR: $@"
    else
        cat
    fi
    exit 1
}

thorsten.vitt's avatar
thorsten.vitt committed
314
315
task() {
    spec="$1"
316
    echo "Running subtask '$spec' ($corpusdir) ..."
thorsten.vitt's avatar
thorsten.vitt committed
317
318
    if [ -r "$spec" ]
    then
319
       echo "(a) file $spec"
thorsten.vitt's avatar
thorsten.vitt committed
320
321
322
        pipeline "$spec"
    elif [ -r "$corpusdir/$spec" ]
    then
323
       echo "(b) file $corpusdir/$spec"
thorsten.vitt's avatar
thorsten.vitt committed
324
325
326
        pipeline "$corpusdir/$spec"
    elif [ -r "$corpusdir/Literatur-${spec}.xml" ]
    then
327
328
       echo "(c) file $corpusdir/Literatur-${spec}.xml"
        pipeline "$corpusdir/Literatur-${spec}.xml"
thorsten.vitt's avatar
thorsten.vitt committed
329
    else
330
       echo "(d) ... trying to read $spec as a pattern ..."
331
        find "$corpusdir" -iname "*$spec*" |\
thorsten.vitt's avatar
thorsten.vitt committed
332
333
            while read f
            do
334
               echo "   - match: $f"
thorsten.vitt's avatar
thorsten.vitt committed
335
336
337
338
339
                task "$f"
            done
    fi
}

340
341
342
################### main function call ###########################

setup
Betz Katrin's avatar
Betz Katrin committed
343

thorsten.vitt's avatar
thorsten.vitt committed
344
345
346
347
348
349
350
351
352
353
if [ $# -gt 0 ]
then
    while [ $# -gt 0 ]
    do
        task "$1"
        shift
    done
else
    for f in "$corpusdir/"Literatur-*.xml 
    do
354
       case "$f" in 
355
          *-Nietzsche*|*-Winckelmann*)
thorsten.vitt's avatar
thorsten.vitt committed
356
            echo "Skipping $f ..."
357
358
            ;;
         *)
thorsten.vitt's avatar
thorsten.vitt committed
359
            pipeline "$f"
360
361
            ;;
      esac
thorsten.vitt's avatar
thorsten.vitt committed
362
363
    done
fi