~ubuntu-branches/ubuntu/precise/trac/precise

« back to all changes in this revision

Viewing changes to trac/util/autoreload.py

  • Committer: Bazaar Package Importer
  • Author(s): Luis Matos
  • Date: 2008-07-13 23:46:20 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20080713234620-13ynpdpkbaymfg1z
Tags: 0.11-2
* Re-added python-setup-tools to build dependences. Closes: #490320 #468705
* New upstream release Closes: 489727
* Added sugestion for other vcs support available: git bazaar mercurial 
* Added spamfilter plugin to sugests
* Moved packaging from python-support to python-central
* Added an entry to the NEWS about the cgi Closes: #490275
* Updated 10_remove_trac_suffix_from_title patch to be used in 0.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
 
14
14
import os
15
15
import sys
 
16
import thread
16
17
import time
17
 
import thread
18
18
 
19
19
_SLEEP_TIME = 1
20
20
 
21
21
def _reloader_thread(modification_callback):
22
22
    """When this function is run from the main thread, it will force other
23
23
    threads to exit when any modules currently loaded change.
24
 
    @param modification_callback: Function taking a single argument, the
25
 
    modified file, and is called after a modification is detected."""
 
24
    
 
25
    @param modification_callback: a function taking a single argument, the
 
26
        modified file, which is called every time a modification is detected
 
27
    """
26
28
    mtimes = {}
27
29
    while True:
28
 
        for filename in filter(None, [getattr(module, "__file__", None)
 
30
        for filename in filter(None, [getattr(module, '__file__', None)
29
31
                                      for module in sys.modules.values()]):
30
32
            while not os.path.isfile(filename): # Probably in an egg or zip file
31
33
                filename = os.path.dirname(filename)
34
36
            if not filename: # Couldn't map to physical file, so just ignore
35
37
                continue
36
38
 
37
 
            if filename.endswith(".pyc"):
 
39
            if filename.endswith('.pyc') or filename.endswith('.pyo'):
38
40
                filename = filename[:-1]
39
41
 
 
42
            if not os.path.isfile(filename):
 
43
                # Compiled file for non-existant source
 
44
                continue
 
45
 
40
46
            mtime = os.stat(filename).st_mtime
41
47
            if filename not in mtimes:
42
48
                mtimes[filename] = mtime
49
55
def _restart_with_reloader():
50
56
    while True:
51
57
        args = [sys.executable] + sys.argv
52
 
        if sys.platform == "win32":
 
58
        if sys.platform == 'win32':
53
59
            args = ['"%s"' % arg for arg in args]
54
60
        new_environ = os.environ.copy()
55
 
        new_environ["RUN_MAIN"] = 'true'
 
61
        new_environ['RUN_MAIN'] = 'true'
56
62
 
57
63
        # This call reinvokes ourself and goes into the other branch of main as
58
64
        # a new process.
61
67
        if exit_code != 3:
62
68
            return exit_code
63
69
 
64
 
def main(main_func, modification_callback):
65
 
    """Run `main_func` and restart any time modules are changed."""
66
 
 
67
 
    if os.environ.get("RUN_MAIN"):
 
70
def main(func, modification_callback, *args, **kwargs):
 
71
    """Run the given function and restart any time modules are changed."""
 
72
    if os.environ.get('RUN_MAIN'):
68
73
        # Lanch the actual program as a child thread
69
 
        thread.start_new_thread(main_func, ())
70
 
 
 
74
        thread.start_new_thread(func, args, kwargs)
71
75
        try:
72
76
            # Now wait for a file modification and quit
73
77
            _reloader_thread(modification_callback)
74
78
        except KeyboardInterrupt:
75
79
            pass
76
 
 
77
80
    else:
78
81
        # Initial invocation just waits around restarting this executable
79
82
        try: