Share Pastebin
Guest
Public paste!

wilkes

By: a guest | Jan 31st, 2008 | Syntax: Python | Size: 3.15 KB | Hits: 163 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. from FSEvents import *
  2. import objc
  3. import os, sys
  4.  
  5. class Autotester(object):
  6.     def __init__(self):
  7.         self.snapshot = {}
  8.         self.target = os.path.abspath(os.curdir)
  9.  
  10.     def run(self):
  11.         if self.__check_changes():
  12.             self.__run_nose()
  13.  
  14.     def __remove_from(self, l, items):
  15.         for i in items:
  16.             l.remove(i)
  17.  
  18.     def __check_changes(self):
  19.         changes = []
  20.         for root, dirs, files in os.walk(self.target):
  21.             self.__remove_from(dirs, [d for d in dirs if d.startswith('.')])
  22.             self.__remove_from(files, [f for f in files if not f.endswith('.py')])
  23.             for name in files:
  24.                 f = os.path.join(root, name)
  25.                 last_update = os.path.getmtime(f)
  26.                 last_known = self.snapshot.setdefault(f, last_update)
  27.                 if last_update != last_known:
  28.                     changes.append(f)
  29.                     self.snapshot[f] = last_update
  30.         return changes
  31.  
  32.     def __run_nose(self):
  33.         try:
  34.             if os.system('nosetests ' + self.target):
  35.                 os.system('growlnotify -n autotest -p 0 -m "what the hell dude?"')
  36.             else:
  37.                 os.system('growlnotify -n autotest -p 0 -m "yeah, boi"')
  38.         except:
  39.             pass
  40. tester = Autotester()
  41.  
  42. def timer_callback(timer, stream):
  43.     FSEventStreamFlushAsync(stream)
  44.    
  45. def fsevents_callback(stream, clientInfo, numEvents, eventPaths, eventMasks, eventIDs):
  46.     tester.run()
  47.    
  48. def createStream(full_path):
  49.     stream = FSEventStreamCreate(kCFAllocatorDefault,            # allocator
  50.                                   fsevents_callback,             # callback  
  51.                                   full_path,                     # path      
  52.                                   [full_path],                   # path
  53.                                   kFSEventStreamEventIdSinceNow, # since_when
  54.                                   1.0,                           # latency  
  55.                                   0)                             # flags    
  56.  
  57.     assert stream, "ERROR: FSEVentStreamCreate() => NULL"
  58.     return stream
  59.  
  60.  
  61.     assert stream, "ERROR: FSEVentStreamCreate() => NULL"
  62.     return stream
  63.  
  64. def run_loop(stream):
  65.     FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)
  66.     assert FSEventStreamStart(stream), "Failed to start stream"
  67.     timer = CFRunLoopTimerCreate(kCFAllocatorDefault,
  68.                                 CFAbsoluteTimeGetCurrent() + 1.0,
  69.                                 1.0,
  70.                                 0,
  71.                                 0,
  72.                                 timer_callback,
  73.                                 stream)
  74.     CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode)
  75.     try:
  76.         CFRunLoopRun()
  77.     finally:
  78.         FSEventStreamStop(stream)
  79.         FSEventStreamInvalidate(stream)
  80.         FSEventStreamRelease(stream)
  81.  
  82. def main():
  83.     tester.target = sys.argv[1]
  84.     tester.run()
  85.     abspath = os.path.join(os.path.abspath(os.curdir), tester.target)
  86.     run_loop(createStream(abspath))
  87.  
  88. if __name__ == '__main__':
  89.     main()