Login

Autoreload Apache on project modifications

Author:
ivank
Posted:
October 7, 2007
Language:
Python
Version:
.96
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.