Commits

Jonathan Blakes  committed c1cc47f Draft

improved error handling at application entry point

  • Participants
  • Parent commits 26bce1d

Comments (0)

Files changed (3)

 
 import setproctitle
 
+from enthought.traits.trait_errors import TraitError
+
 #import infobiotics.qstring
 
 simulate = 'mcss'#'simulate'
     **globals()
 )
 
+def exit(message, exit_code=1):
+    if os.isatty(sys.stdout.fileno()):
+        print message
+        sys.exit(exit_code)
+    from PyQt4.QtGui import QApplication, QMessageBox
+    app = QApplication([])
+    QMessageBox.warning(None, 'Missing dependency', message, buttons=QMessageBox.Ok, defaultButton=QMessageBox.Ok)
+    sys.exit(exit_code)
+
+def missing_executable(executable):
+	message =  ''
+	message += 'Problem with Infobiotics Workbench installation: '
+	message += '\n executable "{executable}" not found in PATH. '.format(executable=executable if not sys.platform == 'win32' else executable+'.exe')
+	message += '\nUpdate PATH or reinstall.'
+#	message += 'PATH="{PATH}"'.format(PATH=os.environ['PATH'])
+	exit(message)
 
 from infobiotics.thirdparty.which import which, WhichError
 try:
 #    print mcss
 #    raise WhichError() # test
 except WhichError:
-    error = 'mcss not found in PATH: update PATH or reinstall Infobiotics Workbench'
-    from PyQt4.QtGui import QApplication, QMessageBox
-    app = QApplication([])
-    QMessageBox.warning(None, 'Missing dependency', 'mcss not found in PATH: update PATH or reinstall Infobiotics Workbench', buttons=QMessageBox.Ok, defaultButton=QMessageBox.Ok)
-    exit(usage())
+	missing_executable('mcss')
 
 
 def main(argv):
     args = argv[1:]
     
-    if len(args) == 0:
-        setproctitle.setproctitle('Infobiotics Dashboard')        
-        from infobiotics.dashboard import run
-        exitcode = run.main()
-        sys.exit(exitcode)
-    
-    command = args[0].lower()
+    try:
+		if len(args) == 0:
+		    setproctitle.setproctitle('Infobiotics Dashboard')        
+		    from infobiotics.dashboard import run
+		    exitcode = run.main()
+		    sys.exit(exitcode)
+		
+		command = args[0].lower()
 
-    if len(args) > 2 or command.lower() not in commands:
-        sys.exit(usage())
+		if len(args) > 2 or command.lower() not in commands:
+		    exit(usage(), 2)
 
-    params = ''
-    model = ''
-    results = ''
-    
-    if len(args) == 2:
-        args1 = args[1]
-        if not os.path.exists(args1):
-            sys.exit("The file '%s' does not exist" % args1)
-        if not os.path.isabs(args1):
-            args1 = os.path.normpath(os.path.join(os.getcwd(), args1))
-        if args1.lower().endswith('.params'):
-            params = args1
-        elif args1.lower().endswith('.lpp'):
-            model = args1
-        elif command == simulate and args1.lower().endswith('.sbml'):
-            # mcss accepts SBML models too
-            model = args1
-        elif command == simulation_results and args1.lower().endswith('.h5'):
-            results = args1
-        elif command == checking_results and args1.lower().rsplit('.')[1] in ('mc2', 'psm'):
-            results = args1
+		params = ''
+		model = ''
+		results = ''
+		
+		if len(args) == 2:
+		    args1 = args[1]
+		    if not os.path.exists(args1):
+		        sys.exit("The file '%s' does not exist" % args1)
+		    if not os.path.isabs(args1):
+		        args1 = os.path.normpath(os.path.join(os.getcwd(), args1))
+		    if args1.lower().endswith('.params'):
+		        params = args1
+		    elif args1.lower().endswith('.lpp'):
+		        model = args1
+		    elif command == simulate and args1.lower().endswith('.sbml'):
+		        # mcss accepts SBML models too
+		        model = args1
+		    elif command == simulation_results and args1.lower().endswith('.h5'):
+		        results = args1
+		    elif command == checking_results and args1.lower().rsplit('.')[1] in ('mc2', 'psm'):
+		        results = args1
 
-    if model:
-        directory, model = os.path.split(model)
-    
-    if command == simulation_results:
-        from infobiotics.mcss.results import mcss_results_widget
-        return mcss_results_widget.main(results)
+		if model:
+		    directory, model = os.path.split(model)
+		
+		if command == simulation_results:
+		    from infobiotics.mcss.results import mcss_results_widget
+		    return mcss_results_widget.main(results)
 
-    if command == checking_results:
-        if results:
-            from infobiotics.pmodelchecker.pmodelchecker_results import PModelCheckerResults
-            return PModelCheckerResults(results).configure()
-        else:
-            sys.exit(usage())
-    
-    if command == optimisation_results:
-        if params:
-            from infobiotics.poptimizer.poptimizer_experiment import POptimizerExperiment
-            from infobiotics.poptimizer.poptimizer_results import POptimizerResults
-            return POptimizerResults(experiment=POptimizerExperiment(params)).configure()
-        else:
-            sys.exit(usage())
-            
-    if command == simulate:
-        from infobiotics.mcss.mcss_experiment import McssExperiment as Experiment #@UnusedImport
-    elif command == check_mc2:
-        from infobiotics.pmodelchecker.mc2.mc2_experiment import MC2Experiment as Experiment #@UnusedImport @Reimport
-    elif command == check_prism:
-        from infobiotics.pmodelchecker.prism.prism_experiment import PRISMExperiment as Experiment #@UnusedImport @Reimport
-    elif command == optimise:
-        from infobiotics.poptimizer.poptimizer_experiment import POptimizerExperiment as Experiment #@Reimport
-    
-    experiment = Experiment()
+		if command == checking_results:
+		    if results:
+		        from infobiotics.pmodelchecker.pmodelchecker_results import PModelCheckerResults
+		        return PModelCheckerResults(results).configure()
+		    else:
+		        sys.exit(usage())
+		
+		if command == optimisation_results:
+		    if params:
+		        from infobiotics.poptimizer.poptimizer_experiment import POptimizerExperiment
+		        from infobiotics.poptimizer.poptimizer_results import POptimizerResults
+		        return POptimizerResults(experiment=POptimizerExperiment(params)).configure()
+		    else:
+		        sys.exit(usage())
+		        
+		if command == simulate:
+		    from infobiotics.mcss.mcss_experiment import McssExperiment as Experiment #@UnusedImport
+		elif command == check_mc2:
+		    from infobiotics.pmodelchecker.mc2.mc2_experiment import MC2Experiment as Experiment #@UnusedImport @Reimport
+		elif command == check_prism:
+		    from infobiotics.pmodelchecker.prism.prism_experiment import PRISMExperiment as Experiment #@UnusedImport @Reimport
+		elif command == optimise:
+		    from infobiotics.poptimizer.poptimizer_experiment import POptimizerExperiment as Experiment #@Reimport
+		
+		experiment = Experiment()
 
-    setproctitle.setproctitle(experiment.executable_name)
-    
-    if command == simulate:
-        if model != '':
-            experiment.directory = directory
-            experiment.model_file = model
+		setproctitle.setproctitle(experiment.executable_name)
+		
+		if command == simulate:
+		    if model != '':
+		        experiment.directory = directory
+		        experiment.model_file = model
 
-    elif command in (check_mc2, check_prism):
-        if model != '':
-            experiment.directory = directory
-            experiment.model_specification = model
+		elif command in (check_mc2, check_prism):
+		    if model != '':
+		        experiment.directory = directory
+		        experiment.model_specification = model
 
-    if params != '':
-        experiment.load(params)
+		if params != '':
+		    experiment.load(params)
 
-    experiment.configure() # starts event loop
-#    experiment.perform() # useful for debugging without threads
-    
-
+		experiment.configure() # starts event loop
+#		experiment.perform() # useful for debugging without threads
+    except TraitError as e:
+		message = e.args[0]
+		if "The 'executable' trait of a " in message:
+			executable = message.split("'")[5]
+			if len(executable) > 0:
+				missing_executable(executable)
+		exit(e)
+		
+		
 if __name__ == '__main__':
     main(sys.argv)# + ['mcss-results'])

File infobiotics/poptimizer/poptimizer_preferences.py

-from infobiotics.core.params_preferences import ParamsPreferencesHelper, ParamsPreferencesPage
+from infobiotics.core.params_preferences import ParamsPreferencesHelper, ParamsPreferencesPage, RelativeFile
+import sys
 
-PREFERENCES_PATH = 'poptimizer' #TODO
+PREFERENCES_PATH = 'poptimizer'
+
+name = 'poptimizer.exe' if sys.platform.startswith('win') else 'poptimizer' 
+POptimizerExecutable = RelativeFile(name, filter=name, absolute=False, auto_set=True, executable=True) # executable=True implies exists=True
 
 class POptimizerParamsPreferencesHelper(ParamsPreferencesHelper):
-    preferences_path = PREFERENCES_PATH 
+    preferences_path = PREFERENCES_PATH
+    executable = POptimizerExecutable 
 
 class POptimizerParamsPreferencesPage(ParamsPreferencesPage):
     preferences_path = PREFERENCES_PATH
     name = PREFERENCES_PATH
+    executable = POptimizerExecutable
+
 #python -c 'import infobiotics.dashboard.run as run; run.main()'
 echo
 echo "If not source ./sdist.sh do"
-echo "export PYTHONPATH=$PYTHONPATH; echo \$PYTHONPATH; cd $PYTHONPATH" 
+echo "export PYTHONPATH=$PYTHONPATH # not necessary with 'source sdist.sh'"
+echo "$PYTHONPATH/bin/_ibw.py"