1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
# Example: daemonize pyinotify's notifier.
# Requires Python >= 2.5
import functools
import sys
import pyinotify
import subprocess
class Counter(object):
"""
Simple counter.
"""
def __init__(self):
self.count = 0
def plusone(self):
self.count += 1
class EventHandler(pyinotify.ProcessEvent):
def process_IN_CLOSE_WRITE(self, event):
print "Close_write:", event.pathname
fichier = event.pathname
if '.mp4' in fichier or '.mkv' in fichier or '.avi' in fichier:
process = subprocess.call(['ncftpput', '-m', '-u LOGIN', '-p PASS', 'FTP.SERVEUR1.COM' '/', fichier])
process = subprocess.call(['ncftpput', '-m', '-u LOGIN', '-p PASS', 'FTP.SERVEUR2.COM' '/', fichier])
process = subprocess.call(['ncftpput', '-m', '-u LOGIN', '-p PASS', 'FTP.SERVEUR3.COM' '/', fichier])
process = subprocess.call(['rm', fichier, 'home/jo/testftp2/'])
def on_loop(notifier, counter):
"""
Dummy function called after each event loop, this method only
ensures the child process eventually exits (after 5 iterations).
"""
sys.stdout.write("Loop %d\n" % counter.count)
counter.plusone()
handler = EventHandler()
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, handler)
wm.add_watch('/home/jo/testftp/', pyinotify.IN_CLOSE_WRITE)
on_loop_func = functools.partial(on_loop, counter=Counter())
try:
notifier.loop(daemonize=True, callback=on_loop_func,
pid_file='/tmp/pyinotify.pid', stdout='/tmp/stdout.txt')
except pyinotify.NotifierError, err:
print >> sys.stderr, err |
Partager