aboutsummaryrefslogtreecommitdiff
path: root/project
blob: a99e0a6d96640f3370811319a7f92f6271e8a992 (plain)
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
#!/bin/sh
#
# High-level script to manage the project.
# Run `./project --help' for a description of how to use it.
#
# Copyright (C) 2019-2021 Mohammad Akhlaghi <mohammad@akhlaghi.org>
# Copyright (C) 2021      Raul Infante-Sainz <infantesainz@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


# Basic settings
# --------------
# Stop the script if there are any errors.
set -e





# Default option values
jobs=0     # 0 is for the default for the `configure.sh' script.
group=
debug=
host_cc=0
operation=
build_dir=
input_dir=
check_config=
make_targets=
software_dir=
clean_texdir=0
prepare_redo=0
highlightnew=0
all_highlevel=0
existing_conf=0
highlightnotes=0
separatesupplement=0
scriptname="./project"





# Parse the options
# -----------------
#
# Separate command-line arguments from options. Then put the option value
# into the respective variable.
#
# Each option has two lines because we want to process both these formats:
# `--name=value' and `--name value'. The former (with `=') is a single
# command-line argument, so we just need to shift the counter by one. The
# latter (without `=') is two arguments, so we'll need two shifts.
#
# Note on the case strings: for every option, we need three lines: one when
# the option name and value are separate. Another when there is an equal
# between them, and finally one where the value is immediately after the
# short-format. This exact order is important. Otherwise, there will be a
# conflict between them.

print_help() {
    # Print the output.
    cat <<EOF
Usage: $scriptname configure [OPTIONS]
       $scriptname shell     [OPTIONS]
       $scriptname make      [OPTIONS]

Top-level script to manage the reproducible project. The high-level
operation is defined by the (mandatory) second argument:

  configure    - Configure project for this machine (e.g., build software).
  make         - Run the project (do analysis and build outputs).
  shell        - Execute the project's shell for interactive testing.

RECOMMENDATION: If this is the first time you are configuring this
template, please don't use the options and let the script explain each
parameter in full detail by simply running './project configure'.

Project 'make' special features.
  ./project make           Build the project on one thread
  ./project make -jN       Built the project in parallel on N threads.
  ./project make clean     Clean all files generated by 'make' (not software).
  ./project make texclean  Clean all files built by (La)TeX.
  ./project make distclean Clean everything (including compiled software).
  ./project make dist      Produce a LaTeX-ready-to-build distribution tarball
                           ('tar.gz') of the project. This is ready to be
                           uploaded to servers like 'arXiv.org'.
  ./project make dist-lzip Similar to 'dist', but compress to '.tar.lz'.
  ./project make dist-zip  Similar to 'dist', but compress to '.zip'.
  ./project make dist-software  Build a .tar.gz tarball containing all
                                software source tarballs necessary for the
                                project.

With the options below you can modify the default behavior.
Configure options:
  -b, --build-dir=STR      Top directory to build the project in.
  -e, --existing-conf      Use (possibly existing) local configuration.
      --host-cc            Use host system's C compiler, don't build GCC.
  -i, --input-dir=STR      Directory containing input datasets (optional).
  -s, --software-dir=STR   Directory containing necessary software tarballs.
      --check-config       During configuration, show what is being built.
      --clean-texdir       Remove possibly existing build-time subdirectories
                           under the project's 'tex/' directory (can happen
                           when source is from arXiv for example).
      --all-highlevel      Build all high-level software (for development).

Configure and Make options:
  -d, --debug[=FLAGS]      In configure: use -j1, no -k, and no Zenodo check.
                           In make: 'FLAGS' will be directly passed to 'make'.
  -g, --group=STR          Build and run with write permissions for a group.
  -j, --jobs=INT           Number of threads to build/run the software.
  -?, --help               Print this help list.

Make (analysis) options:
  -p, --prepare-redo       Re-do preparation (only done automatically once).

Make (final PDF) options:
      --highlight-new      Highlight '\new' parts of text as green.
      --highlight-notes    Show '\tonote' regions as red text in PDF.
      --supplement         Build the appendices as a separate supplement PDF.

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

Maneage URL: https://maneage.org

Report bugs to mohammad@akhlaghi.org
EOF
}

on_off_option_error() {
    if [ "x$2" = x ]; then
        echo "$scriptname: '$1' doesn't take any values."
    else
        echo "$scriptname: '$1' (or '$2') doesn't take any values."
    fi
    exit 1
}

check_v() {
    if [ x"$2" = x ]; then
        echo "$scriptname: option '$1' requires an argument."
        echo "Try '$scriptname --help' for more information."
        exit 1;
    fi
}

func_operation_set() {
    if [ x$operation = x ]; then
        operation=$1
    else
        echo "Only one operation ('configure', 'make' or 'shell') may be given."
        exit 1
    fi
}

while [ $# -gt 0 ]
do
 case $1 in
  # Main operation.
  configure)    func_operation_set $1; shift;;
  make)         func_operation_set $1; shift;;
  shell)        func_operation_set $1; shift;;


  # Configure options:
  -b|--build-dir)        build_dir="$2";                                     check_v "$1" "$build_dir";    shift;shift;;
  -b=*|--build-dir=*)    build_dir="${1#*=}";                                check_v "$1" "$build_dir";    shift;;
  -b*)                   build_dir=$(echo    "$1" | sed -e's/-b//');         check_v "$1" "$build_dir";    shift;;
  -e|--existing-conf)    existing_conf=1;                                                                  shift;;
  -e*|--existing-conf=*) on_off_option_error --existing-conf -e;;
  --host-cc)             host_cc=1;                                                                        shift;;
  --host-cc=*)           on_off_option_error --host-cc;;
  -i|--input-dir)        input_dir="$2";                                     check_v "$1" "$input_dir";    shift;shift;;
  -i=*|--input-dir=*)    input_dir="${1#*=}";                                check_v "$1" "$input_dir";    shift;;
  -i*)                   input_dir=$(echo    "$1" | sed -e's/-i//');         check_v "$1" "$input_dir";    shift;;
  -s|--software-dir)     software_dir="$2";                                  check_v "$1" "$software_dir"; shift;shift;;
  -s=*|--software-dir=*) software_dir="${1#*=}";                             check_v "$1" "$software_dir"; shift;;
  -s*)                   software_dir=$(echo "$1" | sed -e's/-s//');         check_v "$1" "$software_dir"; shift;;
  --check-config)        check_config=1;                                                                   shift;;
  --check-config=*)      on_off_option_error --check-config;;
  --clean-texdir)        clean_texdir=1;                                                                   shift;;
  --clean-texdir=*)      on_off_option_error --clean-texdir;;
  --all-highlevel)       all_highlevel=1;                                                                  shift;;
  --all-highlevel=*)     on_off_option_error --all-highlevel;;

  # Configure and Make options:
  -g|--group)             group="$2";                            check_v group "$group";   shift;shift;;
  -g=*|--group=*)         group="${1#*=}";                       check_v group "$group";   shift;;
  -g*)                    group=$(echo   "$1" | sed -e's/-g//'); check_v group "$group";   shift;;
  -j|--jobs)              jobs="$2";                             check_v jobs  "$jobs";    shift;shift;;
  -j=*|--jobs=*)          jobs="${1#*=}";                        check_v jobs  "$jobs";    shift;;
  -j*)                    jobs=$(echo "$1" | sed -e's/-j//');    check_v jobs  "$jobs";    shift;;
  -'?'|--help)            print_help; exit 0;;
  -'?'*|--help=*)         on_off_option_error --help -?;;

  # Make options
  # ------------
  #
  # Note that Make's `debug' can take values, but when called without any
  # value, it is like giving it a value of `a'):
  --supplement)           separatesupplement=1;                                            shift;;
  --supplement=*)         on_off_option_error --supplement;;
  --highlight-new)        highlightnew=1;                                                  shift;;
  --highlight-new=*)      on_off_option_error --highlight-new;;
  --highlight-notes)      highlightnotes=1;                                                shift;;
  --highlight-notes=*)    on_off_option_error --highlight-notes;;
  -d|--debug)             if [ x"$2" = x ]; then debug=a;          shift;
                          else debug="$2"; check_v debug "$debug"; shift;shift; fi;;
  -d=*|--debug=*)         debug="${1#*=}";                       check_v debug "$debug";   shift;;
  -d*)                    debug=$(echo "$1" | sed -e's/-d//');   check_v debug "$debug";   shift;;
  -p|--prepare-redo)      prepare_redo=1;                                                  shift;;
  -p=*|--prepare-redo=*)  on_off_option_error --prepare-redo;                              shift;;

  # Unrecognized option:
  -*) echo "$scriptname: unknown option '$1'"; exit 1;;

  # Not an option, an argument (so its a Make target).
  *) make_targets="$make_targets $1"; shift;;
 esac
done





# Check configuration status
# --------------------------
if ! [ x$check_config = x ]; then
    # Find the color option to pass to `ls'. Note that `--color' (for GNU
    # Coreutils `ls') should be checked first because it also has `-G', but
    # for something else.
    if   ls --color 2> /dev/null > /dev/null; then coloropt="--color=auto"
    elif ls -G      2> /dev/null > /dev/null; then coloropt="-G"
    else                               coloropt=""
    fi

    # Print a notice to let the user know what is happening.
    cat <<EOF

This is an infinite loop that will print what software are being built at
every moment. It is actually a listing ('ls' command) of the temporary
directory where software source code are unpacked while they are being
built. If the project isn't being configured, the output (every second)
will either be empty (only a date) or a with an error about a non-existant
directory. This feature is thus only useful when the project's software are
being built.

EOF

    # Run the infinite loop. To be able to find the last built programs, we
    # need to put all the Python programs and high-level program into one
    # directory.
    checkdir=.local/version-info/.for-check-config
    while true; do

        # Make sure the '.build' directory has already been created.
        echo;
        echo "========================"
        if [ -d .build ]; then
            echo "$(date)    [[press CTRL-C to stop]]";
            echo "--- Currently being built:"
            if [ -d .build/software/build-tmp ]; then
                ls $coloropt .build/software/build-tmp || junk=1;
            fi

            # Make the temporary directory, delete its contents, then put new
            # links of all built software.
            if ! [ -d $checkdir ]; then mkdir $checkdir; fi
            rm -f $checkdir/*

            # Check if any programs exist in the given directory yet.
            printresults=0
            check=$(ls .local/version-info/python/)
            if ! [ "x$check" = x ]; then
                printresults=1
                ln -s .local/version-info/python/*  $checkdir/
            fi
            check=$(ls .local/version-info/proglib/)
            if ! [ "x$check" = x ]; then
                printresults=1
                ln -s "$(pwd)"/.local/version-info/proglib/* $checkdir/
            fi

            # If something was actually found, then print them.
            if [ $printresults = 1 ]; then
                echo "--- Last 5 packages that were built:"

                # Then sort all the links based on the most recent dates of the
                # files they link to (with '-L').
                ls -Llt $checkdir \
                    | awk '/^-/ && c++<5 {printf "[at %s] %s\n", $(NF-1), $NF}'
            fi
        else
            cat <<EOF
The connection to the build directory (.build) is not yet created.

If you have just ran './project configure', it should be created and will
be used in a few seconds to report the build status of various software.
If not, please run './project configure' (in another terminal) and you will
see the results shortly afterwards.
EOF
        fi
        echo "========================"

        # Wait for the next round of checks.
        sleep 1
    done
    exit 0
fi





# Basic group settings
# --------------------
if ! [ x$group = x ]; then

    # Check if group is usable.
    if ! sg "$group" "echo Group \'$group\' exists"; then
        echo "$scriptname: '$group' is not a usable group name on this system.";
        echo "(TIP: you can use the 'groups' command to see your groups)"
        exit 1
    fi

    # Set the group option for running Make.
    gopt="maneage_group_name=$group"
fi





# Error when configuration isn't run
configuration_necessary() {
    cat <<EOF

The project is either (1) not configured on this system, or (2) the
configuration wasn't successful.

(1) If it hasn't been configured at all, use the command below to configure
it (set a build directory and let it build its necessary software in it).

      $ ./project configure

(2) If it has been configured, but the configuration failed in a step, you
can re-configure it using your previous settings with the command
below. All successful steps will be skipped, allowing a fast completion.

      $ ./project configure -e

If there was a problem, please let us know by filling this online form:
      http://savannah.nongnu.org/support/?func=additem&group=reproduce

EOF
    exit 1
}





# Run operations in controlled environment
# ----------------------------------------
controlled_env() {
    # Get the full address of the build directory:
    bdir=`.local/bin/realpath .build`

    # Remove all existing environment variables (with `env -i') and only
    # use some pre-defined environment variables, then build the project.
    envmake=".local/bin/env -i HOME=$bdir sys_rm=$(which rm) $gopt"
    envmake="$envmake separatesupplement=$separatesupplement "
    envmake="$envmake highlightnew=$highlightnew"
    envmake="$envmake highlightnotes=$highlightnotes .local/bin/make "
    envmake="$envmake --no-builtin-rules --no-builtin-variables -f $1"
    if ! [ x"$debug" = x  ]; then envmake="$envmake --debug=$debug"; fi

    # Set the number of jobs. Note that for the `configure.sh' script the
    # default value has to be 0, so the default is the maximum number of
    # threads. But here, the default value is 1.
    if ! [ x"$jobs"  = x0 ]; then envmake="$envmake -j$jobs";  fi

    # Run the project
    if [ x"$group" = x ]; then
        $envmake $make_targets
    else
        # Set the group and permission flags.
        sg "$group" "umask $perms && $envmake $make_targets"
    fi
}





# Do requested operation
# ----------------------
perms="u+r,u+w,g+r,g+w,o-r,o-w,o-x"
configscript=./reproduce/software/shell/configure.sh
case $operation in

    # Build the project's software.
    configure)

        # Set executable flags
        # --------------------
        #
        # In some scenarios (for example when using a tarball from arXiv),
        # it may happen that the host server has removed the executable
        # flags of all the files. In `README.md' we instruct the readers on
        # setting the executable flag of this script. But we don't want the
        # user to have to worry about any other file that needs an
        # executable flag.
        #
        # Basically, all the project shell scripts need executable flags so
        # to make sure they have them, we are activating the executable
        # flags by default here every time './project configure' is run. If
        # any other file in your project needs such flags, add them here.
        chmod +x reproduce/software/shell/* reproduce/software/config/*.sh \
              reproduce/analysis/bash/*

        # If the user requested, clean the TeX directory from the extra
        # (to-be-built) directories that may already be there (and will not
        # allow the configuration to complete).
        if [ x"$clean_texdir" = x1 ]; then
            rm -rf tex/build tex/tikz
        fi

        # Variables to pass to the configuration script.
        export jobs=$jobs
        export debug=$debug
        export host_cc=$host_cc
        export build_dir=$build_dir
        export input_dir=$input_dir
        export scriptname=$scriptname
        export maneage_group_name=$group
        export software_dir=$software_dir
        export existing_conf=$existing_conf
        export all_highlevel=$all_highlevel

        # Run the configuration script
        if [ x"$group" = x ]; then
            $configscript
        else
            # Set the group and permission flags.
            sg "$group" "umask $perms && $configscript"

            # Set the group writing permission for everything in the
            # installed software directory. The common build process sets
            # the writing permissions of the installed programs/libraries
            # to `755'. So group members can't write over a file. This
            # creates problems when another group member wants to update
            # the software for example. We thus need to manually add the
            # group writing flag to all installed software files.
            echo "Enabling group writing permission on all installed software..."
            .local/bin/chmod -R g+w .local/;
        fi
        ;;





    # Batch execution of the project.
    make)

        # Make sure the configure script has been completed properly
        # (`configuration-done.txt' exists).
        if ! [ -f .build/software/configuration-done.txt ]; then
            configuration_necessary
        fi

        # Run data preparation phase (optionally build Makefiles with
        # special values for optimizing the main `top-make.mk'). But note
        # that data preparation is only done automatically the first time
        # the project is built (when `.build/software/preparation-done.mk'
        # doesn't yet exist). After that, if the user wants to re-do the
        # preparation they have to use the `--prepare-redo' option.
        if ! [ -f .build/software/preparation-done.mk ] \
                || [ x"$prepare_redo" = x1 ]; then
            controlled_env reproduce/analysis/make/top-prepare.mk
        fi

        # Run the actual project.
        controlled_env reproduce/analysis/make/top-make.mk

        # Print the number of words (if the user has pdftotext outside of
        # Maneage! For now!!!), AND there actually is a 'paper.pdf' (for
        # example when running './project make clean' there isn't any
        # 'paper.pdf').
        if [ -f paper.pdf ]; then
            if type pdftotext > /dev/null 2>/dev/null; then
                numwords=$(pdftotext paper.pdf && cat paper.txt | wc -w)
                numeff=$(echo $numwords | awk '{print $1-850+500}')
                echo; echo "Number of words in full PDF: $numwords"
                if [ $separatesupplement = 1 ]; then
                    echo "No abstract, and captions (250 for each figure): $numeff"
                fi
                rm paper.txt
            fi
        fi
        ;;


    shell)

        # Make sure the configure script has been completed properly
        # (`configuration-done.txt' exists).
        if ! [ -f .build/software/configuration-done.txt ]; then
            configuration_necessary
        fi

        # Run the project's own shell without inheriting any environment
        # from the host. The 'TERM' environment variable is necessary for
        # tools like some text editors.
        bdir=`.local/bin/realpath .build`
        instdir="$bdir"/software/installed
        .local/bin/env -i \
                       HOME="$bdir" \
                       TERM="$TERM" \
                       CCACHE_DISABLE=1 \
                       PATH="$instdir"/bin \
                       LDFLAGS=-L"$instdir"/lib \
                       SHELL="$instdir"/bin/bash \
                       CPPFLAGS=-I"$instdir"/include \
                       LD_LIBRARY_PATH="$instdir"/lib \
                       OMPI_MCA_plm_rsh_agent=/bin/false \
                       PYTHONPATH="$instdir"/lib/python/site-packages \
                       PYTHONPATH3="$instdir"/lib/python/site-packages \
                       PS1="[\[\033[32m\](maneage)\[\033[00m\] \u@\h \W]$ " \
                       "$instdir"/bin/bash
        ;;


    # Operation not specified.
    *)
        cat <<EOF

No operation defined!

Please run with '--help' for more information.
(TIP: available operations are: 'configure', 'make', or 'shell').

EOF
        exit 1
    ;;
esac