예제 #1
0
def main(argv=[]):
    """
    This is the first function called when the program starts.  Do all initialization here.
    @param argv Optionally pass the command-line arguments.
    @return Exit code.  0 for success, non-zero for unnatural exit.
    """
    # Write pid to file.
    if not os.path.exists("./logs"):
        os.mkdir("logs");
    
    run_unit_tests  = False;
    config_file     = os.path.abspath("config.cfg");
    debug_mode      = False;
    
    # The first argument isn't important to us -- it's the program's name.
    del argv[0];
    
    for arg in argv:
        if arg == "--test" or arg == "-t":
            # Testing mode enabled.  The server will not run; it will perform unit tests.
            run_unit_tests = True;
            
        elif arg.startswith("-p") or arg.startswith("--port"):
            # Let user know how to set a port.
            print "Port cannot be set through command line, look in the config file.";
            return 1;
        
        elif arg == "-d" or arg == "--debug":
            # Enable debugging.
            debug_reporter("Debug mode enabled.");
            debug_mode = True;
        
        elif arg == "--generate-config" or arg == "-g":
            # Generate a configuration file.
            temp_config = Config.VTank_Config();
            result = temp_config.generate_config(os.path.abspath(".") + os.sep + "config.cfg");
            debug_reporter("Result: %s" % result);
            return int(result);
        
        elif arg == "--load-slice" or arg == "-s":
            # Load slice dynamically without generating files.
            load_slice_files();
        
        elif arg.startswith("--config-file="):
            # User is specifying the config file's name.
            config_file = arg[len("--config-file="):];
            if not config_file:
                print "--config-file usage:";
                print "--config-file=[FILENAME]";
                return 1;
            
            if not os.path.exists(os.path.abspath(config_file)):
                print "Config file does not exist:", config_file;
                return 1;
            
            debug_reporter("Using config file " + config_file + ".");
            
        else:
            # Unknown.
            print "Unknown option:", arg, " -- ignoring it.";
            
    # Run unit testing here.
    if run_unit_tests:
        import Unit_Test;
        return Unit_Test.run_all_tests();
    
    import Log;
    import World;
    import Config;
    import Map_Manager;
    
    if debug_mode:
        reporter_func = debug_reporter;
    else:
        reporter_func = Log.log_reporter;
    
    # Start handling the world.
    try:
        World.initialize_world(config_file, reporter_func);
        world = World.get_world();
        Map_Manager.initialize(world.get_config(), world.get_database(), reporter_func);
        
        # Write the process ID of this server to file.
        file = open('echelon.pid', 'w');
        with file:
            file.write(str(os.getpid()));
            
        stackless.tasklet(VersionChecker.get_mysql_version)();
        stackless.run();
        
        # Unless some design changes, it will likely never reach this point.
        # Instead, it will be running through the stackless scheduler.
        reporter_func("Waiting for Ice shutdown.");
        World.get_world().get_communicator().waitForShutdown();
    except Force_Exit_Exception, e:
        Log.log_print("CRITICAL_ERROR.log", "", "Startup failure: " + str(e));
        reporter_func("Critical error occurred: " + str(e));
        from sys import exit;
        return e.exit_code;