comparison piecrust/commands/builtin/publishing.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 e2e955a3bb25
children 6abb436fea5b
comparison
equal deleted inserted replaced
620:c2708f20a87b 621:8f9c0bdb3724
1 import logging 1 import logging
2 import urllib.parse
2 from piecrust.commands.base import ChefCommand 3 from piecrust.commands.base import ChefCommand
3 from piecrust.publishing.publisher import Publisher 4 from piecrust.publishing.publisher import Publisher, find_publisher_name
4 5
5 6
6 logger = logging.getLogger(__name__) 7 logger = logging.getLogger(__name__)
7 8
8 9
22 parser.add_argument( 23 parser.add_argument(
23 '--log-publisher', 24 '--log-publisher',
24 metavar='LOG_FILE', 25 metavar='LOG_FILE',
25 help="Log the publisher's output to a given file.") 26 help="Log the publisher's output to a given file.")
26 parser.add_argument( 27 parser.add_argument(
28 '--preview',
29 action='store_true',
30 help="Only preview what the publisher would do.")
31 parser.add_argument(
27 'target', 32 'target',
28 nargs='?', 33 nargs='?',
29 default='default', 34 default='default',
30 help="The publish target to use.") 35 help="The publish target to use.")
31 36
35 if not pub_cfg: 40 if not pub_cfg:
36 logger.info("No available publish targets.") 41 logger.info("No available publish targets.")
37 return 42 return
38 43
39 for name, cfg in pub_cfg.items(): 44 for name, cfg in pub_cfg.items():
40 desc = cfg.get('description') 45 if isinstance(cfg, dict):
41 if not desc: 46 pub_type = cfg.get('type')
42 logger.info(name) 47 if pub_type:
48 desc = cfg.get('description')
49 bake_first = cfg.get('bake', True)
50 msg = '%s (%s)' % (name, pub_type)
51 if not bake_first:
52 msg += ' (no local baking)'
53 if desc:
54 msg += ': ' + desc
55 logger.info(msg)
56 else:
57 logger.error(
58 "%s (unknown type '%s')" % (name, pub_type))
59 elif isinstance(cfg, str):
60 comps = urllib.parse.urlparse(str(cfg))
61 pub_name = find_publisher_name(ctx.app, comps.scheme)
62 if pub_name:
63 logger.info("%s (%s)" % (name, pub_name))
64 else:
65 logger.error(
66 "%s (unknown scheme '%s')" %
67 (name, comps.scheme))
43 else: 68 else:
44 logger.info("%s: %s" % (name, desc)) 69 logger.error(
70 "%s (incorrect configuration)" % name)
45 return 71 return
46 72
47 pub = Publisher(ctx.app) 73 pub = Publisher(ctx.app)
48 pub.run(ctx.args.target, log_file=ctx.args.log_publisher) 74 pub.run(
75 ctx.args.target,
76 preview=ctx.args.preview,
77 log_file=ctx.args.log_publisher)
49 78