-
Notifications
You must be signed in to change notification settings - Fork 4
/
scaffold
executable file
·125 lines (104 loc) · 3.79 KB
/
scaffold
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
#!/bin/bash
#
# scaffold sets up the scaffolding for a new subject.
# Example command is:
#
# ./scaffold new_subject
#
# This will create $PROJ_DIR/subjects/new_subject/
#
# If you make a new function in the $PROJ_DIR/prototype/link directory and you want
# that to be available in a participant's directory then run
# `./scaffold new_subject` and it will copy over to the participant
# directory. Because that file is linked, you can then make edits to either
# trace
#
# This should mean that scaffolding NEVER deletes or overwrites anything and thus can be done whenever
#
# If you want to run this on all participants than use:
# ./scripts/re-scaffold.sh
#
# Copyright (C) 2010 mason simon ([email protected])
#
# 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/>.
set -e # stop immediately when an error occurs
#source globals.sh
SUBJECTS_DIR=subjects/
# set global variables
TEMPLATE_DIR=prototype
LINK_DIR=$TEMPLATE_DIR/link
COPY_DIR=$TEMPLATE_DIR/copy
SUBJ_ID_PATTERN='<<SUBJECT_ID>>'
# display usage instructions if this script was run improperly
if [ $# -ne 1 ]; then
echo "
usage: `basename $0` subject_id
sets up the directory structure and default files (scaffolding) for the subject
with given ID, based on the template in $TEMPLATE_DIR.
if scaffold has already been called for that subject, the subject's directory
will be updated with any files and folders added to the template since the
subject was last scaffolded
"
exit
fi
# stick parameters into the right variables
subj=$1
subj_dir=$SUBJECTS_DIR/$subj
if [ ! -d "$SUBJECTS_DIR" ]; then
echo "if this is your first time setting up a subject with NeuroPipe,"
echo "read README.txt in the subject's directory (just created)"
fi
mkdir -p $subj_dir
# link in each file in LINK_DIR, making their parent directories as needed. to
# keep things portable, the code makes relative links, which complicates the
# logic. see the notes within this loop for more explanation
for path in $(find $LINK_DIR/*); do
rel_path=${path##$LINK_DIR/} # removes $LINK_DIR from front of path
rel_dir=$(dirname $rel_path)
if [ -d "$path" ]; then
mkdir -p $subj_dir/$rel_path
continue
fi
# because we're recursing into LINK_DIR, we have to build up a prefix of
# ../'s that gets one longer for each directory we dive into. that's what the
# following hunk of code does
p=$path
prefix=''
while [ "$(dirname $p)" != '.' ]; do
prefix="$prefix../"
p=$(dirname $p)
done
ln -s -f ${prefix}$path $subj_dir/$rel_path
done
# copy in each file in COPY_DIR, making their parent directories as needed, and
# replacing all instances of $SUBJ_ID_PATTERN with the subject's ID
for path in $(find $COPY_DIR/*); do
rel_path=${path##$COPY_DIR/}
rel_dir=$(dirname $rel_path)
mkdir -p $rel_dir
if [ -d $path ]; then
mkdir -p $subj_dir/$rel_path
continue
fi
tmp=$(mktemp -t tmp.XXXXXX)
cat $path | sed "s:$SUBJ_ID_PATTERN:$subj:g" > $tmp;
if [ -e "$subj_dir/$rel_path" ] && [ -n "$(diff $tmp $subj_dir/$rel_path)" ]; then
echo "WARNING: $rel_path has been modified. not updating it" > /dev/stderr
rm -f $tmp
continue
fi
mv $tmp $subj_dir/$rel_path
chmod 770 $subj_dir/$rel_path #Change the directory permission
done
echo "created subject directory '$subj_dir'"