comparison piecrust/publishing/shell.py @ 613:e2e955a3bb25

publish: Add publish command. * Add `shell` publisher. * Refactor admin panel's publishing backend to use that, along with the new PID file support.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 04 Feb 2016 08:05:03 -0800
parents
children a2d9ef307a08
comparison
equal deleted inserted replaced
612:2edaefcb82cd 613:e2e955a3bb25
1 import sys
2 import shlex
3 import logging
4 import threading
5 import subprocess
6 from piecrust.publishing.base import Publisher
7
8
9 logger = logging.getLogger(__name__)
10
11
12 class ShellCommandPublisher(Publisher):
13 PUBLISHER_NAME = 'shell'
14
15 def __init__(self, app, target):
16 super(ShellCommandPublisher, self).__init__(app, target)
17 self.is_using_custom_logging = True
18
19 def run(self, ctx):
20 target_cmd = self.getConfigValue('cmd')
21 if not target_cmd:
22 raise Exception("No command specified for publish target: %s" %
23 self.target)
24 args = shlex.split(target_cmd)
25
26 logger.debug(
27 "Running shell command: %s" % args)
28
29 proc = subprocess.Popen(
30 args, cwd=self.app.root_dir, bufsize=0,
31 stdout=subprocess.PIPE,
32 universal_newlines=False)
33
34 logger.debug("Running publishing monitor for PID %d" % proc.pid)
35 thread = _PublishThread(proc, ctx.custom_logging_file)
36 thread.start()
37 proc.wait()
38 thread.join()
39
40 if proc.returncode != 0:
41 logger.error(
42 "Publish process returned code %d" % proc.returncode)
43 else:
44 logger.debug("Publish process returned successfully.")
45
46 return proc.returncode == 0
47
48
49 class _PublishThread(threading.Thread):
50 def __init__(self, proc, log_fp):
51 super(_PublishThread, self).__init__(
52 name='publish_monitor', daemon=True)
53 self.proc = proc
54 self.log_fp = log_fp
55
56 def run(self):
57 for line in iter(self.proc.stdout.readline, b''):
58 line_str = line.decode('utf8')
59 sys.stdout.write(line_str)
60 sys.stdout.flush()
61 if self.log_fp:
62 self.log_fp.write(line_str)
63
64 self.proc.communicate()
65 logger.debug("Publish monitor exiting.")
66