forked from FISCO-BCOS/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_env.sh
179 lines (162 loc) · 4.53 KB
/
init_env.sh
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
#!/bin/bash
shell_env=""
shell_rc=""
ZSH="/bin/sh"
REALZSH="/bin/zsh"
BASH="/bin/bash"
ZSHRC="${HOME}/.zshrc"
BASHRC="${HOME}/.bashrc"
LOG_WARN()
{
local content=${1}
echo -e "\033[31m[WARN] ${content}\033[0m"
}
LOG_ERROR()
{
local content=${1}
echo -e "\033[31m[ERROR] ${content}\033[0m"
exit 1
}
execute_cmd()
{
command="${1}"
#LOG_INFO "RUN: ${command}"
eval ${command}
ret=$?
if [ $ret -ne 0 ];then
LOG_ERROR "FAILED execution of command: ${command}"
exit 1
fi
}
LOG_INFO()
{
local content=${1}
echo -e "\033[32m[INFO] ${content}\033[0m"
}
# check python
check_python()
{
version_f1=$(execute_cmd "python -V 2>&1|awk '{print \$2}'|awk -F '.' '{print \$1}'")
# must be python 3
if [ $(($version_f1)) -lt 3 ];then
echo "1"
return
fi
# must be >= python 3.6
version_f2=$(execute_cmd "python -V 2>&1|awk '{print \$2}'|awk -F '.' '{print \$2}'")
#`python -V 2>&1|awk '{print $2}'|awk -F '.' '{print $2}'`
if [ $(($version_f2)) -lt 6 ];then
echo "1"
return
fi
echo "0"
}
install_pyenv()
{
local pyenv_str=$(grep "pyenv" ${shell_rc})
local pydir="${HOME}/.pyenv"
local pydir_virtual="${HOME}/.pyenv/plugins/pyenv-virtualenv"
LOG_INFO "clone and init pyenv to install python 3.7.3 !"
# clone pyenv
if [ ! -d "${pydir}" ];then
execute_cmd "git clone https://github.com/pyenv/pyenv.git ~/.pyenv"
fi
if [ ! -d "${pydir_virtual}" ];then
execute_cmd "git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv"
fi
if [ ! -z "${pyenv_str}" ] && [ -d "${pydir}" ] && [ -d "${pydir_virtual}" ];then
LOG_INFO "pyenv has already been inited!"
return
fi
# export env
execute_cmd "echo 'export PATH=~/.pyenv/bin:\$PATH' >> ${shell_rc}"
execute_cmd "echo 'export PYENV_ROOT=~/.pyenv' >> ${shell_rc}"
execute_cmd "echo 'eval \"\$(pyenv init -)\"' >> ${shell_rc}"
source ${shell_rc}
execute_cmd "echo 'eval \"\$(pyenv virtualenv-init -)\"' >> ~/.bash_profile"
LOG_INFO "init pyenv succeed!"
}
install_python3()
{
version=3.7.3
python_versions=$(pyenv versions | grep "${version}")
if [ -z "${python_versions}" ];then
execute_cmd "wget https://www.python.org/ftp/python/$version/Python-$version.tar.xz -P ~/.pyenv/cache/ && pyenv install $version"
fi
python_versions=$(pyenv virtualenvs | grep "python-sdk")
if [ -z "${python_versions}" ];then
execute_cmd "pyenv virtualenv ${version} python-sdk"
fi
}
init_config()
{
if [ ! -f "client_config.py" ];then
LOG_INFO "copy config file..."
execute_cmd "cp client_config.py.template client_config.py"
fi
solc_path=".py-solc/solc-v0.4.25/bin/solc"
if [ ! -f "${solc_path}" ];then
LOG_INFO "install solc v0.4.25..."
python -m solc.install v0.4.25
if [ $? -eq 1 ];then
if [ -d "${HOME}/.py-solc/solc-v0.4.25/" ];then
execute_cmd "rm -rf ~/.py-solc/solc-v0.4.25/"
fi
LOG_INFO "install solc v0.4.25 failed, try to install slocjs"
execute_cmd "npm install [email protected]"
fi
fi
}
python_init()
{
shell_env=$(echo $SHELL)
if [ "${shell_env}" = "${ZSH}" ] || [ "${shell_env}" = "${REALZSH}" ];then
shell_rc="${ZSHRC}"
# default is ~/.bashrc
else
shell_rc="${BASHRC}"
fi
local ret=$(check_python)
# need to install python
if [ ${ret} = "1" ];then
version="3.7.3"
which pyenv
# already install pyenv, then check the versions
if [ $? -eq 0 ];then
python_versions=$(pyenv versions | grep python-sdk)
if [ ! -z "${python_versions}" ];then
LOG_INFO "already install python ${version}, please activate the version with command: pyenv activate python-sdk"
return
fi
fi
install_pyenv
source ${shell_rc}
install_python3
LOG_INFO "install python ${version} success, please activate with command: pyenv activate python-sdk"
else
python_version=$(execute_cmd "python -V 2>&1")
LOG_INFO "python version already ${python_version} already meet requirement~"
fi
}
function help()
{
echo $1
cat << EOF
Usage:
-p <Optional> init python environment, install python-3.7.3 if current python version is lower than python-3.6
-i <Required> init the basic environment
EOF
}
main()
{
while getopts "pih" option; do
case ${option} in
p) python_init
;;
i) init_config
;;
h) help
esac
done
}
main $@