annotate piecrust/publishing/sftp.py @ 1188:a7c43131d871

bake: Fix file write flushing problem with Python 3.8+ Writing the cache files fails in Python 3.8 because it looks like flushing behaviour has changed. We need to explicitly flush. And even then, in very rare occurrences, it looks like it can still run into racing conditions, so we do a very hacky and ugly "retry" loop when fetching cached data :(
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 15 Jun 2021 22:36:23 -0700
parents 5713b6a2850d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
759
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import os
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os.path
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import logging
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 from piecrust.publishing.base import Publisher, PublisherConfigurationError
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 logger = logging.getLogger(__name__)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 class SftpPublisher(Publisher):
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 PUBLISHER_NAME = 'sftp'
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 PUBLISHER_SCHEME = 'sftp'
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 def setupPublishParser(self, parser, app):
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 parser.add_argument(
885
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
16 '--force',
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
17 action='store_true',
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
18 help=("Upload the entire bake directory instead of only "
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
19 "the files changed by the last bake."))
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
20
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
21 def parseUrlTarget(self, url):
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
22 self.config = {'host': str(url)}
759
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 def run(self, ctx):
885
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
25 host = self.config.get('host')
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
26 if not host:
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
27 raise PublisherConfigurationError(
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
28 "Publish target '%s' doesn't specify a 'host'." %
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
29 self.target)
923
5713b6a2850d chef: Optimize startup time a little bit.
Ludovic Chabant <ludovic@chabant.com>
parents: 885
diff changeset
30
5713b6a2850d chef: Optimize startup time a little bit.
Ludovic Chabant <ludovic@chabant.com>
parents: 885
diff changeset
31 import urllib.parse
885
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
32 remote = urllib.parse.urlparse(host)
759
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 hostname = remote.hostname
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 port = remote.port or 22
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 path = remote.path
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 if not hostname:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 hostname = path
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 path = ''
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40
885
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
41 username = self.config.get('username', remote.username)
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
42 path = self.config.get('path', path)
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
43 pkey_path = self.config.get('key')
759
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 password = None
767
3b33d9fb007c publish: Add support for `--preview` for the SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 759
diff changeset
46 if username and not ctx.preview:
923
5713b6a2850d chef: Optimize startup time a little bit.
Ludovic Chabant <ludovic@chabant.com>
parents: 885
diff changeset
47 import getpass
759
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 password = getpass.getpass("Password for '%s': " % username)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49
767
3b33d9fb007c publish: Add support for `--preview` for the SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 759
diff changeset
50 if ctx.preview:
3b33d9fb007c publish: Add support for `--preview` for the SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 759
diff changeset
51 logger.info("Would connect to %s:%s..." % (hostname, port))
3b33d9fb007c publish: Add support for `--preview` for the SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 759
diff changeset
52 self._previewUpload(ctx, path)
3b33d9fb007c publish: Add support for `--preview` for the SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 759
diff changeset
53 return
3b33d9fb007c publish: Add support for `--preview` for the SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 759
diff changeset
54
923
5713b6a2850d chef: Optimize startup time a little bit.
Ludovic Chabant <ludovic@chabant.com>
parents: 885
diff changeset
55 import paramiko
5713b6a2850d chef: Optimize startup time a little bit.
Ludovic Chabant <ludovic@chabant.com>
parents: 885
diff changeset
56
759
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 logger.debug("Connecting to %s:%s..." % (hostname, port))
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 lfk = (not username and not pkey_path)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 sshc = paramiko.SSHClient()
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 sshc.load_system_host_keys()
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 sshc.set_missing_host_key_policy(paramiko.WarningPolicy())
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 sshc.connect(
885
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
63 hostname, port=port,
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
64 username=username, password=password,
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
65 key_filename=pkey_path,
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
66 look_for_keys=lfk)
759
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 try:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 logger.info("Connected as %s" %
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 sshc.get_transport().get_username())
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 client = sshc.open_sftp()
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 try:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 self._upload(sshc, client, ctx, path)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 finally:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 client.close()
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 finally:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 sshc.close()
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77
767
3b33d9fb007c publish: Add support for `--preview` for the SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 759
diff changeset
78 def _previewUpload(self, ctx, dest_dir):
3b33d9fb007c publish: Add support for `--preview` for the SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 759
diff changeset
79 if not ctx.args.force:
3b33d9fb007c publish: Add support for `--preview` for the SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 759
diff changeset
80 logger.info("Would upload new/changed files...")
3b33d9fb007c publish: Add support for `--preview` for the SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 759
diff changeset
81 else:
3b33d9fb007c publish: Add support for `--preview` for the SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 759
diff changeset
82 logger.info("Would upload entire website...")
3b33d9fb007c publish: Add support for `--preview` for the SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 759
diff changeset
83
759
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 def _upload(self, session, client, ctx, dest_dir):
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 if dest_dir:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 if dest_dir.startswith('~/'):
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 _, out_chan, _ = session.exec_command("echo $HOME")
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 home_dir = out_chan.read().decode('utf8').strip()
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 dest_dir = home_dir + dest_dir[1:]
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 logger.debug("CHDIR %s" % dest_dir)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 try:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 client.chdir(dest_dir)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 except IOError:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94 client.mkdir(dest_dir)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 client.chdir(dest_dir)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 known_dirs = {}
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98 if ctx.was_baked and not ctx.args.force:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99 to_upload = list(self.getBakedFiles(ctx))
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 to_delete = list(self.getDeletedFiles(ctx))
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 if to_upload or to_delete:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102 logger.info("Uploading new/changed files...")
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 for path in self.getBakedFiles(ctx):
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 rel_path = os.path.relpath(path, ctx.bake_out_dir)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 logger.info(rel_path)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106 if not ctx.preview:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 self._putFile(client, path, rel_path, known_dirs)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 logger.info("Deleting removed files...")
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 for path in self.getDeletedFiles(ctx):
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 rel_path = os.path.relpath(path, ctx.bake_out_dir)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111 logger.info("%s [DELETE]" % rel_path)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112 if not ctx.preview:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113 try:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114 client.remove(rel_path)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115 except OSError:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116 pass
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117 else:
885
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
118 logger.info(
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
119 "Nothing to upload or delete on the remote server.")
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
120 logger.info(
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
121 "If you want to force uploading the entire website, "
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
122 "use the `--force` flag.")
759
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
123 else:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
124 logger.info("Uploading entire website...")
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
125 for dirpath, dirnames, filenames in os.walk(ctx.bake_out_dir):
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126 for f in filenames:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127 abs_f = os.path.join(dirpath, f)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
128 rel_f = os.path.relpath(abs_f, ctx.bake_out_dir)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
129 logger.info(rel_f)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
130 if not ctx.preview:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
131 self._putFile(client, abs_f, rel_f, known_dirs)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
132
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
133 def _putFile(self, client, local_path, remote_path, known_dirs):
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
134 # Split the remote path in bits.
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
135 remote_path = os.path.normpath(remote_path)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
136 if os.sep != '/':
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
137 remote_path = remote_path.sub(os.sep, '/')
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
138
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
139 # Make sure each directory in the remote path exists... to prevent
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
140 # testing the same directories several times, we keep a cache of
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
141 # `known_dirs` which we know exist.
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
142 remote_bits = remote_path.split('/')
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
143 cur = ''
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
144 for b in remote_bits[:-1]:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
145 cur = os.path.join(cur, b)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
146 if cur not in known_dirs:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
147 try:
885
13e8b50a2113 publish: Fix publishers API and add a simple "copy" publisher.
Ludovic Chabant <ludovic@chabant.com>
parents: 767
diff changeset
148 client.stat(cur)
759
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
149 except FileNotFoundError:
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
150 logger.debug("Creating remote dir: %s" % cur)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
151 client.mkdir(cur)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
152 known_dirs[cur] = True
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
153
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
154 # Should be all good! Upload the file.
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
155 client.put(local_path, remote_path)
dd03385adb62 publish: Add SFTP publisher.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
156