2014-02-10 13:30:58 -05:00
from __future__ import print_function
2014-01-03 13:32:13 -05:00
__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
2014-02-10 13:30:58 -05:00
from stat import S_IRWXU , S_IRWXG , S_IRWXO
import sys
if sys . version_info . major > = 3 :
raw_input = input
2014-01-03 13:32:13 -05:00
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 ) :
2014-04-11 23:37:06 -04:00
install_directory = self . config . directory . bin_directory + os . sep + u " node "
if os . path . exists ( install_directory ) :
print ( u " Skipping Node download because " + install_directory + " exists. " )
else :
self . downloader . download ( )
self . downloader . decompress ( )
2014-01-03 13:32:13 -05:00
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 "
2014-01-05 19:51:52 -05:00
#check for node here
2014-01-03 13:32:13 -05:00
if self . config . system . operating_system in [ " mac " , " linux " ] and not which ( " node " ) :
2014-04-11 23:37:06 -04:00
unzipped_node_path = self . findUnzippedNodePath ( )
2014-02-10 13:30:58 -05:00
print ( " Copying node into /usr/local/bin/... " )
2014-01-03 13:32:13 -05:00
shutil . copy ( unzipped_node_path + os . sep + " bin " + os . sep + " node " , " /usr/local/bin/ " )
2014-02-10 13:30:58 -05:00
os . chmod ( " /usr/local/bin/node " , S_IRWXG | S_IRWXO | S_IRWXU )
2014-04-11 22:58:52 -04:00
if os . path . exists ( install_directory ) :
print ( u " Skipping creation of " + install_directory + " because it exists. " )
else :
2014-04-11 23:37:06 -04:00
unzipped_node_path = self . findUnzippedNodePath ( )
2014-04-11 22:58:52 -04:00
shutil . copytree ( self . findUnzippedNodePath ( ) , install_directory )
2014-01-03 13:32:13 -05:00
wants_to_upgrade = True
if self . check_if_executable_installed ( u " npm " ) :
2015-01-27 17:44:53 -05:00
warning_string = u " A previous version of npm has been found. \n You may experience problems if you have a version of npm that ' s too old. Would you like to upgrade?(y/n) "
2014-01-03 13:32:13 -05:00
from distutils . util import strtobool
2014-02-10 13:30:58 -05:00
print ( warning_string )
2014-01-03 13:32:13 -05:00
#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 :
2014-02-10 13:30:58 -05:00
print ( u " Please enter y or n. " )
2014-01-11 01:36:48 -05:00
user_input = raw_input ( )
2014-01-03 13:32:13 -05:00
continue
break
if wants_to_upgrade :
2014-02-10 13:30:58 -05:00
if sys . version_info . major < 3 :
import urllib2 , urllib
else :
import urllib . request as urllib
print ( u " Retrieving npm update script... " )
2014-01-03 13:32:13 -05:00
npm_install_script_path = install_directory + os . sep + u " install.sh "
urllib . urlretrieve ( u " https://npmjs.org/install.sh " , filename = npm_install_script_path )
2014-02-10 13:30:58 -05:00
print ( u " Retrieved npm install script. Executing... " )
2014-01-03 13:32:13 -05:00
subprocess . call ( [ u " sh " , npm_install_script_path ] )
2014-02-10 13:30:58 -05:00
print ( u " Updated npm version installed " )
2014-01-03 13:32:13 -05:00
def findUnzippedNodePath ( self ) :
return self . downloader . download_directory + os . sep + \
2014-02-10 13:30:58 -05:00
( next ( os . walk ( self . downloader . download_directory ) ) [ 1 ] ) [ 0 ]
2014-01-03 13:32:13 -05:00
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 ) :
2014-02-10 13:30:58 -05:00
print ( u " Downloading Node from URL " + self . download_url )
2014-01-03 13:32:13 -05:00
self . download_file ( self . download_url , self . downloaded_file_path )
self . check_download ( )
def decompress ( self ) :
2014-02-10 13:30:58 -05:00
print ( u " Decompressing Node... " )
2014-01-03 13:32:13 -05:00
tfile = tarfile . open ( self . downloaded_file_path )
#TODO: make directory handler class
tfile . extractall ( self . download_directory )
2014-02-10 13:30:58 -05:00
print ( u " Decompressed Node into " + self . download_directory )
2014-01-03 13:32:13 -05:00
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 :
2014-12-29 15:22:49 -05:00
return u " http://nodejs.org/dist/v0.10.35/node-v0.10.35-linux-x64.tar.gz "
2014-01-03 13:32:13 -05:00
else :
2014-12-29 15:22:49 -05:00
return u " http://nodejs.org/dist/v0.10.35/node-v0.10.35-linux-x86.tar.gz "
2014-01-03 13:32:13 -05:00
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 :
2014-12-29 15:22:49 -05:00
return u " http://nodejs.org/dist/v0.10.35/x64/node.exe "
2014-01-03 13:32:13 -05:00
else :
2014-12-29 15:22:49 -05:00
return u " http://nodejs.org/dist/v0.10.35/node.exe "
2014-01-03 13:32:13 -05:00
class MacNodeDownloader ( NodeDownloader ) :
@property
def download_url ( self ) :
if self . dependency . config . mem_width == 64 :
2014-12-29 15:22:49 -05:00
return u " http://nodejs.org/dist/v0.10.35/node-v0.10.35-darwin-x64.tar.gz "
2014-01-03 13:32:13 -05:00
else :
2014-12-29 15:22:49 -05:00
return u " http://nodejs.org/dist/v0.10.35/node-v0.10.35-darwin-x86.tar.gz "