Login

Autoreload Apache on project modifications

Author:
ivank
Posted:
October 7, 2007
Language:
Python
Version:
.96
Tags:
apache reload autoreload modified linux pyinotify
Score:
6 (after 6 ratings)

This script will reload apache when any modifications are detected in a folder - useful only if the development server or mod_python's reload does not suit your needs for testing. It requires Linux 2.6.13+, Python 2.5, and the development version of pyinotify.

 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python

# Usage: reloader.py dirToWatch [minDelay]
# reloader.py is Public Domain.

# Requires the development version of pyinotify
# See: http://seb.dbzteam.com/pages/pyinotify-dev.html
# Download: http://seb.dbzteam.com/pub/pyinotify.py

# Change the two settings below-

# changes to files with these extensions will trigger runCmd
reloadExtensions = ['.py', '.wsgi'] 
# command passed to os.system
runCmd = "/usr/sbin/apache2ctl restart"

import os
import sys
import time

import pyinotify

try:
    watchDir = sys.argv[1]
except IndexError:
    print "Usage: reloader.py dirToWatch [minDelay]"
    sys.exit()

minDelay = 0.5 # minimum delay before running the command again
if len(sys.argv) > 2:
    minDelay = float(sys.argv[2])
mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE | \
     pyinotify.IN_MODIFY # watched events

lastTime = 0

wm = pyinotify.WatchManager()

class PTmp(pyinotify.ProcessEvent):
    def process_default(self, event):
        global lastTime
        print("Change in %s" % os.path.join(event.path, event.name))
        if os.path.splitext(event.name)[-1] in reloadExtensions:
            if time.time() > (lastTime + minDelay):
                print "Running " + runCmd
                os.system(runCmd)
                lastTime = time.time()
            else:
                print "Timeout not reached, not running command."

notifier = pyinotify.Notifier(wm, PTmp())

wdd = wm.add_watch(watchDir, mask, rec=True, auto_add=True)

while True:
    try:
        notifier.process_events()
        if notifier.check_events():
            notifier.read_events()
    except KeyboardInterrupt:
        notifier.stop()
        break

More like this

  1. Middleware to reload translation for each request by asksol 5 years, 8 months ago
  2. django under apache / mod_fcgid by mike_45 6 years, 2 months ago
  3. Log Django exceptions to Apache error log in mod_wsgi by simon 5 years, 7 months ago
  4. Python fixup handler for Apache by ofalk 5 years, 10 months ago
  5. Upload, Progressbar with sessions by revolunet 6 years, 8 months ago

Comments

Please login first before commenting.