Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8aa785c

Browse files
committedJul 10, 2015
Fix #1883 -- nikola auto --no-server
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
1 parent c8d97e2 commit 8aa785c

File tree

2 files changed

+53
-25
lines changed

2 files changed

+53
-25
lines changed
 

Diff for: ‎CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ New in master
44
Features
55
--------
66

7+
* Add ``--no-server`` option to ``nikola auto`` (Issue #1883)
78
* Remove logging handlers (Issue #1797)
89
* Add ``-d``, ``--detach`` option to ``nikola serve`` (Issue #1871)
910
* Use provided teaser format (``*_READ_MORE_LINK``) with custom teaser text

Diff for: ‎nikola/plugins/command/auto/__init__.py

+52-25
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import os
3232
import re
3333
import subprocess
34+
import time
3435
try:
3536
from urlparse import urlparse
3637
except ImportError:
@@ -77,6 +78,7 @@ class CommandAuto(Command):
7778
"""Start debugging console."""
7879
name = "auto"
7980
logger = None
81+
has_server = True
8082
doc_purpose = "builds and serves a site; automatically detects site changes, rebuilds, and optionally refreshes a browser"
8183
cmd_options = [
8284
{
@@ -100,7 +102,7 @@ class CommandAuto(Command):
100102
'short': 'b',
101103
'long': 'browser',
102104
'type': bool,
103-
'help': 'Start a web browser.',
105+
'help': 'Start a web browser',
104106
'default': False,
105107
},
106108
{
@@ -111,6 +113,13 @@ class CommandAuto(Command):
111113
'type': bool,
112114
'help': 'Use IPv6',
113115
},
116+
{
117+
'name': 'no-server',
118+
'long': 'no-server',
119+
'default': False,
120+
'type': bool,
121+
'help': 'Disable the server, automate rebuilds only'
122+
},
114123
]
115124

116125
def _execute(self, options, args):
@@ -166,10 +175,14 @@ def _execute(self, options, args):
166175

167176
host = options['address'].strip('[').strip(']') or dhost
168177

178+
# Server can be disabled (Issue #1883)
179+
self.has_server = not options['no-server']
180+
169181
# Instantiate global observer
170182
observer = Observer()
171-
# Watch output folders and trigger reloads
172-
observer.schedule(OurWatchHandler(self.do_refresh), out_folder, recursive=True)
183+
if self.has_server:
184+
# Watch output folders and trigger reloads
185+
observer.schedule(OurWatchHandler(self.do_refresh), out_folder, recursive=True)
173186

174187
# Watch input folders and trigger rebuilds
175188
for p in watched:
@@ -182,6 +195,7 @@ def _execute(self, options, args):
182195
observer.schedule(ConfigWatchHandler(_conf_fn, self.do_rebuild), _conf_dn, recursive=False)
183196

184197
try:
198+
self.logger.info("Watching files for changes...")
185199
observer.start()
186200
except KeyboardInterrupt:
187201
pass
@@ -195,29 +209,42 @@ def __call__(self, environ, start_response):
195209
return parent.serve_static(environ, start_response)
196210
return super(Mixed, self).__call__(environ, start_response)
197211

198-
ws = make_server(
199-
host, port, server_class=WSGIServer,
200-
handler_class=WebSocketWSGIRequestHandler,
201-
app=Mixed(handler_cls=LRSocket)
202-
)
203-
ws.initialize_websockets_manager()
204-
self.logger.info("Serving HTTP on {0} port {1}...".format(host, port))
205-
if browser:
206-
if options['ipv6'] or '::' in host:
207-
server_url = "http://[{0}]:{1}/".format(host, port)
208-
else:
209-
server_url = "http://{0}:{1}/".format(host, port)
210-
211-
self.logger.info("Opening {0} in the default web browser...".format(server_url))
212-
# Yes, this is racy
213-
webbrowser.open('http://{0}:{1}'.format(host, port))
212+
if self.has_server:
213+
ws = make_server(
214+
host, port, server_class=WSGIServer,
215+
handler_class=WebSocketWSGIRequestHandler,
216+
app=Mixed(handler_cls=LRSocket)
217+
)
218+
ws.initialize_websockets_manager()
219+
self.logger.info("Serving HTTP on {0} port {1}...".format(host, port))
220+
if browser:
221+
if options['ipv6'] or '::' in host:
222+
server_url = "http://[{0}]:{1}/".format(host, port)
223+
else:
224+
server_url = "http://{0}:{1}/".format(host, port)
225+
226+
self.logger.info("Opening {0} in the default web browser...".format(server_url))
227+
# Yes, this is racy
228+
webbrowser.open('http://{0}:{1}'.format(host, port))
229+
230+
try:
231+
ws.serve_forever()
232+
except KeyboardInterrupt:
233+
self.logger.info("Server is shutting down.")
234+
observer.stop()
235+
observer.join()
236+
else:
237+
# Workaround: can’t have nothing running (instant exit)
238+
# but also can’t join threads (no way to exit)
239+
# The joys of threading.
240+
try:
241+
while True:
242+
time.sleep(1)
243+
except KeyboardInterrupt:
244+
self.logger.info("Shutting down.")
245+
observer.stop()
246+
observer.join()
214247

215-
try:
216-
ws.serve_forever()
217-
except KeyboardInterrupt:
218-
self.logger.info("Server is shutting down.")
219-
observer.stop()
220-
observer.join()
221248

222249
def do_rebuild(self, event):
223250
self.logger.info('REBUILDING SITE (from {0})'.format(event.src_path))

0 commit comments

Comments
 (0)
Please sign in to comment.