Skip to content

Python 3 compatibility. #6

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

Closed
wants to merge 2 commits into from
Closed
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
58 changes: 29 additions & 29 deletions watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import subprocess
from types import *
from string import Template
import ConfigParser
import configparser
import argparse

class Daemon:
Expand All @@ -65,7 +65,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 +80,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+', 0)
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 +108,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 +129,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 +145,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 +163,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 +201,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 @@ -367,7 +367,7 @@ def log(msg):
args = parser.parse_args()

# Parse the config file
config = ConfigParser.ConfigParser()
config = configparser.ConfigParser()
if(args.config):
confok = config.read(args.config)
else:
Expand All @@ -392,6 +392,6 @@ def log(msg):
elif 'debug' == args.command:
daemon.run()
else:
print "Unkown Command"
print("Unkown Command")
sys.exit(2)
sys.exit(0)