/
python
executable file
·210 lines (178 loc) · 6.92 KB
/
python
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
#!/bin/bash
echo "Beginning ISStreamer Python Easy Installation!"
echo " _________ "
echo " NN NN "
echo " N N "
echo " N NNNNNNN N "
echo " N NN N N"
echo "NNNNNNN N N N N"
echo "N N N N N N"
echo "N N N N N N"
echo " "
echo "NNNN NN NNNNNNNN NNNNNNN"
echo "N N N N N N"
echo "N N N N N N"
echo "N N NN N NNNNNNN"
echo " N NNNNNNN N "
echo " N N "
echo " NN NN "
echo " --------- "
echo "This may take a couple minutes to install, grab some coffee :)"
echo "But don't forget to come back, I'll have questions later!"
echo ""
INITIALSTATE_AUTH_SVC=${INITIALSTATE_AUTH_SVC:="auth"}
function check_for_easy_install {
if hash easy_install 2>/dev/null; then
easy_install_version=$(easy_install --version)
echo "Found easy_install: $easy_install_version"
else
echo "easy_install not found, installing now..."
if hash apt-get 2>/dev/null; then
if [[ $1 -eq '-v' ]]; then
apt-get -y install python-setuptools
else
apt-get -y install python-setuptools > /dev/null
fi
else
echo "no apt-get, using curl..."
if [[ $1 -eq '-v' ]]; then
curl https://bootstrap.pypa.io/ez_setup.py -o - | python
else
curl https://bootstrap.pypa.io/ez_setup.py -o - | python > /dev/null
fi
fi
fi
}
function check_for_pip {
if hash pip 2>/dev/null; then
pip_version=$(pip --version)
echo "Found pip: $pip_version"
pip_version_array=(${pip_version//" "/ })
pip_version_num=${pip_version_array[1]}
pip_version_num_array=(${pip_version_num//./ })
pip_major_version=${pip_version_num_array[0]}
pip_minor=${pip_version_num_array[1]}
pip_minor_version_array=(${pip_minor//"rc"/ })
pip_minor_version=${pip_minor_version_array[0]}
echo "pip major version: $pip_major_version"
echo "pip minor version: $pip_minor_version"
else
echo "pip not found, installing now..."
if [[ $1 -eq '-v' ]]; then
easy_install pip
else
easy_install pip > /dev/null
fi
fi
}
function check_for_isstreamer {
isstreamer=$(pip freeze | grep ISStreamer)
if [ -z "$isstreamer" ]; then
echo "ISStreamer not found, installing..."
pip install ISStreamer
else
echo "ISStreamer found, updating..."
pip install --upgrade ISStreamer
fi
isstreamer_version=$(python -c "import ISStreamer.version; print(ISStreamer.version.__version__)")
if [[ $isstreamer_version == "" ]] ; then
echo "No ISStreamer found..."
exit 1
else
echo "Found ISStreamer: $isstreamer_version"
isstreamer_version_array=(${isstreamer_version//" "/ })
isstreamer_version_num=${isstreamer_version_array[0]}
isstreamer_version_num_array=(${isstreamer_version_num//./ })
isstreamer_major_version=${isstreamer_version_num_array[0]}
isstreamer_minor=${isstreamer_version_num_array[1]}
isstreamer_minor_version_array=(${isstreamer_minor//"rc"/ })
isstreamer_minor_version=${isstreamer_minor_version_array[0]}
echo "isstreamer major version: $isstreamer_major_version"
echo "isstreamer minor version: $isstreamer_minor_version"
fi
}
function download_script {
echo "Please select which Initial State app you're using: "
echo "1) app.initialstate.com"
echo "2) [NEW!] iot.app.initialstate.com"
echo -n "Enter choice 1 or 2: "
read app_version < /dev/tty
if [ "$app_version" = "1" ]; then
echo -n "Enter app.initialstate.com user email: "
read username < /dev/tty
echo -n "Enter app.initialstate.com password [input hidden]: "
read -s password < /dev/tty
echo ""
if [ -z "$example_location" ]; then
example_location="./is_example.py"
fi
python_example=$(curl -X POST "https://api.initialstate.com/api/v1/python/example" -H "X-USER: $username" -H "X-SEC: $password" -H "Accept-Content: text/plain" -m 30 -d "" -s)
if [ "$python_example" = "INVALID_CREDENTIALS" ]; then
echo "invalid credentials, let's try that again!"
download_script
else
echo "$python_example" | sudo -u $SUDO_USER tee "$example_location" > /dev/null
fi
else
echo -n "Enter iot.app.initialstate.com user email: "
read username < /dev/tty
echo -n "Enter iot.app.initialstate.com password [input hidden]: "
read -s password < /dev/tty
echo ""
if [ -z "$example_location" ]; then
example_location="./is_example.py"
fi
echo "getting python example script"
if [ "$INITIALSTATE_AUTH_SVC" != "auth" ]; then
echo "using $INITIALSTATE_AUTH_SVC"
fi
python_example=$(curl -X POST "https://api.init.st/$INITIALSTATE_AUTH_SVC/python/example" -H "X-USER: $username" -H "X-PASS: $password" -H "Accept-Content: text/plain" -m 30 -d "" -s)
if [ "$python_example" = "INVALID_CREDENTIALS" ]; then
echo "invalid credentials, let's try that again!"
download_script
elif [ "$python_example" = "NOT_CURRENT" ]; then
echo "your account hasn't been migrated, go to iot.app.initialstate.com to sign in or create a new account"
download_script
elif [ "$python_example" = "{\"message\":\"Missing Authentication Token\"}" ]; then
echo "there's currently an issue with this endpoint, try again later or email support@initialstate.com. Thanks!"
exit 1
else
echo "$python_example" | sudo -u $SUDO_USER tee "$example_location" > /dev/null
fi
fi
}
function download_script_old {
echo -n "Enter app.initialstate.com user email: "
read username < /dev/tty
echo -n "Enter app.initialstate.com password [input hidden]: "
read -s password < /dev/tty
echo ""
if [ -z "$example_location" ]; then
example_location="./is_example.py"
fi
python_example=$(curl -X POST "https://api.initialstate.com/api/v1/python/example" -H "X-USER: $username" -H "X-SEC: $password" -H "Accept-Content: text/plain" -m 30 -d "" -s)
if [ "$python_example" = "INVALID_CREDENTIALS" ]; then
echo "invalid credentials, let's try that again!"
download_script
else
echo "$python_example" | sudo -u $SUDO_USER tee "$example_location" > /dev/null
fi
}
function setup_first_script {
echo -n "Do you want automagically get an example script? [y/N] "
read is_wanted < /dev/tty
if [ "$is_wanted" = "y" ] || [ "$is_wanted" = "Y" ]; then
echo "Where do you want to save the example? [default: ./is_example.py]: "
read example_location < /dev/tty
if [ "$isstreamer_major_version" -gt "0" ]; then
download_script
else
download_script_old
fi
fi
echo "All done!"
}
check_for_easy_install
check_for_pip
check_for_isstreamer
setup_first_script