-
Notifications
You must be signed in to change notification settings - Fork 1
/
wcdo.sh
executable file
·397 lines (316 loc) · 9.86 KB
/
wcdo.sh
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
#!/bin/bash
# These may be overridden in $HOME/.wcdo/main-local.sh
wcdo_image_cache="."
wcdo_image_url="https://www.phy.bnl.gov/~bviren/simg"
wcdo-get-image () {
local siname="$1"; shift
if [ -z "$siname" ] ; then
echo "usage: wcdo get-image <name> [dir [url]]" 1>&2
echo -e "\t<name>\tan Singularity image name" 1>&2
echo -e "\t<dir>\ta local directory to find images" 1>&2
echo -e "\t<url>\tbase URL to find images" 1>&2
return
fi
local sifile="${siname}.simg"
if [[ "$siname" =~ \.simg$ ]] ; then
sifile="$siname"
fi
if [ -f $(basename $sifile) ] ; then
echo "Image already exists" 1>&2
return
fi
if [ -f "$sifile" ] ; then
ln -s $sifile .
return
fi
local cache="${1:-$wcdo_image_cache}" ; shift
local baseurl="${1:-$wcdo_image_url}" ; shift
local bsifile="$(basename $sifile)"
local sitmp="$cache/$bsifile"
if [ ! -f "$sitmp" ] ; then
wget -O "$sitmp" "$baseurl/$bsifile" 1>&2
fi
ln -sf "$sitmp" .
}
# These semantically identified directories need to be synced on both
# native and container side. They get baked into the generated rc file below.
wct_dev="src/wct"
mrb_dev="src/mrb"
wct_data="share/wirecell/data"
wct_cfg="share/wirecell/cfg"
ups_products="lib/ups"
# Initialize current directory as a workspace project
wcdo-init () {
if [ ! -d "$mrb_dev" ] ; then mkdir -p "$mrb_dev" ; fi
if [ ! -d "$ups_products/.upsfiles" ] ; then
echo "Priming UPS area"
mkdir -p "$ups_products/.upsfiles"
cat > "$ups_products/.upsfiles/dbconfig" <<EOF
FILE = DBCONFIG
AUTHORIZED_NODES = *
VERSION_SUBDIR = 1
PROD_DIR_PREFIX = \${UPS_THIS_DB}
UPD_USERCODE_DIR = \${UPS_THIS_DB}/.updfiles
EOF
fi
if [ ! -d "$ups_products/.updfiles" ] ; then
mkdir -p "$ups_products/.updfiles"
cat > "$ups_products/.updfiles/updconfig" <<EOF
File = updconfig
GROUP:
product = ANY
flavor = ANY
qualifiers = ANY
options = ANY
dist_database = ANY
dist_node = ANY
COMMON:
UPS_THIS_DB = "\${UPD_USERCODE_DB}"
UPS_PROD_DIR = "\${UPS_PROD_NAME}/\${UPS_PROD_VERSION}/\${DASH_PROD_FLAVOR}\${DASH_PROD_QUALIFIERS}"
UNWIND_PROD_DIR = "\${PROD_DIR_PREFIX}/\${UPS_PROD_DIR}"
UPS_UPS_DIR = "ups"
UNWIND_UPS_DIR = "\${UNWIND_PROD_DIR}/\${UPS_UPS_DIR}"
UPS_TABLE_FILE = "\${UPS_PROD_NAME}.table"
UNWIND_TABLE_DIR = "\${UNWIND_UPS_DIR}"
END:
EOF
cat > "$ups_products/.updfiles/updusr.pm" <<EOF
require 'default_updusr.pm';
EOF
fi
if [ -f wcdo.rc ] ; then
echo "A wcdo.rc already exists. Move it aside to get a fresh copy".
return
fi
echo "Getting wcdo.rc from GitHub"
wget https://raw.githubusercontent.com/WireCell/wire-cell-singularity/master/wcdo.rc
}
wcdo-wct-one () {
local name="$1" ; shift # required, "data", "cfg", etc.
local dir="$1" ; shift # required
local acc="${1:-anonymous}" ;shift
local br="${1:-master}" ;shift
if [ ! -d "$dir" ] ; then
mkdir -p "$dir"
fi
pushd "$dir"
if [ ! -d .git ] ; then
git init
fi
# start from known state
git remote remove origin 2>/dev/null || true
giturl="[email protected]:WireCell/wire-cell-${name}.git"
if [ "$acc" = "anonymous" ] ; then
giturl="https://github.com/WireCell/wire-cell-${name}.git"
fi
git remote add --tags origin "$giturl"
git fetch
git checkout $br
popd
}
wcdo-wct-data () {
local acc="${1:-anonymous}" ;shift
local br="${1:-master}" ;shift
if [ $br != "master" ] ; then
echo "WCT data doesn't have branches, forcing master instead of $br"
br="master"
fi
wcdo-wct-one data "$wct_data" "$acc" "$br"
}
wcdo-wct-cfg () {
local acc="${1:-anonymous}" ;shift
local br="${1:-master}" ;shift
wcdo-wct-one cfg "$wct_cfg" "$acc" "$br"
}
wcdo-wct-source () {
local acc="${1:-anonymous}" ;shift
local br="${1:-master}" ;shift
wcdo-wct-one toolkit "$wct_dev" "$acc" "$br"
# wcdo-wct-one build "$wct_dev" "$acc" "$br"
# pushd "$wct_dev"
# if [ "$acc" = "anonymous" ] ; then
# ./switch-git-urls anon
# else
# ./switch-git-urls dev
# fi
# git checkout -b $br $br
# git submodule init
# git submodule update
# if [ "$br" != "master" ] ; then
# git submodule foreach git checkout -b $br origin/$br
# else
# git submodule foreach git checkout master
# fi
# git submodule foreach git pull origin $br
# popd
}
wcdo-wct () {
local acc="${1:-anonymous}" ;shift
local br="${1:-master}" ;shift
wcdo-wct-data "$acc" "$br"
wcdo-wct-cfg "$acc" "$br"
wcdo-wct-source "$acc" "$br"
}
# Make a project in the current workspace current directory
# This creates wcdo-*.sh and wcdo-*.rc files
wcdo-make-project () {
local name="$1" ; shift
local simage="$1" ; shift
local morebindings=$@
if [ -z "$simage" ] ; then
echo "usage wcdo-make-project <name> <image>"
echo -e "\t<name>\tsome short name for the project"
echo -e "\t<image>\ta Singularity image file"
return
fi
wcdo-get-image $simage
local siname=$(basename $simage .simg)
simage="${siname}.simg" # wash
local here=$(pwd)
# Bind workspace and any extras
local bindings="${here}:/wcdo"
local wcdo_init=""
if [ -d /cvmfs ] ; then
bindings="$bindings /cvmfs"
wcdo_init="source /cvmfs/larsoft.opensciencegrid.org/products/setup"
fi
for one in $morebindings
do
bindings="$bindings $one"
done
rcfile="wcdo-${name}.rc"
shfile="wcdo-${name}.sh"
touch $shfile
chmod +w $shfile
cat <<EOF > "$shfile"
#!/bin/bash
# Run this to enter an image for project $name.
# This file is generated.
wcdo_image=${here}/${simage}
wcdo_generated_bindings="$bindings"
wcdo_bindings=""
wcdo_rcfile=${here}/${rcfile}
# Given shared hook, eg to add singularity location to PATH
# Or, override some wcdo_* variables
for script in \${HOME}/.wcdo/project-local.sh ${here}/wcdo-local.sh ${here}/wcdo-local-${name}.sh
do
if [ -f \$script ] ; then
source \$script
fi
done
bindargs=""
for one in \$wcdo_bindings \$wcdo_generated_bindings
do
bindargs="\$bindargs --bind \$one"
done
cmd="singularity exec \$bindargs \$wcdo_image env -i /bin/bash --rcfile \$wcdo_rcfile"
if [ "\$1" = "bundle" ] ; then
keep=""
for one in ${here}/wcdo.rc ${here}/wcdo-${name}.rc ${here}/wcdo-${name}.sh ${here}/wcdo-local-${name}.rc ${here}/wcdo-local-${name}.sh \$HOME/.wcdo/main-local.sh \$HOME/.wcdo/project-local.rc \$HOME/.wcdo/project-local.sh
do
if [ ! -f \$one ] ; then
continue
fi
keep="\$keep \$(readlink -f \$one)"
done
bname="wcdo-bundle-${name}"
tar -czf \${bname}.tgz \$keep
sha1sum \$wcdo_image \${bname}.tgz > ${here}/\${bname}.txt
echo \$cmd >> ${here}/\${bname}.txt
ls -l \${bname}.*
exit
fi
echo \$cmd
\$cmd
EOF
chmod +x $shfile
echo "Generated $shfile, don't edit"
chmod 555 $shfile
local lrcfile="wcdo-local-${name}.rc"
if [ -f "$lrcfile" ] ; then
echo "Local rc file exists: $lrcfile"
else
cat <<EOF > $lrcfile
#!/bin/bash
# This is a local wcdo rc file for project ${name}.
# It was initally generated but is recomended for customizing by you, dear user.
# It is included at the end of the main RC files.
# These are optional but required if wcdo-mrb-* commands are to be used.
wcdo_mrb_project_name=""
wcdo_mrb_project_version=""
wcdo_mrb_project_quals=""
# Additional variables may be usefully set since this file was
# first generated.
# It is perhaps useful to end this with some command to be called
# on each entry to the contaner.
# $wcdo_init
EOF
echo "Generated $lrcfile, please edit"
chmod 644 $lrcfile
fi
touch $rcfile
chmod +w $rcfile
cat <<EOF > "$rcfile"
#!/bin/bash
# This is a bash RC file for project $name.
# It is generated and should NOT be edited.
# Instead, create and edit wcdo-local.rc for
# all projects or wcdo-local-${name}.rc for this one.
# Give the container a name, eg for the shell prompt.
wcdo_simg="$siname"
# Canonical locations inside native-side $wcdo_workspace
wcdo_wct_dev=/wcdo/$wct_dev
wcdo_mrb_dev=/wcdo/$mrb_dev
wcdo_wct_data=/wcdo/$wct_data
wcdo_wct_cfg=/wcdo/$wct_cfg
wcdo_ups_products=/wcdo/$ups_products
# prime this
WIRECELL_PATH=${wcdo_wct_data}:${wcdo_wct_cfg}
# Some limited chunks of user environment to pass through.
PATH=/bin:/usr/bin:/usr/local/bin:/wcdo/bin
LD_LIBRARY_PATH=/usr/local/lib:/wcdo/lib
export TERM="xterm-color"
export DISPLAY="$DISPLAY"
export PAGER="$PAGER"
export EDITOR="$EDITOR"
export LANG=C
export HOME="$HOME"
export LOGNAME="$LOGNAME"
export USER="$USER"
export XAUTHORITY="$XAUTHORITY"
# Finally include the set of wcdo-* functions.
source /wcdo/wcdo.rc
# Hook in more global rc files:
for maybe in \$HOME/.wcdo/project-local.rc /wcdo/wcdo-local.rc /wcdo/wcdo-local-${name}.rc
do
if [ -f \$maybe ] ; then
source \$maybe
fi
done
EOF
echo "Generated $rcfile, don't edit"
chmod 444 $rcfile
}
wcdo-help () {
cat <<EOF
This script has several command and is run like:
wcdo.sh <cmd> [cmd options]
All commands assume current working directory is a "wcdo workspace".
The commands are:
init
initialize current directory as a wcdo workspace
wct [access [branch]]
Get Wire-Cell Toolkit source, data and configuration
get-image <imagename> [cachedir [url]]
Add a Singularity image by name to current directory
make-project <projname> <imagename> [bindings]
Make files to run and configure a project in the current directory
For details see:
https://github.com/WireCell/wire-cell-singularity/blob/master/wcdo.org
EOF
}
if [ -f $HOME/.wcdo/main-local.sh ] ; then
source $HOME/.wcdo/main-local.sh
fi
cmd="${1:-help}"; shift
wcdo-$cmd $@