From 3351bfce2febf19fce4b370b80e9ef86112a27ec Mon Sep 17 00:00:00 2001 From: Isaac Date: Tue, 11 Feb 2014 04:30:58 +1000 Subject: [PATCH] Modified python scripts to support both python2.7 and python3 natively --- CONTRIBUTING.md | 2 +- bin/coco-run | 5 ++++ scripts/devSetup/downloader.py | 13 +++++++--- scripts/devSetup/mongo.py | 11 +++++--- scripts/devSetup/node.py | 34 ++++++++++++++++--------- scripts/devSetup/repositoryInstaller.py | 13 +++++----- scripts/devSetup/ruby.py | 5 ++-- scripts/devSetup/systemConfiguration.py | 5 +++- 8 files changed, 58 insertions(+), 30 deletions(-) create mode 100755 bin/coco-run diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2a280174d..42f4e7800 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,4 +4,4 @@ It just grants us a non-exclusive license to use your contribution and certifies you have the right to contribute the code you submit. For both our sakes, we need this before we can accept a pull request. Don't worry, it's super easy. -For more info, see [http://codecombat.com/legal](http://codecombat.com/legal). \ No newline at end of file +For more info, see [http://codecombat.com/legal](http://codecombat.com/legal). diff --git a/bin/coco-run b/bin/coco-run new file mode 100755 index 000000000..9524c2b69 --- /dev/null +++ b/bin/coco-run @@ -0,0 +1,5 @@ +#!/bin/bash + +./coco-mongodb & +./coco-brunch & +./coco-dev-server diff --git a/scripts/devSetup/downloader.py b/scripts/devSetup/downloader.py index 9c016f883..477af3b44 100644 --- a/scripts/devSetup/downloader.py +++ b/scripts/devSetup/downloader.py @@ -1,6 +1,11 @@ +from __future__ import print_function __author__ = 'schmatz' from configuration import Configuration -import urllib +import sys +if sys.version_info.major < 3: + import urllib +else: + import urllib.request as urllib from dependency import Dependency class Downloader: def __init__(self,dependency): @@ -27,10 +32,10 @@ class Downloader: progress_fraction = float(amount_of_data_downloaded_so_far) / float(totalsize) progress_percentage = progress_fraction * 1e2 stringToDisplay = '\r[{0}] {1:.1f}%'.format('#'*int(bars_to_display*progress_fraction),progress_percentage) - print stringToDisplay, + print(stringToDisplay,end=' ') if amount_of_data_downloaded_so_far >= totalsize: - print "\n", + print("\n",end=' ') else: stringToDisplay = '\r File size unknown. Read {0} bytes.'.format(amount_of_data_downloaded_so_far) - print stringToDisplay, + print(stringToDisplay,end=' ') diff --git a/scripts/devSetup/mongo.py b/scripts/devSetup/mongo.py index b706c2ae8..a426f5850 100644 --- a/scripts/devSetup/mongo.py +++ b/scripts/devSetup/mongo.py @@ -1,3 +1,4 @@ +from __future__ import print_function __author__ = u'schmatz' from downloader import Downloader import tarfile @@ -6,6 +7,8 @@ import warnings import os from configuration import Configuration from dependency import Dependency +import sys + class MongoDB(Dependency): def __init__(self,configuration): @@ -39,7 +42,7 @@ class MongoDB(Dependency): def findUnzippedMongoBinPath(self): return self.downloader.download_directory + os.sep + \ - (os.walk(self.downloader.download_directory).next()[1])[0] + os.sep + u"bin" + (next(os.walk(self.downloader.download_directory))[1])[0] + os.sep + u"bin" @@ -55,15 +58,15 @@ class MongoDBDownloader(Downloader): def downloaded_file_path(self): return self.download_directory + os.sep + u"mongodb.tgz" def download(self): - print u"Downloading MongoDB from URL " + self.download_url + print(u"Downloading MongoDB from URL " + self.download_url) self.download_file(self.download_url,self.downloaded_file_path) self.check_download() def decompress(self): - print u"Decompressing MongoDB..." + print(u"Decompressing MongoDB...") tfile = tarfile.open(self.downloaded_file_path) #TODO: make directory handler class tfile.extractall(self.download_directory) - print u"Decompressed MongoDB into " + self.download_directory + print(u"Decompressed MongoDB into " + self.download_directory) def check_download(self): isFileValid = tarfile.is_tarfile(self.downloaded_file_path) diff --git a/scripts/devSetup/node.py b/scripts/devSetup/node.py index a788d0a69..065634aad 100644 --- a/scripts/devSetup/node.py +++ b/scripts/devSetup/node.py @@ -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) diff --git a/scripts/devSetup/repositoryInstaller.py b/scripts/devSetup/repositoryInstaller.py index d1336a07d..2bf3f263d 100644 --- a/scripts/devSetup/repositoryInstaller.py +++ b/scripts/devSetup/repositoryInstaller.py @@ -1,3 +1,4 @@ +from __future__ import print_function __author__ = u'schmatz' import configuration import errors @@ -36,11 +37,11 @@ class RepositoryInstaller(): else: return False def cloneRepository(self): - print u"Cloning repository..." + print(u"Cloning repository...") #TODO: CHANGE THIS BEFORE LAUNCH return_code = True git_folder = self.config.directory.root_install_directory + os.sep + "coco" - print "Installing into " + git_folder + print("Installing into " + git_folder) return_code = subprocess.call("git clone " + self.config.repository_url +" coco",cwd=self.config.directory.root_install_directory,shell=True) #TODO: remove this on windos subprocess.call("chown -R " +git_folder + " 0777",shell=True) @@ -51,15 +52,15 @@ class RepositoryInstaller(): #sys.stdout.flush() raw_input(u"Copy it now") #shutil.copytree(u"/Users/schmatz/coco",self.config.directory.root_install_directory + os.sep + u"coco") - print u"Copied tree just for you" + print(u"Copied tree just for you") #print("FAILED TO CLONE GIT REPOSITORY") #input("Clone the repository and click any button to continue") elif self.config.system.operating_system == u"windows": raise errors.CoCoError(u"Windows doesn't support automated installations of npm at this point.") else: - print u"Cloned git repository" + print(u"Cloned git repository") def install_node_packages(self): - print u"Installing node packages..." + print(u"Installing node packages...") #TODO: "Replace npm with more robust package #npm_location = self.config.directory.bin_directory + os.sep + "node" + os.sep + "bin" + os.sep + "npm" npm_location = u"npm" @@ -67,4 +68,4 @@ class RepositoryInstaller(): if return_code: raise errors.CoCoError(u"Failed to install node packages") else: - print u"Installed node packages!" + print(u"Installed node packages!") diff --git a/scripts/devSetup/ruby.py b/scripts/devSetup/ruby.py index f7c0f373c..0fc59b177 100644 --- a/scripts/devSetup/ruby.py +++ b/scripts/devSetup/ruby.py @@ -1,3 +1,4 @@ +from __future__ import print_function __author__ = u'root' import dependency @@ -18,7 +19,7 @@ class Ruby(dependency.Dependency): elif not is_ruby_installed: self.install_ruby() elif is_ruby_installed and is_gem_installed: - print u"Ruby found." + print(u"Ruby found.") def check_if_ruby_exists(self): ruby_path = which(u"ruby") return bool(ruby_path) @@ -38,4 +39,4 @@ class Ruby(dependency.Dependency): raise NotImplementedError def install_gems(self): - gem_install_status = subprocess.call([u"gem",u"install",u"sass"]) \ No newline at end of file + gem_install_status = subprocess.call([u"gem",u"install",u"sass"]) diff --git a/scripts/devSetup/systemConfiguration.py b/scripts/devSetup/systemConfiguration.py index 88ddb4676..9d49e95ce 100644 --- a/scripts/devSetup/systemConfiguration.py +++ b/scripts/devSetup/systemConfiguration.py @@ -22,7 +22,10 @@ class SystemConfiguration(object): raise NotSupportedError(u"Your platform," + sys.platform + u",isn't supported.") def get_current_working_directory(self): - return os.getcwdu() + if sys.version_info.major < 3: + return os.getcwdu() + else: + return os.getcwd() def get_virtual_memory_address_width(self): is64Bit = sys.maxsize/3 > 2**32