forked from audstanley/NodeJs-Raspberry-Pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-install
executable file
·250 lines (232 loc) · 10.6 KB
/
node-install
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
#!/usr/bin/python
# Richard Stanley
import os
if os.geteuid() != 0:
exit("You need to run node-installer as root.")
import re
import imp
import argparse
from subprocess import Popen, PIPE
class col:
BLUE = '\033[34m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
CYAN = '\033[36m'
MAGENTA = '\033[35'
RED = '\033[31m'
DEFAULT = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def printLogo():
print( col.CYAN + " ; " + col.DEFAULT)
print( col.CYAN + " +++ " + col.DEFAULT)
print( col.CYAN + " +++ " + col.DEFAULT)
print( col.CYAN + " +++ " + col.DEFAULT)
print( col.CYAN + " ''++'' :;;', ,+++;+++ ''++'' " + col.DEFAULT)
print( col.CYAN + ":++++++++++: ;;;;;;;'''' '++++'+++++ :++++++++++:" + col.DEFAULT)
print( col.CYAN + "+++. .++' ';;;;;;;;'' +++ +++ '++. '' .+++" + col.DEFAULT)
print( col.CYAN + "+++. .++' '';;;;;;;;' +++ +++ '++. '' '' " + col.DEFAULT)
print( col.CYAN + "+++. .++' ''';;;;;;;; '++++'+++++ :+++++, " + col.DEFAULT)
print( col.CYAN + ": , '';;;, ,+++; ''++'. " + col.DEFAULT)
print( col.GREEN + " #+''#';+''+' " + col.DEFAULT)
print( col.GREEN + " ';;;+#'+;;' " + col.DEFAULT)
print( col.RED + " .###''@#@ " + col.DEFAULT)
print( col.RED + " '@++@@'+#@ " + col.DEFAULT)
print( col.RED + " :'@'''@'''''+ " + col.DEFAULT)
print( col.RED + " ##@@'''#@+#@ " + col.DEFAULT)
print( col.RED + " .'''#''@''# " + col.DEFAULT)
print( col.RED + " '#'''#' " + col.DEFAULT)
print("\n")
def checkForPip():
o, e = Popen(['whereis', 'pip'], stdout=PIPE, stderr=PIPE).communicate()
o = o.split()
if '/usr/local/bin/pip' in o or '/usr/bin/pip' in o:
return True
else:
return False
def checkForRequests():
try:
imp.find_module('requests')
return True
except ImportError:
return False
def installDependencies():
if not checkForPip():
print(col.CYAN + "Updating Repositories..." + col.DEFAULT)
o, e = Popen(['apt-get', 'update'], stdout=PIPE, stderr=PIPE).communicate()
print(col.CYAN + "Need to install some dependencies, this may take a few minutes..." + col.DEFAULT)
print(col.CYAN + "Installing Python Pip..." + col.DEFAULT)
o, e = Popen(['apt-get', 'install', 'python-pip', '-y'], stdout=PIPE, stderr=PIPE).communicate()
if not checkForRequests():
print(col.CYAN + "Installing Python Requests Library..." + col.DEFAULT)
o, e = Popen(['pip', 'install', 'requests'], stdout=PIPE, stderr=PIPE).communicate()
if checkForPip and checkForRequests():
return True
return False
modules = list()
try:
modules = os.listdir('/usr/lib/node_modules')
except:
pass
def modulesInstall():
for v in modules:
o = Popen(['npm', 'i', v, 'g'], stdout=PIPE, stderr=PIPE).communicate()[0].decode('utf-8')
print(o)
def checkForTheRequestsLibrary():
try:
import requests
except ImportError:
try:
import pip
except ImportError:
o = Popen(['apt-get', 'install', 'python-pip', '-y'], stdout=PIPE, stderr=PIPE).communicate()[0].decode('utf-8')
o = Popen(['pip', 'install', 'requests'], stdout=PIPE, stderr=PIPE).communicate()[0].decode('utf-8')
def InstallSpecificNodeVersion(v):
v = v.replace('\n','')
v = v.replace('\r','')
uname = Popen(['uname', '-m'], stdout=PIPE, stderr=PIPE).communicate()[0].decode('utf-8')
uname = uname.replace('\n','')
uname = uname.replace('\r','')
if uname == 'armv7l' or uname == 'armv6l' or uname == 'arm64':
# Get a list of old global npm modules (if you were using node previously):
oldGlobals = list()
try:
os.chdir('/opt/nodejs/lib/node_modules')
oldGlobals = os.listdir('./')
oldGlobals = list(filter(lambda x: x != 'npm', oldGlobals))
print(col.CYAN + "npm Globals currently installed: " + col.DEFAULT + str(oldGlobals))
except:
pass
# Move to a temporary Directory
os.chdir('/')
try:
os.mkdir('tempNode-' + v)
except:
pass
os.chdir('/tempNode-' + v)
print(col.CYAN + "Creating, and Changing into directory:" + col.DEFAULT + "/tempNode-" + v)
# Download and Install:
link = 'https://nodejs.org/dist/v' + v + '/node-v' + v + '-linux-' + uname + '.tar.gz'
fname = 'node-v' + v + '-linux-' + uname + '.tar.gz'
folderName = '/tempNode-' + v + '/node-v' + v + '-linux-' + uname + '/'
upperFolderName = '/tempNode-' + v + '/'
print(col.CYAN + "Downloading: " + col.DEFAULT + link)
o = Popen([ 'wget', link ], stdout=PIPE, stderr=PIPE).communicate()[0].decode('utf-8')
print(col.CYAN + "Extracting: " + col.DEFAULT + fname)
Popen([ 'tar', '-xzf', fname ], stdout=PIPE, stderr=PIPE).communicate()[0].decode('utf-8')
# Delete tar file after extraction:
Popen([ 'rm', fname ], stdout=PIPE, stderr=PIPE).communicate()[0].decode('utf-8')
# Delete older version of NodeJs:
Popen([ 'rm', '-R', '-f', '/opt/nodejs/' ], stdout=PIPE, stderr=PIPE).communicate()[0].decode('utf-8')
# Delete all symlinks:
Popen([ 'rm',
'/usr/bin/node',
'/usr/bin/nodejs',
'/usr/lib/nodejs',
'/usr/sbin/node',
'/sbin/node',
'/sbin/node',
'/usr/local/bin/node',
'/usr/bin/npm',
'/usr/sbin/npm',
'/sbin/npm',
'/usr/local/bin/npm' ], stdout=PIPE, stderr=PIPE).communicate()[0].decode('utf-8')
Popen([ 'mv', folderName, '/opt/nodejs/' ], stdout=PIPE, stderr=PIPE).communicate()[0].decode('utf-8')
Popen([ 'update-alternatives', '--install','/usr/bin/node','node','/opt/nodejs/bin/node','1' ], stdout=PIPE, stderr=PIPE).communicate()[0].decode('utf-8')
Popen([ 'update-alternatives', '--install','/usr/bin/npm','npm','/opt/nodejs/bin/npm','1' ], stdout=PIPE, stderr=PIPE).communicate()[0].decode('utf-8')
for i,k in enumerate(oldGlobals):
print(col.CYAN + "Reinstalling global npm modules: " + col.YELLOW + str(i+1) + ". " + col.DEFAULT + k)
Popen([ 'npm', 'i', k, '-g' ], stdout=PIPE, stderr=PIPE).communicate()[0].decode('utf-8')
try:
os.chdir('/opt/nodejs/bin/')
bins = os.listdir('./')
bins = list(filter(lambda x: x != 'node', bins))
bins = list(filter(lambda x: x != 'npm', bins))
print(col.CYAN + 'Binaries to Update: ' + col.DEFAULT + str(bins))
for i,k in enumerate(bins):
print(col.CYAN + 'Adding Binaries: ' + col.YELLOW + str(i) + '. ' + col.DEFAULT + k)
Popen([ 'update-alternatives', '--install','/usr/bin/' + k, k,'/opt/nodejs/bin/' + k,'1' ], stdout=PIPE, stderr=PIPE).communicate()[0].decode('utf-8')
except:
pass
# Delete the temporary files:
Popen([ 'rm', '-R', '-f', upperFolderName ], stdout=PIPE, stderr=PIPE).communicate()[0].decode('utf-8')
print(col.YELLOW + "Installation is Complete." + col.DEFAULT)
else:
exit("You are not running this installer on an ARM based processor.")
def validVersionCheck(st):
regex = re.compile(r"(\d{1,2})\.?(\d{1,2})?\.?(\d{1,2})?")
r = re.search(regex, st)
if r:
checkForTheRequestsLibrary()
a,b,c = r.groups()
if a == '0':
exit(col.RED + "Cannot install Node version: 0.X.X, You must install a version 4.0.0 or above" + col.DEFAULT)
if a is not None and b is not None and c is not None:
import requests
req = requests.get('https://nodejs.org/dist/')
regex2 = re.compile(r'<a href="v(\d{1,2}\.\d{1,2}\.\d{1,2})')
r2 = re.search(regex2, req.text)
if r2:
m2 = regex2.findall(req.text)
if len(m2) > 0:
m2 = list(map(lambda x : str(x), m2))
m2 = list(filter(lambda x : x[0] != '0', m2))
versionString = ".".join([a,b,c])
if versionString in m2:
InstallSpecificNodeVersion(versionString)
else:
exit(col.RED + "There is no version of Node: " + versionString + " available to install." + col.DEFAULT)
elif a is not None:
import requests
req = requests.get('https://nodejs.org/dist/')
regex2 = re.compile(r'<a href="v((\d{1,2})\.\d{1,2}\.\d{1,2})')
r2 = re.search(regex2, req.text)
if r2:
m2 = regex2.findall(req.text)
m2 = list(map(lambda x : (str(x[0]), str(x[1])), m2))
m2 = list(filter(lambda x : x[1] != '0', m2))
m2 = list(filter(lambda x : x[1] == a, m2))
m2 = list(map(lambda x : x[0], m2))
m2.reverse()
print(col.YELLOW + "Select from list to install specific version of Node " + str(a) + ".X.X:" + col.DEFAULT)
for i,k in enumerate(m2):
print(col.CYAN + str(i+1) + ".\t" + col.DEFAULT + k)
try:
uInput = input(col.CYAN + "Selection: " + col.DEFAULT)
if type(uInput) == int:
if uInput > 0 and uInput < len(m2) + 1:
InstallSpecificNodeVersion(m2[uInput-1])
else:
exit(col.RED + "That was not a valid number selection, Please input an integer from the list." + col.DEFAULT)
except:
exit(col.RED + "That was not a valid selection, Please input an integer from the list." + col.DEFAULT)
def installLatestVersion():
import requests
req1 = requests.get('https://nodejs.org/dist/')
regex1 = re.compile(r'<a href="latest-v(\d{1,2})')
r1 = re.search(regex1, req1.text)
if r1:
m1 = regex1.findall(req1.text)
m1 = list(map(lambda x : int(x), m1))
mx = max(m1)
req2 = requests.get('https://nodejs.org/dist/latest-v' + str(mx) + '.x/')
regex2 = re.compile(r'<a href="node-v(\d{1,2}\.\d{1,2}\.\d{1,2})')
r2 = re.search(regex2, req2.text)
if r2:
m2 = r2.group(1)
InstallSpecificNodeVersion(m2)
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--version', help='Installs the NodeJs version specified. Must pass in the version number', action='store', required=False, dest='version')
parser.add_argument("-a", '--automatic', action='store_true', help='Automaically installs the latest version of NodeJs', default=False, dest='automatic')
args = parser.parse_args()
if args.version and not args.automatic:
printLogo()
installDependencies()
validVersionCheck(args.version)
elif args.automatic and not args.version:
printLogo()
installDependencies()
installLatestVersion()
else:
print("You cannot select a version: " + args.version + " and auto install the latest version at the same time.")