-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-modulefile
More file actions
executable file
·162 lines (136 loc) · 4.39 KB
/
generate-modulefile
File metadata and controls
executable file
·162 lines (136 loc) · 4.39 KB
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
#!/usr/bin/env bash
PROG=$(basename $0)
warn() { echo >&2 "$@"; }
die() {
[[ $# -gt 0 ]] && warn "$@";
exit 1;
}
OPTS_SHORT='hl:'
OPTS_LONG='help,lang:'
print_usage() {
echo "Usage: $0 INSTALL_PREFIX [OPTIONS...]"
echo "Generates a base modulefile by looking for conventional subdirectories."
}
print_help() {
print_usage
cat <<HERE
ARGUMENTS
INSTALL_PREFIX The prefix where a package was installed, which likely has
like subdirectories like bin/, lib/ and/or include/.
This is also where the modulefile will be placed.
OPTIONS
-l, --lang=LANG Modulefile language. Choices:
tcl: Make a legacy TCL Environment Module. (file will be
called 'modulefile')
lua: Make a Lua module for Lmod. (file will be called
'modulefile.lua')
auto: [default] Choose based on the output of `module -v`.
HERE
}
TEMP=$(getopt -o hl: --long help,lang: -n '$PROG' -- "$@")
[ $? != 0 ] && die
eval set -- "$TEMP"
lang=auto
force=
while [ $# -ne 0 ]; do
case "$1" in
-h|--help) print_help; exit 0; shift;;
-l|--lang) lang=$2; shift 2;;
[?]) print_usage >&2; exit 1;;
--) shift; break;;
esac
done
[[ $# -eq 1 ]] || {
print_usage >&2
exit 1
}
dir=$1
# =============================================================================
# validate args
callable() {
which "$1" >/dev/null 2>&1 && return 0
[[ $(type -t "$1") == 'function' ]] && return 0
false
}
detect_language() {
if ! callable 'module'; then
die "'module' command not available; please specify --lang."
fi
if module -v 2>&1 | grep 'based on Lua' >/dev/null; then
echo lua
else
echo tcl
fi
}
if [[ $lang != lua && $lang != tcl && $lang != auto ]]; then
die "bad lang: $lang"
fi
if [[ $lang == auto ]]; then
lang=$(detect_language) || exit 1
fi
[[ -d "$dir" ]] || die "$dir: Path is not a directory"
dir_absolute=$(readlink -f "$dir")
output=$dir/modulefile
if [[ $lang == lua ]]; then
output=$output.lua
fi
if [[ -e $output ]]; then
output=$output.gen
echo >&2 "Writing to $output to avoid clobbering the existing modulefile."
fi
# =============================================================================
# run
if [[ $lang == tcl ]]; then
cat >"$output" <<HERE
#%Module -*- tcl -*-
##
## modulefile
##
proc ModulesHelp { } {
puts stderr "\tuser module"
}
module-whatis "user module"
set root "$dir_absolute"
HERE
[[ $? -eq 0 ]] || exit 1
elif [[ $lang == lua ]]; then
# NOTE: We avoid myModuleFileName in case the file is used via dofile()
cat >"$output" <<HERE
function shell(str)
return string.gsub(subprocess(str), '%s*\$', '')
end
local root = "$dir_absolute"
local version = myModuleVersion()
local pkgName = myModuleFullName()
local hierA = hierarchyA(pkgName, 1)
logUsage(pkgName)
whatis("Description: user module")
whatis(string.format("Version: %s", version))
HERE
[[ $? -eq 0 ]] || exit 1
else
echo >&2 "bad lang: $lang"
exit 1
fi
has_libs () { find "$1" -name '*.so' -or -name '*.a' >/dev/null; }
has_cheader () { find "$1" -name '*.h' >/dev/null; }
prepend() {
echo >&2 "Found something for $1"
if [[ $lang == tcl ]]; then
printf >>"$output" 'prepend-path %-20s $root/%s\n' "$1" "$2" || die "error modifying $1";
elif [[ $lang == lua ]]; then
printf >>"$output" 'prepend_path(%-20s, pathJoin(root, %s))\n' "'$1'" "'$2'" || die "error modifying $1";
else
echo >&2 "bad lang: $lang"
exit 1
fi
}
[[ -e $dir/bin ]] && { prepend PATH 'bin' ; }
[[ -e $dir/lib ]] && has_libs "$dir/lib" && { prepend LIBRARY_PATH 'lib' ; }
[[ -e $dir/lib ]] && has_libs "$dir/lib" && { prepend LD_LIBRARY_PATH 'lib' ; }
[[ -e $dir/lib64 ]] && has_libs "$dir/lib64" && { prepend LIBRARY_PATH 'lib64' ; }
[[ -e $dir/lib64 ]] && has_libs "$dir/lib64" && { prepend LD_LIBRARY_PATH 'lib64' ; }
[[ -e $dir/include ]] && has_cheader "$dir/include" && { prepend CPATH 'include' ; }
[[ -e $dir/share/man ]] && { prepend MANPATH 'share/man' ; }
[[ -e $dir/lib/pkgconfig ]] && { prepend PKG_CONFIG_PATH 'lib/pkgconfig' ; }
true