Fix #62: Move contrib/* to klaus.contrib package
Jonas Haag
11 years ago
0 | import os | |
1 | from klaus import make_app | |
2 | ||
3 | application = make_app( | |
4 | os.environ['KLAUS_REPOS'].split(), | |
5 | os.environ['KLAUS_SITE_NAME'], | |
6 | os.environ.get('KLAUS_USE_SMARTHTTP'), | |
7 | os.environ.get('KLAUS_HTDIGEST_FILE'), | |
8 | ) |
0 | import os | |
1 | import time | |
2 | import threading | |
3 | from klaus import make_app | |
4 | ||
5 | ||
6 | # Shared state between poller and application wrapper | |
7 | class _: | |
8 | #: the real WSGI app | |
9 | inner_app = None | |
10 | should_reload = True | |
11 | ||
12 | ||
13 | def poll_for_changes(interval, dir): | |
14 | """ | |
15 | Polls `dir` for changes every `interval` seconds and sets `should_reload` | |
16 | accordingly. | |
17 | """ | |
18 | old_contents = os.listdir(dir) | |
19 | while 1: | |
20 | time.sleep(interval) | |
21 | if _.should_reload: | |
22 | # klaus application has not seen our change yet | |
23 | continue | |
24 | new_contents = os.listdir(dir) | |
25 | if new_contents != old_contents: | |
26 | # Directory contents changed => should_reload | |
27 | new_contents = old_contents | |
28 | _.should_reload = True | |
29 | ||
30 | ||
31 | def make_autoreloading_app(repos_root, *args, **kwargs): | |
32 | def app(environ, start_response): | |
33 | if _.should_reload: | |
34 | # Refresh inner application with new repo list | |
35 | print "Reloading repository list..." | |
36 | _.inner_app = make_app( | |
37 | [os.path.join(repos_root, x) for x in os.listdir(repos_root)], | |
38 | *args, **kwargs | |
39 | ) | |
40 | _.should_reload = False | |
41 | return _.inner_app(environ, start_response) | |
42 | ||
43 | # Background thread that polls the directory for changes | |
44 | poller_thread = threading.Thread(target=(lambda: poll_for_changes(10, repos_root))) | |
45 | poller_thread.daemon = True | |
46 | poller_thread.start() | |
47 | ||
48 | return app | |
49 | ||
50 | ||
51 | application = make_autoreloading_app( | |
52 | os.environ['KLAUS_REPOS'], | |
53 | os.environ['KLAUS_SITE_NAME'], | |
54 | os.environ.get('KLAUS_USE_SMARTHTTP'), | |
55 | os.environ.get('KLAUS_HTDIGEST_FILE'), | |
56 | ) |
0 | import os | |
1 | from klaus import make_app | |
2 | ||
3 | application = make_app( | |
4 | os.environ['KLAUS_REPOS'].split(), | |
5 | os.environ['KLAUS_SITE_NAME'], | |
6 | os.environ.get('KLAUS_USE_SMARTHTTP'), | |
7 | os.environ.get('KLAUS_HTDIGEST_FILE'), | |
8 | ) |
0 | import os | |
1 | import time | |
2 | import threading | |
3 | from klaus import make_app | |
4 | ||
5 | ||
6 | # Shared state between poller and application wrapper | |
7 | class _: | |
8 | #: the real WSGI app | |
9 | inner_app = None | |
10 | should_reload = True | |
11 | ||
12 | ||
13 | def poll_for_changes(interval, dir): | |
14 | """ | |
15 | Polls `dir` for changes every `interval` seconds and sets `should_reload` | |
16 | accordingly. | |
17 | """ | |
18 | old_contents = os.listdir(dir) | |
19 | while 1: | |
20 | time.sleep(interval) | |
21 | if _.should_reload: | |
22 | # klaus application has not seen our change yet | |
23 | continue | |
24 | new_contents = os.listdir(dir) | |
25 | if new_contents != old_contents: | |
26 | # Directory contents changed => should_reload | |
27 | new_contents = old_contents | |
28 | _.should_reload = True | |
29 | ||
30 | ||
31 | def make_autoreloading_app(repos_root, *args, **kwargs): | |
32 | def app(environ, start_response): | |
33 | if _.should_reload: | |
34 | # Refresh inner application with new repo list | |
35 | print "Reloading repository list..." | |
36 | _.inner_app = make_app( | |
37 | [os.path.join(repos_root, x) for x in os.listdir(repos_root)], | |
38 | *args, **kwargs | |
39 | ) | |
40 | _.should_reload = False | |
41 | return _.inner_app(environ, start_response) | |
42 | ||
43 | # Background thread that polls the directory for changes | |
44 | poller_thread = threading.Thread(target=(lambda: poll_for_changes(10, repos_root))) | |
45 | poller_thread.daemon = True | |
46 | poller_thread.start() | |
47 | ||
48 | return app | |
49 | ||
50 | ||
51 | application = make_autoreloading_app( | |
52 | os.environ['KLAUS_REPOS'], | |
53 | os.environ['KLAUS_SITE_NAME'], | |
54 | os.environ.get('KLAUS_USE_SMARTHTTP'), | |
55 | os.environ.get('KLAUS_HTDIGEST_FILE'), | |
56 | ) |