def _doSecondaryActions(action, tzpath, xmlfile, url): tzdb = SecondaryTimezoneDatabase(tzpath, xmlfile, url) try: tzdb.readDatabase() except: pass if action == "cache": print("Caching from secondary server: {}".format(url, )) observer = FileLogObserver( sys.stdout, lambda event: formatEventAsClassicLogText(event)) Logger.beginLoggingTo([observer], redirectStandardIO=False) reactor.callLater(0, _runInReactor, tzdb) reactor.run() else: usage("Invalid action: {}".format(action, ))
def utilityMain( configFileName, serviceClass, reactor=None, serviceMaker=None, patchConfig=None, onShutdown=None, verbose=False, loadTimezones=False ): """ Shared main-point for utilities. This function will: - Load the configuration file named by C{configFileName}, - launch a L{CalDAVServiceMaker}'s with the C{ProcessType} of C{"Utility"} - run the reactor, with start/stop events hooked up to the service's C{startService}/C{stopService} methods. It is C{serviceClass}'s responsibility to stop the reactor when it's complete. @param configFileName: the name of the configuration file to load. @type configuration: C{str} @param serviceClass: a 1-argument callable which takes an object that provides L{ICalendarStore} and/or L{IAddressbookStore} and returns an L{IService}. @param patchConfig: a 1-argument callable which takes a config object and makes and changes necessary for the tool. @param onShutdown: a 0-argument callable which will run on shutdown. @param reactor: if specified, the L{IReactorTime} / L{IReactorThreads} / L{IReactorTCP} (etc) provider to use. If C{None}, the default reactor will be imported and used. """ from calendarserver.tap.caldav import CalDAVServiceMaker, CalDAVOptions if serviceMaker is None: serviceMaker = CalDAVServiceMaker # We want to validate that the actual service is always an instance of WorkerService, so wrap the # service maker callback inside a function that does that check def _makeValidService(store): service = serviceClass(store) assert isinstance(service, WorkerService) return service # Install std i/o observer observers = [] if verbose: observers.append(FileLogObserver(sys.stdout, lambda event: formatEventAsClassicLogText(event))) if reactor is None: from twisted.internet import reactor try: config = loadConfig(configFileName) if patchConfig is not None: patchConfig(config) checkDirectories(config) utilityLogFile = LogFile.fromFullPath( config.UtilityLogFile, rotateLength=config.ErrorLogRotateMB * 1024 * 1024, maxRotatedFiles=config.ErrorLogMaxRotatedFiles ) utilityLogObserver = FileLogObserver(utilityLogFile, lambda event: formatEventAsClassicLogText(event)) utilityLogObserver._encoding = "utf-8" observers.append(utilityLogObserver) Logger.beginLoggingTo(observers, redirectStandardIO=False) config.ProcessType = "Utility" config.UtilityServiceClass = _makeValidService autoDisableMemcached(config) maker = serviceMaker() # Only perform post-import duties if someone has explicitly said to maker.doPostImport = getattr(maker, "doPostImport", False) options = CalDAVOptions service = maker.makeService(options) reactor.addSystemEventTrigger("during", "startup", service.startService) reactor.addSystemEventTrigger("before", "shutdown", service.stopService) if onShutdown is not None: reactor.addSystemEventTrigger("before", "shutdown", onShutdown) if loadTimezones: TimezoneCache.create() except (ConfigurationError, OSError), e: sys.stderr.write("Error: %s\n" % (e,)) return