-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpymultiplexer
executable file
·48 lines (41 loc) · 1.23 KB
/
pymultiplexer
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
#!/bin/bash
# -*- mode: bash -*-
#
# Wrapper for multiplexing a python or pip to use the same version as the most
# recent caller in its process hierarchy.
#
# This lets us have a single command (pip/python) work for both python2 and
# python3 notebooks, eg
# !pip install ...
# will install for the version of python currently in use.
# This is the default: assume python2.
SUFFIX=2
# We know *this* process isn't python, so we start with the parent.
NEXT_PID=${PPID}
while true; do
# Determine where to look up the parent of this process (if it exists).
PID_STAT="/proc/${NEXT_PID}/stat"
if [[ ! -f ${PID_STAT} ]]; then
break
fi
# Figure out the parent PID: proc(5) says this
# is the fourth field in /proc/${NEXT_PID}/stat.
PARENT_PID=$(cut -d\ -f4 <${PID_STAT})
PARENT_EXE="/proc/${PARENT_PID}/exe"
if [[ ! -f ${PARENT_EXE} ]]; then
break
fi
# We want to know the real filename, so resolve any symlinks.
CALLER=$(readlink -fnq ${PARENT_EXE})
if [[ ${CALLER} == *"/bin/python3"* ]]; then
SUFFIX=3
break
elif [[ ${CALLER} == *"/bin/python2"* ]]; then
SUFFIX=2
break
fi
# Repeat with the parent process.
NEXT_PID=${PARENT_PID}
done
COMMAND="$(basename $0)${SUFFIX}"
exec "${COMMAND}" "$@"