comparison piecrust/publishing/base.py @ 758:6abb436fea5b

publish: Make publisher more powerful and better exposed on the command line. * Make the `chef publish` command have one sub-command per publish target. * Add custom argument parsing per publisher to have strong extra arguments available per publish target. * Make publish targets a first class citizen of the `PieCrust` app class.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 25 Jun 2016 17:03:29 -0700
parents 8f9c0bdb3724
children f69fdc601845
comparison
equal deleted inserted replaced
757:7147b06670fd 758:6abb436fea5b
1 import os.path 1 import os.path
2 import shlex 2 import shlex
3 import urllib.parse
3 import logging 4 import logging
4 import threading 5 import threading
5 import subprocess 6 import subprocess
7 from piecrust.configuration import try_get_dict_value
6 8
7 9
8 logger = logging.getLogger(__name__) 10 logger = logging.getLogger(__name__)
9 11
10 12
13 FILE_MODIFIED = 1
14 FILE_DELETED = 2
15
16
17 class PublisherConfigurationError(Exception):
18 pass
19
20
11 class PublishingContext(object): 21 class PublishingContext(object):
12 def __init__(self): 22 def __init__(self):
13 self.bake_out_dir = None 23 self.bake_out_dir = None
24 self.bake_record = None
25 self.processing_record = None
26 self.was_baked = False
14 self.preview = False 27 self.preview = False
28 self.args = None
15 29
16 30
17 class Publisher(object): 31 class Publisher(object):
18 def __init__(self, app, target): 32 PUBLISHER_NAME = 'undefined'
33 PUBLISHER_SCHEME = None
34
35 def __init__(self, app, target, config):
19 self.app = app 36 self.app = app
20 self.target = target 37 self.target = target
21 self.parsed_url = None 38 self.config = config
39 self.has_url_config = isinstance(config, urllib.parse.ParseResult)
22 self.log_file_path = None 40 self.log_file_path = None
23 41
24 @property 42 def setupPublishParser(self, parser, app):
25 def has_url_config(self): 43 return
26 return self.parsed_url is not None
27 44
28 @property 45 def getConfigValue(self, name, default_value=None):
29 def url_config(self):
30 if self.parsed_url is not None:
31 return self.getConfig()
32 raise Exception("This publisher has a full configuration.")
33
34 def getConfig(self):
35 return self.app.config.get('publish/%s' % self.target)
36
37 def getConfigValue(self, name):
38 if self.has_url_config: 46 if self.has_url_config:
39 raise Exception("This publisher only has a URL configuration.") 47 raise Exception("This publisher only has a URL configuration.")
40 return self.app.config.get('publish/%s/%s' % (self.target, name)) 48 return try_get_dict_value(self.config, name, default_value)
41 49
42 def run(self, ctx): 50 def run(self, ctx):
43 raise NotImplementedError() 51 raise NotImplementedError()
52
53 def getBakedFiles(self, ctx):
54 for e in ctx.bake_record.entries:
55 for sub in e.subs:
56 if sub.was_baked:
57 yield sub.out_path
58 for e in ctx.processing_record.entries:
59 if e.was_processed:
60 yield from [os.path.join(ctx.processing_record.out_dir, p)
61 for p in e.rel_outputs]
62
63 def getDeletedFiles(self, ctx):
64 yield from ctx.bake_record.deleted
65 yield from ctx.processing_record.deleted
44 66
45 67
46 class ShellCommandPublisherBase(Publisher): 68 class ShellCommandPublisherBase(Publisher):
47 def __init__(self, app, target): 69 def __init__(self, app, target):
48 super(ShellCommandPublisherBase, self).__init__(app, target) 70 super(ShellCommandPublisherBase, self).__init__(app, target)