-
Notifications
You must be signed in to change notification settings - Fork 2
/
pip-8210
144 lines (128 loc) · 4.12 KB
/
pip-8210
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env bash
if [[ ${-} != *i* ]]; then
source_once &> /dev/null && return 0
fi
#*# docker/recipes/pip-8210
#**
# ========
# pip 8210
# ========
#
# .. default-domain:: bash
#
# .. file:: pip-8210
#
# This script allows us to install a package outside of pip-tools. In order to accomplish this correctly, we use the ``requirements.txt`` file as a constraint file to handle corner cases like pip issue 8210.
#
# To accomplish this, it adds the ``-c`` constraints to ``pip install``, ``pip download``, and ``pip wheel`` sub-commands.
#
# You should activate your virtualenv before calling this; it will use the ``pip`` on the path.
#
# :Arguments: * [--if-not-found <executable name>] - Only runs the ``pip`` command if a specified executable is not found.
# * ``$1``... - Args to pass to ``pip``. E.g. ``install pip-tools``
#
# .. note::
#
# If the first package is ``pip-tools``, it will install pip-tools even if it is not in ``requirements.txt`` (necessary to bootstrap new ``requirements.txt`` files). If additional arguments were added in this corner case, the return value will be non-zero since not all the packages got installed, but ``pip-tools`` did get installed.
#
# .. seealso::
#
# https://github.com/pypa/pip/issues/8210
#
# .. function:: pip-8210
#
# Same syntax as :file:`pip-8210`
#**
if [ -z "${VSI_COMMON_DIR+set}" ]; then
VSI_COMMON_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.."; pwd)"
fi
if [ -f "${VSI_COMMON_DIR}/linux/command_tools.bsh" ]; then
source "${VSI_COMMON_DIR}/linux/command_tools.bsh"
else
# Copied from command_tools.bsh for portability
function parse-generic()
{
command_args=()
subcommand_args=()
while (( ${#} )); do
case "${1}" in
-*)
for arg in ${arguments_with_value[@]+"${arguments_with_value[@]}"}; do
if [ "${1}" = "${arg}" ]; then
command_args+=("${1}" "${2}")
shift 2
continue 2
fi
done
command_args+=("${1}")
shift 1
;;
*)
subcommand="${1}"
shift 1
subcommand_args=(${@+"${@}"})
break
;;
esac
done
}
fi
function parse_pip()
{
local arguments_with_value=(--python --log --keyring-provider --proxy
--retries --timeout --exists-action --trusted-host
--cert --client-cert --cache-dir --use-feature --use-deprecated)
parse-generic ${@+"${@}"}
}
function pip-8210()
{
# local args_parsed=0
# local command_checks=()
# parse_args args_parsed --if-not-found +command_checks:\
# -- ${@+"${@}"}
# shift "${args_parsed}"
# local command_check
# for command_check in ${command_checks[@]+"${command_checks[@]}"}; do
# if command -v "${command_check}" &> /dev/null; then
# return 0
# fi
# done
# Don't use parse_args to decrease dependencies
while (( $# )); do
case "${1}" in
--if-not-found)
if command -v "${2}" &> /dev/null; then
return 0
fi
shift 2
;;
*)
break
esac
done
local cmd command_args subcommand subcommand_args
parse_pip ${@+"${@}"}
case "${subcommand}" in
install|download|wheel)
local temp_8210=$(mktemp)
# Only remove editables, since extras are taken care right after `pip-compile`
grep -v '^-e ' "${PIP_REQUIREMENTS-/src/requirements.txt}" > "${temp_8210}"
pip ${command_args[@]+"${command_args[@]}"} "${subcommand}" -c "${temp_8210}" ${subcommand_args[@]+"${subcommand_args[@]}"} || (
# If pip-tools is the first argument and it failed, force it to be installed for bootstrap reasons
[ "${subcommand}" = "install" -a "${subcommand_args[0]}" = "pip-tools" ] && \
pip install pip-tools && \
[ "${#}" = "1" ] # Add this check last, so pip-tools gets installed, but return value is false
) || rv=$?
# Cleanup
rm "${temp_8210}"
return "${rv-0}"
;;
*)
pip ${@+"${@}"}
;;
esac
}
if [ "${BASH_SOURCE[0]}" = "${0}" ] || [ "$(basename "${BASH_SOURCE[0]}")" = "${0}" ]; then
set -eu
pip-8210 "${@}"
fi