def main(): #initial settings dwarfBeard.MY_FULLNAME = os.path.normpath(os.path.abspath(__file__)) dwarfBeard.MY_NAME = os.path.basename(dwarfBeard.MY_FULLNAME) dwarfBeard.PROG_DIR = os.path.dirname(dwarfBeard.MY_FULLNAME) dwarfBeard.DATA_DIR = dwarfBeard.PROG_DIR #load the config file path if it is not loaded if not dwarfBeard.CONFIG_FILE: dwarfBeard.CONFIG_FILE = os.path.join(dwarfBeard.DATA_DIR, "config.ini") #load the path to the db if it is not loaded if not dwarfBeard.DB_FILE: dwarfBeard.DB_FILE = os.path.join(dwarfBeard.DATA_DIR, "dwarf.db") # Make sure that we can create the data dir if not os.access(dwarfBeard.DATA_DIR, os.F_OK): try: os.makedirs(dwarfBeard.DATA_DIR, 0744) except os.error: sys.exit("Unable to create data directory: " + dwarfBeard.DATA_DIR + " Exiting.") # Make sure we can write to the data dir if not os.access(dwarfBeard.DATA_DIR, os.W_OK): sys.exit("Data directory: " + dwarfBeard.DATA_DIR + " must be writeable (write permissions). Exiting.") # Make sure we can write to the config file if not os.access(dwarfBeard.CONFIG_FILE, os.W_OK): if os.path.isfile(dwarfBeard.CONFIG_FILE): sys.exit("Config file: " + dwarfBeard.CONFIG_FILE + " must be writeable (write permissions). Exiting.") elif not os.access(os.path.dirname(dwarfBeard.CONFIG_FILE), os.W_OK): sys.exit("Config file directory: " + os.path.dirname(dwarfBeard.CONFIG_FILE) + " must be writeable (write permissions). Exiting") #change the working directory os.chdir(dwarfBeard.DATA_DIR) # Load the config and publish it to the dwarfBeard package dwarfBeard.CFG = ConfigObj(dwarfBeard.CONFIG_FILE) #if the db does not exist create it. if not os.path.isfile(dwarfBeard.DB_FILE): dbConn = sqlite3.connect(dwarfBeard.DB_FILE) dbConn.close() #init the db if needed myDb = DBConnection(dwarfBeard.DB_FILE) if not myDb.initTest(): myDb.createInitialSchema() # Initialize dwarfBeard and the config dwarfBeard.initialize() # Use this PID for everything dwarfBeard.PID = os.getpid() #set the log dir if dwarfBeard.WEB_LOG: log_dir = dwarfBeard.LOG_DIR else: log_dir = None #try to init the web server try: initWebServer({ 'port': dwarfBeard.WEB_PORT, 'host': dwarfBeard.WEB_HOST, 'data_root': os.path.join(dwarfBeard.PROG_DIR, 'webData'), 'web_root': dwarfBeard.WEB_ROOT, 'log_dir': log_dir, 'username': dwarfBeard.WEB_USERNAME, 'password': dwarfBeard.WEB_PASSWORD, }) except IOError: dwarfBeard.launchBrowser(dwarfBeard.WEB_PORT) print "Unable to start web server, is something else running on port: " + str(dwarfBeard.WEB_PORT) # Launch browser if we're supposed to if dwarfBeard.LAUNCH_BROWSER: dwarfBeard.launchBrowser(dwarfBeard.WEB_PORT) #create a task timer that executes the task action list at a set interval #this is how we will control the time between profession jobs taskActionTimer = TaskTimer(1, executeTaskActionList) #create a bool for use during blackout hours #this is to simulate the user getting some sleep blackoutActive = False blackoutActiveLastScan = False #used for edge detection #here is the run loop while True: #decide if the current local time is within the black out time blackoutActive = ((time.localtime()[3] >= int(dwarfBeard.BLACKOUT_START_HOUR)) or (time.localtime()[3] < int(dwarfBeard.BLACKOUT_END_HOUR))) and dwarfBeard.BLACKOUT_EN #signal when black out becomes active if blackoutActive and not blackoutActiveLastScan: print 'Blackout just became active @', time.localtime()[3] elif blackoutActiveLastScan and not blackoutActive: print 'Blackout just ended @', time.localtime()[3] #when we get the runTasks signal start the task timer #if its not already running #if task collection is in progress the timer will not be running if dwarfBeard.runTasks and not blackoutActive: if not (taskActionTimer.running or dwarfBeard.taskExecRunning): print 'starting task timer' #set a default interval. this will be reset by the log out function taskActionTimer.interval = 1 taskActionTimer.start() else: if taskActionTimer.running: if blackoutActive: print 'stopping task timer during blackout period' else: print 'stopping task timer' taskActionTimer.stop() #set any vars for edge detection blackoutActiveLastScan = blackoutActive #sleep at the end of each scan so processor time is not consumed time.sleep(1.0)