-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-install-approximately.sh
More file actions
executable file
·146 lines (124 loc) · 4.02 KB
/
python-install-approximately.sh
File metadata and controls
executable file
·146 lines (124 loc) · 4.02 KB
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
#!/usr/bin/env bash
set -Eeuo pipefail
## Check url for valid link
__validate_url() {
if [[ $# -lt 1 ]]; then
printf "%s" \
"__validate_url requires at least one argument: <list-urls-to-check>"
return 2
fi
local url re domain domain_length
local -i error_count=0
## Strict check
## Schema
re='^(https?|ftp)://'
## Auth
re+='([^\/@]+(:([^\/@]|%[0-9a-fA-F]{2})*)?@)?'
## Domain
re+='(([a-zA-Z0-9-]{1,63}\.)+[a-zA-Z]{2,63}|'
## IPv4
re+='((25[0-5]|2[0-4][0-9]'
re+='|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|'
re+='[01]?[0-9][0-9]?)|'
## IPv6
re+='(\[(([a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}|'
re+='(:[a-fA-F0-9]{1,4}){1,7}|'
re+='[a-fA-F0-9]{1,4}(:[a-fA-F0-9]{1,4}){1,7}|'
re+='([a-fA-F0-9]{1,4}:){1,6}:[a-fA-F0-9]{1,4}|'
re+='([a-fA-F0-9]{1,4}:){1,5}(:[a-fA-F0-9]{1,4}){1,2}|'
re+='([a-fA-F0-9]{1,4}:){1,4}(:[a-fA-F0-9]{1,4}){1,3}|'
re+='([a-fA-F0-9]{1,4}:){1,3}(:[a-fA-F0-9]{1,4}){1,4}|'
re+='([a-fA-F0-9]{1,4}:){1,2}(:[a-fA-F0-9]{1,4}){1,5}|'
re+='[a-fA-F0-9]{1,4}:((:[a-fA-F0-9]{1,4}){1,6})|'
re+=':((:[a-fA-F0-9]{1,4}){1,7}|:)|'
re+='fe80:(:[a-fA-F0-9]{0,4}){0,4}%[0-9a-zA-Z]+|'
re+='::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}'
re+='(25[0-5]|(2[0-4]|1?[0-9])?[0-9])|([a-fA-F0-9]{1,4}:){1,4}'
re+=':((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])'
re+='?[0-9]))\]))'
## Port
re+='(:[0-9]{1,5})?'
## Path
re+='(\/[^[:space:]?#]*)?'
## Query
re+='(\?[^[:space:]#<>]*)?'
## Fragment
re+='(\#[^[:space:]]*)?$'
for url in "$@"; do
## Check main catch
[[ ${url} =~ ${re} ]] || {
: $((error_count++))
continue
}
## Check domain length
if [[ ${url} =~ ://([^/@:]+) ]]; then
domain=${BASH_REMATCH[1]%:*}
[[ -n ${domain} ]] || {
: $((error_count++))
continue
}
domain_length=${#domain}
if ((domain_length > 253)); then
: $((error_count++))
continue
fi
fi
done
return "${error_count}"
}
## Check first arg on exist
: "${1:?Define python version or URL and try again}"
install_python() {
local full_version python_revision
local desired="${1}"
## Check if identity is link or specific version
if __validate_url "${desired}"; then
## Install deps
apt-install.sh curl tar
## Download and install python from URL
curl --silent \
"${desired}" \
| tar -C "/usr/local" --strip-components 1 -zx
## Remove deps
apt-env.sh apt-remove.sh curl
## Define variables for /etc/environment
python_revision='Installed-from-URL'
## Else installed base component form aptitude source
else
## Update cache
apt-env.sh apt-get update -qq
## Search python version on repository if received version is approximately
full_version=$(apt-cache show python3-minimal \
| grep -E '^Version:' \
| grep "${desired}" \
| sort -rV \
| head -n1 \
| awk '{print $2}' || echo '')
## Return error if version is not find
[[ -n ${full_version} ]] || {
echo "[Error]: Could not find python3 version matching '${desired}'" >&2
return 1
}
## Install python with deps
apt-install.sh \
"python3-minimal=${full_version}" \
python3-pip \
python3-venv
## Define variables for /etc/environment
python_revision="${full_version}"
fi
## Clean cache
apt-clean.sh
## Filling /etc/environment
local python_major_minor_patch_version python_major_minor_version
python_major_minor_patch_version=$(python3 -c 'import platform; major, minor, patch = platform.python_version_tuple();print(f"{major}.{minor}.{patch}")')
python_major_minor_version=$(python3 -c 'import platform; major, minor, patch = platform.python_version_tuple();print(f"{major}.{minor}")')
{
echo "PYTHON_MAJOR_MINOR_PATCH_VERSION=${python_major_minor_patch_version}"
echo "PYTHON_MAJOR_MINOR_VERSION=${python_major_minor_version}"
echo "PYTHON_REVISION=${python_revision}"
echo "BEGIN_BUILD_IN_EPOCH=$(date '+%s')"
} >>/etc/environment
}
install_python "${1}"
exit 0