def front_end(no_interp=None): global startupfiles global gtk_options global randomseed ## From here on is the serial version. # VTK is started even in text mode to allow off-screen rendering, # interpretation of scripted mouse clicks, etc. from ooflib.SWIG.common.IO import vtkutils vtkutils.initialize_vtk() # When loading modules, use utils.OOFexec so that names are # imported into the oof environment, not the oof.run environment. if not (runtimeflags.text_mode or config.no_gui()): # The gtk import dance described below doesn't work when the program # has been packaged by cx_freeze. # TODO 3.1: is checking frozen required for gtk2? frozen = hasattr(sys, 'frozen') if not frozen: import pygtk pygtk.require("2.0") import gtk msg = gtk.check_version(2, 6, 0) if msg: print msg sys.exit(3) # The GUI initialization modules must be called before any # calls to mainthread.run(), because mainthread.run() is # redefined when mainthreadGUI.py is loaded (by # common/IO/GUI/initialize.py) import ooflib.common.IO.GUI.initialize import ooflib.engine.IO.GUI.initialize import ooflib.image.IO.GUI.initialize import ooflib.orientationmap.GUI.initialize import ooflib.tutorials.initialize if replaydelay is not None: from ooflib.common.IO.GUI import gtklogger gtklogger.set_delay(int(replaydelay)) else: # text mode # Load non-gui initialization modules. import ooflib.common.initialize import ooflib.engine.initialize import ooflib.image.initialize import ooflib.orientationmap.initialize import ooflib.EXTENSIONS.initialize # The random number generator must be seeded *after* the gui has # been started, because libfontconfig is using random numbers. We # want the numbers to be the same in text and gui modes, so that # the test suite gets predictable answers. if debug.debug() or randomseed is not None: if randomseed is None: randomseed = 17 random.seed(randomseed) crandom.rndmseed(randomseed) for module in startupimports: exec('import ' + module) if not (runtimeflags.text_mode or config.no_gui()): reporter.report("Welcome to %s version %s!" % (program_name.upper(), oofversion.version)) if not no_interp: # Default case, run on local thread. from ooflib.common.IO.GUI import oofGUI oofGUI.start(files=startupfiles) # This call never returns. print "This line should never be printed. rank =", _rank else: # TODO 3.1: The gui and no_interp combination is # thinkable, but has problems. You have to run the GUI on # a separate thread, but then exceptions show up as modal # dialog boxes in the GUI, and block the menu items which # raised them, causing a loss of control. Also, the # current threading scheme requires that all gtk activity # happen on the main thread. print "GUI no_interp mode not implemented. Sorry." raise NotImplementedError("GUI no_interp mode") else: # text mode from ooflib.common import quit # Allow exceptions to propagate to the user if in batch mode # or not running an interpreter. Otherwise, exceptions are # caught and reported to the user, but the program keeps # running. if runtimeflags.batch_mode or no_interp: from ooflib.common import worker worker.propagate_exceptions = True threadstate.textMode() lock.disableLocks() # disables Locks, but not SLocks if startupfiles: loadStartUpFiles(startupfiles) if runtimeflags.batch_mode: # Batch mode runs startupfiles and quits immediately. quit.set_quiet() quit.quit() if sys.exc_info()[0] is not None: sys.exit(1) sys.exit(0) # Format the banner for the current line width. if not quit.quiet(): width = utils.screenwidth() wiggles = "//=*=\\\\=*=" nwiggles = (width - 2) / len(wiggles) welcome = "Welcome to %s version %s!" % (program_name.upper(), oofversion.version) nblanks = (width - len(welcome)) / 2 banner = ( wiggles * nwiggles + "//\n\n" + " " * nblanks + welcome + "\n" + string.join( utils.format(banner1 % {'name': program_name.upper()}, width), "\n") + "\n\n" + wiggles * nwiggles + "//\n" + string.join( utils.format(banner2 % {'name': program_name.upper()}, width), "\n")) else: banner = "" if not no_interp: import code # Try to import readline, which allows command line # editing in text mode. If it's not there, don't worry -- # it's possible to live without it. Some systems don't # seem to have it, although it's supposedly available on # all Unix systems. try: import readline except ImportError: pass # Start up the interpreter in the __main__ namespace. # This is the namespace that utils.OOFeval and OOFdefine # use. It's not necessarily *this* namespace. interp = code.InteractiveConsole(sys.modules['__main__'].__dict__) interp.interact(banner)
def run(no_interp=None): global _rank global startupfiles global program_name program_name = os.path.basename(sys.argv[0]) process_inline_options() # execute well-formed oof options # Look for .oof2rc or .oof3drc in the user's home directory. if not no_rc: oofrcpath = os.path.join(os.path.expanduser("~"), ".%src" % program_name) if os.path.exists(oofrcpath): startupfiles = [StartUpScriptNoLog(oofrcpath)] + startupfiles if (thread_enable.query() and not (runtimeflags.text_mode or config.no_gui())): # TODO OPT: Is this still necessary? garbage.disable() # work-around for gtk bug? start_parallel_machine() # start parallel suite (if available) if _rank == 0: if parallel_enable.enabled(): from ooflib.SWIG.common import mpitools _size = mpitools.Size() mpitools.Isend_Bool(thread_enable.enabled(), range(1, _size)) if parallel_enable.enabled(): from ooflib.common.IO import socket2me if config.petsc(): print "Going to InitPETSc" from ooflib.SWIG.engine.PETSc.petsc_solverdriver import InitPETSc InitPETSc(sys.argv) for s in sys.argv: print s start_sockets_Front_End() # Import mainmenu only *after* processing command line options, so # that the options can affect which menus are loaded. global mainmenu from ooflib.common.IO import mainmenu front_end(no_interp) # all non-parallel menu items are executed here. else: # parallel back-end parallel_enable.set(True) # notify back-end of its parallel status # thread status at the back-ends from ooflib.SWIG.common import mpitools thread_enable.set(mpitools.Recv_Bool(0)) if not thread_enable.enabled(): lock.disableLocks() if parallel_enable.enabled(): from ooflib.common.IO import socket2me if config.petsc(): print "Going to InitPETSc" from ooflib.SWIG.engine.PETSc.petsc_solverdriver import InitPETSc InitPETSc(sys.argv) for s in sys.argv: print s debug.set_debug_mode() # set for debugging parallel mode from ooflib.common import quit quit.set_quiet() ## back-end exits quietly. start_sockets_Back_End() # socket initialization from ooflib.common import backEnd # import back end machine # The back end shouldn't run the gui! runtimeflags.text_mode = True backEnd.back_end() # back-end awaits for your command
try: (optlist, args) = getopt.getopt(sys.argv[1:], '', option_list) except getopt.error, message: # Malformed arguments have been found. Exit. print message state_options_and_quit() for opt in optlist: if opt[0] == '--gtk': gtk_options = opt[1] remove_option(opt[0], opt[1]) elif opt[0] == '--parallel': parallel_enable.set(True) remove_option(opt[0]) elif opt[0] in ('--unthreaded', ): thread_enable.set(False) lock.disableLocks() remove_option(opt[0]) elif opt[0] in ('--text', ): runtimeflags.text_mode = True remove_option(opt[0]) elif opt[0] in ('--help', ): help_mode = True remove_option(opt[0]) elif opt[0] in ('--version', ): version_mode = True remove_option(opt[0]) elif opt[0] in ('--script', ): startupfiles.append(StartUpScript(opt[1])) remove_option(opt[0], opt[1]) elif opt[0] in ('--command', ): startupfiles.append(StartUpCommand(opt[1]))