Mercurial > piecrust2
comparison piecrust/publishing/publisher.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 | 8f9c0bdb3724 |
comparison
equal
deleted
inserted
replaced
612:2edaefcb82cd | 613:e2e955a3bb25 |
---|---|
1 import logging | |
2 from piecrust.publishing.base import PublishingContext | |
3 | |
4 | |
5 logger = logging.getLogger(__name__) | |
6 | |
7 | |
8 class InvalidPublishTargetError(Exception): | |
9 pass | |
10 | |
11 | |
12 class PublishingError(Exception): | |
13 pass | |
14 | |
15 | |
16 class Publisher(object): | |
17 def __init__(self, app): | |
18 self.app = app | |
19 | |
20 def run(self, target, log_file=None): | |
21 target_cfg = self.app.config.get('publish/%s' % target) | |
22 if not target_cfg: | |
23 raise InvalidPublishTargetError( | |
24 "No such publish target: %s" % target) | |
25 | |
26 target_type = target_cfg.get('type') | |
27 if not target_type: | |
28 raise InvalidPublishTargetError( | |
29 "Publish target '%s' doesn't specify a type." % target) | |
30 | |
31 pub = None | |
32 for pub_cls in self.app.plugin_loader.getPublishers(): | |
33 if pub_cls.PUBLISHER_NAME == target_type: | |
34 pub = pub_cls(self.app, target) | |
35 break | |
36 if pub is None: | |
37 raise InvalidPublishTargetError( | |
38 "Publish target '%s' has invalid type: %s" % | |
39 (target, target_type)) | |
40 | |
41 ctx = PublishingContext() | |
42 | |
43 hdlr = None | |
44 if log_file: | |
45 if not pub.is_using_custom_logging: | |
46 logger.debug("Adding file handler for: %s" % log_file) | |
47 hdlr = logging.FileHandler(log_file, mode='w', encoding='utf8') | |
48 logger.addHandler(hdlr) | |
49 else: | |
50 logger.debug("Creating custom log file: %s" % log_file) | |
51 ctx.custom_logging_file = open( | |
52 log_file, mode='w', encoding='utf8') | |
53 | |
54 intro_msg = ("Running publish target '%s' with publisher: %s" % | |
55 (target, pub.PUBLISHER_NAME)) | |
56 logger.debug(intro_msg) | |
57 if ctx.custom_logging_file: | |
58 ctx.custom_logging_file.write(intro_msg + "\n") | |
59 | |
60 try: | |
61 success = pub.run(ctx) | |
62 except Exception as ex: | |
63 raise PublishingError( | |
64 "Error publishing to target: %s" % target) from ex | |
65 finally: | |
66 if ctx.custom_logging_file: | |
67 ctx.custom_logging_file.close() | |
68 if hdlr: | |
69 logger.removeHandler(hdlr) | |
70 hdlr.close() | |
71 | |
72 if not success: | |
73 raise PublishingError( | |
74 "Unknown error publishing to target: %s" % target) | |
75 |