Modified python scripts to support both python2.7 and python3 natively

This commit is contained in:
Isaac 2014-02-11 04:30:58 +10:00
parent eb3f57ce34
commit 3351bfce2f
8 changed files with 58 additions and 30 deletions
scripts/devSetup

View file

@ -1,3 +1,4 @@
from __future__ import print_function
__author__ = u'schmatz'
from downloader import Downloader
import tarfile
@ -10,6 +11,12 @@ from dependency import Dependency
import shutil
from which import which
import subprocess
from stat import S_IRWXU,S_IRWXG,S_IRWXO
import sys
if sys.version_info.major >= 3:
raw_input = input
class Node(Dependency):
def __init__(self,configuration):
super(self.__class__, self).__init__(configuration)
@ -39,39 +46,42 @@ class Node(Dependency):
#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/..."
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)
os.chmod("/usr/local/bin/node",S_IRWXG|S_IRWXO|S_IRWXU)
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
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. "
print(u"Please enter y or n. ")
user_input = raw_input()
continue
break
if wants_to_upgrade:
import urllib2, urllib
print u"Retrieving npm update script..."
if sys.version_info.major < 3:
import urllib2, urllib
else:
import urllib.request as 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..."
print(u"Retrieved npm install script. Executing...")
subprocess.call([u"sh", npm_install_script_path])
print u"Updated npm version installed"
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]
(next(os.walk(self.downloader.download_directory))[1])[0]
def check_if_executable_installed(self,name):
executable_path = which(name)
if executable_path:
@ -98,15 +108,15 @@ class NodeDownloader(Downloader):
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
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..."
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
print(u"Decompressed Node into " + self.download_directory)
def check_download(self):
isFileValid = tarfile.is_tarfile(self.downloaded_file_path)