xref: /aosp_15_r20/external/pytorch/scripts/release/promote/prep_binary_for_pypi.sh (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1#!/usr/bin/env bash
2
3# Preps binaries for publishing to pypi by removing the
4# version suffix we normally add for all binaries
5# (outside of default ones, CUDA 10.2 currently)
6
7# Usage is:
8# $ prep_binary_for_pypy.sh <path_to_whl_file> <path_to_multiple_whl_files>
9
10# Will output a whl in your current directory
11
12set -eou pipefail
13shopt -s globstar
14
15OUTPUT_DIR=${OUTPUT_DIR:-$(pwd)}
16
17tmp_dir="$(mktemp -d)"
18trap 'rm -rf ${tmp_dir}' EXIT
19
20for whl_file in "$@"; do
21    whl_file=$(realpath "${whl_file}")
22    whl_dir="${tmp_dir}/$(basename "${whl_file}")_unzipped"
23    mkdir -pv "${whl_dir}"
24    (
25        set -x
26        unzip -q "${whl_file}" -d "${whl_dir}"
27    )
28    version_with_suffix=$(grep '^Version:' "${whl_dir}"/*/METADATA | cut -d' ' -f2)
29    version_with_suffix_escaped=${version_with_suffix/+/%2B}
30    # Remove all suffixed +bleh versions
31    version_no_suffix=${version_with_suffix/+*/}
32    new_whl_file=${OUTPUT_DIR}/$(basename "${whl_file/${version_with_suffix_escaped}/${version_no_suffix}}")
33    dist_info_folder=$(find "${whl_dir}" -type d -name '*.dist-info' | head -1)
34    basename_dist_info_folder=$(basename "${dist_info_folder}")
35    dirname_dist_info_folder=$(dirname "${dist_info_folder}")
36    (
37        set -x
38        find "${dist_info_folder}" -type f -exec sed -i "s!${version_with_suffix}!${version_no_suffix}!" {} \;
39        # Moves distinfo from one with a version suffix to one without
40        # Example: torch-1.8.0+cpu.dist-info => torch-1.8.0.dist-info
41        mv "${dist_info_folder}" "${dirname_dist_info_folder}/${basename_dist_info_folder/${version_with_suffix}/${version_no_suffix}}"
42        cd "${whl_dir}"
43        zip -qr "${new_whl_file}" .
44    )
45done
46