Skip to content

Commit c228867

Browse files
kevinsungbabbush
authored andcommitted
Add development scripts to facilitate packaging (#46)
* add packaging scripts * fix
1 parent def0d1c commit c228867

File tree

4 files changed

+264
-1
lines changed

4 files changed

+264
-1
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2018 The OpenFermion Developers
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
################################################################################
18+
# Produces wheels that can be uploaded to the pypi package repository.
19+
#
20+
# First argument must be the output directory. Second argument is an optional
21+
# version specifier. If not set, the version from `_version.py` is used. If set,
22+
# it overwrites `_version.py`.
23+
#
24+
# Usage:
25+
# dev_tools/packaging/produce-package.sh output_dir [version]
26+
################################################################################
27+
28+
PROJECT_NAME=openfermionpyscf
29+
30+
set -e
31+
32+
if [ -z "${1}" ]; then
33+
echo -e "\e[31mNo output directory given.\e[0m"
34+
exit 1
35+
fi
36+
out_dir=$(realpath "${1}")
37+
38+
SPECIFIED_VERSION="${2}"
39+
40+
# Get the working directory to the repo root.
41+
cd "$( dirname "${BASH_SOURCE[0]}" )"
42+
repo_dir=$(git rev-parse --show-toplevel)
43+
cd ${repo_dir}
44+
45+
# Make a clean copy of HEAD, without files ignored by git (but potentially kept by setup.py).
46+
if [ ! -z "$(git status --short)" ]; then
47+
echo -e "\e[31mWARNING: You have uncommitted changes. They won't be included in the package.\e[0m"
48+
fi
49+
tmp_git_dir=$(mktemp -d "/tmp/produce-package-git.XXXXXXXXXXXXXXXX")
50+
trap "{ rm -rf ${tmp_git_dir}; }" EXIT
51+
cd "${tmp_git_dir}"
52+
git init --quiet
53+
git fetch ${repo_dir} HEAD --quiet --depth=1
54+
git checkout FETCH_HEAD -b work --quiet
55+
if [ ! -z "${SPECIFIED_VERSION}" ]; then
56+
echo '__version__ = "'"${SPECIFIED_VERSION}"'"' > "${tmp_git_dir}/${PROJECT_NAME}/_version.py"
57+
fi
58+
59+
# Python wheel.
60+
echo "Producing python package files..."
61+
python3 setup.py -q sdist -d "${out_dir}"
62+
63+
ls "${out_dir}"
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2018 The OpenFermion Developers
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
################################################################################
18+
# Produces and uploads dev-version wheels to a pypi package repository. Uploads
19+
# to the test pypi repository unless the --prod switch is added.
20+
#
21+
# The pypi credentials given to twine are specified via environment variables.
22+
#
23+
# Usage:
24+
# export TEST_TWINE_USERNAME=...
25+
# export TEST_TWINE_PASSWORD=...
26+
# export PROD_TWINE_USERNAME=...
27+
# export PROD_TWINE_PASSWORD=...
28+
# dev_tools/packaging/publish-dev-package.sh EXPECTED_VERSION [--test|--prod]
29+
#
30+
# The uploaded package can be installed with pip, but if it's the test version
31+
# then the requirements must be installed separately first (because they do not
32+
# all exist on test pypi).
33+
#
34+
# Prod installation:
35+
#
36+
# pip install openfermionpyscf==VERSION_YOU_UPLOADED
37+
#
38+
# Test installation:
39+
#
40+
# pip install -r requirements.txt
41+
# pip install --index-url https://test.pypi.org/simple/ openfermionpyscf==VERSION_YOU_UPLOADED
42+
################################################################################
43+
44+
PROJECT_NAME=openfermionpyscf
45+
set -e
46+
trap "{ echo -e '\e[31mFAILED\e[0m'; }" ERR
47+
48+
EXPECTED_VERSION=$1
49+
PROD_SWITCH=$2
50+
51+
if [ -z "${EXPECTED_VERSION}" ]; then
52+
echo -e "\e[31mFirst argument must be the expected version.\e[0m"
53+
exit 1
54+
fi
55+
if [[ "${EXPECTED_VERSION}" != *dev* ]]; then
56+
echo -e "\e[31mExpected version must include 'dev'.\e[0m"
57+
exit 1
58+
fi
59+
ACTUAL_VERSION_LINE=$(cat "${PROJECT_NAME}/_version.py" | tail -n 1)
60+
if [ "${ACTUAL_VERSION_LINE}" != '__version__ = "'"${EXPECTED_VERSION}"'"' ]; then
61+
echo -e "\e[31mExpected version (${EXPECTED_VERSION}) didn't match the one in ${PROJECT_NAME}/_version.py (${ACTUAL_VERSION_LINE}).\e[0m"
62+
exit 1
63+
fi
64+
65+
if [ -z "${PROD_SWITCH}" ] || [ "${PROD_SWITCH}" = "--test" ]; then
66+
PYPI_REPOSITORY_FLAG="--repository-url=https://test.pypi.org/legacy/"
67+
PYPI_REPO_NAME="TEST"
68+
USERNAME="${TEST_TWINE_USERNAME}"
69+
PASSWORD="${TEST_TWINE_PASSWORD}"
70+
if [ -z "${USERNAME}" ]; then
71+
echo -e "\e[31mTEST_TWINE_USERNAME environment variable must be set.\e[0m"
72+
exit 1
73+
fi
74+
if [ -z "${PASSWORD}" ]; then
75+
echo -e "\e[31mTEST_TWINE_PASSWORD environment variable must be set.\e[0m"
76+
exit 1
77+
fi
78+
elif [ "${PROD_SWITCH}" = "--prod" ]; then
79+
PYPI_REPOSITORY_FLAG=''
80+
PYPI_REPO_NAME="PROD"
81+
USERNAME="${PROD_TWINE_USERNAME}"
82+
PASSWORD="${PROD_TWINE_PASSWORD}"
83+
if [ -z "${USERNAME}" ]; then
84+
echo -e "\e[31mPROD_TWINE_USERNAME environment variable must be set.\e[0m"
85+
exit 1
86+
fi
87+
if [ -z "${PASSWORD}" ]; then
88+
echo -e "\e[31mPROD_TWINE_PASSWORD environment variable must be set.\e[0m"
89+
exit 1
90+
fi
91+
else
92+
echo -e "\e[31mSecond argument must be empty, '--test' or '--prod'.\e[0m"
93+
exit 1
94+
fi
95+
96+
97+
UPLOAD_VERSION="${EXPECTED_VERSION}$(date "+%Y%m%d%H%M%S")"
98+
echo -e "Producing package with version \e[33m\e[100m${UPLOAD_VERSION}\e[0m to upload to \e[33m\e[100m${PYPI_REPO_NAME}\e[0m pypi repository"
99+
100+
# Get the working directory to the repo root.
101+
cd "$( dirname "${BASH_SOURCE[0]}" )"
102+
cd "$(git rev-parse --show-toplevel)"
103+
104+
# Temporary workspace.
105+
tmp_package_dir=$(mktemp -d "/tmp/publish-dev-package_package.XXXXXXXXXXXXXXXX")
106+
trap "{ rm -rf ${tmp_package_dir}; }" EXIT
107+
108+
# Produce packages.
109+
dev_tools/packaging/produce-package.sh "${tmp_package_dir}" "${UPLOAD_VERSION}"
110+
twine upload --username="${USERNAME}" --password="${PASSWORD}" ${PYPI_REPOSITORY_FLAG} "${tmp_package_dir}/*"
111+
112+
echo -e "\e[32mUploaded package with version ${UPLOAD_VERSION} to ${PYPI_REPO_NAME} pypi repository\e[0m"
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2018 The OpenFermion Developers
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
################################################################################
18+
# Downloads and tests openfermionpyscf wheels from the pypi package repository. Uses the
19+
# prod pypi repository unless the --test switch is added.
20+
#
21+
# CAUTION: when targeting the test pypi repository, this script assumes that the
22+
# local version of openfermionpyscf has the same dependencies as the remote one (because the
23+
# dependencies must be installed from the non-test pypi repository). If the
24+
# dependencies disagree, the tests can spuriously fail.
25+
#
26+
# Usage:
27+
# dev_tools/packaging/verify-published-package.sh PACKAGE_VERSION [--test|--prod]
28+
################################################################################
29+
30+
set -e
31+
trap "{ echo -e '\e[31mFAILED\e[0m'; }" ERR
32+
33+
34+
PROJECT_NAME=openfermionpyscf
35+
PROJECT_VERSION=$1
36+
PROD_SWITCH=$2
37+
38+
if [ -z "${PROJECT_VERSION}" ]; then
39+
echo -e "\e[31mFirst argument must be the package version to test.\e[0m"
40+
exit 1
41+
fi
42+
43+
if [ "${PROD_SWITCH}" = "--test" ]; then
44+
PYPI_REPOSITORY_FLAG="--index-url=https://test.pypi.org/simple/"
45+
PYPI_REPO_NAME="TEST"
46+
elif [ -z "${PROD_SWITCH}" ] || [ "${PROD_SWITCH}" = "--prod" ]; then
47+
PYPI_REPOSITORY_FLAG=''
48+
PYPI_REPO_NAME="PROD"
49+
else
50+
echo -e "\e[31mSecond argument must be empty, '--prod' or '--test'.\e[0m"
51+
exit 1
52+
fi
53+
54+
# Find the repo root.
55+
cd "$( dirname "${BASH_SOURCE[0]}" )"
56+
REPO_ROOT="$(git rev-parse --show-toplevel)"
57+
58+
# Temporary workspace.
59+
tmp_dir=$(mktemp -d "/tmp/verify-published-package.XXXXXXXXXXXXXXXX")
60+
cd "${tmp_dir}"
61+
trap "{ rm -rf ${tmp_dir}; }" EXIT
62+
63+
# Test both the python 2 and python 3 versions.
64+
for PYTHON_VERSION in python2 python3; do
65+
# Prepare.
66+
RUNTIME_DEPS_FILE="${REPO_ROOT}/requirements.txt"
67+
echo -e "\n\e[32m${PYTHON_VERSION}\e[0m"
68+
echo "Working in a fresh virtualenv at ${tmp_dir}/${PYTHON_VERSION}"
69+
virtualenv --quiet "--python=/usr/bin/${PYTHON_VERSION}" "${tmp_dir}/${PYTHON_VERSION}"
70+
71+
# Install package.
72+
if [ "${PYPI_REPO_NAME}" == "TEST" ]; then
73+
echo "Pre-installing dependencies since they don't all exist in TEST pypi"
74+
"${tmp_dir}/${PYTHON_VERSION}/bin/pip" install --quiet -r "${RUNTIME_DEPS_FILE}"
75+
fi
76+
echo Installing "${PROJECT_NAME}==${PROJECT_VERSION} from ${PYPI_REPO_NAME} pypi"
77+
"${tmp_dir}/${PYTHON_VERSION}/bin/pip" install --quiet ${PYPI_REPOSITORY_FLAG} "${PROJECT_NAME}==${PROJECT_VERSION}"
78+
79+
# Run tests.
80+
echo Installing pytest
81+
"${tmp_dir}/${PYTHON_VERSION}/bin/pip" install --quiet pytest
82+
PY_VER=$(ls "${tmp_dir}/${PYTHON_VERSION}/lib")
83+
echo Running tests
84+
"${tmp_dir}/${PYTHON_VERSION}/bin/pytest" --quiet --disable-pytest-warnings "${tmp_dir}/${PYTHON_VERSION}/lib/${PY_VER}/site-packages/${PROJECT_NAME}"
85+
done
86+
87+
echo
88+
echo -e '\e[32mVERIFIED\e[0m'

openfermionpyscf/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
# limitations under the License.
1212

1313
"""Define version number here and read it from setup.py automatically"""
14-
__version__ = "0.3"
14+
__version__ = "0.4.dev"

0 commit comments

Comments
 (0)