forked from enotodden/lj-venv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lj-venv
executable file
·265 lines (213 loc) · 7.74 KB
/
lj-venv
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
#!/usr/bin/env bash
# lj-venv
# =================
# This script installs LuaJIT + LuaRocks in a self contained directory. It is
# originally a 'fork' of my turbo-virtual-env script. It has been updated to
# work with a more general naming scheme for versions of the JIT, including
# modified versions which expose a different user language. These updates are
# not yet robust and there is no testing coverage yet as of 2017/10/16.
# This version has been modified for use at WINDGO Inc. The modifications
# include
# - An additional option -v|--version whereby the git branch to follow is
# specified. For example passing --version 2.0 will result in the invocation
# of git checkout v2.0.
# - The default argument to the version option has been set to 2.1, because
# the features incorporated with the beta in LuaJIT 2.1 are highly useful to
# our work.
# - The "declaration of corniness" due to this script's delivery in the bash
# language has been removed, as there is nothing corny about using a lesser
# set of common dependencies in a bootstrap procedure. This utility should
# NOT be written in Lua.
# - Corrections made to include directory and binary name for arbitrary
# branches. A match is done against the result of 'ls *jit*' using 'find'
# executed with shell aliases discarded to enforce consistent results across
# differing shell configurations.
# NOTE: In a venv with multiple versions of LuaJIT, this will return an
# incorrect result, but doing this would violate the express purpose of
# development from within a virtual environment.
# TODO Handle incompatible GITVERSION and VERSION_CHECKOUT combinations with an
# explicit error.
# TODO Fix the missing shell quotes around variables, as this is generally
# sloppy practice and leads to frustrating errors when pathnames contain
# spaces.
LUAJIT_GITVERSION="2.0"
LUAJIT_VERSION_CHECKOUT="2.1"
LUAJIT_VERSION_BINARY="luajit"
LUAJIT_GIT_URL="http://luajit.org/git/luajit-$LUAJIT_GITVERSION.git"
LUAROCKS_GIT_URL="https://github.com/keplerproject/luarocks.git"
MAKE="make -j`getconf _NPROCESSORS_ONLN`"
set -e;
set -x;
CHECKFOR="readlink git make"
for x in $CHECKFOR; do
which $x > /dev/null;
if [ $? -ne 0 ]; then
echo "Error: Could not find command '$x'."
exit 1
fi
done
usage(){
echo -e "\
** lj-venv **
Usage: lj-venv -c/--create PATH_TO_ENV_DIR
[-r/--luarocks-requirements PATH_TO_LUAROCKS_REQUIREMENTS_FILE]
Example Usage:
\$ cd /path/to/my/project
\$ lj-venv --create ./env
\$ source ./env/bin/activate
Description:
A script to install LuaJIT and LuaRocks together in an 'isolated' environment,
inspired by the virtual-env tool for Python.
- Clones, builds and installs LuaJIT and LuaRocks
- Creates a source-able 'activate' script in DIR_SUPPLIED_ON_COMMAND_LINE/bin/activate
Issues: github.com/enotodden/lj-venv/issues
Source: github.com/entoodden/lj-venv
License: MIT"
}
log () {
echo "lj-venv: $@"
}
luajit_full_version(){
LUAJIT_DIR="$ENVDIR/src/luajit-$LUAJIT_GITVERSION"
LUAJIT_MAJVER=`cat $LUAJIT_DIR/etc/luajit.pc | grep '^majver' | sed 's/majver\s*=\s*//g'`
LUAJIT_MINVER=`cat $LUAJIT_DIR/etc/luajit.pc | grep '^minver' | sed 's/minver\s*=\s*//g'`
LUAJIT_RELVER=`cat $LUAJIT_DIR/etc/luajit.pc | grep '^relver' | sed 's/relver\s*=\s*//g'`
echo "$LUAJIT_MAJVER.$LUAJIT_MINVER.$LUAJIT_RELVER"
}
install_luarocks(){
LUAROCKS_CLONEDIR=/tmp/ljenv-luarocks
LUAROCKS_SRCDIR="$ENVDIR/src/luarocks"
if [ ! -d $LUAROCKS_CLONEDIR ]; then
log "Cloning LuaRocks to $LUAROCKS_CLONEDIR"
git clone $LUAROCKS_GIT_URL $LUAROCKS_CLONEDIR
else
log "Found LuaRocks in $LUAROCKS_CLONEDIR, not cloning repo."
fi
log "Copying files"
mkdir -p $LUAROCKS_SRCDIR
cp -rvf $LUAROCKS_CLONEDIR/* $LUAROCKS_SRCDIR/
log "Building LuaRocks"
$LUAROCKS_SRCDIR/configure --prefix=$ENVDIR --force-config --with-lua=$ENVDIR --with-lua-include=$ENVDIR/include/luajit-$LUAJIT_VERSION_CHECKOUT
mv config.unix $LUAROCKS_SRCDIR
$MAKE -C $LUAROCKS_SRCDIR build
log "Installing LuaRocks"
$MAKE -C $LUAROCKS_SRCDIR install
}
install_luajit(){
LUAJIT_CLONEDIR="/tmp/ljenv-luajit-$LUAJIT_GITVERSION"
LUAJIT_SRCDIR="$ENVDIR/src/luajit-$LUAJIT_GITVERSION"
if [ ! -d $LUAJIT_CLONEDIR ]; then
log "Cloning LuaJIT from $LUAJIT_GIT_URL to $LUAJIT_CLONEDIR"
git clone $LUAJIT_GIT_URL $LUAJIT_CLONEDIR
else
log "Found LuaJIT in $LUAJIT_CLONEDIR, not cloning repo."
fi
# 2017/10/16 - William Whitacre BEGIN
pushd "$LUAJIT_CLONEDIR"
log "Entered $LUAJIT_CLONEDIR."
log "Switching to branch "\'"$LUAJIT_VERSION_CHECKOUT"\'"."
git checkout v"$LUAJIT_VERSION_CHECKOUT"
log "Ensuring up-to-date with remote."
git pull origin master
popd
log "Returned to `pwd`."
# 2017/10/16 - William Whitacre END
log "Copying files"
mkdir -p $LUAJIT_SRCDIR
cp -rvf $LUAJIT_CLONEDIR/* $LUAJIT_SRCDIR
log "Building LuaJIT"
$MAKE -C $LUAJIT_SRCDIR PREFIX=$ENVDIR
log "Installing LuaJIT to $ENVDIR"
$MAKE -C $LUAJIT_SRCDIR install PREFIX=$ENVDIR
# 2017/10/16 - William Whitacre - This one liner will recognize binaries
# which are non-conforming to the downstream versioning scheme, which is
# required to obtain correct results when using a beta release, as well as
# in the case that one uses a custom build which is deliberately renamed
# in order to avoid conflicts or confusion with the mainline downstream.
LUAJIT_BINNAME=`(cd "$ENVDIR/bin"; for f in $(\ls *jit*) ; do /usr/bin/test -x $f -a ! -d $f && echo $f ; done)`
ln -sf "$ENVDIR/bin/$LUAJIT_BINNAME" "$ENVDIR/bin/lua"
}
install_luarocks_requirements() {
log "Installing luarocks from $LUAROCKS_REQUIREMENTS_FILE"
source $ENVDIR/bin/activate
for line in `cat $LUAROCKS_REQUIREMENTS_FILE`; do
luarocks install $line
done
}
write_activate_script(){
log "Writing activation script to $ENVDIR/bin/activate"
echo -e '
if [ "$LJ_VENV" = "'"$ENVDIR"'" ]; then
echo "Already activated!"
else
export LJ_VENV='"$ENVDIR"'
export LJ_VENV_OLD_PATH="$PATH"
export PATH="$LJ_VENV/bin:$LJ_VENV/lib:$PATH"
export LJ_VENV_OLD_LD_LIBRARY_PATH="$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="$LJ_VENV/lib:$LJ_VENV/lib/lua/5.1:$LD_LIBRARY_PATH"
deactivate() {
export PATH="$LJ_VENV_OLD_PATH"
export LD_LIBRARY_PATH="$LJ_VENV_OLD_LD_LIBRARY_PATH"
unset LJ_VENV
if [ ! -z "$LJ_VENV_OLD_PROMPT" ]; then
export PS1="$LJ_VENV_OLD_PROMPT"
fi
unset LJ_VENV_OLD_PROMPT
unset LJ_VENV_OLD_PATH
unset LJ_VENV_OLD_LD_LIBRARY_PATH
}
export LJ_VENV_OLD_PROMPT="$PS1";
if [ -z "$LJ_VENV_DISABLE_PROMPT" ]; then
export PS1="ljenv -> $PS1";
fi
fi
' > $ENVDIR/bin/activate
}
create(){
if [ -d $ENVDIR ]; then
if [ ! -f "$ENVDIR/.lj-venv" ]; then
echo "Directory '$ENVDIR' already exists and is not a lj-venv directory."
exit 1
fi
fi
log "Creating directories"
mkdir -p "$ENVDIR/bin"
mkdir -p "$ENVDIR/src"
touch "$ENVDIR/.lj-venv"
write_activate_script
install_luajit
install_luarocks
if [ ! -z "$LUAROCKS_REQUIREMENTS_FILE" ]; then
install_luarocks_requirements
fi
log "All done, please source $ENVDIR/bin/activate"
}
ARGS=`getopt -o "hc:d:r:" -l "help,create:,luarocks-requirements:" -n "lj-venv" -- "$@"`
eval set -- "$ARGS"
ACTION=""
while true; do
case "$1" in
-h|--help)
usage
exit
shift;;
-r|--luarocks-requirements)
LUAROCKS_REQUIREMENTS_FILE=`readlink -f "$2"`
shift 2;;
-v|--version)
LUAROCKS_VERSION_CHECKOUT="$2"
shift 2;;
-c|--create)
ENVDIR=`readlink -f "$2"`
ACTION="create"
shift 2;;
--)
shift
break;;
esac
done
if [ "$ACTION" = "create" ]; then
create
else
usage
fi