Esempio n. 1
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    settings.parse_argv(argv)

    if settings.verbose or settings.print_settings:
        settings.dump()

    if settings.print_settings:
        return 0

    if len(settings.array_of_paths) != 1:
        usage(argv[0])

    full_path = os.path.abspath(settings.array_of_paths[0])

    streamRef = my_FSEventStreamCreate(full_path)

    FSEvents.FSEventStreamScheduleWithRunLoop(streamRef,
                                              Cocoa.CFRunLoopGetCurrent(),
                                              Cocoa.kCFRunLoopDefaultMode)

    startedOK = FSEvents.FSEventStreamStart(streamRef)
    if not startedOK:
        settings.error("failed to start the FSEventStream")
        return

    # NOTE: we get the initial size *after* we start the
    #       FSEventStream so that there is no window
    #       during which we would miss events.
    #
    # dir_sz = get_directory_size(full_path, 1)
    print("Initial total size is: %d for path: %s" %
          (get_total_size(), full_path))

    if settings.flush_seconds >= 0:
        settings.debug("CFAbsoluteTimeGetCurrent() => %.3f",
                       Cocoa.CFAbsoluteTimeGetCurrent())

        timer = Cocoa.CFRunLoopTimerCreate(
            FSEvents.FSEventStreamGetSinceWhen(streamRef),
            Cocoa.CFAbsoluteTimeGetCurrent() + settings.flush_seconds,
            settings.flush_seconds,
            0,
            0,
            timer_callback,
            streamRef,
        )
        Cocoa.CFRunLoopAddTimer(Cocoa.CFRunLoopGetCurrent(), timer,
                                Cocoa.kCFRunLoopDefaultMode)

    # Run
    Cocoa.CFRunLoopRun()

    # Stop / Invalidate / Release
    FSEvents.FSEventStreamStop(streamRef)
    FSEvents.FSEventStreamInvalidate(streamRef)
    # FSEventStreamRelease(streamRef)
    return
Esempio n. 2
0
def main():
  configure_logging()

  global CRANKD_OPTIONS, CRANKD_CONFIG
  CRANKD_OPTIONS = process_commandline()
  CRANKD_CONFIG = load_config(CRANKD_OPTIONS)

  if "NSWorkspace" in CRANKD_CONFIG:
    add_workspace_notifications(CRANKD_CONFIG["NSWorkspace"])

  if "SystemConfiguration" in CRANKD_CONFIG:
    add_sc_notifications(CRANKD_CONFIG["SystemConfiguration"])

  if "FSEvents" in CRANKD_CONFIG:
    add_fs_notifications(CRANKD_CONFIG["FSEvents"])

  # We reuse our FSEvents code to watch for changes to our files and
  # restart if any of our libraries have been updated:
  add_conditional_restart(
      CRANKD_OPTIONS.config_file,
      "Configuration file %s changed" % CRANKD_OPTIONS.config_file)
  for m in [
      i for i
      in list(sys.modules.values())
      if i
      and hasattr(i, "__file__")
      and i.__file__ is not None
  ]:
    if m.__name__ == "__main__":
      msg = "%s was updated" % m.__file__
    else:
      msg = "Module %s was updated" % m.__name__

    add_conditional_restart(m.__file__, msg)

  signal.signal(signal.SIGHUP, functools.partial(restart, "SIGHUP received"))

  start_fs_events()

  # NOTE: This timer is basically a kludge around the fact that we can't
  # reliably get signals or Control-C inside a runloop. This wakes us up
  # often enough to appear tolerably responsive:
  Cocoa.CFRunLoopAddTimer(
      Cocoa.NSRunLoop.currentRunLoop().getCFRunLoop(),
      Cocoa.CFRunLoopTimerCreate(None, Cocoa.CFAbsoluteTimeGetCurrent(), 2.0, 0,
                                 0, timer_callback, None),
      Cocoa.kCFRunLoopCommonModes)

  try:
    AppHelper.runConsoleEventLoop(installInterrupt=True)
  except KeyboardInterrupt:
    logging.info("KeyboardInterrupt received, exiting")

  sys.exit(0)