Upload a file to the remote node. :type local_path: ``st`` :param local_path: File path on the local node. :type remote_path: ``str`` :param remote_path: File path on the remote node. :type mode: ``int`` :param mode: Permissions mode for th
(self, local_path, remote_path, mode=None, mirror_local_mode=False)
| 172 | return True |
| 173 | |
| 174 | def put(self, local_path, remote_path, mode=None, mirror_local_mode=False): |
| 175 | """ |
| 176 | Upload a file to the remote node. |
| 177 | |
| 178 | :type local_path: ``st`` |
| 179 | :param local_path: File path on the local node. |
| 180 | |
| 181 | :type remote_path: ``str`` |
| 182 | :param remote_path: File path on the remote node. |
| 183 | |
| 184 | :type mode: ``int`` |
| 185 | :param mode: Permissions mode for the file. E.g. 0744. |
| 186 | |
| 187 | :type mirror_local_mode: ``int`` |
| 188 | :param mirror_local_mode: Should remote file mirror local mode. |
| 189 | |
| 190 | :return: Attributes of the remote file. |
| 191 | :rtype: :class:`posix.stat_result` or ``None`` |
| 192 | """ |
| 193 | |
| 194 | if not local_path or not remote_path: |
| 195 | raise Exception( |
| 196 | "Need both local_path and remote_path. local: %s, remote: %s" |
| 197 | % local_path, |
| 198 | remote_path, |
| 199 | ) |
| 200 | local_path = quote_unix(local_path) |
| 201 | remote_path = quote_unix(remote_path) |
| 202 | |
| 203 | extra = { |
| 204 | "_local_path": local_path, |
| 205 | "_remote_path": remote_path, |
| 206 | "_mode": mode, |
| 207 | "_mirror_local_mode": mirror_local_mode, |
| 208 | } |
| 209 | self.logger.debug("Uploading file", extra=extra) |
| 210 | |
| 211 | if not os.path.exists(local_path): |
| 212 | raise Exception("Path %s does not exist locally." % local_path) |
| 213 | |
| 214 | rattrs = self.sftp.put(local_path, remote_path) |
| 215 | |
| 216 | if mode or mirror_local_mode: |
| 217 | local_mode = mode |
| 218 | if not mode or mirror_local_mode: |
| 219 | local_mode = os.stat(local_path).st_mode |
| 220 | |
| 221 | # Cast to octal integer in case of string |
| 222 | if isinstance(local_mode, six.string_types): |
| 223 | local_mode = int(local_mode, 8) |
| 224 | local_mode = local_mode & 0o7777 |
| 225 | remote_mode = rattrs.st_mode |
| 226 | # Only bitshift if we actually got an remote_mode |
| 227 | if remote_mode is not None: |
| 228 | remote_mode = remote_mode & 0o7777 |
| 229 | if local_mode != remote_mode: |
| 230 | self.sftp.chmod(remote_path, local_mode) |
| 231 |