Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python3 support and a tiny bit of "PEP8"-ing #18

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ It's written in Python, making it easier to hack.

## Requirements

You need Python 2.7 and the [pyinotify](http://github.com/seb-m/pyinotify)
library.
This fork is updated for Python 3 to run on Ubuntu 22.04 and similar.
Watcher uses the [pyinotify](http://github.com/seb-m/pyinotify) library.

In Ubuntu (and Debian) you can install these with:
In Ubuntu (and Debian) you can install it with:

sudo apt-get install python python-pyinotify
sudo apt-get install python3-pyinotify

## Configuration

Expand All @@ -33,7 +33,6 @@ Make sure watcher.py is marked as executable:

chmod +x watcher.py


Start the daemon with:

./watcher.py start
Expand All @@ -46,6 +45,10 @@ Restart it with:

./watcher.py restart

Debug it with: (log output to console)

./watcher.py debug

If you don't want the daemon to fork to the background, start it with

./watcher.py debug
Expand Down
85 changes: 46 additions & 39 deletions watcher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# Copyright (c) 2010 Greggory Hernandez

# Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -31,16 +31,23 @@
# jobs when files or directories change
### END INIT INFO

import sys, os, time, atexit
from signal import SIGTERM
import pyinotify
import sys, os
import argparse
import atexit
import datetime
import subprocess
from types import *
import os
import time
from signal import SIGTERM
import sys
from string import Template
import ConfigParser
import argparse
from types import *

import pyinotify

try:
import ConfigParser
except ImportError:
import configparser as ConfigParser


class Daemon:
"""
Expand All @@ -65,7 +72,7 @@ def daemonize(self):
if pid > 0:
#exit first parent
sys.exit(0)
except OSError, e:
except OSError as e:
sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)

Expand All @@ -80,24 +87,24 @@ def daemonize(self):
if pid > 0:
# exit from second parent
sys.exit(0)
except OSError, e:
except OSError as e:
sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)

#redirect standard file descriptors
sys.stdout.flush()
sys.stderr.flush()
si = file(self.stdin, 'r')
so = file(self.stdout, 'a+')
se = file(self.stderr, 'a+', 0)
si = open(self.stdin, 'r')
so = open(self.stdout, 'a+')
se = open(self.stderr, 'a+')
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())

#write pid file
atexit.register(self.delpid)
pid = str(os.getpid())
file(self.pidfile, 'w+').write("%s\n" % pid)
open(self.pidfile, 'w+').write("%s\n" % pid)

def delpid(self):
os.remove(self.pidfile)
Expand All @@ -108,7 +115,7 @@ def start(self):
"""
# Check for a pidfile to see if the daemon already runs
try:
pf = file(self.pidfile, 'r')
pf = open(self.pidfile, 'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
Expand All @@ -129,7 +136,7 @@ def stop(self):
"""
# get the pid from the pidfile
try:
pf = file(self.pidfile, 'r')
pf = open(self.pidfile, 'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
Expand All @@ -145,13 +152,13 @@ def stop(self):
while 1:
os.kill(pid, SIGTERM)
time.sleep(0.1)
except OSError, err:
except OSError as err:
err = str(err)
if err.find("No such process") > 0:
if os.path.exists(self.pidfile):
os.remove(self.pidfile)
else:
print str(err)
print(str(err))
sys.exit(1)

def restart(self):
Expand All @@ -163,17 +170,17 @@ def restart(self):

def status(self):
try:
pf = file(self.pidfile, 'r')
pf = open(self.pidfile, 'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
pid = None

if pid:
print "service running"
print("service running")
sys.exit(0)
if not pid:
print "service not running"
print("service not running")
sys.exit(3)

def run(self):
Expand Down Expand Up @@ -201,51 +208,51 @@ def runCommand(self, event):
cookie=self.shellquote(event.cookie if hasattr(event, "cookie") else 0))
try:
os.system(command)
except OSError, err:
print "Failed to run command '%s' %s" % (command, str(err))
except OSError as err:
print("Failed to run command '%s' %s" % (command, str(err)))

def process_IN_ACCESS(self, event):
print "Access: ", event.pathname
print("Access: ", event.pathname)
self.runCommand(event)

def process_IN_ATTRIB(self, event):
print "Attrib: ", event.pathname
print("Attrib: ", event.pathname)
self.runCommand(event)

def process_IN_CLOSE_WRITE(self, event):
print "Close write: ", event.pathname
print("Close write: ", event.pathname)
self.runCommand(event)

def process_IN_CLOSE_NOWRITE(self, event):
print "Close nowrite: ", event.pathname
print("Close nowrite: ", event.pathname)
self.runCommand(event)

def process_IN_CREATE(self, event):
print "Creating: ", event.pathname
print("Creating: ", event.pathname)
self.runCommand(event)

def process_IN_DELETE(self, event):
print "Deleteing: ", event.pathname
print("Deleteing: ", event.pathname)
self.runCommand(event)

def process_IN_MODIFY(self, event):
print "Modify: ", event.pathname
print("Modify: ", event.pathname)
self.runCommand(event)

def process_IN_MOVE_SELF(self, event):
print "Move self: ", event.pathname
print("Move self: ", event.pathname)
self.runCommand(event)

def process_IN_MOVED_FROM(self, event):
print "Moved from: ", event.pathname
print("Moved from: ", event.pathname)
self.runCommand(event)

def process_IN_MOVED_TO(self, event):
print "Moved to: ", event.pathname
print("Moved to: ", event.pathname)
self.runCommand(event)

def process_IN_OPEN(self, event):
print "Opened: ", event.pathname
print("Opened: ", event.pathname)
self.runCommand(event)

class WatcherDaemon(Daemon):
Expand Down Expand Up @@ -297,7 +304,7 @@ def run(self):


def _parseMask(self, masks):
ret = False;
ret = False

for mask in masks:
mask = mask.strip()
Expand Down Expand Up @@ -370,11 +377,11 @@ def log(msg):
if(args.config):
confok = config.read(args.config)
else:
confok = config.read(['/etc/watcher.ini', os.path.expanduser('~/.watcher.ini')]);
confok = config.read(['/etc/watcher.ini', os.path.expanduser('~/.watcher.ini')])

if(not confok):
sys.stderr.write("Failed to read config file. Try -c parameter\n")
sys.exit(4);
sys.exit(4)

# Initialize the daemon
daemon = WatcherDaemon(config)
Expand All @@ -391,6 +398,6 @@ def log(msg):
elif 'debug' == args.command:
daemon.run()
else:
print "Unkown Command"
print("Unkown Command")
sys.exit(2)
sys.exit(0)