Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
Some refactoring of Tink Python create_release.sh script:
Browse files Browse the repository at this point in the history
 - Rename `build_linux` as `create_distribution_for_linux` and split it into two functions, one for wheels build and one for source distribution build
 - Rename `build_macos` as `create_distribution_for_macos`.
 - Add comments to functions
 - Remove `set_owner_within_tar` as this is no longer needed: since pypa/setuptools#2800 the parameters `--owner` and `--group` are propagated to setuptools.

PiperOrigin-RevId: 459048112
  • Loading branch information
morambro authored and Copybara-Service committed Jul 5, 2022
1 parent 7a4fa64 commit 2233867
Showing 1 changed file with 58 additions and 28 deletions.
86 changes: 58 additions & 28 deletions python/tools/distribution/create_release.sh
Expand Up @@ -37,11 +37,21 @@ readonly IMAGE_NAME="quay.io/pypa/manylinux2014_x86_64"
readonly IMAGE_DIGEST="sha256:31d7d1cbbb8ea93ac64c3113bceaa0e9e13d65198229a25eee16dc70e8bf9cf7"
readonly IMAGE="${IMAGE_NAME}@${IMAGE_DIGEST}"

build_linux() {
echo "### Building Linux binary wheels ###"
#######################################
# Builds Tink Python built distribution (Wheel) [1].
#
# This function must be called from within the Tink Python's root folder.
#
# [1] https://packaging.python.org/en/latest/glossary/#term-Built-Distribution
# Globals:
# None
# Arguments:
# None
#######################################
__create_and_test_wheels_for_linux() {
echo "### Building and testing Linux binary wheels ###"
local -r tink_py_relative_path="${PWD##*/}"
local -r workdir="/tmp/tink/${tink_py_relative_path}"

# Use signatures for getting images from registry (see
# https://docs.docker.com/engine/security/trust/content_trust/).
export DOCKER_CONTENT_TRUST=1
Expand All @@ -64,23 +74,34 @@ build_linux() {
"${IMAGE}" \
"${workdir}/tools/distribution/test_linux_binary_wheels.sh"

# Restore the original WORKSPACE.
mv WORKSPACE.bak WORKSPACE

# Docker runs as root so we transfer ownership to the non-root user.
sudo chown -R "$(id -un):$(id -gn)" "${TINK_PYTHON_ROOT_PATH}"
# Restore the original WORKSPACE.
mv WORKSPACE.bak WORKSPACE
}

echo "### Building Linux source distribution ###"
#######################################
# Builds Tink Python source distribution [1].
#
# This function must be called from within the Tink Python's root folder.
#
# [1] https://packaging.python.org/en/latest/glossary/#term-Source-Distribution-or-sdist
# Globals:
# PYTHON_VERSIONS
# Arguments:
# None
#######################################
__create_and_test_sdist_for_linux() {
echo "### Building and testing Linux source distribution ###"
local sorted=( $( echo "${PYTHON_VERSIONS[@]}" \
| xargs -n1 | sort -V | xargs ) )
local latest="${sorted[${#sorted[@]}-1]}"
enable_py_version "${latest}"

# Build source distribution.
export TINK_PYTHON_SETUPTOOLS_OVERRIDE_BASE_PATH="${TINK_PYTHON_ROOT_PATH}/.."
python3 setup.py sdist
python3 setup.py sdist --owner=root --group=root
local sdist_filename="tink-${TINK_VERSION}.tar.gz"
set_owner_within_tar "dist/${sdist_filename}"
cp "dist/${sdist_filename}" release/

# Test install from source distribution.
Expand All @@ -92,7 +113,32 @@ build_linux() {
| xargs -0 -n1 python3
}

build_macos() {
#######################################
# Creates a Tink Python distribution for Linux.
#
# This function must be called from within the Tink Python's root folder.
#
# Globals:
# None
# Arguments:
# None
#######################################
create_distribution_for_linux() {
__create_and_test_wheels_for_linux
__create_and_test_sdist_for_linux
}

#######################################
# Creates a Tink Python distribution for MacOS.
#
# This function must be called from within the Tink Python's root folder.
#
# Globals:
# PYTHON_VERSIONS
# Arguments:
# None
#######################################
create_distribution_for_macos() {
echo "### Building macOS binary wheels ###"

for v in "${PYTHON_VERSIONS[@]}"; do
Expand Down Expand Up @@ -123,30 +169,14 @@ enable_py_version() {
python3 -m pip install --upgrade wheel
}

# setuptools does not replicate the distutils feature of explicitly setting
# user/group ownership on the files within the source distribution archive.
#
# This function is an easy workaround that doesn't require monkey-patching
# setuptools. This behavior is desired to produce deterministic release
# artifacts.
set_owner_within_tar() {
local tar_file="$1"
local tmp_dir="$(mktemp -d tink-py-tar-XXXXXX)"
tar -C "${tmp_dir}" -xzf "${tar_file}"
local tink_dir="$(basename $(ls -d ${tmp_dir}/tink*))"
tar -C "${tmp_dir}" -czf "${tar_file}" \
--owner=root --group=root "${tink_dir}"
rm -r "${tmp_dir}"
}

main() {
eval "$(pyenv init -)"
mkdir -p release

if [[ "${PLATFORM}" == 'linux' ]]; then
build_linux
create_distribution_for_linux
elif [[ "${PLATFORM}" == 'darwin' ]]; then
build_macos
create_distribution_for_macos
else
echo "${PLATFORM} is not a supported platform."
exit 1
Expand Down

0 comments on commit 2233867

Please sign in to comment.