Mercurial > piecrust2
comparison piecrust/publishing/shell.py @ 885:13e8b50a2113
publish: Fix publishers API and add a simple "copy" publisher.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 20 Jun 2017 21:12:35 -0700 |
parents | 42da89d8bd51 |
children | 8ca228956cc6 |
comparison
equal
deleted
inserted
replaced
884:18b3e2acd069 | 885:13e8b50a2113 |
---|---|
1 import os.path | |
1 import shlex | 2 import shlex |
2 from piecrust.publishing.base import ShellCommandPublisherBase | 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 ShellCommandPublisherBase(Publisher): | |
13 def __init__(self, app, target, config): | |
14 super(ShellCommandPublisherBase, self).__init__(app, target, config) | |
15 self.expand_user_args = True | |
16 | |
17 def run(self, ctx): | |
18 args = self._getCommandArgs(ctx) | |
19 if self.expand_user_args: | |
20 args = [os.path.expanduser(i) for i in args] | |
21 | |
22 if ctx.preview: | |
23 preview_args = ' '.join([shlex.quote(i) for i in args]) | |
24 logger.info( | |
25 "Would run shell command: %s" % preview_args) | |
26 return True | |
27 | |
28 logger.debug( | |
29 "Running shell command: %s" % args) | |
30 | |
31 proc = subprocess.Popen( | |
32 args, cwd=self.app.root_dir, bufsize=0, | |
33 stdout=subprocess.PIPE) | |
34 | |
35 logger.debug("Running publishing monitor for PID %d" % proc.pid) | |
36 thread = _PublishThread(proc) | |
37 thread.start() | |
38 proc.wait() | |
39 thread.join() | |
40 | |
41 if proc.returncode != 0: | |
42 logger.error( | |
43 "Publish process returned code %d" % proc.returncode) | |
44 else: | |
45 logger.debug("Publish process returned successfully.") | |
46 | |
47 return proc.returncode == 0 | |
48 | |
49 def _getCommandArgs(self, ctx): | |
50 raise NotImplementedError() | |
51 | |
52 | |
53 class _PublishThread(threading.Thread): | |
54 def __init__(self, proc): | |
55 super(_PublishThread, self).__init__( | |
56 name='publish_monitor', daemon=True) | |
57 self.proc = proc | |
58 self.root_logger = logging.getLogger() | |
59 | |
60 def run(self): | |
61 for line in iter(self.proc.stdout.readline, b''): | |
62 line_str = line.decode('utf8') | |
63 logger.info(line_str.rstrip('\r\n')) | |
64 for h in self.root_logger.handlers: | |
65 h.flush() | |
66 | |
67 self.proc.communicate() | |
68 logger.debug("Publish monitor exiting.") | |
3 | 69 |
4 | 70 |
5 class ShellCommandPublisher(ShellCommandPublisherBase): | 71 class ShellCommandPublisher(ShellCommandPublisherBase): |
6 PUBLISHER_NAME = 'shell' | 72 PUBLISHER_NAME = 'shell' |
7 | 73 |