diff --git a/check/all b/check/all index ed6741dc1e1..50b9ff107f4 100755 --- a/check/all +++ b/check/all @@ -31,8 +31,9 @@ ################################################################################ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? # Parse arguments. apply_arg="" @@ -44,7 +45,7 @@ for arg in "$@"; do elif [[ "${arg}" == "--apply-format-changes" ]]; then apply_arg="--apply" elif [ -z "${rev}" ]; then - if [ "$(git cat-file -t ${arg} 2> /dev/null)" != "commit" ]; then + if [ "$(git cat-file -t "${arg}" 2> /dev/null)" != "commit" ]; then echo -e "\033[31mNo revision '${arg}'.\033[0m" >&2 exit 1 fi diff --git a/check/asv_run b/check/asv_run index 7ac4e005444..5b2d33350b4 100755 --- a/check/asv_run +++ b/check/asv_run @@ -8,7 +8,8 @@ ################################################################################ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? asv run "$@" diff --git a/check/build-changed-protos b/check/build-changed-protos index cd234f12de0..ad5c39a1641 100755 --- a/check/build-changed-protos +++ b/check/build-changed-protos @@ -26,12 +26,13 @@ ################################################################################ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? # Figure out which revision to compare against. -if [ ! -z "$1" ] && [[ $1 != -* ]]; then - if [ "$(git cat-file -t $1 2> /dev/null)" != "commit" ]; then +if [ -n "$1" ] && [[ $1 != -* ]]; then + if [ "$(git cat-file -t "$1" 2> /dev/null)" != "commit" ]; then echo -e "\033[31mNo revision '$1'.\033[0m" >&2 exit 1 fi @@ -63,7 +64,7 @@ dev_tools/build-protos.sh # but the error logic will still work. uncommitted=$(git status --porcelain 2>/dev/null | grep -E "^?? cirq-google" | cut -d " " -f 3) -if [[ ! -z "$uncommitted" ]]; then +if [[ -n "$uncommitted" ]]; then echo -e "\033[31mERROR: Uncommitted generated files found! Please generate and commit these files using dev_tools/build-protos.sh:\033[0m" for generated in $uncommitted do diff --git a/check/doctest b/check/doctest index d839a23b2ce..a26b6cff433 100755 --- a/check/doctest +++ b/check/doctest @@ -14,8 +14,9 @@ ################################################################################ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? source dev_tools/pypath diff --git a/check/format-incremental b/check/format-incremental index 947b7d0254f..eca80ac4499 100755 --- a/check/format-incremental +++ b/check/format-incremental @@ -32,8 +32,9 @@ ################################################################################ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? # Parse arguments. @@ -46,7 +47,7 @@ for arg in "$@"; do elif [[ "${arg}" == "--all" ]]; then only_changed=0 elif [ -z "${rev}" ]; then - if [ "$(git cat-file -t ${arg} 2> /dev/null)" != "commit" ]; then + if [ "$(git cat-file -t "${arg}" 2> /dev/null)" != "commit" ]; then echo -e "\033[31mNo revision '${arg}'.\033[0m" >&2 exit 1 fi @@ -82,7 +83,7 @@ if (( only_changed == 1 )); then # Get the modified, added and moved python files. IFS=$'\n' read -r -d '' -a format_files < \ - <(git diff --name-only --diff-filter=MAR ${rev} -- '*.py' ':(exclude)cirq-google/cirq_google/cloud/*' ':(exclude)*_pb2.py') + <(git diff --name-only --diff-filter=MAR "${rev}" -- '*.py' ':(exclude)cirq-google/cirq_google/cloud/*' ':(exclude)*_pb2.py') else echo -e "Formatting all python files." >&2 IFS=$'\n' read -r -d '' -a format_files < \ diff --git a/check/misc b/check/misc index 795f1fb55d9..1ad4aa33378 100755 --- a/check/misc +++ b/check/misc @@ -8,8 +8,9 @@ ################################################################################ # Get the working directory to the repo root. -cd "$( dirname "${BASH_SOURCE[0]}" )" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? # Check for non-contrib references to contrib. results=$(grep -Rl "\bcirq\.contrib\b" cirq-core | grep -v "cirq/contrib" | grep -v "__") diff --git a/check/mypy b/check/mypy index f19f4f06a67..81b54726d01 100755 --- a/check/mypy +++ b/check/mypy @@ -8,15 +8,17 @@ ################################################################################ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? CONFIG_FILE='mypy.ini' -CIRQ_PACKAGES=$(env PYTHONPATH=. python dev_tools/modules.py list --mode package-path) +read -r -a CIRQ_PACKAGES < \ + <(env PYTHONPATH=. python dev_tools/modules.py list --mode package-path) echo -e -n "\033[31m" -mypy --config-file=dev_tools/conf/$CONFIG_FILE "$@" $CIRQ_PACKAGES dev_tools examples +mypy --config-file=dev_tools/conf/$CONFIG_FILE "$@" "${CIRQ_PACKAGES[@]}" dev_tools examples result=$? echo -e -n "\033[0m" diff --git a/check/nbformat b/check/nbformat index b4aab40a817..19749d8c81d 100755 --- a/check/nbformat +++ b/check/nbformat @@ -25,8 +25,9 @@ for arg in "$@"; do done # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? pip show tensorflow-docs > /dev/null || exit 1 @@ -35,7 +36,7 @@ FORMAT_CMD="python3 -m tensorflow_docs.tools.nbfmt --indent=1" # Test the notebooks unformatted=$($FORMAT_CMD --test docs 2>&1 | grep "\- docs" || true) needed_changes=0 -if [ ! -z "${unformatted}" ]; then +if [ -n "${unformatted}" ]; then needed_changes=1 if (( only_print == 0 )); then $FORMAT_CMD docs diff --git a/check/npm b/check/npm index a685af4fc52..03aa252d8d3 100755 --- a/check/npm +++ b/check/npm @@ -23,7 +23,8 @@ ################################################################################ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? npm --prefix 'cirq-web/cirq_ts' "$@" diff --git a/check/npx b/check/npx index bc2bf65c95b..c0e2a589c5b 100755 --- a/check/npx +++ b/check/npx @@ -22,8 +22,8 @@ ################################################################################ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? -cd 'cirq-web/cirq_ts' +cd "${topdir}/cirq-web/cirq_ts" || exit $? npx "$@" diff --git a/check/pylint b/check/pylint index 0c869934e56..4fb05d7e556 100755 --- a/check/pylint +++ b/check/pylint @@ -8,10 +8,12 @@ ################################################################################ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? -CIRQ_MODULES=$(env PYTHONPATH=. python dev_tools/modules.py list --mode package-path) +read -r -a CIRQ_MODULES < \ + <(env PYTHONPATH=. python dev_tools/modules.py list --mode package-path) # Add dev_tools to $PYTHONPATH so that pylint can find custom checkers -env PYTHONPATH=dev_tools pylint --jobs=0 --rcfile=dev_tools/conf/.pylintrc "$@" $CIRQ_MODULES dev_tools examples +env PYTHONPATH=dev_tools pylint --jobs=0 --rcfile=dev_tools/conf/.pylintrc "$@" "${CIRQ_MODULES[@]}" dev_tools examples diff --git a/check/pylint-changed-files b/check/pylint-changed-files index c70a087e961..4cbe78ef125 100755 --- a/check/pylint-changed-files +++ b/check/pylint-changed-files @@ -25,12 +25,13 @@ ################################################################################ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? # Figure out which revision to compare against. -if [ ! -z "$1" ] && [[ $1 != -* ]]; then - if [ "$(git cat-file -t $1 2> /dev/null)" != "commit" ]; then +if [ -n "$1" ] && [[ $1 != -* ]]; then + if [ "$(git cat-file -t "$1" 2> /dev/null)" != "commit" ]; then echo -e "\033[31mNo revision '$1'.\033[0m" >&2 exit 1 fi @@ -55,7 +56,7 @@ fi typeset -a changed IFS=$'\n' read -r -d '' -a changed < \ - <(git diff --name-only ${rev} -- '*.py' ':(exclude)cirq-google/cirq_google/cloud/*' ':(exclude)*_pb2.py' \ + <(git diff --name-only "${rev}" -- '*.py' ':(exclude)cirq-google/cirq_google/cloud/*' ':(exclude)*_pb2.py' \ | grep -E "^(cirq|dev_tools|examples).*.py$" ) diff --git a/check/pytest b/check/pytest index a0e73e0dd1f..72c439b4ec3 100755 --- a/check/pytest +++ b/check/pytest @@ -12,8 +12,9 @@ ################################################################################ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? # Run in parallel by default. Pass the `-n0` option for a single-process run. # (the last `-n` option wins) diff --git a/check/pytest-and-incremental-coverage b/check/pytest-and-incremental-coverage index abbb05f2a70..9fa1c2b8eb7 100755 --- a/check/pytest-and-incremental-coverage +++ b/check/pytest-and-incremental-coverage @@ -79,7 +79,7 @@ pytest_result=$? # assume successful cover_result in case coverage is not run cover_result=0 -if (( $ANALYZE_COV )); then +if (( ANALYZE_COV )); then # Convert to .py,cover files. coverage annotate @@ -92,7 +92,7 @@ if (( $ANALYZE_COV )); then fi # Report result. -if (( ${pytest_result} || ${cover_result} )); then +if (( pytest_result || cover_result )); then exit 1 fi exit 0 diff --git a/check/pytest-changed-files b/check/pytest-changed-files index f7dbacd5b94..df89599ebba 100755 --- a/check/pytest-changed-files +++ b/check/pytest-changed-files @@ -28,18 +28,19 @@ ################################################################################ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? # Figure out which branch to compare against. -rest=$@ -if [ ! -z "$1" ] && [[ $1 != -* ]]; then - if [ "$(git cat-file -t $1 2> /dev/null)" != "commit" ]; then +rest=( "$@" ) +if [ -n "$1" ] && [[ $1 != -* ]]; then + if [ "$(git cat-file -t "$1" 2> /dev/null)" != "commit" ]; then echo -e "\033[31mNo revision '$1'.\033[0m" >&2 exit 1 fi rev=$1 - rest=${@:2} + rest=( "${@:2}" ) elif [ "$(git cat-file -t upstream/master 2> /dev/null)" == "commit" ]; then rev=upstream/master elif [ "$(git cat-file -t origin/master 2> /dev/null)" == "commit" ]; then @@ -77,4 +78,4 @@ fi source dev_tools/pypath -pytest ${rest} "${changed[@]}" +pytest "${rest[@]}" "${changed[@]}" diff --git a/check/pytest-changed-files-and-incremental-coverage b/check/pytest-changed-files-and-incremental-coverage index 200a1fc22a7..56fa26a6cc4 100755 --- a/check/pytest-changed-files-and-incremental-coverage +++ b/check/pytest-changed-files-and-incremental-coverage @@ -33,8 +33,8 @@ cd "$( dirname "${BASH_SOURCE[0]}" )" || exit 1 cd "$(git rev-parse --show-toplevel)" || exit 1 # Figure out which revision to compare against. -if [ ! -z "$1" ] && [[ $1 != -* ]]; then - if [ "$(git cat-file -t $1 2> /dev/null)" != "commit" ]; then +if [ -n "$1" ] && [[ $1 != -* ]]; then + if [ "$(git cat-file -t "$1" 2> /dev/null)" != "commit" ]; then echo -e "\033[31mNo revision '$1'.\033[0m" >&2 exit 1 fi diff --git a/check/ts-build b/check/ts-build index 504342dff4e..3066b3c7507 100755 --- a/check/ts-build +++ b/check/ts-build @@ -22,7 +22,8 @@ ################################################################################ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? check/npx webpack --mode production "$@" diff --git a/check/ts-build-current b/check/ts-build-current index 0c6352b5f89..ced3b389ec9 100755 --- a/check/ts-build-current +++ b/check/ts-build-current @@ -26,7 +26,7 @@ check/ts-build # Find the changed bundle.js files, if any untracked=$(git status --porcelain 2>/dev/null | grep "cirq-web/cirq_ts/dist/" | cut -d " " -f 3) -if [[ ! -z "$untracked" ]]; then +if [[ -n "$untracked" ]]; then echo -e "\033[31mERROR: Uncommitted changes to bundle file(s) found! Please commit these files:\033[0m" for generated in $untracked do @@ -35,4 +35,4 @@ if [[ ! -z "$untracked" ]]; then exit 1 fi -exit 0 \ No newline at end of file +exit 0 diff --git a/check/ts-coverage b/check/ts-coverage index 191fd583e78..5f96d3e8095 100755 --- a/check/ts-coverage +++ b/check/ts-coverage @@ -22,7 +22,8 @@ ################################################################################ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? check/npm run coverage "$@" diff --git a/check/ts-lint b/check/ts-lint index 515928cdb10..a9a5b5f1d09 100755 --- a/check/ts-lint +++ b/check/ts-lint @@ -22,7 +22,8 @@ ################################################################################ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? check/npm run lint "$@" diff --git a/check/ts-lint-and-format b/check/ts-lint-and-format index efc9b7df0c0..d824cb1bd15 100755 --- a/check/ts-lint-and-format +++ b/check/ts-lint-and-format @@ -22,7 +22,8 @@ ################################################################################ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? check/npm run fix "$@" diff --git a/check/ts-test b/check/ts-test index 980aed9600f..a21e6ff00ac 100755 --- a/check/ts-test +++ b/check/ts-test @@ -24,7 +24,8 @@ ################################################################################ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? check/npm run test "$@" diff --git a/check/ts-test-e2e b/check/ts-test-e2e index a1eb89be363..3b6888a8d0b 100755 --- a/check/ts-test-e2e +++ b/check/ts-test-e2e @@ -24,7 +24,8 @@ ################################################################################ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -cd "$(git rev-parse --show-toplevel)" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? check/npm run test-e2e "$@" diff --git a/dev_tools/conf/pip-install-minimal-for-pytest-changed-files.sh b/dev_tools/conf/pip-install-minimal-for-pytest-changed-files.sh index 1d0ed3bf3cd..f0388511c27 100755 --- a/dev_tools/conf/pip-install-minimal-for-pytest-changed-files.sh +++ b/dev_tools/conf/pip-install-minimal-for-pytest-changed-files.sh @@ -20,10 +20,10 @@ set -e cd "$( dirname "${BASH_SOURCE[0]}" )" cd "$(git rev-parse --show-toplevel)" -REQS="-r dev_tools/requirements/pytest-minimal.env.txt" +reqs=( -r dev_tools/requirements/pytest-minimal.env.txt ) # Install contrib requirements only if needed. changed=$(git diff --name-only origin/master | grep "cirq/contrib" || true) -[ "${changed}" = "" ] || REQS="$REQS -r cirq-core/cirq/contrib/requirements.txt" +[ "${changed}" = "" ] || reqs+=( -r cirq-core/cirq/contrib/requirements.txt ) -pip install $REQS \ No newline at end of file +pip install "${reqs[@]}" diff --git a/dev_tools/docs/build-rtd-docs.sh b/dev_tools/docs/build-rtd-docs.sh index e53fc809112..93065b61719 100755 --- a/dev_tools/docs/build-rtd-docs.sh +++ b/dev_tools/docs/build-rtd-docs.sh @@ -33,7 +33,7 @@ set -e trap "{ echo -e '\033[31mFAILED\033[0m'; }" ERR -[[ $1 == 'fast' ]] && CPUS='-j auto' || CPUS='' +[[ $1 == 'fast' ]] && cpus=( -j auto ) || cpus=( ) # Get the working directory to the repo root. cd "$(git rev-parse --show-toplevel)"/rtd_docs @@ -48,7 +48,7 @@ rm -rf "${docs_conf_dir}/generated" rm -rf "${out_dir}" # Regenerate docs. -sphinx-build -M html "${docs_conf_dir}" "${out_dir}" -W --keep-going $CPUS +sphinx-build -M html "${docs_conf_dir}" "${out_dir}" -W --keep-going "${cpus[@]}" # Cleanup newly generated temporary files. rm -rf "${docs_conf_dir}/generated" diff --git a/dev_tools/packaging/generate-dev-version-id.sh b/dev_tools/packaging/generate-dev-version-id.sh index 147509c25b2..afbaf8351f7 100755 --- a/dev_tools/packaging/generate-dev-version-id.sh +++ b/dev_tools/packaging/generate-dev-version-id.sh @@ -41,8 +41,8 @@ cd "${repo_dir}" PROJECT_NAME=cirq-core/cirq -ACTUAL_VERSION_LINE=$(cat "${PROJECT_NAME}/_version.py" | tail -n 1) -ACTUAL_VERSION=`echo $ACTUAL_VERSION_LINE | cut -d'"' -f 2` +ACTUAL_VERSION_LINE=$(tail -n 1 "${PROJECT_NAME}/_version.py") +ACTUAL_VERSION=$(echo "$ACTUAL_VERSION_LINE" | cut -d'"' -f 2) if [[ ${ACTUAL_VERSION} == *"dev" ]]; then echo "${ACTUAL_VERSION}$(date "+%Y%m%d%H%M%S")" diff --git a/dev_tools/packaging/packaging_test.sh b/dev_tools/packaging/packaging_test.sh index ab249cbde16..fc1fc784431 100755 --- a/dev_tools/packaging/packaging_test.sh +++ b/dev_tools/packaging/packaging_test.sh @@ -24,18 +24,19 @@ set -e # Temporary workspace. tmp_dir=$(mktemp -d) -trap "{ rm -rf ${tmp_dir}; }" EXIT +trap '{ rm -rf "${tmp_dir}"; }' EXIT # New virtual environment echo "Working in a fresh virtualenv at ${tmp_dir}/env" virtualenv --quiet "--python=/usr/bin/python3" "${tmp_dir}/env" -export CIRQ_PRE_RELEASE_VERSION=$(dev_tools/packaging/generate-dev-version-id.sh) +export CIRQ_PRE_RELEASE_VERSION +CIRQ_PRE_RELEASE_VERSION=$(dev_tools/packaging/generate-dev-version-id.sh) out_dir=${tmp_dir}/dist -dev_tools/packaging/produce-package.sh ${out_dir} $CIRQ_PRE_RELEASE_VERSION +dev_tools/packaging/produce-package.sh "${out_dir}" "$CIRQ_PRE_RELEASE_VERSION" # test installation -"${tmp_dir}/env/bin/python" -m pip install ${out_dir}/* +"${tmp_dir}/env/bin/python" -m pip install "${out_dir}"/* echo =========================== echo Testing that code executes @@ -51,7 +52,7 @@ echo ======================================= CIRQ_PACKAGES=$(env PYTHONPATH=. python dev_tools/modules.py list --mode package) for p in $CIRQ_PACKAGES; do - echo --- Testing $p ----- + echo "--- Testing $p -----" python_test="import $p; print($p); assert '${tmp_dir}' in $p.__file__, 'Package path seems invalid.'" env PYTHONPATH='' "${tmp_dir}/env/bin/python" -c "$python_test" && echo -e "\033[32mPASS\033[0m" || echo -e "\033[31mFAIL\033[0m" -done \ No newline at end of file +done diff --git a/dev_tools/packaging/produce-package.sh b/dev_tools/packaging/produce-package.sh index 79744767123..4673cb68755 100755 --- a/dev_tools/packaging/produce-package.sh +++ b/dev_tools/packaging/produce-package.sh @@ -42,16 +42,16 @@ repo_dir=$(git rev-parse --show-toplevel) cd "${repo_dir}" # Make a clean copy of HEAD, without files ignored by git (but potentially kept by setup.py). -if [ ! -z "$(git status --short)" ]; then +if [ -n "$(git status --short)" ]; then echo -e "\033[31mWARNING: You have uncommitted changes. They won't be included in the package.\033[0m" fi tmp_git_dir=$(mktemp -d "/tmp/produce-package-git.XXXXXXXXXXXXXXXX") -trap "{ rm -rf ${tmp_git_dir}; }" EXIT +trap '{ rm -rf "${tmp_git_dir}"; }' EXIT cd "${tmp_git_dir}" git init --quiet git fetch "${repo_dir}" HEAD --quiet --depth=1 git checkout FETCH_HEAD -b work --quiet -if [ ! -z "${SPECIFIED_VERSION}" ]; then +if [ -n "${SPECIFIED_VERSION}" ]; then CIRQ_PACKAGES=$(env PYTHONPATH=. python dev_tools/modules.py list --mode package-path) for PROJECT_NAME in $CIRQ_PACKAGES; do echo '__version__ = "'"${SPECIFIED_VERSION}"'"' > "${tmp_git_dir}/${PROJECT_NAME}/_version.py" @@ -65,7 +65,7 @@ CIRQ_MODULES=$(env PYTHONPATH=. python dev_tools/modules.py list --mode folder - for m in $CIRQ_MODULES; do echo "processing $m/setup.py..." - cd $m + cd "$m" python3 setup.py -q bdist_wheel -d "${out_dir}" cd .. done diff --git a/dev_tools/packaging/publish-dev-package.sh b/dev_tools/packaging/publish-dev-package.sh index a110d5ed1c9..2a478abb27b 100755 --- a/dev_tools/packaging/publish-dev-package.sh +++ b/dev_tools/packaging/publish-dev-package.sh @@ -62,14 +62,14 @@ if [[ "${EXPECTED_VERSION}" != *dev* ]]; then echo -e "\033[31mExpected version must include 'dev'.\033[0m" exit 1 fi -ACTUAL_VERSION_LINE=$(cat "${PROJECT_NAME}/_version.py" | tail -n 1) +ACTUAL_VERSION_LINE=$(tail -n 1 "${PROJECT_NAME}/_version.py") if [ "${ACTUAL_VERSION_LINE}" != '__version__ = "'"${EXPECTED_VERSION}"'"' ]; then echo -e "\033[31mExpected version (${EXPECTED_VERSION}) didn't match the one in ${PROJECT_NAME}/_version.py (${ACTUAL_VERSION_LINE}).\033[0m" exit 1 fi if [ -z "${PROD_SWITCH}" ] || [ "${PROD_SWITCH}" = "--test" ]; then - PYPI_REPOSITORY_FLAG="--repository-url=https://test.pypi.org/legacy/" + PYPI_REPOSITORY_FLAG=( "--repository-url=https://test.pypi.org/legacy/" ) PYPI_REPO_NAME="TEST" USERNAME="${TEST_TWINE_USERNAME}" PASSWORD="${TEST_TWINE_PASSWORD}" @@ -82,7 +82,7 @@ if [ -z "${PROD_SWITCH}" ] || [ "${PROD_SWITCH}" = "--test" ]; then exit 1 fi elif [ "${PROD_SWITCH}" = "--prod" ]; then - PYPI_REPOSITORY_FLAG='' + PYPI_REPOSITORY_FLAG=( ) PYPI_REPO_NAME="PROD" USERNAME="${PROD_TWINE_USERNAME}" PASSWORD="${PROD_TWINE_PASSWORD}" @@ -109,13 +109,14 @@ cd "$(git rev-parse --show-toplevel)" # Temporary workspace. tmp_package_dir=$(mktemp -d "/tmp/publish-dev-package_package.XXXXXXXXXXXXXXXX") -trap "{ rm -rf ${tmp_package_dir}; }" EXIT +trap '{ rm -rf "${tmp_package_dir}"; }' EXIT # Configure to push to a pre-release package of cirq. -export CIRQ_PRE_RELEASE_VERSION=$(dev_tools/packaging/generate-dev-version-id.sh) +export CIRQ_PRE_RELEASE_VERSION +CIRQ_PRE_RELEASE_VERSION=$(dev_tools/packaging/generate-dev-version-id.sh) # Produce packages. dev_tools/packaging/produce-package.sh "${tmp_package_dir}" "${UPLOAD_VERSION}" -twine upload --username="${USERNAME}" --password="${PASSWORD}" ${PYPI_REPOSITORY_FLAG} "${tmp_package_dir}/*" +twine upload --username="${USERNAME}" --password="${PASSWORD}" "${PYPI_REPOSITORY_FLAG[@]}" "${tmp_package_dir}/*" echo -e "\033[32mUploaded package with version ${UPLOAD_VERSION} to ${PYPI_REPO_NAME} pypi repository\033[0m" diff --git a/dev_tools/packaging/verify-published-package.sh b/dev_tools/packaging/verify-published-package.sh index 9edf0e45b62..1434153b041 100755 --- a/dev_tools/packaging/verify-published-package.sh +++ b/dev_tools/packaging/verify-published-package.sh @@ -67,43 +67,41 @@ REPO_ROOT="$(git rev-parse --show-toplevel)" # Temporary workspace. tmp_dir=$(mktemp -d "/tmp/verify-published-package.XXXXXXXXXXXXXXXX") cd "${tmp_dir}" -trap "{ rm -rf ${tmp_dir}; }" EXIT - -# Test python 3 versions. -for PYTHON_VERSION in python3; do - # Prepare. - CORE_DEPS_FILE="${REPO_ROOT}/cirq-core/requirements.txt" - GOOGLE_DEPS_FILE="${REPO_ROOT}/cirq-google/requirements.txt" - CONTRIB_DEPS_FILE="${REPO_ROOT}/cirq-core/cirq/contrib/requirements.txt" - DEV_DEPS_FILE="${REPO_ROOT}/dev_tools/requirements/deps/dev-tools.txt" - - echo -e "\n\033[32m${PYTHON_VERSION}\033[0m" - echo "Working in a fresh virtualenv at ${tmp_dir}/${PYTHON_VERSION}" - virtualenv --quiet "--python=/usr/bin/${PYTHON_VERSION}" "${tmp_dir}/${PYTHON_VERSION}" - - echo Installing "${PYPI_PROJECT_NAME}==${PROJECT_VERSION} from ${PYPI_REPO_NAME} pypi" - "${tmp_dir}/${PYTHON_VERSION}/bin/pip" install --quiet ${PIP_FLAGS} "${PYPI_PROJECT_NAME}==${PROJECT_VERSION}" --extra-index-url https://pypi.python.org/simple - - # Check that code runs without dev deps. - echo Checking that code executes - "${tmp_dir}/${PYTHON_VERSION}/bin/python" -c "import cirq_google; print(cirq_google.Sycamore)" - "${tmp_dir}/${PYTHON_VERSION}/bin/python" -c "import cirq; print(cirq.Circuit(cirq.CZ(*cirq.LineQubit.range(2))))" - - # Install pytest + dev deps. - "${tmp_dir}/${PYTHON_VERSION}/bin/pip" install -r ${DEV_DEPS_FILE} - - # Run tests. - PY_VER=$(ls "${tmp_dir}/${PYTHON_VERSION}/lib") - echo Running cirq tests - cirq_dir="${tmp_dir}/${PYTHON_VERSION}/lib/${PY_VER}/site-packages/${PROJECT_NAME}" - "${tmp_dir}/${PYTHON_VERSION}/bin/pytest" --quiet --disable-pytest-warnings --ignore="${cirq_dir}/contrib" "${cirq_dir}" - - echo "Installing contrib dependencies" - "${tmp_dir}/${PYTHON_VERSION}/bin/pip" install --quiet -r "${CONTRIB_DEPS_FILE}" - - echo "Running contrib tests" - "${tmp_dir}/${PYTHON_VERSION}/bin/pytest" --quiet --disable-pytest-warnings "${cirq_dir}/contrib" -done +trap '{ rm -rf "${tmp_dir}"; }' EXIT + +# Test installation from published package +PYTHON_VERSION=python3 + +# Prepare. +CONTRIB_DEPS_FILE="${REPO_ROOT}/cirq-core/cirq/contrib/requirements.txt" +DEV_DEPS_FILE="${REPO_ROOT}/dev_tools/requirements/deps/dev-tools.txt" + +echo -e "\n\033[32m${PYTHON_VERSION}\033[0m" +echo "Working in a fresh virtualenv at ${tmp_dir}/${PYTHON_VERSION}" +virtualenv --quiet "--python=/usr/bin/${PYTHON_VERSION}" "${tmp_dir}/${PYTHON_VERSION}" + +echo Installing "${PYPI_PROJECT_NAME}==${PROJECT_VERSION} from ${PYPI_REPO_NAME} pypi" +"${tmp_dir}/${PYTHON_VERSION}/bin/pip" install --quiet ${PIP_FLAGS} "${PYPI_PROJECT_NAME}==${PROJECT_VERSION}" --extra-index-url https://pypi.python.org/simple + +# Check that code runs without dev deps. +echo Checking that code executes +"${tmp_dir}/${PYTHON_VERSION}/bin/python" -c "import cirq_google; print(cirq_google.Sycamore)" +"${tmp_dir}/${PYTHON_VERSION}/bin/python" -c "import cirq; print(cirq.Circuit(cirq.CZ(*cirq.LineQubit.range(2))))" + +# Install pytest + dev deps. +"${tmp_dir}/${PYTHON_VERSION}/bin/pip" install -r "${DEV_DEPS_FILE}" + +# Run tests. +PY_VER=$(ls "${tmp_dir}/${PYTHON_VERSION}/lib") +echo Running cirq tests +cirq_dir="${tmp_dir}/${PYTHON_VERSION}/lib/${PY_VER}/site-packages/${PROJECT_NAME}" +"${tmp_dir}/${PYTHON_VERSION}/bin/pytest" --quiet --disable-pytest-warnings --ignore="${cirq_dir}/contrib" "${cirq_dir}" + +echo "Installing contrib dependencies" +"${tmp_dir}/${PYTHON_VERSION}/bin/pip" install --quiet -r "${CONTRIB_DEPS_FILE}" + +echo "Running contrib tests" +"${tmp_dir}/${PYTHON_VERSION}/bin/pytest" --quiet --disable-pytest-warnings "${cirq_dir}/contrib" echo echo -e '\033[32mVERIFIED\033[0m' diff --git a/dev_tools/pr_monitor.sh b/dev_tools/pr_monitor.sh index 49f8d4ab737..b8a8414e61e 100755 --- a/dev_tools/pr_monitor.sh +++ b/dev_tools/pr_monitor.sh @@ -48,10 +48,10 @@ # Get the working directory to the repo root. -cd "$(dirname "${BASH_SOURCE[0]}")" -repo_dir=$(git rev-parse --show-toplevel) -cd "${repo_dir}" +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +repo_dir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${repo_dir}" || exit $? # Do the thing. export PYTHONPATH=${repo_dir} -python3 ${repo_dir}/dev_tools/pr_monitor.py "$@" +python3 "${repo_dir}/dev_tools/pr_monitor.py" "$@" diff --git a/dev_tools/pypath b/dev_tools/pypath index b15b243814a..807b1d32aab 100755 --- a/dev_tools/pypath +++ b/dev_tools/pypath @@ -1,3 +1,5 @@ +#!/usr/bin/env bash + # Copyright 2018 The Cirq Developers # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,6 +23,7 @@ if [ -n "${ZSH_VERSION}" ]; then + # shellcheck disable=2296,2298 _PYPATH_BASE_DIR="${${(%):-%x}:a:h:h}" else _PYPATH_BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"