#!/bin/bash
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# make-python-sdists: Generate sdists for Aurora Python code for use with pip or upload to PyPI.
# Usage:
#   ./build-support/release/make-python-sdists
#
# Examples:
#   Install the aurora client in a virtualenv:
#     ./build-support/virtualenv ~/aurora-virtualenv
#     source ~/aurora-virtualenv/bin/activate
#     pip install -f dist apache.aurora.client
#     aurora --help
#     aurora_admin --help
set -o errexit

# Use the convention of a python_library exporting a setup_py alongside each public target
# to generate the list of setup_pys our project provides.
TARGETS=($(./pants filter --type=python_binary src/main:: | sed 's@:.*@@' | sort -u))

cd "`git rev-parse --show-toplevel`"
# Run in a virtualenv so that we use a consistent version of setuptools to generate the sdist.
# Without this indirection pants will use the system setuptools, which on Ubuntu 12.04 is old,
# buggy, and produces a broken apache.thermos.observer.
# TODO(ksweeney): Remove this indirection when pants supports this natively.
if [[ ! -f build-support/make-python-sdists.venv/BOOTSTRAPPED ]]; then
  ./build-support/virtualenv build-support/make-python-sdists.venv
  touch build-support/make-python-sdists.venv/BOOTSTRAPPED
fi
source build-support/make-python-sdists.venv/bin/activate

for t in "${TARGETS[@]}"; do
  ./pants setup-py --recursive $t
done
