Replaced mixed error handling with exception usage

This commit is contained in:
Nick Bolton 2010-06-16 18:55:06 +00:00
parent ad7b4a92fc
commit 4bb716e5b8
2 changed files with 121 additions and 169 deletions

View File

@ -100,19 +100,46 @@ class InternalCommands:
) % (app, app) ) % (app, app)
def configure(self): def configure(self):
err = self.configure_internal() self.configure_internal()
if err == 0:
print ('Configure complete!\n\n' print ('Configure complete!\n\n'
'Open project now: %s open\n' 'Open project now: %s open\n'
'Command line build: %s build' 'Command line build: %s build'
) % (self.this_cmd, self.this_cmd) ) % (self.this_cmd, self.this_cmd)
return True
else:
return False
# TODO: handle svn not installed def configure_internal(self):
# TODO: implement for other platforms
# ensure latest setup and do not ask config for generator (only fall
# back to prompt if not specified as arg)
self.ensure_setup_latest()
# ensure that we have access to cmake
_cmake_cmd = self.persist_cmake()
# now that we know we've got the latest setup, we can ask the config
# file for the generator (but again, we only fall back to this if not
# specified as arg).
generator = self.get_generator_from_config()
if generator != '':
cmake_args = '%s -G "%s"' % (self.source_dir, generator)
else:
cmake_args = self.source_dir
cmake_cmd_string = '%s %s' % (_cmake_cmd, cmake_args)
print "Configuring with CMake (%s)..." % cmake_cmd_string
# Run from build dir so we have an out-of-source build.
self.try_chdir(self.bin_dir)
err = os.system(cmake_cmd_string)
self.restore_chdir()
if err != 0:
raise Exception('CMake encountered error: ' + str(err))
self.set_conf_run()
def persist_cmake(self): def persist_cmake(self):
if sys.platform == 'win32': if sys.platform == 'win32':
@ -146,61 +173,27 @@ class InternalCommands:
if not os.path.exists('tool'): if not os.path.exists('tool'):
os.mkdir('tool') os.mkdir('tool')
os.system(r'svn checkout https://synergy-plus.googlecode.com/svn/tools/win/cmake tool\cmake') err = os.system(r'svn checkout https://synergy-plus.googlecode.com/svn/tools/win/cmake tool\cmake')
if err != 0:
raise Exception('Unable to get cmake from repository with error code code: ' + str(err))
found_cmd = r'..\tool\cmake\bin\%s' % self.cmake_cmd found_cmd = r'..\tool\cmake\bin\%s' % self.cmake_cmd
found_cmake = True found_cmake = True
# if cmake was not found # if cmake was not found
if not found_cmake: if not found_cmake:
print 'Cannot continue without CMake, exiting.' raise Exception('Cannot continue without CMake, exiting.')
sys.exit(1)
return found_cmd return found_cmd
else: else:
return self.cmake_cmd return self.cmake_cmd
def configure_internal(self):
# ensure latest setup and do not ask config for generator (only fall
# back to prompt if not specified as arg)
self.ensure_setup_latest()
# ensure that we have access to cmake
_cmake_cmd = self.persist_cmake()
# now that we know we've got the latest setup, we can ask the config
# file for the generator (but again, we only fall back to this if not
# specified as arg).
generator = self.get_generator_from_config()
if generator != '':
cmake_args = '%s -G "%s"' % (self.source_dir, generator)
else:
cmake_args = self.source_dir
cmake_cmd_string = '%s %s' % (_cmake_cmd, cmake_args)
print "Configuring with CMake (%s)..." % cmake_cmd_string
# Run from build dir so we have an out-of-source build.
self.try_chdir(self.bin_dir)
err = os.system(cmake_cmd_string)
self.restore_chdir()
if err != 0:
print 'CMake encountered error:', err
else:
self.set_conf_run()
return err;
def build(self, mode = None): def build(self, mode = None):
self.ensure_setup_latest() self.ensure_setup_latest()
if not self.has_conf_run(): if not self.has_conf_run():
if self.configure_internal() != 0: self.configure_internal()
return False
generator = self.get_generator_from_config() generator = self.get_generator_from_config()
@ -211,21 +204,12 @@ class InternalCommands:
err = os.system(self.make_cmd) err = os.system(self.make_cmd)
self.restore_chdir() self.restore_chdir()
if err == 0: if err != 0:
return True raise Exception('GNU Make failed: ' + str(err))
else:
print 'GNU Make failed:', err
return False
elif generator.startswith('Visual Studio'): elif generator.startswith('Visual Studio'):
ret = self.run_vcbuild(generator, mode) self.run_vcbuild(generator, mode)
if ret == 0:
return True
else:
print 'VCBuild failed:', ret
return False
elif generator == 'Xcode': elif generator == 'Xcode':
@ -234,15 +218,11 @@ class InternalCommands:
err = os.system(self.xcodebuild_cmd) err = os.system(self.xcodebuild_cmd)
self.restore_chdir() self.restore_chdir()
if err == 0: if err != 0:
return True raise Exception('Xcode failed:', err)
else:
print 'Xcode failed:', err
return False
else: else:
print 'Not supported with generator:',generator raise Exception('Not supported with generator: ' + generator)
return False
def clean(self, mode = None): def clean(self, mode = None):
@ -255,33 +235,18 @@ class InternalCommands:
err = os.system(self.make_cmd + ' clean') err = os.system(self.make_cmd + ' clean')
self.restore_chdir() self.restore_chdir()
if err == 0: if err != 0:
return True raise Exception('GNU Make failed: ' + str(err))
else:
print 'GNU Make failed: %s' % err
return False
# special case for version 10, use new /target:clean # special case for version 10, use new /target:clean
elif generator.startswith('Visual Studio 10'): elif generator.startswith('Visual Studio 10'):
ret = self.run_vcbuild(generator, mode, '/target:clean') self.run_vcbuild(generator, mode, '/target:clean')
if ret == 0:
return True
else:
print 'VCBuild failed:', ret
return False
# any other version of visual studio, use /clean # any other version of visual studio, use /clean
elif generator.startswith('Visual Studio'): elif generator.startswith('Visual Studio'):
ret = self.run_vcbuild(generator, mode, '/clean') self.run_vcbuild(generator, mode, '/clean')
if ret == 0:
return True
else:
print 'VCBuild failed:', ret
return False
elif generator == 'Xcode': elif generator == 'Xcode':
@ -290,45 +255,42 @@ class InternalCommands:
err = os.system(xcodebuild_cmd + ' clean') err = os.system(xcodebuild_cmd + ' clean')
self.restore_chdir() self.restore_chdir()
if err == 0: if err != 0:
return True raise Exception('Xcode failed:', err)
else:
print 'XCode failed:', err
return False
else: else:
print 'clean: Not supported on platform:',sys.platform raise Exception('Not supported with generator: ' + generator)
return False
def open(self): def open(self):
generator = self.get_generator_from_config() generator = self.get_generator_from_config()
if generator.startswith('Visual Studio'): if generator.startswith('Visual Studio'):
print 'Opening with %s...' % generator print 'Opening with %s...' % generator
self.open_internal(self.sln_filepath()) self.open_internal(self.sln_filepath())
return True
elif generator.startswith('Xcode'): elif generator.startswith('Xcode'):
print 'Opening with %s...' % generator print 'Opening with %s...' % generator
self.open_internal(xcodeproj_filepath(), 'open') self.open_internal(xcodeproj_filepath(), 'open')
return True
else: else:
print 'Not supported with generator:',generator raise Exception('Not supported with generator: ' + generator)
return False
def update(self): def update(self):
print "Running Subversion update..." print "Running Subversion update..."
os.system('svn update') err = os.system('svn update')
if err != 0:
raise Exception('Could not update from repository with error code code: ' + str(err))
def revision(self): def revision(self):
# While this doesn't print out the revision specifically, it will do. # While this doesn't print out the revision specifically, it will do.
os.system('svn info') err = os.system('svn info')
if err != 0:
raise Exception('Could not get revision info with error code code: ' + str(err))
def kill(self): def kill(self):
if sys.platform == 'win32': if sys.platform == 'win32':
os.system('taskkill /F /FI "IMAGENAME eq synergy*"') return os.system('taskkill /F /FI "IMAGENAME eq synergy*"')
return True
else: else:
print 'kill: Error: Command not implemented for current platform' raise Exception('Not implemented for platform: ' + sys.platform)
return False
def package(self, type): def package(self, type):
@ -339,60 +301,43 @@ class InternalCommands:
self.package_usage() self.package_usage()
elif type == 'src': elif type == 'src':
if sys.platform in ['linux2', 'darwin']: if sys.platform in ['linux2', 'darwin']:
self.package_tgz() self.package_run('make package_source')
else: else:
package_unsupported = True package_unsupported = True
elif type == 'rpm': elif type == 'rpm':
if sys.platform == 'linux2': if sys.platform == 'linux2':
self.package_rpm() self.package_run('cpack -G RPM')
else: else:
package_unsupported = True package_unsupported = True
elif type == 'deb': elif type == 'deb':
if sys.platform == 'linux2': if sys.platform == 'linux2':
self.package_deb() self.package_run('cpack -G DEB')
else: else:
package_unsupported = True package_unsupported = True
elif type == 'win': elif type == 'win':
if sys.platform == 'win32': if sys.platform == 'win32':
self.package_win() self.package_run('cpack -G NSIS')
else: else:
package_unsupported = True package_unsupported = True
elif type == 'mac': elif type == 'mac':
if sys.platform == 'darwin': if sys.platform == 'darwin':
self.package_mac() self.package_run('cpack -G PackageMaker')
else: else:
package_unsupported = True package_unsupported = True
else: else:
print 'Not yet implemented: package %s' % type print 'Not yet implemented: package %s' % type
if package_unsupported: if package_unsupported:
print ('Package type, %s is not ' raise Exception(
'supported for platform, %s') % (type, sys.platform) ("Package type, '%s' is not supported for platform, '%s'")
% (type, sys.platform))
def package_tgz(self): def package_run(self, command):
self.try_chdir(self.bin_dir) self.try_chdir(self.bin_dir)
os.system('make package_source') err = os.system(command)
self.restore_chdir()
def package_rpm(self):
self.try_chdir(self.bin_dir)
os.system('cpack -G RPM')
self.restore_chdir()
def package_deb(self):
self.try_chdir(self.bin_dir)
os.system('cpack -G DEB')
self.restore_chdir()
def package_win(self):
self.try_chdir(self.bin_dir)
os.system('cpack -G NSIS')
self.restore_chdir()
def package_mac(self):
self.try_chdir(self.bin_dir)
os.system('cpack -G PackageMaker')
self.restore_chdir() self.restore_chdir()
if err != 0:
raise Exception('Package failed: ' + str(err))
def package_usage(self): def package_usage(self):
print ('Usage: %s package [package-type]\n' print ('Usage: %s package [package-type]\n'
@ -431,14 +376,16 @@ class InternalCommands:
def open_internal(self, project_filename, application = ''): def open_internal(self, project_filename, application = ''):
if not os.path.exists(project_filename): if not os.path.exists(project_filename):
print 'Project file (%s) not found, run hm conf first.' % project_filename raise Exception('Project file (%s) not found, run hm conf first.' % project_filename)
return False
else: else:
path = project_filename path = project_filename
if application != '': if application != '':
path = application + ' ' + path path = application + ' ' + path
os.system(path)
return True err = os.system(path)
if err != 0:
raise Exception('Could not open project with error code code: ' + str(err))
def setup(self): def setup(self):
print "Running setup..." print "Running setup..."
@ -604,6 +551,7 @@ class InternalCommands:
if not os.path.exists(path): if not os.path.exists(path):
raise Exception("'%s' not found." % path) raise Exception("'%s' not found." % path)
return path return path
def run_vcbuild(self, generator, mode, args=''): def run_vcbuild(self, generator, mode, args=''):
@ -651,19 +599,23 @@ class InternalCommands:
file.write(cmd) file.write(cmd)
file.close() file.close()
return os.system(temp_bat) err = os.system(temp_bat)
if err != 0:
raise Exception('Microsoft compiler failed with error code: ' + str(err))
def ensure_setup_latest(self): def ensure_setup_latest(self):
if not self.min_setup_version(self.setup_version): if not self.min_setup_version(self.setup_version):
self.setup() self.setup()
def reformat(self): def reformat(self):
# TODO: error handling err = os.system(
os.system(
r'tool\astyle\AStyle.exe ' r'tool\astyle\AStyle.exe '
'--quiet --suffix=none --style=java --indent=force-tab=4 --recursive ' '--quiet --suffix=none --style=java --indent=force-tab=4 --recursive '
'lib/*.cpp lib/*.h cmd/*.cpp cmd/*.h') 'lib/*.cpp lib/*.h cmd/*.cpp cmd/*.h')
if err != 0:
raise Exception('Reformat failed with error code: ' + str(err))
# the command handler should be called only from hm.py (i.e. directly # the command handler should be called only from hm.py (i.e. directly
# from the command prompt). the purpose of this class is so that we # from the command prompt). the purpose of this class is so that we
# don't need to do argument handling all over the place in the internal # don't need to do argument handling all over the place in the internal

10
hm.py
View File

@ -126,6 +126,8 @@ def start_cmd(argv):
def run_cmd(cmd, argv = []): def run_cmd(cmd, argv = []):
verbose = False
try:
options_pair = cmd_opt_dict[cmd] options_pair = cmd_opt_dict[cmd]
options = global_options + options_pair[0] options = global_options + options_pair[0]
@ -136,7 +138,6 @@ def run_cmd(cmd, argv = []):
opts, args = getopt(argv, options, options_long) opts, args = getopt(argv, options, options_long)
verbose = False
for o, a in opts: for o, a in opts:
if o in ('-v', '--verbose'): if o in ('-v', '--verbose'):
verbose = True verbose = True
@ -149,12 +150,11 @@ def run_cmd(cmd, argv = []):
# use reflection to get the function pointer # use reflection to get the function pointer
cmd_func = getattr(handler, cmd) cmd_func = getattr(handler, cmd)
try:
cmd_func() cmd_func()
except Exception as ex: except:
if not verbose: if not verbose:
# print friendly error for users # print friendly error for users
print 'Error:',ex sys.stderr.write('Error: ' + sys.exc_info()[1].__str__())
exit(1) exit(1)
else: else:
# if user wants to be verbose let python do it's thing # if user wants to be verbose let python do it's thing
@ -167,7 +167,7 @@ def main(argv):
sys.exit(1) sys.exit(1)
try: try:
sys.exit(start_cmd(argv)) start_cmd(argv)
except KeyboardInterrupt: except KeyboardInterrupt:
print '\n\nUser aborted, exiting.' print '\n\nUser aborted, exiting.'