#!/usr/bin/python import time, _winreg, os, os.path, sendGuildData import win32serviceutil, win32service, win32event, win32api from logging import getLogger, shutdown, Logger, Formatter, CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET from logging.handlers import RotatingFileHandler class GuildInfoTableSender(win32serviceutil.ServiceFramework): """GuildInfoTable service""" _svc_name_ = "GuildInfoTable Service" _svc_display_name_ = "Gwydden Telaid GuildInfoTable Service" _svc_description_ = "Automatically uploads the latest WoW guild info to Greg's machine." initialSleep = 15 * 1000 iterationSleep = 60 * 20 * 1000 def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) self.log = getLogger(self._svc_display_name_) filename = '%s.log' % self._svc_display_name_ recorder = RotatingFileHandler(filename, 'a', 50000, 5) formatter = Formatter('%(asctime)s, %(levelname)-8s, %(message)s') recorder.setLevel(DEBUG) recorder.setFormatter(formatter) self.log.addHandler(recorder) self.log.setLevel(DEBUG) # Find the path to World of Warcraft. wowRegKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Blizzard Entertainment\World of Warcraft') (wowPath, wowPathType) = _winreg.QueryValueEx(wowRegKey, 'InstallPath') wowRegKey.Close() self.saveDataPath = os.path.join(wowPath, r'WTF\Account') self.stopEvent = win32event.CreateEvent(None,0,0,None) self.log.info("saveDataPath is %s" % self.saveDataPath) def SvcDoRun(self): # Now, monitor these and submit them. self.log.info("Starting...") rc = win32event.WaitForSingleObject(self.stopEvent, self.initialSleep) if (rc == win32event.WAIT_OBJECT_0): # Stop was called. self.log.info("Got stop - exiting.") return elif (rc != win32event.WAIT_TIMEOUT): self.log.info("Huh?? got event %d" % rc) while (True): self.log.info("Woke up") # Iterate over each account... try: accountDirs = [os.path.join(self.saveDataPath, fileOrDir) for fileOrDir in os.listdir(self.saveDataPath) if os.path.isdir(os.path.join(self.saveDataPath, fileOrDir))] for accountDir in accountDirs: try: guildDataInfoPath = os.path.join(accountDir, r'SavedVariables\GuildInfoTable.lua') self.log.info("account path is %s" % guildDataInfoPath) # Send the data. if (os.path.exists(guildDataInfoPath)): sendGuildData.sendGuildInfoFile(guildDataInfoPath) except: # Something's wrong here, so we'll try the next account. self.log.info("something went wrong: continuing...") pass except: # Hmm, something's pretty wrong. We'll try again later. self.log.info("something went wrong in accountdirs - ack!") pass # Sleep until the next iteration, or until we're stopped. rc = win32event.WaitForSingleObject(self.stopEvent, self.iterationSleep) if (rc == win32event.WAIT_OBJECT_0): # Stop was called. self.log.info("Got stop - exiting.") return elif (rc != win32event.WAIT_TIMEOUT): self.log.info("Huh?? got event %d" % rc) def SvcStop(self): self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) self.log.info("Stopping...") # Set the stop event. win32event.SetEvent(self.stopEvent) SvcShutdown = SvcStop if (__name__ == '__main__'): win32serviceutil.HandleCommandline(GuildInfoTableSender)