comparison piecrust/publishing/copy.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
children
comparison
equal deleted inserted replaced
884:18b3e2acd069 885:13e8b50a2113
1 import os
2 import os.path
3 import shutil
4 import logging
5 from piecrust.publishing.base import Publisher
6
7
8 logger = logging.getLogger(__name__)
9
10
11 class CopyPublisher(Publisher):
12 PUBLISHER_NAME = 'copy'
13 PUBLISHER_SCHEME = 'file'
14
15 def parseUrlTarget(self, url):
16 self.config = {'output': (url.netloc + url.path)}
17
18 def run(self, ctx):
19 dest = self.config.get('output')
20
21 if ctx.was_baked:
22 to_upload = list(self.getBakedFiles(ctx))
23 to_delete = list(self.getDeletedFiles(ctx))
24 if to_upload or to_delete:
25 logger.info("Copying new/changed files...")
26 for path in to_upload:
27 rel_path = os.path.relpath(path, ctx.bake_out_dir)
28 dest_path = os.path.join(dest, rel_path)
29 dest_dir = os.path.dirname(dest_path)
30 os.makedirs(dest_dir, exist_ok=True)
31 try:
32 dest_mtime = os.path.getmtime(dest_path)
33 except OSError:
34 dest_mtime = 0
35 if os.path.getmtime(path) >= dest_mtime:
36 logger.info(rel_path)
37 if not ctx.preview:
38 shutil.copyfile(path, dest_path)
39
40 logger.info("Deleting removed files...")
41 for path in self.getDeletedFiles(ctx):
42 rel_path = os.path.relpath(path, ctx.bake_out_dir)
43 logger.info("%s [DELETE]" % rel_path)
44 if not ctx.preview:
45 try:
46 os.remove(path)
47 except OSError:
48 pass
49 else:
50 logger.info("Nothing to copy to the output folder.")
51