diff piecrust/publishing/shell.py @ 621:8f9c0bdb3724

publish: Polish/refactor the publishing workflows. * Add a `--preview` option. * The `--list` option gives a nicer output, and prints warnings/errors for incorrect configuration. * Moved most of the `shell` code into a base class that's reusable. * Simplified the code to log publishing to a file. * Nicer output overall, with times.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 08 Feb 2016 20:44:26 -0800
parents a2d9ef307a08
children 42da89d8bd51
line wrap: on
line diff
--- a/piecrust/publishing/shell.py	Sat Feb 06 21:49:50 2016 -0800
+++ b/piecrust/publishing/shell.py	Mon Feb 08 20:44:26 2016 -0800
@@ -1,67 +1,15 @@
-import sys
 import shlex
-import logging
-import threading
-import subprocess
-from piecrust.publishing.base import Publisher
+from piecrust.publishing.base import ShellCommandPublisherBase
 
 
-logger = logging.getLogger(__name__)
-
-
-class ShellCommandPublisher(Publisher):
+class ShellCommandPublisher(ShellCommandPublisherBase):
     PUBLISHER_NAME = 'shell'
 
-    def __init__(self, app, target):
-        super(ShellCommandPublisher, self).__init__(app, target)
-        self.is_using_custom_logging = True
-
-    def run(self, ctx):
+    def _getCommandArgs(self, ctx):
         target_cmd = self.getConfigValue('cmd')
         if not target_cmd:
             raise Exception("No command specified for publish target: %s" %
                             self.target)
         args = shlex.split(target_cmd)
-
-        logger.debug(
-                "Running shell command: %s" % args)
-
-        proc = subprocess.Popen(
-                args, cwd=self.app.root_dir, bufsize=0,
-                stdout=subprocess.PIPE,
-                universal_newlines=False)
-
-        logger.debug("Running publishing monitor for PID %d" % proc.pid)
-        thread = _PublishThread(proc, ctx.custom_logging_file)
-        thread.start()
-        proc.wait()
-        thread.join()
-
-        if proc.returncode != 0:
-            logger.error(
-                    "Publish process returned code %d" % proc.returncode)
-        else:
-            logger.debug("Publish process returned successfully.")
+        return args
 
-        return proc.returncode == 0
-
-
-class _PublishThread(threading.Thread):
-    def __init__(self, proc, log_fp):
-        super(_PublishThread, self).__init__(
-                name='publish_monitor', daemon=True)
-        self.proc = proc
-        self.log_fp = log_fp
-
-    def run(self):
-        for line in iter(self.proc.stdout.readline, b''):
-            line_str = line.decode('utf8')
-            sys.stdout.write(line_str)
-            sys.stdout.flush()
-            if self.log_fp:
-                self.log_fp.write(line_str)
-                self.log_fp.flush()
-
-        self.proc.communicate()
-        logger.debug("Publish monitor exiting.")
-