Repository navigation
Expand file tree
/
Copy pathnode.py
More file actions
143 lines (128 loc) · 5.65 KB
/
Copy pathnode.py
File metadata and controls
143 lines (128 loc) · 5.65 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
__author__ = u'schmatz'
from downloader import Downloader
import tarfile
import errors
from errors import DownloadCorruptionError
import warnings
import os
from configuration import Configuration
from dependency import Dependency
import shutil
from which import which
import subprocess
class Node(Dependency):
def __init__(self,configuration):
super(self.__class__, self).__init__(configuration)
operating_system = configuration.system.operating_system
self.config.directory.create_directory_in_tmp(u"node")
if operating_system == u"mac":
self.downloader = MacNodeDownloader(self)
elif operating_system == u"win":
self.downloader = WindowsNodeDownloader(self)
self.config.directory.create_directory_in_bin(u"node") #TODO: Fix windows node installation
elif operating_system == u"linux":
self.downloader = LinuxNodeDownloader(self)
@property
def tmp_directory(self):
return self.config.directory.tmp_directory
@property
def bin_directory(self):
return self.config.directory.bin_directory
def download_dependencies(self):
self.downloader.download()
self.downloader.decompress()
def bashrc_string(self):
return "COCO_NODE_PATH=" + self.config.directory.bin_directory + os.sep + u"node" + os.sep + "bin" + os.sep +"node"
def install_dependencies(self):
install_directory = self.config.directory.bin_directory + os.sep + u"node"
#check for node here
unzipped_node_path = self.findUnzippedNodePath()
if self.config.system.operating_system in ["mac","linux"] and not which("node"):
print "Copying node into /usr/local/bin/..."
shutil.copy(unzipped_node_path + os.sep + "bin" + os.sep + "node","/usr/local/bin/")
os.chmod("/usr/local/bin/node",0777)
shutil.copytree(self.findUnzippedNodePath(),install_directory)
wants_to_upgrade = True
if self.check_if_executable_installed(u"npm"):
warning_string = u"A previous version of npm has been found. \nYou may experience problems if you have a version of npm that's too old.Would you like to upgrade?(y/n) "
from distutils.util import strtobool
print warning_string
#for bash script, you have to somehow redirect stdin to raw_input()
user_input = raw_input()
while True:
try:
wants_to_upgrade = strtobool(user_input)
except:
print u"Please enter y or n. "
continue
break
if wants_to_upgrade:
import urllib2, urllib
print u"Retrieving npm update script..."
npm_install_script_path = install_directory + os.sep + u"install.sh"
urllib.urlretrieve(u"https://npmjs.org/install.sh",filename=npm_install_script_path)
print u"Retrieved npm install script. Executing..."
subprocess.call([u"sh", npm_install_script_path])
print u"Updated npm version installed"
def findUnzippedNodePath(self):
return self.downloader.download_directory + os.sep + \
(os.walk(self.downloader.download_directory).next()[1])[0]
def check_if_executable_installed(self,name):
executable_path = which(name)
if executable_path:
return True
else:
return False
def check_node_version(self):
version = subprocess.check_output(u"node -v")
return version
def check_npm_version(self):
version = subprocess.check_output(u"npm -v")
return version
class NodeDownloader(Downloader):
@property
def download_url(self):
raise NotImplementedError
@property
def download_directory(self):
return self.dependency.tmp_directory + os.sep + u"node"
@property
def downloaded_file_path(self):
return self.download_directory + os.sep + u"node.tgz"
def download(self):
print u"Downloading Node from URL " + self.download_url
self.download_file(self.download_url,self.downloaded_file_path)
self.check_download()
def decompress(self):
print u"Decompressing Node..."
tfile = tarfile.open(self.downloaded_file_path)
#TODO: make directory handler class
tfile.extractall(self.download_directory)
print u"Decompressed Node into " + self.download_directory
def check_download(self):
isFileValid = tarfile.is_tarfile(self.downloaded_file_path)
if not isFileValid:
raise DownloadCorruptionError(u"Node download was corrupted.")
class LinuxNodeDownloader(NodeDownloader):
@property
def download_url(self):
if self.dependency.config.mem_width == 64:
return u"http://nodejs.org/dist/v0.10.24/node-v0.10.24-linux-x64.tar.gz"
else:
return u"http://nodejs.org/dist/v0.10.24/node-v0.10.24-linux-x86.tar.gz"
class WindowsNodeDownloader(NodeDownloader):
@property
def download_url(self):
raise NotImplementedError(u"Needs MSI to be executed to install npm")
#"http://nodejs.org/dist/v0.10.24/x64/node-v0.10.24-x64.msi"
if self.dependency.config.mem_width == 64:
return u"http://nodejs.org/dist/v0.10.24/x64/node.exe"
else:
return u"http://nodejs.org/dist/v0.10.24/node.exe"
class MacNodeDownloader(NodeDownloader):
@property
def download_url(self):
if self.dependency.config.mem_width == 64:
return u"http://nodejs.org/dist/v0.10.24/node-v0.10.24-darwin-x64.tar.gz"
else:
return u"http://nodejs.org/dist/v0.10.24/node-v0.10.24-darwin-x86.tar.gz"