scp.py-0.13.0/0000775000000000000000000000000013372364146011520 5ustar rootrootscp.py-0.13.0/scp.py0000664000000000000000000004672413372364146012674 0ustar rootroot# scp.py # Copyright (C) 2008 James Bardin """ Utilities for sending files over ssh using the scp1 protocol. """ __version__ = '0.13.0' import locale import os import re from socket import timeout as SocketTimeout import types # this is quote from the shlex module, added in py3.3 _find_unsafe = re.compile(br'[^\w@%+=:,./~-]').search def _sh_quote(s): """Return a shell-escaped version of the string `s`.""" if not s: return b"" if _find_unsafe(s) is None: return s # use single quotes, and put single quotes into double quotes # the string $'b is then quoted as '$'"'"'b' return b"'" + s.replace(b"'", b"'\"'\"'") + b"'" # Unicode conversion functions; assume UTF-8 def asbytes(s): """Turns unicode into bytes, if needed. Assumes UTF-8. """ if isinstance(s, bytes): return s else: return s.encode('utf-8') def asunicode(s): """Turns bytes into unicode, if needed. Uses UTF-8. """ if isinstance(s, bytes): return s.decode('utf-8', 'replace') else: return s # os.path.sep is unicode on Python 3, no matter the platform bytes_sep = asbytes(os.path.sep) # Unicode conversion function for Windows # Used to convert local paths if the local machine is Windows def asunicode_win(s): """Turns bytes into unicode, if needed. """ if isinstance(s, bytes): return s.decode(locale.getpreferredencoding()) else: return s class SCPClient(object): """ An scp1 implementation, compatible with openssh scp. Raises SCPException for all transport related errors. Local filesystem and OS errors pass through. Main public methods are .put and .get The get method is controlled by the remote scp instance, and behaves accordingly. This means that symlinks are resolved, and the transfer is halted after too many levels of symlinks are detected. The put method uses os.walk for recursion, and sends files accordingly. Since scp doesn't support symlinks, we send file symlinks as the file (matching scp behaviour), but we make no attempt at symlinked directories. """ def __init__(self, transport, buff_size=16384, socket_timeout=10.0, progress=None, progress4=None, sanitize=_sh_quote): """ Create an scp1 client. @param transport: an existing paramiko L{Transport} @type transport: L{Transport} @param buff_size: size of the scp send buffer. @type buff_size: int @param socket_timeout: channel socket timeout in seconds @type socket_timeout: float @param progress: callback - called with (filename, size, sent) during transfers @param progress4: callback - called with (filename, size, sent, peername) during transfers. peername is a tuple contains (IP, PORT) @param sanitize: function - called with filename, should return safe or escaped string. Uses _sh_quote by default. @type progress: function(string, int, int, tuple) """ self.transport = transport self.buff_size = buff_size self.socket_timeout = socket_timeout self.channel = None self.preserve_times = False if progress is not None and progress4 is not None: raise TypeError("You may only set one of progress, progress4") elif progress4 is not None: self._progress = progress4 elif progress is not None: self._progress = lambda *a: progress(*a[:3]) else: self._progress = None self._recv_dir = b'' self._rename = False self._utime = None self.sanitize = sanitize self._dirtimes = {} self.peername = self.transport.getpeername() def __enter__(self): self.channel = self._open() return self def __exit__(self, type, value, traceback): self.close() def put(self, files, remote_path=b'.', recursive=False, preserve_times=False): """ Transfer files and directories to remote host. @param files: A single path, or a list of paths to be transferred. recursive must be True to transfer directories. @type files: string OR list of strings @param remote_path: path in which to receive the files on the remote host. defaults to '.' @type remote_path: str @param recursive: transfer files and directories recursively @type recursive: bool @param preserve_times: preserve mtime and atime of transferred files and directories. @type preserve_times: bool """ self.preserve_times = preserve_times self.channel = self._open() self._pushed = 0 self.channel.settimeout(self.socket_timeout) scp_command = (b'scp -t ', b'scp -r -t ')[recursive] self.channel.exec_command(scp_command + self.sanitize(asbytes(remote_path))) self._recv_confirm() if not isinstance(files, (list, tuple)): files = [files] if recursive: self._send_recursive(files) else: self._send_files(files) self.close() def putfo(self, fl, remote_path, mode='0644', size=None): """ Transfer file-like object to remote host. @param fl: opened file or file-like object to copy @type fl: file-like object @param remote_path: full destination path @type remote_path: str @param mode: permissions (posix-style) for the uploaded file @type mode: str @param size: size of the file in bytes. If ``None``, the size will be computed using `seek()` and `tell()`. """ if size is None: pos = fl.tell() fl.seek(0, os.SEEK_END) # Seek to end size = fl.tell() - pos fl.seek(pos, os.SEEK_SET) # Seek back self.channel = self._open() self.channel.settimeout(self.socket_timeout) self.channel.exec_command(b'scp -t ' + self.sanitize(asbytes(remote_path))) self._recv_confirm() self._send_file(fl, remote_path, mode, size=size) self.close() def get(self, remote_path, local_path='', recursive=False, preserve_times=False): """ Transfer files and directories from remote host to localhost. @param remote_path: path to retrieve from remote host. since this is evaluated by scp on the remote host, shell wildcards and environment variables may be used. @type remote_path: str @param local_path: path in which to receive files locally @type local_path: str @param recursive: transfer files and directories recursively @type recursive: bool @param preserve_times: preserve mtime and atime of transferred files and directories. @type preserve_times: bool """ if not isinstance(remote_path, (list, tuple)): remote_path = [remote_path] remote_path = [self.sanitize(asbytes(r)) for r in remote_path] self._recv_dir = local_path or os.getcwd() self._rename = (len(remote_path) == 1 and not os.path.isdir(os.path.abspath(local_path))) if len(remote_path) > 1: if not os.path.exists(self._recv_dir): raise SCPException("Local path '%s' does not exist" % asunicode(self._recv_dir)) elif not os.path.isdir(self._recv_dir): raise SCPException("Local path '%s' is not a directory" % asunicode(self._recv_dir)) rcsv = (b'', b' -r')[recursive] prsv = (b'', b' -p')[preserve_times] self.channel = self._open() self._pushed = 0 self.channel.settimeout(self.socket_timeout) self.channel.exec_command(b"scp" + rcsv + prsv + b" -f " + b' '.join(remote_path)) self._recv_all() self.close() def _open(self): """open a scp channel""" if self.channel is None or self.channel.closed: self.channel = self.transport.open_session() return self.channel def close(self): """close scp channel""" if self.channel is not None: self.channel.close() self.channel = None def _read_stats(self, name): """return just the file stats needed for scp""" if os.name == 'nt': name = asunicode(name) stats = os.stat(name) mode = oct(stats.st_mode)[-4:] size = stats.st_size atime = int(stats.st_atime) mtime = int(stats.st_mtime) return (mode, size, mtime, atime) def _send_files(self, files): for name in files: (mode, size, mtime, atime) = self._read_stats(name) if self.preserve_times: self._send_time(mtime, atime) fl = open(name, 'rb') self._send_file(fl, name, mode, size) fl.close() def _send_file(self, fl, name, mode, size): basename = asbytes(os.path.basename(name)) # The protocol can't handle \n in the filename. # Quote them as the control sequence \^J for now, # which is how openssh handles it. self.channel.sendall(("C%s %d " % (mode, size)).encode('ascii') + basename.replace(b'\n', b'\\^J') + b"\n") self._recv_confirm() file_pos = 0 if self._progress: if size == 0: # avoid divide-by-zero self._progress(basename, 1, 1, self.peername) else: self._progress(basename, size, 0, self.peername) buff_size = self.buff_size chan = self.channel while file_pos < size: chan.sendall(fl.read(buff_size)) file_pos = fl.tell() if self._progress: self._progress(basename, size, file_pos, self.peername) chan.sendall('\x00') self._recv_confirm() def _chdir(self, from_dir, to_dir): # Pop until we're one level up from our next push. # Push *once* into to_dir. # This is dependent on the depth-first traversal from os.walk # add path.sep to each when checking the prefix, so we can use # path.dirname after common = os.path.commonprefix([from_dir + bytes_sep, to_dir + bytes_sep]) # now take the dirname, since commonprefix is character based, # and we either have a separator, or a partial name common = os.path.dirname(common) cur_dir = from_dir.rstrip(bytes_sep) while cur_dir != common: cur_dir = os.path.split(cur_dir)[0] self._send_popd() # now we're in our common base directory, so on self._send_pushd(to_dir) def _send_recursive(self, files): for base in files: if not os.path.isdir(base): # filename mixed into the bunch self._send_files([base]) continue last_dir = asbytes(base) for root, dirs, fls in os.walk(base): self._chdir(last_dir, asbytes(root)) self._send_files([os.path.join(root, f) for f in fls]) last_dir = asbytes(root) # back out of the directory while self._pushed > 0: self._send_popd() def _send_pushd(self, directory): (mode, size, mtime, atime) = self._read_stats(directory) basename = asbytes(os.path.basename(directory)) if self.preserve_times: self._send_time(mtime, atime) self.channel.sendall(('D%s 0 ' % mode).encode('ascii') + basename.replace(b'\n', b'\\^J') + b'\n') self._recv_confirm() self._pushed += 1 def _send_popd(self): self.channel.sendall('E\n') self._recv_confirm() self._pushed -= 1 def _send_time(self, mtime, atime): self.channel.sendall(('T%d 0 %d 0\n' % (mtime, atime)).encode('ascii')) self._recv_confirm() def _recv_confirm(self): # read scp response msg = b'' try: msg = self.channel.recv(512) except SocketTimeout: raise SCPException('Timeout waiting for scp response') # slice off the first byte, so this compare will work in py2 and py3 if msg and msg[0:1] == b'\x00': return elif msg and msg[0:1] == b'\x01': raise SCPException(asunicode(msg[1:])) elif self.channel.recv_stderr_ready(): msg = self.channel.recv_stderr(512) raise SCPException(asunicode(msg)) elif not msg: raise SCPException('No response from server') else: raise SCPException('Invalid response from server', msg) def _recv_all(self): # loop over scp commands, and receive as necessary command = {b'C': self._recv_file, b'T': self._set_time, b'D': self._recv_pushd, b'E': self._recv_popd} while not self.channel.closed: # wait for command as long as we're open self.channel.sendall('\x00') msg = self.channel.recv(1024) if not msg: # chan closed while recving break assert msg[-1:] == b'\n' msg = msg[:-1] code = msg[0:1] if code not in command: raise SCPException(asunicode(msg[1:])) command[code](msg[1:]) # directory times can't be set until we're done writing files self._set_dirtimes() def _set_time(self, cmd): try: times = cmd.split(b' ') mtime = int(times[0]) atime = int(times[2]) or mtime except: self.channel.send(b'\x01') raise SCPException('Bad time format') # save for later self._utime = (atime, mtime) def _recv_file(self, cmd): chan = self.channel parts = cmd.strip().split(b' ', 2) try: mode = int(parts[0], 8) size = int(parts[1]) if self._rename: path = self._recv_dir self._rename = False elif os.name == 'nt': path = os.path.join(asunicode_win(self._recv_dir), parts[2].decode('utf-8')) else: path = os.path.join(asbytes(self._recv_dir), parts[2]) except: chan.send('\x01') chan.close() raise SCPException('Bad file format') try: file_hdl = open(path, 'wb') except IOError as e: chan.send(b'\x01' + str(e).encode('utf-8')) chan.close() raise if self._progress: if size == 0: # avoid divide-by-zero self._progress(path, 1, 1, self.peername) else: self._progress(path, size, 0, self.peername) buff_size = self.buff_size pos = 0 chan.send(b'\x00') try: while pos < size: # we have to make sure we don't read the final byte if size - pos <= buff_size: buff_size = size - pos file_hdl.write(chan.recv(buff_size)) pos = file_hdl.tell() if self._progress: self._progress(path, size, pos, self.peername) msg = chan.recv(512) if msg and msg[0:1] != b'\x00': raise SCPException(asunicode(msg[1:])) except SocketTimeout: chan.close() raise SCPException('Error receiving, socket.timeout') file_hdl.truncate() try: os.utime(path, self._utime) self._utime = None os.chmod(path, mode) # should we notify the other end? finally: file_hdl.close() # '\x00' confirmation sent in _recv_all def _recv_pushd(self, cmd): parts = cmd.split(b' ', 2) try: mode = int(parts[0], 8) if self._rename: path = self._recv_dir self._rename = False elif os.name == 'nt': path = os.path.join(asunicode_win(self._recv_dir), parts[2].decode('utf-8')) else: path = os.path.join(asbytes(self._recv_dir), parts[2]) except: self.channel.send(b'\x01') raise SCPException('Bad directory format') try: if not os.path.exists(path): os.mkdir(path, mode) elif os.path.isdir(path): os.chmod(path, mode) else: raise SCPException('%s: Not a directory' % path) self._dirtimes[path] = (self._utime) self._utime = None self._recv_dir = path except (OSError, SCPException) as e: self.channel.send(b'\x01' + asbytes(str(e))) raise def _recv_popd(self, *cmd): self._recv_dir = os.path.split(self._recv_dir)[0] def _set_dirtimes(self): try: for d in self._dirtimes: os.utime(d, self._dirtimes[d]) finally: self._dirtimes = {} class SCPException(Exception): """SCP exception class""" pass def put(transport, files, remote_path=b'.', recursive=False, preserve_times=False): """ Transfer files and directories to remote host. This is a convenience function that creates a SCPClient from the given transport and closes it at the end, useful for one-off transfers. @param files: A single path, or a list of paths to be transferred. recursive must be True to transfer directories. @type files: string OR list of strings @param remote_path: path in which to receive the files on the remote host. defaults to '.' @type remote_path: str @param recursive: transfer files and directories recursively @type recursive: bool @param preserve_times: preserve mtime and atime of transferred files and directories. @type preserve_times: bool """ with SCPClient(transport) as client: client.put(files, remote_path, recursive, preserve_times) def get(transport, remote_path, local_path='', recursive=False, preserve_times=False): """ Transfer files and directories from remote host to localhost. This is a convenience function that creates a SCPClient from the given transport and closes it at the end, useful for one-off transfers. @param transport: an paramiko L{Transport} @type transport: L{Transport} @param remote_path: path to retrieve from remote host. since this is evaluated by scp on the remote host, shell wildcards and environment variables may be used. @type remote_path: str @param local_path: path in which to receive files locally @type local_path: str @param recursive: transfer files and directories recursively @type recursive: bool @param preserve_times: preserve mtime and atime of transferred files and directories. @type preserve_times: bool """ with SCPClient(transport) as client: client.get(remote_path, local_path, recursive, preserve_times) scp.py-0.13.0/README.rst0000664000000000000000000000664413372364146013221 0ustar rootrootPure python scp module ====================== The scp.py module uses a paramiko transport to send and recieve files via the scp1 protocol. This is the protocol as referenced from the openssh scp program, and has only been tested with this implementation. Example ------- .. code-block:: python from paramiko import SSHClient from scp import SCPClient ssh = SSHClient() ssh.load_system_host_keys() ssh.connect('example.com') # SCPCLient takes a paramiko transport as an argument scp = SCPClient(ssh.get_transport()) scp.put('test.txt', 'test2.txt') scp.get('test2.txt') # Uploading the 'test' directory with its content in the # '/home/user/dump' remote directory scp.put('test', recursive=True, remote_path='/home/user/dump') scp.close() .. code-block:: $ md5sum test.txt test2.txt fc264c65fb17b7db5237cf7ce1780769 test.txt fc264c65fb17b7db5237cf7ce1780769 test2.txt Using 'with' keyword -------------------- .. code-block:: python from paramiko import SSHClient from scp import SCPClient ssh = SSHClient() ssh.load_system_host_keys() ssh.connect('example.com') with SCPClient(ssh.get_transport()) as scp: scp.put('test.txt', 'test2.txt') scp.get('test2.txt') .. code-block:: $ md5sum test.txt test2.txt fc264c65fb17b7db5237cf7ce1780769 test.txt fc264c65fb17b7db5237cf7ce1780769 test2.txt Uploading file-like objects --------------------------- The ``putfo`` method can be used to upload file-like objects: .. code-block:: python import io from paramiko import SSHClient from scp import SCPClient ssh = SSHClient() ssh.load_system_host_keys() ssh.connect('example.com') # SCPCLient takes a paramiko transport as an argument scp = SCPClient(ssh.get_transport()) # generate in-memory file-like object fl = io.BytesIO() fl.write(b'test') fl.seek(0) # upload it directly from memory scp.putfo(fl, '/tmp/test.txt') # close connection scp.close() # close file handler fl.close() Tracking progress of your file uploads/downloads ------------------------------------------------ A ``progress`` function can be given as a callback to the SCPClient to handle how the current SCP operation handles the progress of the transfers. In the example below we print the percentage complete of the file transfer. .. code-block:: python from paramiko import SSHClient from scp import SCPClient import sys ssh = SSHClient() ssh.load_system_host_keys() ssh.connect('example.com') # Define progress callback that prints the current percentage completed for the file def progress(filename, size, sent): sys.stdout.write("%s\'s progress: %.2f%% \r" % (filename, float(sent)/float(size)*100) ) # SCPCLient takes a paramiko transport and progress callback as its arguments. scp = SCPClient(ssh.get_transport(), progress=progress) # you can also use progress4, which adds a 4th parameter to track IP and port # useful with multiple threads to track source def progress4(filename, size, sent, peername): sys.stdout.write("(%s:%s) %s\'s progress: %.2f%% \r" % (peername[0], peername[1], filename, float(sent)/float(size)*100) ) scp = SCPClient(ssh.get_transport(), progress4=progress4) scp.put('test.txt', '~/test.txt') # Should now be printing the current progress of your put function. scp.close() scp.py-0.13.0/setup.py0000664000000000000000000000250113372364146013230 0ustar rootroot#!/usr/bin/env python import io import os from setuptools import setup # Need to specify encoding for PY3 with io.open('README.rst', encoding='utf-8') as fp: description = fp.read() setup( name = 'scp', version = '0.13.0', author = 'James Bardin', author_email = 'j.bardin@gmail.com', license = 'LGPL', url = 'https://github.com/jbardin/scp.py', description='scp module for paramiko', long_description=description, py_modules = ['scp'], install_requires = ['paramiko'], keywords=['paramiko', 'ssh', 'scp', 'transfer'], classifiers=[ 'Development Status :: 5 - Production/Stable', 'Intended Audience :: Developers', 'License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)', 'Operating System :: OS Independent', 'Programming Language :: Python', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Topic :: Internet', ], ) scp.py-0.13.0/setup.cfg0000664000000000000000000000003413372364146013336 0ustar rootroot[bdist_wheel] universal = 1 scp.py-0.13.0/CHANGELOG.md0000664000000000000000000000204713372364146013334 0ustar rootroot# Changelog ## 0.13.0 (2018-11-12) - Remove all introspection logic for `progress` callback introduced in 0.12 - `progress` callback only accept 3 arguments again - Introduce `progress4` parameter which accepts the peername as 4th argument ## 0.12.1 (2018-10-12) - Fix `progress` callback failing when it is an instance or class method ## 0.12.0 (2018-10-09) - Fix README.rst for PyPI - Add possibility of getting the peer IP and port from the `progress` callback - Make `putfo()` work with file-like objects that don't provide `getvalue()` ## 0.11.0 (2018-05-05) - Add `putfo()` method, allowing one to upload a file-like object - Add top-level `get()` and `put()` functions for convenience - Increase default socket time from 5 to 10 seconds ## 0.10.2 (2015-05-15) - Fixes using the SCPClient multiple times ## 0.10.0 (2015-05-07) - SCPClient can be used as a context manager - Added `close()` ## 0.9.0 (2015-02-04) - Add changelog - Finish up py3k and unicode support - Unicode should work on OSX, Windows and Linux - Some tests have been added scp.py-0.13.0/LICENSE.txt0000664000000000000000000000132713372364146013346 0ustar rootroot# This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA scp.py-0.13.0/MANIFEST.in0000664000000000000000000000011413372364146013252 0ustar rootrootinclude LICENSE.txt include README.rst include CHANGELOG.md include test.py scp.py-0.13.0/.travis/0000775000000000000000000000000013372364146013106 5ustar rootrootscp.py-0.13.0/.travis/setup_ssh.sh0000775000000000000000000000207713372364146015470 0ustar rootroot#!/bin/sh set -e # Sets up an SSH server rm -Rf /tmp/ssh_server mkdir /tmp/ssh_server cd /tmp/ssh_server # Server config file cat >config <<'EOF' Port 10022 ListenAddress 127.0.0.1 Protocol 2 HostKey /tmp/ssh_server/key_rsa HostKey /tmp/ssh_server/key_dsa UsePrivilegeSeparation no # Authentication LoginGraceTime 10 PermitRootLogin no StrictModes no UsePAM no RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile /tmp/ssh_server/client/id_rsa.pub PrintMotd yes EOF # Server keys ssh-keygen -f key_rsa -N '' -t rsa ssh-keygen -f key_dsa -N '' -t dsa # Client keys umask 077 mkdir client || true ssh-keygen -f client/id_rsa -N '' -t rsa umask 022 # Starts the server /usr/sbin/sshd -f config -h key_rsa -h key_dsa -p 10022 # Sets up the client umask 077 mkdir ~/.ssh || true cp client/id_rsa ~/.ssh/id_rsa umask 022 rm -f ~/.ssh/known_hosts # ssh-keyscan is bugged, don't use it # ssh-keyscan -v -p 10022 -t rsa 127.0.0.1 >> ~/.ssh/known_hosts ssh -o StrictHostKeyChecking=no \ -o PasswordAuthentication=no \ -p 10022 127.0.0.1 exit cat ~/.ssh/known_hosts scp.py-0.13.0/.travis.yml0000664000000000000000000000061513372364146013633 0ustar rootrootlanguage: python python: - "2.7" - "3.4" - "3.5" - "3.6" install: - pip install readme_renderer - .travis/setup_ssh.sh - sudo apt-get update -qq - sudo apt-get install -qq openssh-client openssh-server - if [[ $TRAVIS_PYTHON_VERSION == 2.6 ]]; then pip install unittest2; fi - python setup.py install script: - SCPPY_PORT=10022 python test.py - python setup.py check -r -s scp.py-0.13.0/test.py0000664000000000000000000002752613372364146013065 0ustar rootrootfrom __future__ import print_function from io import BytesIO import os import paramiko import random import shutil import sys from scp import SCPClient, SCPException, put, get import tempfile try: import unittest2 as unittest sys.modules['unittest'] = unittest except ImportError: import unittest ssh_info = { 'hostname': os.environ.get('SCPPY_HOSTNAME', '127.0.0.1'), 'port': int(os.environ.get('SCPPY_PORT', 22)), 'username': os.environ.get('SCPPY_USERNAME', None), } # Environment info PY3 = sys.version_info >= (3,) WINDOWS = os.name == 'nt' MACOS = sys.platform == 'darwin' if MACOS: import unicodedata def normalize_paths(names): """Ensures the test names are normalized (NFC). HFS (on Mac OS X) will normalize filenames if necessary. """ normed = set() for n in names: if isinstance(n, bytes): n = n.decode('utf-8') normed.add(unicodedata.normalize('NFC', n).encode('utf-8')) return normed else: normalize_paths = set def unique_names(): """Generates unique sequences of bytes. """ characters = (b"abcdefghijklmnopqrstuvwxyz" b"0123456789") characters = [characters[i:i + 1] for i in range(len(characters))] rng = random.Random() while True: letters = [rng.choice(characters) for i in range(10)] yield b''.join(letters) unique_names = unique_names() class TestDownload(unittest.TestCase): @classmethod def setUpClass(cls): # Server connection cls.ssh = paramiko.SSHClient() cls.ssh.load_system_host_keys() cls.ssh.set_missing_host_key_policy(paramiko.WarningPolicy()) cls.ssh.connect(**ssh_info) # Makes some files on the server chan = cls.ssh.get_transport().open_session() chan.exec_command( b'if ! echo -ne "/tmp/r\\xC3\\xA9mi" | xargs test -d; then ' # Directory b'echo -ne "/tmp/bien rang\\xC3\\xA9" | xargs -0 mkdir; ' # Files b'echo -ne "' b'/tmp/r\\xC3\\xA9mi\\x00' b'/tmp/bien rang\\xC3\\xA9/file\\x00' b'/tmp/bien rang\\xC3\\xA9/b\\xC3\\xA8te\\x00' b'/tmp/p\\xE9t\\xE9' # invalid UTF-8 here b'" | xargs -0 touch; ' b'fi') assert chan.recv_exit_status() == 0 print("Running tests on %s with %s" % ( "Windows" if WINDOWS else "Mac OS X" if MACOS else "POSIX", "Python 3" if PY3 else "Python 2")) def download_test(self, filename, recursive, destination=None, expected_win=[], expected_posix=[]): # Make a temporary directory temp = tempfile.mkdtemp(prefix='scp-py_test_') # Add some unicode in the path if WINDOWS: if isinstance(temp, bytes): temp = temp.decode(sys.getfilesystemencoding()) temp_in = os.path.join(temp, u'cl\xE9') else: if not isinstance(temp, bytes): temp = temp.encode('utf-8') temp_in = os.path.join(temp, b'cl\xC3\xA9') previous = os.getcwd() os.mkdir(temp_in) os.chdir(temp_in) cb3 = lambda filename, size, sent: None try: with SCPClient(self.ssh.get_transport(), progress=cb3) as scp: scp.get(filename, destination if destination is not None else u'.', preserve_times=True, recursive=recursive) actual = [] def listdir(path, fpath): for name in os.listdir(fpath): fname = os.path.join(fpath, name) actual.append(os.path.join(path, name)) if os.path.isdir(fname): listdir(name, fname) listdir(u'' if WINDOWS else b'', u'.' if WINDOWS else b'.') self.assertEqual(normalize_paths(actual), set(expected_win if WINDOWS else expected_posix)) finally: os.chdir(previous) shutil.rmtree(temp) def test_get_bytes(self): self.download_test(b'/tmp/r\xC3\xA9mi', False, b'target', [u'target'], [b'target']) self.download_test(b'/tmp/r\xC3\xA9mi', False, u'target', [u'target'], [b'target']) self.download_test(b'/tmp/r\xC3\xA9mi', False, None, [u'r\xE9mi'], [b'r\xC3\xA9mi']) self.download_test([b'/tmp/bien rang\xC3\xA9/file', b'/tmp/bien rang\xC3\xA9/b\xC3\xA8te'], False, None, [u'file', u'b\xE8te'], [b'file', b'b\xC3\xA8te']) def test_get_unicode(self): self.download_test(u'/tmp/r\xE9mi', False, b'target', [u'target'], [b'target']) self.download_test(u'/tmp/r\xE9mi', False, u'target', [u'target'], [b'target']) self.download_test(u'/tmp/r\xE9mi', False, None, [u'r\xE9mi'], [b'r\xC3\xA9mi']) self.download_test([u'/tmp/bien rang\xE9/file', u'/tmp/bien rang\xE9/b\xE8te'], False, None, [u'file', u'b\xE8te'], [b'file', b'b\xC3\xA8te']) def test_get_folder(self): self.download_test(b'/tmp/bien rang\xC3\xA9', True, None, [u'bien rang\xE9', u'bien rang\xE9\\file', u'bien rang\xE9\\b\xE8te'], [b'bien rang\xC3\xA9', b'bien rang\xC3\xA9/file', b'bien rang\xC3\xA9/b\xC3\xA8te']) def test_get_invalid_unicode(self): self.download_test(b'/tmp/p\xE9t\xE9', False, u'target', [u'target'], [b'target']) if WINDOWS: with self.assertRaises(SCPException): self.download_test(b'/tmp/p\xE9t\xE9', False, None, [], []) elif MACOS: self.download_test(b'/tmp/p\xE9t\xE9', False, None, [u'not windows'], [b'p%E9t%E9']) else: self.download_test(b'/tmp/p\xE9t\xE9', False, None, [u'not windows'], [b'p\xE9t\xE9']) class TestUpload(unittest.TestCase): @classmethod def setUpClass(cls): # Server connection cls.ssh = paramiko.SSHClient() cls.ssh.load_system_host_keys() cls.ssh.set_missing_host_key_policy(paramiko.WarningPolicy()) cls.ssh.connect(**ssh_info) # Makes some files locally cls._temp = tempfile.mkdtemp(prefix='scp_py_test_') if isinstance(cls._temp, bytes): cls._temp = cls._temp.decode(sys.getfilesystemencoding()) inner = os.path.join(cls._temp, u'cl\xE9') os.mkdir(inner) os.mkdir(os.path.join(inner, u'dossi\xE9')) os.mkdir(os.path.join(inner, u'dossi\xE9', u'bien rang\xE9')) open(os.path.join(inner, u'dossi\xE9', u'bien rang\xE9', u'test'), 'w').close() open(os.path.join(inner, u'r\xE9mi'), 'w').close() @classmethod def tearDownClass(cls): shutil.rmtree(cls._temp) def upload_test(self, filenames, recursive, expected=[], fl=None): destination = b'/tmp/upp\xC3\xA9' + next(unique_names) chan = self.ssh.get_transport().open_session() chan.exec_command(b'mkdir ' + destination) assert chan.recv_exit_status() == 0 previous = os.getcwd() cb4 = lambda filename, size, sent, peername: None try: os.chdir(self._temp) with SCPClient(self.ssh.get_transport(), progress4=cb4) as scp: if not fl: scp.put(filenames, destination, recursive) else: prefix = destination.decode(sys.getfilesystemencoding()) remote_path = '%s/%s' % (prefix, filenames) scp.putfo(fl, remote_path) fl.close() chan = self.ssh.get_transport().open_session() chan.exec_command( b'echo -ne "' + destination.decode('iso-8859-1') .encode('ascii', 'backslashreplace') + b'" | xargs find') out_list = b'' while True: data = chan.recv(1024) if not data: break out_list += data prefix = len(destination) + 1 out_list = [l[prefix:] for l in out_list.splitlines() if len(l) > prefix] self.assertEqual(normalize_paths(out_list), set(expected)) finally: os.chdir(previous) chan = self.ssh.get_transport().open_session() chan.exec_command(b'rm -Rf ' + destination) assert chan.recv_exit_status() == 0 @unittest.skipIf(WINDOWS, "Use unicode paths on Windows") def test_put_bytes(self): self.upload_test(b'cl\xC3\xA9/r\xC3\xA9mi', False, [b'r\xC3\xA9mi']) self.upload_test(b'cl\xC3\xA9/dossi\xC3\xA9/bien rang\xC3\xA9/test', False, [b'test']) self.upload_test(b'cl\xC3\xA9/dossi\xC3\xA9', True, [b'dossi\xC3\xA9', b'dossi\xC3\xA9/bien rang\xC3\xA9', b'dossi\xC3\xA9/bien rang\xC3\xA9/test']) def test_put_unicode(self): self.upload_test(u'cl\xE9/r\xE9mi', False, [b'r\xC3\xA9mi']) self.upload_test(u'cl\xE9/dossi\xE9/bien rang\xE9/test', False, [b'test']) self.upload_test(u'cl\xE9/dossi\xE9', True, [b'dossi\xC3\xA9', b'dossi\xC3\xA9/bien rang\xC3\xA9', b'dossi\xC3\xA9/bien rang\xC3\xA9/test']) self.upload_test([u'cl\xE9/dossi\xE9/bien rang\xE9', u'cl\xE9/r\xE9mi'], True, [b'bien rang\xC3\xA9', b'bien rang\xC3\xA9/test', b'r\xC3\xA9mi']) self.upload_test([u'cl\xE9/dossi\xE9', u'cl\xE9/r\xE9mi'], True, [b'dossi\xC3\xA9', b'dossi\xC3\xA9/bien rang\xC3\xA9', b'dossi\xC3\xA9/bien rang\xC3\xA9/test', b'r\xC3\xA9mi']) def test_putfo(self): fl = BytesIO() fl.write(b'r\xC3\xA9mi') fl.seek(0) self.upload_test(u'putfo-test', False, [b'putfo-test'], fl) class TestUpAndDown(unittest.TestCase): @classmethod def setUpClass(cls): # Server connection cls.ssh = paramiko.SSHClient() cls.ssh.load_system_host_keys() cls.ssh.set_missing_host_key_policy(paramiko.WarningPolicy()) cls.ssh.connect(**ssh_info) # Makes some files locally cls._temp = tempfile.mkdtemp(prefix='scp_py_test_') if isinstance(cls._temp, bytes): cls._temp = cls._temp.decode(sys.getfilesystemencoding()) @classmethod def tearDownClass(cls): shutil.rmtree(cls._temp) def test_up_and_down(self): '''send and receive files with the same client''' previous = os.getcwd() testfile = os.path.join(self._temp, 'testfile') testfile_sent = os.path.join(self._temp, 'testfile_sent') testfile_rcvd = os.path.join(self._temp, 'testfile_rcvd') try: os.chdir(self._temp) with open(testfile, 'w') as f: f.write("TESTING\n") put(self.ssh.get_transport(), testfile, testfile_sent) get(self.ssh.get_transport(), testfile_sent, testfile_rcvd) assert open(testfile_rcvd).read() == 'TESTING\n' finally: os.chdir(previous) if __name__ == '__main__': unittest.main()