Improves setup.py

This commit is contained in:
Ricardo Quesada 2014-03-05 14:09:45 -08:00
parent ff14c6ada5
commit a012459b94
1 changed files with 183 additions and 179 deletions

180
setup.py
View File

@ -33,9 +33,9 @@ On Max OS X, when start a shell, it will read these files and execute commands i
And it will read only one of them. So we will add environment variable in the same sequence.
Which means that
* add environment variable into ~/.bash_profile when exists
* add environment variable into ~/.bash_login when exits and ~/.bash_profile not exits
* add environment variable into ~/.profile when exits and ~/.bash_profile & ~/.bash_login not exist
* add environment variables into ~/.bash_profile if it exists
* otherwise it will the add environment variables into ~/.bash_login if it exists
* otherwise it will the add environment variable sinto ~/.profile if it exists
Will create ~/.bash_profile when none of them exist, and add environment variable into it.
@ -50,37 +50,34 @@ COCOS_CONSOLE_ROOT = 'COCOS_CONSOLE_ROOT'
NDK_ROOT = 'NDK_ROOT'
class SetEnvVar:
current_absolute_path = os.path.dirname(os.path.realpath(__file__))
ndk_root = None
class SetEnvVar(object):
def __init__(self, ndk=None):
self.current_absolute_path = os.path.dirname(os.path.realpath(__file__))
self.ndk_root = ndk
# whether the value of "ndk_root" is passed or not
ndk_root_passed = False
file_to_write_environment = None
self.ndk_root_passed = False
self.file_to_write_environment = None
self.file_used_for_setup = ''
self.variable_found_in_env = False
@staticmethod
def _isWindows():
def _isWindows(self):
return sys.platform == 'win32'
@staticmethod
def _isLinux():
def _isLinux(self):
return sys.platform.startswith('linux')
# modify register table to add an environment variable on windows
# TODO: test in on windows
@staticmethod
def _set_environment_variable_win32(key, value):
def _set_environment_variable_win32(self, key, value):
import _winreg
env = _winreg.OpenKeyEx(_winreg._winreg.HKEY_LOCAL_MACHINE,\
r'SYSTEM\ControlSet001\Control\Session Manager\Environment',\
env = _winreg.OpenKeyEx(_winreg._winreg.HKEY_LOCAL_MACHINE,
r'SYSTEM\ControlSet001\Control\Session Manager\Environment',
0,
_winreg.KEY_SET_VALUE|_winreg.KEY_READ)
_winreg.KEY_SET_VALUE | _winreg.KEY_READ)
_winreg.SetValueEx(env, key, 0, _winreg.REG_SZ, value)
@staticmethod
def _set_environment_variable_unix(key, value):
def _set_environment_variable_unix(self, key, value):
home = os.path.expanduser('~')
if os.path.exists(os.path.join(home, '.bash_profile')):
@ -92,10 +89,10 @@ class SetEnvVar:
else:
file_to_write = os.path.join(home, '.bash_profile')
SetEnvVar.file_to_write_environment = file_to_write
self.file_to_write_environment = file_to_write
file = open(file_to_write, 'a')
if SetEnvVar.ndk_root_passed and SetEnvVar._find_string_in_file('export '.join(key), file_to_write):
if self.ndk_root_passed and self._find_string_in_file('export '.join(key), file_to_write):
# if ndk_root is passed and "ndk_root" is set yet, replace existing string in the file
for line in fileinput.input(file_to_write, inplace=1):
if line.startswith('export '.join(key)):
@ -106,29 +103,24 @@ class SetEnvVar:
file.write('export PATH=$%s:$PATH\n' % key)
file.close()
def _set_environment_variable(self, key, value):
@staticmethod
def _set_environment_variable(key, value):
if SetEnvVar._isWindows():
_set_environment_variable_win32(key, value)
if self._isWindows():
self._set_environment_variable_win32(key, value)
else:
SetEnvVar._set_environment_variable_unix(key, value)
self._set_environment_variable_unix(key, value)
@staticmethod
def _find_string_in_file(string, file_path):
def _find_string_in_file(self, string, file_path):
with open(file_path) as f:
for line in f:
if line.startswith(string):
return True
return False
@staticmethod
def _find_environment_variable(var):
def _find_environment_variable(self, var):
try:
result = os.environ[var]
self.variable_found_in_env = true
if result:
return True
else:
@ -137,46 +129,49 @@ class SetEnvVar:
string_to_search = 'export %s' % var
home = os.path.expanduser('~')
# find it in ~/.bash_profile
if os.path.exists(os.path.join(home, '.bash_profile')):
if SetEnvVar._find_string_in_file(string_to_search, os.path.join(home, '.bash_profile')):
# search it in ~/.bash_profile
path = os.path.join(home, '.bash_profile')
if os.path.exists(path):
if self._find_string_in_file(string_to_search, path):
self.file_used_for_setup = path
return True
# find it in ~/.bash_login
if os.path.exists(os.path.join(home, '.bash_login')):
if SetEnvVar._find_string_in_file(string_to_search, os.path.join(home, '.bash_login')):
# search it in ~/.bash_login
path = os.path.join(home, '.bash_login')
if os.path.exists(path):
if self._find_string_in_file(string_to_search, path):
self.file_used_for_setup = path
return True
# find it in ~/.profile if exit
if os.path.exists(os.path.join(home, '.profile')):
if SetEnvVar._find_string_in_file(string_to_search, os.path.join(home, '.profile')):
# search it in ~/.profile
path = os.path.join(home, '.profile')
if os.path.exists(path):
if self._find_string_in_file(string_to_search, path):
self.file_used_for_setup = path
return True
return False
@staticmethod
def _get_ndk_root():
if not SetEnvVar._find_environment_variable(NDK_ROOT):
def _get_ndk_root(self):
if not self._find_environment_variable(NDK_ROOT):
# python on linux doesn't include Tkinter model, so let user input in terminal
if SetEnvVar._isLinux():
SetEnvVar.ndk_root = raw_input('can not find envrironment variable "NDK_ROOT", please enter it: ')
if self._isLinux():
self.ndk_root = raw_input('Couldn\'t find the "NDK_ROOT" envrironment variable. Please enter it: ')
else:
# pop up a window to let user select path for ndk root
import Tkinter, tkFileDialog
import Tkinter
import tkFileDialog
root = Tkinter.Tk()
SetEnvVar._center(root)
self._center(root)
def callback():
SetEnvVar.ndk_root = tkFileDialog.askdirectory()
self.ndk_root = tkFileDialog.askdirectory()
root.destroy()
frame = Tkinter.Frame(root)
Tkinter.Label(frame, text='select path for ndk root').pack(side=Tkinter.LEFT)
Tkinter.Label(frame, text='Select path for NDK_ROOT:').pack(side=Tkinter.LEFT)
Tkinter.Button(frame, text='...', command=callback).pack(side=Tkinter.LEFT)
frame.pack()
root.mainloop()
@ -186,8 +181,7 @@ class SetEnvVar:
return False
# display a window in center and put it on top
@staticmethod
def _center(win):
def _center(self, win):
win.update_idletasks()
width = win.winfo_width()
height = win.winfo_height()
@ -196,65 +190,75 @@ class SetEnvVar:
win.geometry('{}x{}+{}+{}'.format(width, height, x, y))
win.wm_attributes('-topmost', 1)
def set_ndk_root(self, value):
print '-> Adding NDK_ROOT environment variable...',
@staticmethod
def set_ndk_root(value):
ndk_root_updated = False
self.ndk_root_updated = False
if value:
SetEnvVar.ndk_root = value
self.ndk_root = value
ndk_root_updated = True
else:
ndk_root_updated = SetEnvVar._get_ndk_root()
ndk_root_updated = self._get_ndk_root()
if SetEnvVar.ndk_root:
os.environ[NDK_ROOT] = SetEnvVar.ndk_root
SetEnvVar._set_environment_variable(NDK_ROOT, SetEnvVar.ndk_root)
if self.ndk_root:
os.environ[NDK_ROOT] = self.ndk_root
self._set_environment_variable(NDK_ROOT, self.ndk_root)
ndk_root_updated = True
else:
ndk_root_updated = False
if ndk_root_updated:
print 'OK'
return ndk_root_updated
@staticmethod
def set_console_root():
def set_console_root(self):
if not SetEnvVar._find_environment_variable(COCOS_CONSOLE_ROOT):
cocos_consle_root = os.path.join(SetEnvVar.current_absolute_path, 'tools/cocos2d-console/bin')
SetEnvVar._set_environment_variable(COCOS_CONSOLE_ROOT, cocos_consle_root)
print '-> Adding COCOS2D_CONSOLE_ROOT environment variable...',
if not self._find_environment_variable(COCOS_CONSOLE_ROOT):
cocos_consle_root = os.path.join(self.current_absolute_path, 'tools/cocos2d-console/bin')
self._set_environment_variable(COCOS_CONSOLE_ROOT, cocos_consle_root)
print 'OK'
return True
print 'ALREADY ADDED'
return False
@staticmethod
def set_environment_variables(ndk_root):
def set_environment_variables(self, ndk_root):
console_updated = SetEnvVar.set_console_root()
ndk_root_updated = SetEnvVar.set_ndk_root(ndk_root)
print '\nSetting up cocos2d-x...'
if SetEnvVar._isWindows():
console_updated = self.set_console_root()
ndk_root_updated = self.set_ndk_root(ndk_root)
if self._isWindows():
if console_updated or ndk_root_updated:
result_string = 'Set up successful.'
result_string = '\nSet up successful.\n'
if console_updated:
result_string += 'COCOS_CONSOLE_ROOT was added into register table.\n'
result_string += '\tCOCOS_CONSOLE_ROOT was added into the registry.\n'
if ndk_root_updated:
result_string += 'NDK_ROOT was added into register table.'
result_string += '\tNDK_ROOT was added into the registry.\n'
print result_string
else:
print 'cocos2d-x was already setted up. Edit the registry manually in order to change the current settings'
else:
if console_updated or ndk_root_updated:
result_string = 'Set up successful.'
result_string = '\nSet up successful:\n'
if console_updated:
result_string += 'COCOS_CONSOLE_ROOT was added into %s.\n' % SetEnvVar.file_to_write_environment
result_string += '\tCOCOS_CONSOLE_ROOT was added into %s.\n' % self.file_to_write_environment
if ndk_root_updated:
result_string += 'NDK_ROOT was added into %s.' % SetEnvVar.file_to_write_environment
result_string += '\tNDK_ROOT was added into %s.\n' % self.file_to_write_environment
print result_string
else:
if self.variable_found_in_env:
print '\nFound cocos2d-x environment variables. No action needed'
else:
print '\ncocos2d-x was already set up. Edit the script %s manually' % self.file_used_for_setup
if __name__ == '__main__':
parser = OptionParser()
parser.add_option('-n', '--ndkroot', dest='ndk_root', help='directory of ndk root')
opts, args = parser.parse_args()
@ -262,10 +266,10 @@ if __name__ == '__main__':
# ndk_root is passed in
if opts.ndk_root:
os.environ[NDK_ROOT] = opts.ndk_root
SetEnvVar.ndk_root_passed = True
SetEnvVar.set_environment_variables(opts.ndk_root)
env = SetEnvVar(True)
env.set_environment_variables(opts.ndk_root)
exit(0)
# set environment variables
SetEnvVar.set_environment_variables(None)
env = SetEnvVar(False)
env.set_environment_variables(None)