Skip to content

Commit d7dc61f

Browse files
pavoljuhasrht
authored andcommitted
Allow sourcing of dev_tools/pypath from any path (quantumlib#4739)
Also fix its usage from zsh. Avoid changing shell variables `m`, `PREFIX`. Use `python3` instead of `python` in case the latter is not installed.
1 parent 14164cc commit d7dc61f

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

dev_tools/pypath

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,26 @@
1414

1515
#####
1616
# The main file to use to setup your PYTHONPATH. It sets up all cirq modules on the path.
17+
# The file can be sourced from any location with bash or zsh.
1718
# Usage:
1819
# source dev_tools/pypath
1920
#####
2021

2122

22-
#!/usr/bin/bash
23+
if [ -n "${ZSH_VERSION}" ]; then
24+
_PYPATH_BASE_DIR="${${(%):-%x}:a:h:h}"
25+
else
26+
_PYPATH_BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
27+
fi
2328

24-
PREFIX="$(pwd)"
25-
CIRQ_MODULES=$(env PYTHONPATH=. python dev_tools/modules.py list --mode folder)
26-
for m in $CIRQ_MODULES; do
27-
PREFIX="$PREFIX:$(pwd)/$m"
28-
done
29+
_PYPATH_PREFIX="${_PYPATH_BASE_DIR}$(
30+
cd "${_PYPATH_BASE_DIR}" &&
31+
env PYTHONPATH=. python3 dev_tools/modules.py list --mode folder |
32+
xargs printf ":${_PYPATH_BASE_DIR}/%s"
33+
)"
2934

30-
[[ $PYTHONPATH == ${PREFIX}* ]] || export PYTHONPATH="$PREFIX:$PYTHONPATH"
35+
[[ $PYTHONPATH == ${_PYPATH_PREFIX}* ]] ||
36+
export PYTHONPATH="${_PYPATH_PREFIX}${PYTHONPATH:+:}${PYTHONPATH}"
37+
38+
unset _PYPATH_BASE_DIR
39+
unset _PYPATH_PREFIX

0 commit comments

Comments
 (0)