Mercurial > hg
comparison hgext/inotify/server.py @ 8606:1c5752dabf76
inotify: use a decorator instead of dispatching calls
author | Nicolas Dumazet <nicdumz.commits@gmail.com> |
---|---|
date | Fri, 22 May 2009 10:26:56 +0900 |
parents | ed2d9bdbfad2 |
children | e17eba98c789 |
comparison
equal
deleted
inserted
replaced
8605:ed2d9bdbfad2 | 8606:1c5752dabf76 |
---|---|
413 return ret | 413 return ret |
414 except OSError: | 414 except OSError: |
415 self.statcache.pop(wpath, None) | 415 self.statcache.pop(wpath, None) |
416 raise | 416 raise |
417 | 417 |
418 def eventaction(code): | |
419 def decorator(f): | |
420 def wrapper(self, wpath): | |
421 if code == 'm' and wpath in self.lastevent and \ | |
422 self.lastevent[wpath] in 'cm': | |
423 return | |
424 self.lastevent[wpath] = code | |
425 self.timeout = 250 | |
426 | |
427 f(self, wpath) | |
428 | |
429 wrapper.func_name = f.func_name | |
430 return wrapper | |
431 return decorator | |
432 | |
433 @eventaction('c') | |
418 def created(self, wpath): | 434 def created(self, wpath): |
419 if wpath == '.hgignore': | 435 if wpath == '.hgignore': |
420 self.update_hgignore() | 436 self.update_hgignore() |
421 try: | 437 try: |
422 st = self.stat(wpath) | 438 st = self.stat(wpath) |
423 if stat.S_ISREG(st[0]): | 439 if stat.S_ISREG(st[0]): |
424 self.updatefile(wpath, st) | 440 self.updatefile(wpath, st) |
425 except OSError: | 441 except OSError: |
426 pass | 442 pass |
427 | 443 |
444 @eventaction('m') | |
428 def modified(self, wpath): | 445 def modified(self, wpath): |
429 if wpath == '.hgignore': | 446 if wpath == '.hgignore': |
430 self.update_hgignore() | 447 self.update_hgignore() |
431 try: | 448 try: |
432 st = self.stat(wpath) | 449 st = self.stat(wpath) |
434 if self.repo.dirstate[wpath] in 'lmn': | 451 if self.repo.dirstate[wpath] in 'lmn': |
435 self.updatefile(wpath, st) | 452 self.updatefile(wpath, st) |
436 except OSError: | 453 except OSError: |
437 pass | 454 pass |
438 | 455 |
456 @eventaction('d') | |
439 def deleted(self, wpath): | 457 def deleted(self, wpath): |
440 if wpath == '.hgignore': | 458 if wpath == '.hgignore': |
441 self.update_hgignore() | 459 self.update_hgignore() |
442 elif wpath.startswith('.hg/'): | 460 elif wpath.startswith('.hg/'): |
443 if wpath == '.hg/wlock': | 461 if wpath == '.hg/wlock': |
444 self.check_dirstate() | 462 self.check_dirstate() |
445 return | 463 return |
446 | 464 |
447 self.deletefile(wpath, self.repo.dirstate[wpath]) | 465 self.deletefile(wpath, self.repo.dirstate[wpath]) |
448 | 466 |
449 def work(self, wpath, evt): | |
450 try: | |
451 if evt == 'c': | |
452 self.created(wpath) | |
453 elif evt == 'm': | |
454 if wpath in self.lastevent and self.lastevent[wpath] in 'cm': | |
455 return | |
456 self.modified(wpath) | |
457 elif evt == 'd': | |
458 self.deleted(wpath) | |
459 | |
460 self.lastevent[wpath] = evt | |
461 finally: | |
462 self.timeout = 250 | |
463 | |
464 def process_create(self, wpath, evt): | 467 def process_create(self, wpath, evt): |
465 if self.ui.debugflag: | 468 if self.ui.debugflag: |
466 self.ui.note(_('%s event: created %s\n') % | 469 self.ui.note(_('%s event: created %s\n') % |
467 (self.event_time(), wpath)) | 470 (self.event_time(), wpath)) |
468 | 471 |
469 if evt.mask & inotify.IN_ISDIR: | 472 if evt.mask & inotify.IN_ISDIR: |
470 self.scan(wpath) | 473 self.scan(wpath) |
471 else: | 474 else: |
472 self.work(wpath, 'c') | 475 self.created(wpath) |
473 | 476 |
474 def process_delete(self, wpath, evt): | 477 def process_delete(self, wpath, evt): |
475 if self.ui.debugflag: | 478 if self.ui.debugflag: |
476 self.ui.note(_('%s event: deleted %s\n') % | 479 self.ui.note(_('%s event: deleted %s\n') % |
477 (self.event_time(), wpath)) | 480 (self.event_time(), wpath)) |
480 tree = self.dir(self.tree, wpath).copy() | 483 tree = self.dir(self.tree, wpath).copy() |
481 for wfn, ignore in self.walk('?', tree): | 484 for wfn, ignore in self.walk('?', tree): |
482 self.deletefile(join(wpath, wfn), '?') | 485 self.deletefile(join(wpath, wfn), '?') |
483 self.scan(wpath) | 486 self.scan(wpath) |
484 else: | 487 else: |
485 self.work(wpath, 'd') | 488 self.deleted(wpath) |
486 | 489 |
487 def process_modify(self, wpath, evt): | 490 def process_modify(self, wpath, evt): |
488 if self.ui.debugflag: | 491 if self.ui.debugflag: |
489 self.ui.note(_('%s event: modified %s\n') % | 492 self.ui.note(_('%s event: modified %s\n') % |
490 (self.event_time(), wpath)) | 493 (self.event_time(), wpath)) |
491 | 494 |
492 if not (evt.mask & inotify.IN_ISDIR): | 495 if not (evt.mask & inotify.IN_ISDIR): |
493 self.work(wpath, 'm') | 496 self.modified(wpath) |
494 | 497 |
495 def process_unmount(self, evt): | 498 def process_unmount(self, evt): |
496 self.ui.warn(_('filesystem containing %s was unmounted\n') % | 499 self.ui.warn(_('filesystem containing %s was unmounted\n') % |
497 evt.fullpath) | 500 evt.fullpath) |
498 sys.exit(0) | 501 sys.exit(0) |