Skip to content

Commit

Permalink
Allow sourcing of dev_tools/pypath from any path (quantumlib#4739)
Browse files Browse the repository at this point in the history
Also fix its usage from zsh.  Avoid changing shell variables `m`, `PREFIX`.
Use `python3` instead of `python` in case the latter is not installed.
  • Loading branch information
pavoljuhas authored and rht committed May 1, 2023
1 parent 14164cc commit d7dc61f
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions dev_tools/pypath
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,26 @@

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


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

PREFIX="$(pwd)"
CIRQ_MODULES=$(env PYTHONPATH=. python dev_tools/modules.py list --mode folder)
for m in $CIRQ_MODULES; do
PREFIX="$PREFIX:$(pwd)/$m"
done
_PYPATH_PREFIX="${_PYPATH_BASE_DIR}$(
cd "${_PYPATH_BASE_DIR}" &&
env PYTHONPATH=. python3 dev_tools/modules.py list --mode folder |
xargs printf ":${_PYPATH_BASE_DIR}/%s"
)"

[[ $PYTHONPATH == ${PREFIX}* ]] || export PYTHONPATH="$PREFIX:$PYTHONPATH"
[[ $PYTHONPATH == ${_PYPATH_PREFIX}* ]] ||
export PYTHONPATH="${_PYPATH_PREFIX}${PYTHONPATH:+:}${PYTHONPATH}"

unset _PYPATH_BASE_DIR
unset _PYPATH_PREFIX

0 comments on commit d7dc61f

Please sign in to comment.