-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.py-rc
263 lines (228 loc) · 6.11 KB
/
.py-rc
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
set_pythonstartup() {
if [ -z "$PYTHONSTARTUP" ]; then
local py_dir="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
export PYTHONSTARTUP=$py_dir/startup.py
fi
}
set_pythonstartup
defn ve python -m virtualenv
virtualenv_activate() {
python -m virtualenv "$1"
. "$1/bin/activate"
}
defn vea virtualenv_activate
defn pidr pip install --dry-run
defn piu pip install --upgrade
defn piup pip install --upgrade pip
defn piuu pip install --user --upgrade
defn pii pip install
defn pif pip show
piie() {
pip install -e "${@:-.}"
}
export -f piie
defn pis pip search
defn piun pip uninstall
defn piuny pip uninstall -y
defn piuy pip uninstall -y
defn pui pip uninstall
defn puy pip uninstall -y
pip_reinstall() {
pip uninstall -y "$@"
pip install "$@"
}
export -f pip_reinstall
defn piri pip_reinstall
defn puyi pip_reinstall
piui() {
for arg in "$@"; do
pip uninstall "$arg"
pip install "$arg"
done
}
export -f piui
pil() {
if [ $# -eq 0 ]; then
pip list
else
args=()
for arg in "$@"; do
args+=("-e" "$arg")
done
pip list | grep -i "${args[@]}"
fi
}
export -f pil
pilc() {
if [ $# -eq 0 ]; then
pip list
else
pip list | grep "$@"
fi
}
export -f pilc
pir() {
if [ $# -eq 0 ]; then
pip install -r requirements.txt
else
pip install -r "$@"
fi
}
export -f pir
python_executable() {
python -c 'import sys; print(sys.executable)'
}
export -f python_executable
defn pex python_executable
defn pyx python_executable
defn ppe python_executable
python_executable_copy() {
if ! which copy &>/dev/null && ! declare -F copy &>/dev/null; then
echo '`copy` command not found' >&2
return 1
fi
python_executable | head -c -1 | tee >(copy)
}
export -f python_executable_copy
defn pxy python_executable_copy
defn pyi python -i
alias apy="alias-which python"
all_python_versions() {
alias-which python && \
python --version && \
(! type pyenv &>/dev/null || pyenv version) && \
python_executable
}
export -f all_python_versions
defn apv all_python_versions
export PYTHON_DEFAULT_VERSION=3.11.8
default_python_version() {
echo "$PYTHON_DEFAULT_VERSION"
}
export -f default_python_version
defn dpv default_python_version
defn pci python-check-import
defn ppp print-python-path
defn pmp python-module-path
defn py python-eval
alias pyv="python --version"
python_version() {
python --version 2>&1 | grep -o '[0-9.]\+'
}
defn pyvq python_version
python_major_version() {
python --version 2>&1 | grep -o '^Python [0-9]' | grep -o '[0-9]'
}
defn pymv python_major_version
epp() {
echo "$PYTHONPATH"
}
export -f epp
if [ -z "$CONDA" ]; then
if ! which pyenv &>/dev/null && [ -e "$HOME/.pyenv/bin" ]; then
export PATH="$HOME/.pyenv/bin:$PATH"
fi
if which pyenv &>/dev/null; then
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"
fi
fi
if which brew &>/dev/null; then
pyei() {
CPPFLAGS="-I$(brew --prefix zlib)/include -I$(brew --prefix sqlite)/include" pyenv install "$@"
}
else
defn pyei pyenv install
fi
install_pyenv() {
if which -a pyenv; then
echo "pyenv appears to already by installed" >&2
return 1
fi
local sudo=
if [ "$USER" != root ] && [ "$(whoami)" != root ]; then
sudo=sudo
fi
# Attempt to install Python dependencies for current OS, per https://github.com/pyenv/pyenv/wiki
local cmd
if which apt &>/dev/null; then
$sudo apt update -y || return 1
cmd=($sudo apt-get install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev curl git libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev)
elif which yum &>/dev/null; then
if [ -f /etc/system-release ] && grep -q "Amazon Linux release 2" /etc/system-release; then
cmd=($sudo yum install gcc make patch zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl11-devel tk-devel libffi-devel xz-devel)
else
cmd=($sudo yum install gcc make patch zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel libffi-devel xz-devel)
fi
elif which apk &>/dev/null; then
cmd=($sudo apk add --no-cache git bash build-base libffi-dev openssl-dev bzip2-dev zlib-dev xz-dev readline-dev sqlite-dev tk-dev)
elif which brew &>/dev/null; then
cmd=(brew install openssl readline sqlite3 xz zlib tcl-tk@8)
else
echo "Didn't find "'`apt`, `yum`, `apk`, or `brew`' >&2
return 1
fi
echo "Attempting to install dependencies: ${cmd[*]}" >&2
"${cmd[@]}" || return 1
curl https://pyenv.run | bash
source "$HOME/.bashrc"
local uname="$(uname)"
if [ -n "$1" ]; then
echo "Installing Python $1" >&2
pyenv install "$1" || return 1
pyenv global "$1"
python --version
fi
}
pyenv_list() {
if [ $# -gt 0 ]; then
pyenv versions | grep "$@"
else
pyenv versions
fi
}
export -f pyenv_list
defn pyl pyenv_list
defn pyg pyenv global
defn pysg pyenv global
defn pysl pyenv local
pyenv_search() {
pyenv install -l | grep "$@"
}
export -f pyenv_search
defn pys pyenv_search
defn pss simple-server
defn ptlf pytest --lf
defn pylf pytest --lf
defn pun pyenv uninstall
defn punf pyenv uninstall -f
defn pyrm pyenv uninstall
defn pyrmf pyenv uninstall -f
defn pyui pyenv uninstall
defn pyui pyenv uninstall -f
defn pyun pyenv uninstall
defn pyun pyenv uninstall -f
poetry_deps() {
yq -r -ptoml -oj '.tool.poetry.dependencies | to_entries | [ .[] | select(.key != "python") ] | map([.key, .value] | join("==")) | join("\n")' pyproject.toml
}
export -f poetry_deps
poetry_dev_deps() {
yq -r -ptoml -oj '.tool.poetry.dev-dependencies | to_entries | map([.key, .value] | join("==")) | join("\n")' pyproject.toml
}
export -f poetry_dev_deps
find_python_imports() {
git grep -e '^from [^\.]*' -e '^import [^\.]*' \
| sed 's/^[^:]\+://' \
| awk '{ print $1,$2 }' \
| awk -F. '{ print $1 }' \
| awk '{ print $2 }' \
| sort \
| uniq
}
export -f find_python_imports
defn fpi find_python_imports
maybe_init_conda
defn pcr pre-commit run -a -v
alias pyt=pytest