From ad4cddfb3b684b21ddcbf3ac8bc9a29fc0c9c81c Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Tue, 23 May 2017 15:49:30 -0700 Subject: Add Flake8 definitions and as a dependency. --- dev-requirements.txt | 1 + setup.cfg | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/dev-requirements.txt b/dev-requirements.txt index cecc434c..c7a4d4a0 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -9,3 +9,4 @@ releases>=1.1.0,<2.0 semantic_version<3.0 wheel==0.24 twine==1.5 +flake8==3.3.0 diff --git a/setup.cfg b/setup.cfg index 1a8f89de..2abe6f02 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,3 +3,8 @@ universal = 1 [coverage:run] omit = paramiko/_winapi.py + +[flake8] +exclude = sites,.git,build,dist,alt_env,appveyor +ignore = E124,E125,E128,E261,E301,E302,E303 +max-line-length = 79 -- cgit v1.2.3 From c1a0974d8886c6a2d46fb69a4a7df382875374c2 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Tue, 23 May 2017 16:36:21 -0700 Subject: Fix flake8 for top-level modules. --- setup.cfg | 2 +- setup.py | 41 ++++++++++++++++++++--------------------- setup_helper.py | 17 +++++++++-------- tasks.py | 5 +++-- 4 files changed, 33 insertions(+), 32 deletions(-) diff --git a/setup.cfg b/setup.cfg index 2abe6f02..607805ae 100644 --- a/setup.cfg +++ b/setup.cfg @@ -5,6 +5,6 @@ universal = 1 omit = paramiko/_winapi.py [flake8] -exclude = sites,.git,build,dist,alt_env,appveyor +exclude = sites,.git,build,dist,alt_env,appveyor,demos,tests,paramiko,test.py ignore = E124,E125,E128,E261,E301,E302,E303 max-line-length = 79 diff --git a/setup.py b/setup.py index 2e0d4041..80d5ea7f 100644 --- a/setup.py +++ b/setup.py @@ -16,6 +16,13 @@ # along with Paramiko; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA. +import sys +from setuptools import setup + +if sys.platform == 'darwin': + import setup_helper + + setup_helper.install_custom_make_tarball() longdesc = ''' This is a library for making SSH2 connections (client or server). @@ -30,14 +37,6 @@ To install the development version, ``pip install -e git+https://github.com/paramiko/paramiko/#egg=paramiko``. ''' -import sys -from setuptools import setup - - -if sys.platform == 'darwin': - import setup_helper - setup_helper.install_custom_make_tarball() - # Version info -- read without importing _locals = {} @@ -45,22 +44,22 @@ with open('paramiko/_version.py') as fp: exec(fp.read(), None, _locals) version = _locals['__version__'] - setup( - name = "paramiko", - version = version, - description = "SSH2 protocol library", - long_description = longdesc, - author = "Jeff Forcier", - author_email = "jeff@bitprophet.org", - url = "https://github.com/paramiko/paramiko/", - packages = [ 'paramiko' ], - license = 'LGPL', - platforms = 'Posix; MacOS X; Windows', - classifiers = [ + name="paramiko", + version=version, + description="SSH2 protocol library", + long_description=longdesc, + author="Jeff Forcier", + author_email="jeff@bitprophet.org", + url="https://github.com/paramiko/paramiko/", + packages=['paramiko'], + license='LGPL', + platforms='Posix; MacOS X; Windows', + classifiers=[ 'Development Status :: 5 - Production/Stable', 'Intended Audience :: Developers', - 'License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)', + 'License :: OSI Approved :: ' + 'GNU Library or Lesser General Public License (LGPL)', 'Operating System :: OS Independent', 'Topic :: Internet', 'Topic :: Security :: Cryptography', diff --git a/setup_helper.py b/setup_helper.py index 9e3834b3..c359a16c 100644 --- a/setup_helper.py +++ b/setup_helper.py @@ -77,7 +77,7 @@ def make_tarball(base_name, base_dir, compress='gzip', verbose=0, dry_run=0, For 'gzip' and 'bzip2' the internal tarfile module will be used. For 'compress' the .tar will be created using tarfile, and then we will spawn 'compress' afterwards. - The output tar file will be named 'base_name' + ".tar", + The output tar file will be named 'base_name' + ".tar", possibly plus the appropriate compression extension (".gz", ".bz2" or ".Z"). Return the output filename. """ @@ -87,12 +87,14 @@ def make_tarball(base_name, base_dir, compress='gzip', verbose=0, dry_run=0, # "create a tree of hardlinks" step! (Would also be nice to # detect GNU tar to use its 'z' option and save a step.) - compress_ext = { 'gzip': ".gz", - 'bzip2': '.bz2', - 'compress': ".Z" } + compress_ext = { + 'gzip': ".gz", + 'bzip2': '.bz2', + 'compress': ".Z", + } # flags for compression program, each element of list will be an argument - tarfile_compress_flag = {'gzip':'gz', 'bzip2':'bz2'} + tarfile_compress_flag = {'gzip': 'gz', 'bzip2': 'bz2'} compress_flags = {'compress': ["-f"]} if compress is not None and compress not in compress_ext.keys(): @@ -144,11 +146,10 @@ def make_tarball(base_name, base_dir, compress='gzip', verbose=0, dry_run=0, _custom_formats = { 'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"), 'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"), - 'ztar': (make_tarball, [('compress', 'compress')], "compressed tar file"), - 'tar': (make_tarball, [('compress', None)], "uncompressed tar file"), + 'ztar': (make_tarball, [('compress', 'compress')], "compressed tar file"), + 'tar': (make_tarball, [('compress', None)], "uncompressed tar file"), } # Hack in and insert ourselves into the distutils code base def install_custom_make_tarball(): distutils.archive_util.ARCHIVE_FORMATS.update(_custom_formats) - diff --git a/tasks.py b/tasks.py index 41df8b27..42c18bd0 100644 --- a/tasks.py +++ b/tasks.py @@ -1,4 +1,3 @@ -from os import mkdir from os.path import join from shutil import rmtree, copytree @@ -41,7 +40,9 @@ def release(ctx, sdist=True, wheel=True, sign=True, dry_run=False): # Publish publish(ctx, sdist=sdist, wheel=wheel, sign=sign, dry_run=dry_run) # Remind - print("\n\nDon't forget to update RTD's versions page for new minor releases!") + print("\n\nDon't forget to update RTD's versions page for new minor " + "releases!") + # TODO: "replace one task with another" needs a better public API, this is # using unpublished internals & skips all the stuff add_task() does re: -- cgit v1.2.3 From c960e2281a567314e80d46e77e79e05e6b2dc83e Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Tue, 23 May 2017 17:49:28 -0700 Subject: Lots of flake8 fix. --- paramiko/__init__.py | 91 +++++++++++++++++--------------- paramiko/_winapi.py | 18 +++++-- paramiko/auth_handler.py | 132 ++++++++++++++++++++++++++++++---------------- paramiko/config.py | 4 +- paramiko/hostkeys.py | 10 ++-- paramiko/kex_gex.py | 27 +++++++--- paramiko/sftp_file.py | 21 +++++--- paramiko/ssh_exception.py | 36 +++++++------ paramiko/ssh_gss.py | 27 +++++----- setup.cfg | 2 +- 10 files changed, 221 insertions(+), 147 deletions(-) diff --git a/paramiko/__init__.py b/paramiko/__init__.py index 9e2ba013..5c1f38b3 100644 --- a/paramiko/__init__.py +++ b/paramiko/__init__.py @@ -28,7 +28,8 @@ __license__ = "GNU Lesser General Public License (LGPL)" from paramiko.transport import SecurityOptions, Transport -from paramiko.client import SSHClient, MissingHostKeyPolicy, AutoAddPolicy, RejectPolicy, WarningPolicy +from paramiko.client import SSHClient, MissingHostKeyPolicy, AutoAddPolicy, \ + RejectPolicy, WarningPolicy from paramiko.auth_handler import AuthHandler from paramiko.ssh_gss import GSSAuth, GSS_AUTH_AVAILABLE from paramiko.channel import Channel, ChannelFile @@ -55,49 +56,53 @@ from paramiko.hostkeys import HostKeys from paramiko.config import SSHConfig from paramiko.proxy import ProxyCommand -from paramiko.common import AUTH_SUCCESSFUL, AUTH_PARTIALLY_SUCCESSFUL, AUTH_FAILED, \ - OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED, OPEN_FAILED_CONNECT_FAILED, \ - OPEN_FAILED_UNKNOWN_CHANNEL_TYPE, OPEN_FAILED_RESOURCE_SHORTAGE +from paramiko.common import AUTH_SUCCESSFUL, AUTH_PARTIALLY_SUCCESSFUL, \ + AUTH_FAILED, OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED, \ + OPEN_FAILED_CONNECT_FAILED, OPEN_FAILED_UNKNOWN_CHANNEL_TYPE, \ + OPEN_FAILED_RESOURCE_SHORTAGE -from paramiko.sftp import SFTP_OK, SFTP_EOF, SFTP_NO_SUCH_FILE, SFTP_PERMISSION_DENIED, SFTP_FAILURE, \ - SFTP_BAD_MESSAGE, SFTP_NO_CONNECTION, SFTP_CONNECTION_LOST, SFTP_OP_UNSUPPORTED +from paramiko.sftp import SFTP_OK, SFTP_EOF, SFTP_NO_SUCH_FILE, \ + SFTP_PERMISSION_DENIED, SFTP_FAILURE, SFTP_BAD_MESSAGE, \ + SFTP_NO_CONNECTION, SFTP_CONNECTION_LOST, SFTP_OP_UNSUPPORTED from paramiko.common import io_sleep -__all__ = [ 'Transport', - 'SSHClient', - 'MissingHostKeyPolicy', - 'AutoAddPolicy', - 'RejectPolicy', - 'WarningPolicy', - 'SecurityOptions', - 'SubsystemHandler', - 'Channel', - 'PKey', - 'RSAKey', - 'DSSKey', - 'Message', - 'SSHException', - 'AuthenticationException', - 'PasswordRequiredException', - 'BadAuthenticationType', - 'ChannelException', - 'BadHostKeyException', - 'ProxyCommand', - 'ProxyCommandFailure', - 'SFTP', - 'SFTPFile', - 'SFTPHandle', - 'SFTPClient', - 'SFTPServer', - 'SFTPError', - 'SFTPAttributes', - 'SFTPServerInterface', - 'ServerInterface', - 'BufferedFile', - 'Agent', - 'AgentKey', - 'HostKeys', - 'SSHConfig', - 'util', - 'io_sleep' ] +__all__ = [ + 'Transport', + 'SSHClient', + 'MissingHostKeyPolicy', + 'AutoAddPolicy', + 'RejectPolicy', + 'WarningPolicy', + 'SecurityOptions', + 'SubsystemHandler', + 'Channel', + 'PKey', + 'RSAKey', + 'DSSKey', + 'Message', + 'SSHException', + 'AuthenticationException', + 'PasswordRequiredException', + 'BadAuthenticationType', + 'ChannelException', + 'BadHostKeyException', + 'ProxyCommand', + 'ProxyCommandFailure', + 'SFTP', + 'SFTPFile', + 'SFTPHandle', + 'SFTPClient', + 'SFTPServer', + 'SFTPError', + 'SFTPAttributes', + 'SFTPServerInterface', + 'ServerInterface', + 'BufferedFile', + 'Agent', + 'AgentKey', + 'HostKeys', + 'SSHConfig', + 'util', + 'io_sleep', +] diff --git a/paramiko/_winapi.py b/paramiko/_winapi.py index c9797d23..a13d7e87 100644 --- a/paramiko/_winapi.py +++ b/paramiko/_winapi.py @@ -41,7 +41,7 @@ def format_system_message(errno): ctypes.byref(result_buffer), buffer_size, arguments, - ) + ) # note the following will cause an infinite loop if GetLastError # repeatedly returns an error that cannot be formatted, although # this should not happen. @@ -52,13 +52,14 @@ def format_system_message(errno): class WindowsError(builtins.WindowsError): - "more info about errors at http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx" + """more info about errors at + http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx""" def __init__(self, value=None): if value is None: value = ctypes.windll.kernel32.GetLastError() strerror = format_system_message(value) - if sys.version_info > (3,3): + if sys.version_info > (3, 3): args = 0, strerror, None, value else: args = value, strerror @@ -78,6 +79,7 @@ class WindowsError(builtins.WindowsError): def __repr__(self): return '{self.__class__.__name__}({self.winerror})'.format(**vars()) + def handle_nonzero_success(result): if result == 0: raise WindowsError() @@ -133,6 +135,7 @@ ctypes.windll.kernel32.LocalFree.argtypes = ctypes.wintypes.HLOCAL, ##################### # jaraco.windows.mmap + class MemoryMap(object): """ A memory map object which can have security attributes overridden. @@ -189,6 +192,7 @@ class MemoryMap(object): ctypes.windll.kernel32.UnmapViewOfFile(self.view) ctypes.windll.kernel32.CloseHandle(self.filemap) + ############################# # jaraco.windows.api.security @@ -252,12 +256,15 @@ POLICY_EXECUTE = ( POLICY_VIEW_LOCAL_INFORMATION | POLICY_LOOKUP_NAMES) + class TokenAccess: TOKEN_QUERY = 0x8 + class TokenInformationClass: TokenUser = 1 + class TOKEN_USER(ctypes.Structure): num = 1 _fields_ = [ @@ -292,6 +299,7 @@ class SECURITY_DESCRIPTOR(ctypes.Structure): ('Dacl', ctypes.c_void_p), ] + class SECURITY_ATTRIBUTES(ctypes.Structure): """ typedef struct _SECURITY_ATTRIBUTES { @@ -329,6 +337,7 @@ ctypes.windll.advapi32.SetSecurityDescriptorOwner.argtypes = ( ######################### # jaraco.windows.security + def GetTokenInformation(token, information_class): """ Given a token, get the token information for it. @@ -343,6 +352,7 @@ def GetTokenInformation(token, information_class): ctypes.byref(data_size))) return ctypes.cast(data, ctypes.POINTER(TOKEN_USER)).contents + def OpenProcessToken(proc_handle, access): result = ctypes.wintypes.HANDLE() proc_handle = ctypes.wintypes.HANDLE(proc_handle) @@ -350,6 +360,7 @@ def OpenProcessToken(proc_handle, access): proc_handle, access, ctypes.byref(result))) return result + def get_current_user(): """ Return a TOKEN_USER for the owner of this process. @@ -360,6 +371,7 @@ def get_current_user(): ) return GetTokenInformation(process, TOKEN_USER) + def get_security_attributes_for_user(user=None): """ Return a SECURITY_ATTRIBUTES structure with the SID set to the diff --git a/paramiko/auth_handler.py b/paramiko/auth_handler.py index cab27a88..ace79638 100644 --- a/paramiko/auth_handler.py +++ b/paramiko/auth_handler.py @@ -22,18 +22,18 @@ import weakref from paramiko.common import cMSG_SERVICE_REQUEST, cMSG_DISCONNECT, \ - DISCONNECT_SERVICE_NOT_AVAILABLE, DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE, \ + DISCONNECT_SERVICE_NOT_AVAILABLE, \ + DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE, \ cMSG_USERAUTH_REQUEST, cMSG_SERVICE_ACCEPT, DEBUG, AUTH_SUCCESSFUL, INFO, \ cMSG_USERAUTH_SUCCESS, cMSG_USERAUTH_FAILURE, AUTH_PARTIALLY_SUCCESSFUL, \ cMSG_USERAUTH_INFO_REQUEST, WARNING, AUTH_FAILED, cMSG_USERAUTH_PK_OK, \ cMSG_USERAUTH_INFO_RESPONSE, MSG_SERVICE_REQUEST, MSG_SERVICE_ACCEPT, \ MSG_USERAUTH_REQUEST, MSG_USERAUTH_SUCCESS, MSG_USERAUTH_FAILURE, \ - MSG_USERAUTH_BANNER, MSG_USERAUTH_INFO_REQUEST, MSG_USERAUTH_INFO_RESPONSE, \ + MSG_USERAUTH_BANNER, MSG_USERAUTH_INFO_REQUEST, \ + MSG_USERAUTH_INFO_RESPONSE, \ cMSG_USERAUTH_GSSAPI_RESPONSE, cMSG_USERAUTH_GSSAPI_TOKEN, \ - cMSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, cMSG_USERAUTH_GSSAPI_ERROR, \ - cMSG_USERAUTH_GSSAPI_ERRTOK, cMSG_USERAUTH_GSSAPI_MIC,\ - MSG_USERAUTH_GSSAPI_RESPONSE, MSG_USERAUTH_GSSAPI_TOKEN, \ - MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, MSG_USERAUTH_GSSAPI_ERROR, \ + cMSG_USERAUTH_GSSAPI_MIC, MSG_USERAUTH_GSSAPI_RESPONSE, \ + MSG_USERAUTH_GSSAPI_TOKEN, MSG_USERAUTH_GSSAPI_ERROR, \ MSG_USERAUTH_GSSAPI_ERRTOK, MSG_USERAUTH_GSSAPI_MIC, MSG_NAMES from paramiko.message import Message @@ -149,7 +149,7 @@ class AuthHandler (object): if self.auth_event is not None: self.auth_event.set() - ### internals... + # ...internals... def _request_auth(self): m = Message() @@ -237,7 +237,8 @@ class AuthHandler (object): m.add_boolean(True) m.add_string(self.private_key.get_name()) m.add_string(self.private_key) - blob = self._get_session_blob(self.private_key, 'ssh-connection', self.username) + blob = self._get_session_blob( + self.private_key, 'ssh-connection', self.username) sig = self.private_key.sign_ssh_data(blob) m.add_string(sig) elif self.auth_method == 'keyboard-interactive': @@ -267,10 +268,11 @@ class AuthHandler (object): ptype, m = self.transport.packetizer.read_message() if ptype == MSG_USERAUTH_GSSAPI_TOKEN: srv_token = m.get_string() - next_token = sshgss.ssh_init_sec_context(self.gss_host, - mech, - self.username, - srv_token) + next_token = sshgss.ssh_init_sec_context( + self.gss_host, + mech, + self.username, + srv_token) # After this step the GSSAPI should not return any # token. If it does, we keep sending the token to # the server until no more token is returned. @@ -282,7 +284,8 @@ class AuthHandler (object): m.add_string(next_token) self.transport.send_message(m) else: - raise SSHException("Received Package: %s" % MSG_NAMES[ptype]) + raise SSHException( + "Received Package: %s" % MSG_NAMES[ptype]) m = Message() m.add_byte(cMSG_USERAUTH_GSSAPI_MIC) # send the MIC to the server @@ -297,7 +300,6 @@ class AuthHandler (object): maj_status = m.get_int() min_status = m.get_int() err_msg = m.get_string() - lang_tag = m.get_string() # we don't care! raise SSHException("GSS-API Error:\nMajor Status: %s\n\ Minor Status: %s\ \nError Message:\ %s\n") % (str(maj_status), @@ -307,7 +309,8 @@ class AuthHandler (object): self._parse_userauth_failure(m) return else: - raise SSHException("Received Package: %s" % MSG_NAMES[ptype]) + raise SSHException( + "Received Package: %s" % MSG_NAMES[ptype]) elif self.auth_method == 'gssapi-keyex' and\ self.transport.gss_kex_used: kexgss = self.transport.kexgss_ctxt @@ -317,10 +320,13 @@ class AuthHandler (object): elif self.auth_method == 'none': pass else: - raise SSHException('Unknown auth method "%s"' % self.auth_method) + raise SSHException( + 'Unknown auth method "%s"' % self.auth_method) self.transport._send_message(m) else: - self.transport._log(DEBUG, 'Service request "%s" accepted (?)' % service) + self.transport._log( + DEBUG, + 'Service request "%s" accepted (?)' % service) def _send_auth_result(self, username, method, result): # okay, send result @@ -332,7 +338,8 @@ class AuthHandler (object): else: self.transport._log(INFO, 'Auth rejected (%s).' % method) m.add_byte(cMSG_USERAUTH_FAILURE) - m.add_string(self.transport.server_object.get_allowed_auths(username)) + m.add_string( + self.transport.server_object.get_allowed_auths(username)) if result == AUTH_PARTIALLY_SUCCESSFUL: m.add_boolean(True) else: @@ -372,12 +379,19 @@ class AuthHandler (object): username = m.get_text() service = m.get_text() method = m.get_text() - self.transport._log(DEBUG, 'Auth request (type=%s) service=%s, username=%s' % (method, service, username)) + self.transport._log( + DEBUG, + 'Auth request (type=%s) service=%s, username=%s' % ( + method, service, username)) if service != 'ssh-connection': self._disconnect_service_not_available() return - if (self.auth_username is not None) and (self.auth_username != username): - self.transport._log(WARNING, 'Auth rejected because the client attempted to change username in mid-flight') + if ((self.auth_username is not None) and + (self.auth_username != username)): + self.transport._log( + WARNING, + 'Auth rejected because the client attempted to change ' + 'username in mid-flight') self._disconnect_no_more_auth() return self.auth_username = username @@ -396,9 +410,12 @@ class AuthHandler (object): # in this case, just return the raw byte string. pass if changereq: - # always treated as failure, since we don't support changing passwords, but collect - # the list of valid auth types from the callback anyway - self.transport._log(DEBUG, 'Auth request to change passwords (rejected)') + # always treated as failure, since we don't support changing + # passwords, but collect the list of valid auth types from + # the callback anyway + self.transport._log( + DEBUG, + 'Auth request to change passwords (rejected)') newpassword = m.get_binary() try: newpassword = newpassword.decode('UTF-8', 'replace') @@ -406,7 +423,8 @@ class AuthHandler (object): pass result = AUTH_FAILED else: - result = self.transport.server_object.check_auth_password(username, password) + result = self.transport.server_object.check_auth_password( + username, password) elif method == 'publickey': sig_attached = m.get_boolean() keytype = m.get_text() @@ -414,16 +432,21 @@ class AuthHandler (object): try: key = self.transport._key_info[keytype](Message(keyblob)) except SSHException as e: - self.transport._log(INFO, 'Auth rejected: public key: %s' % str(e)) + self.transport._log( + INFO, + 'Auth rejected: public key: %s' % str(e)) key = None except: - self.transport._log(INFO, 'Auth rejected: unsupported or mangled public key') + self.transport._log( + INFO, + 'Auth rejected: unsupported or mangled public key') key = None if key is None: self._disconnect_no_more_auth() return # first check if this key is okay... if not, we can skip the verify - result = self.transport.server_object.check_auth_publickey(username, key) + result = self.transport.server_object.check_auth_publickey( + username, key) if result != AUTH_FAILED: # key is okay, verify it if not sig_attached: @@ -438,12 +461,14 @@ class AuthHandler (object): sig = Message(m.get_binary()) blob = self._get_session_blob(key, service, username) if not key.verify_ssh_sig(blob, sig): - self.transport._log(INFO, 'Auth rejected: invalid signature') + self.transport._log( + INFO, + 'Auth rejected: invalid signature') result = AUTH_FAILED elif method == 'keyboard-interactive': - lang = m.get_string() submethods = m.get_string() - result = self.transport.server_object.check_auth_interactive(username, submethods) + result = self.transport.server_object.check_auth_interactive( + username, submethods) if isinstance(result, InteractiveQuery): # make interactive query instead of response self._interactive_query(result) @@ -457,15 +482,17 @@ class AuthHandler (object): # We can't accept more than one OID, so if the SSH client sends # more than one, disconnect. if mechs > 1: - self.transport._log(INFO, - 'Disconnect: Received more than one GSS-API OID mechanism') + self.transport._log( + INFO, + 'Disconnect: Received more than one GSS-API OID mechanism') self._disconnect_no_more_auth() desired_mech = m.get_string() mech_ok = sshgss.ssh_check_mech(desired_mech) # if we don't support the mechanism, disconnect. if not mech_ok: - self.transport._log(INFO, - 'Disconnect: Received an invalid GSS-API OID mechanism') + self.transport._log( + INFO, + 'Disconnect: Received an invalid GSS-API OID mechanism') self._disconnect_no_more_auth() # send the Kerberos V5 GSSAPI OID to the client supported_mech = sshgss.ssh_gss_oids("server") @@ -515,7 +542,8 @@ class AuthHandler (object): # The OpenSSH server is able to create a TGT with the delegated # client credentials, but this is not supported by GSS-API. result = AUTH_SUCCESSFUL - self.transport.server_object.check_auth_gssapi_with_mic(username, result) + self.transport.server_object.check_auth_gssapi_with_mic( + username, result) elif method == "gssapi-keyex" and gss_auth: mic_token = m.get_string() sshgss = self.transport.kexgss_ctxt @@ -532,14 +560,17 @@ class AuthHandler (object): self._send_auth_result(username, method, result) raise result = AUTH_SUCCESSFUL - self.transport.server_object.check_auth_gssapi_keyex(username, result) + self.transport.server_object.check_auth_gssapi_keyex( + username, result) else: result = self.transport.server_object.check_auth_none(username) # okay, send result self._send_auth_result(username, method, result) def _parse_userauth_success(self, m): - self.transport._log(INFO, 'Authentication (%s) successful!' % self.auth_method) + self.transport._log( + INFO, + 'Authentication (%s) successful!' % self.auth_method) self.authenticated = True self.transport._auth_trigger() if self.auth_event is not None: @@ -553,11 +584,18 @@ class AuthHandler (object): self.transport._log(DEBUG, 'Methods: ' + str(authlist)) self.transport.saved_exception = PartialAuthentication(authlist) elif self.auth_method not in authlist: - self.transport._log(DEBUG, 'Authentication type (%s) not permitted.' % self.auth_method) - self.transport._log(DEBUG, 'Allowed methods: ' + str(authlist)) - self.transport.saved_exception = BadAuthenticationType('Bad authentication type', authlist) + self.transport._log( + DEBUG, + 'Authentication type (%s) not permitted.' % self.auth_method) + self.transport._log( + DEBUG, + 'Allowed methods: ' + str(authlist)) + self.transport.saved_exception = BadAuthenticationType( + 'Bad authentication type', authlist) else: - self.transport._log(INFO, 'Authentication (%s) failed.' % self.auth_method) + self.transport._log( + INFO, + 'Authentication (%s) failed.' % self.auth_method) self.authenticated = False self.username = None if self.auth_event is not None: @@ -566,7 +604,6 @@ class AuthHandler (object): def _parse_userauth_banner(self, m): banner = m.get_string() self.banner = banner - lang = m.get_string() self.transport._log(INFO, 'Auth banner: %s' % banner) # who cares. @@ -580,7 +617,8 @@ class AuthHandler (object): prompt_list = [] for i in range(prompts): prompt_list.append((m.get_text(), m.get_boolean())) - response_list = self.interactive_handler(title, instructions, prompt_list) + response_list = self.interactive_handler( + title, instructions, prompt_list) m = Message() m.add_byte(cMSG_USERAUTH_INFO_RESPONSE) @@ -596,12 +634,14 @@ class AuthHandler (object): responses = [] for i in range(n): responses.append(m.get_text()) - result = self.transport.server_object.check_auth_interactive_response(responses) + result = self.transport.server_object.check_auth_interactive_response( + responses) if isinstance(result, InteractiveQuery): # make interactive query instead of response self._interactive_query(result) return - self._send_auth_result(self.auth_username, 'keyboard-interactive', result) + self._send_auth_result( + self.auth_username, 'keyboard-interactive', result) _handler_table = { MSG_SERVICE_REQUEST: _parse_service_request, diff --git a/paramiko/config.py b/paramiko/config.py index 42831fab..196e32ba 100644 --- a/paramiko/config.py +++ b/paramiko/config.py @@ -58,7 +58,7 @@ class SSHConfig (object): host = {"host": ['*'], "config": {}} for line in file_obj: # Strip any leading or trailing whitespace from the line. - # See https://github.com/paramiko/paramiko/issues/499 for more info. + # Refer to https://github.com/paramiko/paramiko/issues/499 line = line.strip() if not line or line.startswith('#'): continue @@ -68,7 +68,7 @@ class SSHConfig (object): raise Exception("Unparsable line %s" % line) key = match.group(1).lower() value = match.group(2) - + if key == 'host': self._config.append(host) host = { diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py index 2ee3d27f..de7fc8ba 100644 --- a/paramiko/hostkeys.py +++ b/paramiko/hostkeys.py @@ -111,8 +111,8 @@ class HostKeys (MutableMapping): def save(self, filename): """ - Save host keys into a file, in the format used by OpenSSH. The order of - keys in the file will be preserved when possible (if these keys were + Save host keys into a file, in the format used by OpenSSH. The order + of keys in the file will be preserved when possible (if these keys were loaded from a file originally). The single exception is that combined lines will be split into individual key lines, which is arguably a bug. @@ -135,7 +135,8 @@ class HostKeys (MutableMapping): returned. The keytype will be either ``"ssh-rsa"`` or ``"ssh-dss"``. :param str hostname: the hostname (or IP) to lookup - :return: dict of `str` -> `.PKey` keys associated with this host (or ``None``) + :return: dict of `str` -> `.PKey` keys associated with this host + (or ``None``) """ class SubDict (MutableMapping): def __init__(self, hostname, entries, hostkeys): @@ -220,9 +221,6 @@ class HostKeys (MutableMapping): def __len__(self): return len(self.keys()) - def __delitem__(self, key): - k = self[key] - def __getitem__(self, key): ret = self.lookup(key) if ret is None: diff --git a/paramiko/kex_gex.py b/paramiko/kex_gex.py index c980b690..c407d9f1 100644 --- a/paramiko/kex_gex.py +++ b/paramiko/kex_gex.py @@ -34,8 +34,10 @@ from paramiko.ssh_exception import SSHException _MSG_KEXDH_GEX_REQUEST_OLD, _MSG_KEXDH_GEX_GROUP, _MSG_KEXDH_GEX_INIT, \ _MSG_KEXDH_GEX_REPLY, _MSG_KEXDH_GEX_REQUEST = range(30, 35) + c_MSG_KEXDH_GEX_REQUEST_OLD, c_MSG_KEXDH_GEX_GROUP, c_MSG_KEXDH_GEX_INIT, \ - c_MSG_KEXDH_GEX_REPLY, c_MSG_KEXDH_GEX_REQUEST = [byte_chr(c) for c in range(30, 35)] + c_MSG_KEXDH_GEX_REPLY, c_MSG_KEXDH_GEX_REQUEST = \ + [byte_chr(c) for c in range(30, 35)] class KexGex (object): @@ -58,7 +60,8 @@ class KexGex (object): def start_kex(self, _test_old_style=False): if self.transport.server_mode: - self.transport._expect_packet(_MSG_KEXDH_GEX_REQUEST, _MSG_KEXDH_GEX_REQUEST_OLD) + self.transport._expect_packet( + _MSG_KEXDH_GEX_REQUEST, _MSG_KEXDH_GEX_REQUEST_OLD) return # request a bit range: we accept (min_bits) to (max_bits), but prefer # (preferred_bits). according to the spec, we shouldn't pull the @@ -88,9 +91,10 @@ class KexGex (object): return self._parse_kexdh_gex_reply(m) elif ptype == _MSG_KEXDH_GEX_REQUEST_OLD: return self._parse_kexdh_gex_request_old(m) - raise SSHException('KexGex %s asked to handle packet type %d' % self.name, ptype) + raise SSHException( + 'KexGex %s asked to handle packet type %d' % self.name, ptype) - ### internals... + # ...internals... def _generate_x(self): # generate an "x" (1 < x < (p-1)/2). @@ -134,7 +138,10 @@ class KexGex (object): pack = self.transport._get_modulus_pack() if pack is None: raise SSHException('Can\'t do server-side gex with no modulus pack') - self.transport._log(DEBUG, 'Picking p (%d <= %d <= %d bits)' % (minbits, preferredbits, maxbits)) + self.transport._log( + DEBUG, + 'Picking p (%d <= %d <= %d bits)' % ( + minbits, preferredbits, maxbits)) self.g, self.p = pack.get_modulus(minbits, preferredbits, maxbits) m = Message() m.add_byte(c_MSG_KEXDH_GEX_GROUP) @@ -144,7 +151,8 @@ class KexGex (object): self.transport._expect_packet(_MSG_KEXDH_GEX_INIT) def _parse_kexdh_gex_request_old(self, m): - # same as above, but without min_bits or max_bits (used by older clients like putty) + # same as above, but without min_bits or max_bits (used by older + # clients like putty) self.preferred_bits = m.get_int() # smoosh the user's preferred size into our own limits if self.preferred_bits > self.max_bits: @@ -155,8 +163,10 @@ class KexGex (object): pack = self.transport._get_modulus_pack() if pack is None: raise SSHException('Can\'t do server-side gex with no modulus pack') - self.transport._log(DEBUG, 'Picking p (~ %d bits)' % (self.preferred_bits,)) - self.g, self.p = pack.get_modulus(self.min_bits, self.preferred_bits, self.max_bits) + self.transport._log( + DEBUG, 'Picking p (~ %d bits)' % (self.preferred_bits,)) + self.g, self.p = pack.get_modulus( + self.min_bits, self.preferred_bits, self.max_bits) m = Message() m.add_byte(c_MSG_KEXDH_GEX_GROUP) m.add_mpint(self.p) @@ -244,6 +254,7 @@ class KexGex (object): self.transport._verify_key(host_key, sig) self.transport._activate_outbound() + class KexGexSHA256(KexGex): name = 'diffie-hellman-group-exchange-sha256' hash_algo = sha256 diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py index fdf667cd..391d7b0c 100644 --- a/paramiko/sftp_file.py +++ b/paramiko/sftp_file.py @@ -274,7 +274,9 @@ class SFTPFile (BufferedFile): :param int uid: new owner's uid :param int gid: new group id """ - self.sftp._log(DEBUG, 'chown(%s, %r, %r)' % (hexlify(self.handle), uid, gid)) + self.sftp._log( + DEBUG, + 'chown(%s, %r, %r)' % (hexlify(self.handle), uid, gid)) attr = SFTPAttributes() attr.st_uid, attr.st_gid = uid, gid self.sftp._request(CMD_FSETSTAT, self.handle, attr) @@ -308,7 +310,9 @@ class SFTPFile (BufferedFile): :param size: the new size of the file :type size: int or long """ - self.sftp._log(DEBUG, 'truncate(%s, %r)' % (hexlify(self.handle), size)) + self.sftp._log( + DEBUG, + 'truncate(%s, %r)' % (hexlify(self.handle), size)) attr = SFTPAttributes() attr.st_size = size self.sftp._request(CMD_FSETSTAT, self.handle, attr) @@ -362,10 +366,11 @@ class SFTPFile (BufferedFile): .. versionadded:: 1.4 """ - t, msg = self.sftp._request(CMD_EXTENDED, 'check-file', self.handle, - hash_algorithm, long(offset), long(length), block_size) - ext = msg.get_text() - alg = msg.get_text() + t, msg = self.sftp._request( + CMD_EXTENDED, 'check-file', self.handle, + hash_algorithm, long(offset), long(length), block_size) + msg.get_text() # ext + msg.get_text() # alg data = msg.get_remainder() return data @@ -417,7 +422,7 @@ class SFTPFile (BufferedFile): compatibility. """ if file_size is None: - file_size = self.stat().st_size; + file_size = self.stat().st_size # queue up async reads for the rest of the file chunks = [] @@ -465,7 +470,7 @@ class SFTPFile (BufferedFile): self.seek(x[0]) yield self.read(x[1]) - ### internals... + # ...internals... def _get_size(self): try: diff --git a/paramiko/ssh_exception.py b/paramiko/ssh_exception.py index ed36a952..eb63df4e 100644 --- a/paramiko/ssh_exception.py +++ b/paramiko/ssh_exception.py @@ -31,11 +31,11 @@ class AuthenticationException (SSHException): Exception raised when authentication failed for some reason. It may be possible to retry with different credentials. (Other classes specify more specific reasons.) - + .. versionadded:: 1.6 """ pass - + class PasswordRequiredException (AuthenticationException): """ @@ -49,15 +49,15 @@ class BadAuthenticationType (AuthenticationException): Exception raised when an authentication type (like password) is used, but the server isn't allowing that type. (It may only allow public-key, for example.) - + :ivar list allowed_types: list of allowed authentication types provided by the server (possible values are: ``"none"``, ``"password"``, and ``"publickey"``). - + .. versionadded:: 1.1 """ allowed_types = [] - + def __init__(self, explanation, types): AuthenticationException.__init__(self, explanation) self.allowed_types = types @@ -65,7 +65,8 @@ class BadAuthenticationType (AuthenticationException): self.args = (explanation, types, ) def __str__(self): - return SSHException.__str__(self) + ' (allowed_types=%r)' % self.allowed_types + return '{} (allowed_types={!r})'.format( + SSHException.__str__(self), self.allowed_types) class PartialAuthentication (AuthenticationException): @@ -73,7 +74,7 @@ class PartialAuthentication (AuthenticationException): An internal exception thrown in the case of partial authentication. """ allowed_types = [] - + def __init__(self, types): AuthenticationException.__init__(self, 'partial authentication') self.allowed_types = types @@ -84,9 +85,9 @@ class PartialAuthentication (AuthenticationException): class ChannelException (SSHException): """ Exception raised when an attempt to open a new `.Channel` fails. - + :ivar int code: the error code returned by the server - + .. versionadded:: 1.6 """ def __init__(self, code, text): @@ -99,19 +100,20 @@ class ChannelException (SSHException): class BadHostKeyException (SSHException): """ The host key given by the SSH server did not match what we were expecting. - + :ivar str hostname: the hostname of the SSH server :ivar PKey got_key: the host key presented by the server :ivar PKey expected_key: the host key expected - + .. versionadded:: 1.6 """ def __init__(self, hostname, got_key, expected_key): - SSHException.__init__(self, - 'Host key for server %s does not match : got %s expected %s' % ( - hostname, - got_key.get_base64(), - expected_key.get_base64())) + message = 'Host key for server {} does not match : ' \ + 'got {} expected {}' + message = message.format( + hostname, got_key.get_base64(), + expected_key.get_base64()) + SSHException.__init__(self, message) self.hostname = hostname self.key = got_key self.expected_key = expected_key @@ -147,7 +149,7 @@ class NoValidConnectionsError(socket.error): `socket.error` subclass, message, etc) we expose a single unified error message and a ``None`` errno so that instances of this class match most normal handling of `socket.error` objects. - + To see the wrapped exception objects, access the ``errors`` attribute. ``errors`` is a dict whose keys are address tuples (e.g. ``('127.0.0.1', 22)``) and whose values are the exception encountered trying to connect to diff --git a/paramiko/ssh_gss.py b/paramiko/ssh_gss.py index e906a851..6cde0da5 100644 --- a/paramiko/ssh_gss.py +++ b/paramiko/ssh_gss.py @@ -180,7 +180,7 @@ class _SSH_GSSAuth(object): return True # Internals - #-------------------------------------------------------------------------- + # ------------------------------------------------------------------------- def _make_uint32(self, integer): """ Create a 32 bit unsigned integer (The byte sequence of an integer). @@ -258,8 +258,8 @@ class _SSH_GSSAPI(_SSH_GSSAuth): :param str recv_token: The GSS-API token received from the Server :raise SSHException: Is raised if the desired mechanism of the client is not supported - :return: A ``String`` if the GSS-API has returned a token or ``None`` if - no token was returned + :return: A ``String`` if the GSS-API has returned a token or + ``None`` if no token was returned :rtype: String or None """ self._username = username @@ -286,8 +286,9 @@ class _SSH_GSSAPI(_SSH_GSSAuth): else: token = self._gss_ctxt.step(recv_token) except gssapi.GSSException: - raise gssapi.GSSException("{0} Target: {1}".format(sys.exc_info()[1], - self._gss_host)) + message = "{0} Target: {1}".format( + sys.exc_info()[1], self._gss_host) + raise gssapi.GSSException(message) self._gss_ctxt_status = self._gss_ctxt.established return token @@ -408,8 +409,8 @@ class _SSH_SSPI(_SSH_GSSAuth): sspicon.ISC_REQ_MUTUAL_AUTH |\ sspicon.ISC_REQ_DELEGATE else: - self._gss_flags = sspicon.ISC_REQ_INTEGRITY |\ - sspicon.ISC_REQ_MUTUAL_AUTH + self._gss_flags = \ + sspicon.ISC_REQ_INTEGRITY | sspicon.ISC_REQ_MUTUAL_AUTH def ssh_init_sec_context(self, target, desired_mech=None, username=None, recv_token=None): @@ -527,13 +528,13 @@ class _SSH_SSPI(_SSH_GSSAuth): self._username, self._service, self._auth_method) - # Verifies data and its signature. If verification fails, an + # Verifies data and its signature. If verification fails, an # sspi.error will be raised. self._gss_srv_ctxt.verify(mic_field, mic_token) else: # for key exchange with gssapi-keyex # client mode - # Verifies data and its signature. If verification fails, an + # Verifies data and its signature. If verification fails, an # sspi.error will be raised. self._gss_ctxt.verify(self._session_id, mic_token) @@ -546,10 +547,10 @@ class _SSH_SSPI(_SSH_GSSAuth): :rtype: Boolean """ return ( - self._gss_flags & sspicon.ISC_REQ_DELEGATE - ) and ( - self._gss_srv_ctxt_status or (self._gss_flags) - ) + self._gss_flags & sspicon.ISC_REQ_DELEGATE + ) and ( + self._gss_srv_ctxt_status or self._gss_flags + ) def save_client_creds(self, client_token): """ diff --git a/setup.cfg b/setup.cfg index 607805ae..7cf39d46 100644 --- a/setup.cfg +++ b/setup.cfg @@ -5,6 +5,6 @@ universal = 1 omit = paramiko/_winapi.py [flake8] -exclude = sites,.git,build,dist,alt_env,appveyor,demos,tests,paramiko,test.py +exclude = sites,.git,build,dist,alt_env,appveyor,demos,tests,test.py ignore = E124,E125,E128,E261,E301,E302,E303 max-line-length = 79 -- cgit v1.2.3 From 47a648c002d263c5608fc5df0085fef4cf4d2f6e Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Wed, 24 May 2017 12:26:35 -0700 Subject: More flake8 fixes. --- paramiko/kex_gss.py | 2 +- paramiko/sftp_file.py | 68 +++++++++++++++++++++++++++++++++++---------------- paramiko/ssh_gss.py | 13 ++++------ 3 files changed, 53 insertions(+), 30 deletions(-) diff --git a/paramiko/kex_gss.py b/paramiko/kex_gss.py index e21d55b9..483c2dec 100644 --- a/paramiko/kex_gss.py +++ b/paramiko/kex_gss.py @@ -584,7 +584,7 @@ class KexGSSGex(object): maj_status = m.get_int() min_status = m.get_int() err_msg = m.get_string() - lang_tag = m.get_string() # we don't care about the language! + m.get_string() # we don't care about the language (lang_tag)! raise SSHException("GSS-API Error:\nMajor Status: %s\nMinor Status: %s\ \nError Message: %s\n") % (str(maj_status), str(min_status), diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py index 391d7b0c..9ebaaf66 100644 --- a/paramiko/sftp_file.py +++ b/paramiko/sftp_file.py @@ -31,8 +31,8 @@ from paramiko.common import DEBUG from paramiko.file import BufferedFile from paramiko.py3compat import long -from paramiko.sftp import CMD_CLOSE, CMD_READ, CMD_DATA, SFTPError, CMD_WRITE, \ - CMD_STATUS, CMD_FSTAT, CMD_ATTRS, CMD_FSETSTAT, CMD_EXTENDED +from paramiko.sftp import CMD_CLOSE, CMD_READ, CMD_DATA, SFTPError,\ + CMD_WRITE, CMD_STATUS, CMD_FSTAT, CMD_ATTRS, CMD_FSETSTAT, CMD_EXTENDED from paramiko.sftp_attr import SFTPAttributes @@ -87,7 +87,8 @@ class SFTPFile (BufferedFile): BufferedFile.close(self) try: if async: - # GC'd file handle could be called from an arbitrary thread -- don't wait for a response + # GC'd file handle could be called from an arbitrary thread + # -- don't wait for a response self.sftp._async_request(type(None), CMD_CLOSE, self.handle) else: self.sftp._request(CMD_CLOSE, self.handle) @@ -99,7 +100,8 @@ class SFTPFile (BufferedFile): pass def _data_in_prefetch_requests(self, offset, size): - k = [x for x in list(self._prefetch_extents.values()) if x[0] <= offset] + k = [x for x in list(self._prefetch_extents.values()) + if x[0] <= offset] if len(k) == 0: return False k.sort(key=lambda x: x[0]) @@ -110,8 +112,11 @@ class SFTPFile (BufferedFile): if buf_offset + buf_size >= offset + size: # inclusive return True - # well, we have part of the request. see if another chunk has the rest. - return self._data_in_prefetch_requests(buf_offset + buf_size, offset + size - buf_offset - buf_size) + # well, we have part of the request. see if another chunk has + # the rest. + return self._data_in_prefetch_requests( + buf_offset + buf_size, + offset + size - buf_offset - buf_size) def _data_in_prefetch_buffers(self, offset): """ @@ -135,7 +140,8 @@ class SFTPFile (BufferedFile): read data out of the prefetch buffer, if possible. if the data isn't in the buffer, return None. otherwise, behaves like a normal read. """ - # while not closed, and haven't fetched past the current position, and haven't reached EOF... + # while not closed, and haven't fetched past the current position, + # and haven't reached EOF... while True: offset = self._data_in_prefetch_buffers(self._realpos) if offset is not None: @@ -165,7 +171,12 @@ class SFTPFile (BufferedFile): data = self._read_prefetch(size) if data is not None: return data - t, msg = self.sftp._request(CMD_READ, self.handle, long(self._realpos), int(size)) + t, msg = self.sftp._request( + CMD_READ, + self.handle, + long(self._realpos), + int(size) + ) if t != CMD_DATA: raise SFTPError('Expected data') return msg.get_string() @@ -173,8 +184,16 @@ class SFTPFile (BufferedFile): def _write(self, data): # may write less than requested if it would exceed max packet size chunk = min(len(data), self.MAX_REQUEST_SIZE) - self._reqs.append(self.sftp._async_request(type(None), CMD_WRITE, self.handle, long(self._realpos), data[:chunk])) - if not self.pipelined or (len(self._reqs) > 100 and self.sftp.sock.recv_ready()): + sftp_async_request = self.sftp._async_request( + type(None), + CMD_WRITE, + self.handle, + long(self._realpos), + data[:chunk] + ) + self._reqs.append(sftp_async_request) + if not self.pipelined or \ + (len(self._reqs) > 100 and self.sftp.sock.recv_ready()): while len(self._reqs): req = self._reqs.popleft() t, msg = self.sftp._read_response(req) @@ -244,7 +263,8 @@ class SFTPFile (BufferedFile): exactly like `.SFTPClient.stat`, except that it operates on an already-open file. - :return: an `.SFTPAttributes` object containing attributes about this file. + :return: an `.SFTPAttributes` object containing attributes about this + file. """ t, msg = self.sftp._request(CMD_FSTAT, self.handle) if t != CMD_ATTRS: @@ -284,11 +304,11 @@ class SFTPFile (BufferedFile): def utime(self, times): """ Set the access and modified times of this file. If - ``times`` is ``None``, then the file's access and modified times are set - to the current time. Otherwise, ``times`` must be a 2-tuple of numbers, - of the form ``(atime, mtime)``, which is used to set the access and - modified times, respectively. This bizarre API is mimicked from Python - for the sake of consistency -- I apologize. + ``times`` is ``None``, then the file's access and modified times are + set to the current time. Otherwise, ``times`` must be a 2-tuple of + numbers, of the form ``(atime, mtime)``, which is used to set the + access and modified times, respectively. This bizarre API is mimicked + from Python for the sake of consistency -- I apologize. :param tuple times: ``None`` or a tuple of (access time, modified time) in standard @@ -323,9 +343,9 @@ class SFTPFile (BufferedFile): to verify a successful upload or download, or for various rsync-like operations. - The file is hashed from ``offset``, for ``length`` bytes. If ``length`` - is 0, the remainder of the file is hashed. Thus, if both ``offset`` - and ``length`` are zero, the entire file is hashed. + The file is hashed from ``offset``, for ``length`` bytes. + If ``length`` is 0, the remainder of the file is hashed. Thus, if both + ``offset`` and ``length`` are zero, the entire file is hashed. Normally, ``block_size`` will be 0 (the default), and this method will return a byte string representing the requested hash (for example, a @@ -454,7 +474,8 @@ class SFTPFile (BufferedFile): read_chunks = [] for offset, size in chunks: # don't fetch data that's already in the prefetch buffer - if self._data_in_prefetch_buffers(offset) or self._data_in_prefetch_requests(offset, size): + if self._data_in_prefetch_buffers(offset) or \ + self._data_in_prefetch_requests(offset, size): continue # break up anything larger than the max read size @@ -490,7 +511,12 @@ class SFTPFile (BufferedFile): # do these read requests in a temporary thread because there may be # a lot of them, so it may block. for offset, length in chunks: - num = self.sftp._async_request(self, CMD_READ, self.handle, long(offset), int(length)) + num = self.sftp._async_request( + self, + CMD_READ, + self.handle, + long(offset), + int(length)) with self._prefetch_lock: self._prefetch_extents[num] = (offset, length) diff --git a/paramiko/ssh_gss.py b/paramiko/ssh_gss.py index 6cde0da5..8bb0c2e0 100644 --- a/paramiko/ssh_gss.py +++ b/paramiko/ssh_gss.py @@ -405,9 +405,9 @@ class _SSH_SSPI(_SSH_GSSAuth): _SSH_GSSAuth.__init__(self, auth_method, gss_deleg_creds) if self._gss_deleg_creds: - self._gss_flags = sspicon.ISC_REQ_INTEGRITY |\ - sspicon.ISC_REQ_MUTUAL_AUTH |\ - sspicon.ISC_REQ_DELEGATE + self._gss_flags = \ + sspicon.ISC_REQ_INTEGRITY | sspicon.ISC_REQ_MUTUAL_AUTH | \ + sspicon.ISC_REQ_DELEGATE else: self._gss_flags = \ sspicon.ISC_REQ_INTEGRITY | sspicon.ISC_REQ_MUTUAL_AUTH @@ -546,11 +546,8 @@ class _SSH_SSPI(_SSH_GSSAuth): :return: ``True`` if credentials are delegated, otherwise ``False`` :rtype: Boolean """ - return ( - self._gss_flags & sspicon.ISC_REQ_DELEGATE - ) and ( - self._gss_srv_ctxt_status or self._gss_flags - ) + return self._gss_flags & sspicon.ISC_REQ_DELEGATE and \ + (self._gss_srv_ctxt_status or self._gss_flags) def save_client_creds(self, client_token): """ -- cgit v1.2.3 From 71e204d488f53c688ded4f9631c4256cfea30c5d Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Wed, 24 May 2017 16:22:43 -0700 Subject: More flake8 fixes and skip some modules. --- paramiko/file.py | 41 ++++++++++++++++++++++++++--------------- paramiko/hostkeys.py | 21 +++++++++++++++------ paramiko/message.py | 28 ++++++++++++++-------------- setup.cfg | 4 ++-- 4 files changed, 57 insertions(+), 37 deletions(-) diff --git a/paramiko/file.py b/paramiko/file.py index 5b57dfd6..ab95c063 100644 --- a/paramiko/file.py +++ b/paramiko/file.py @@ -15,8 +15,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with Paramiko; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. -from paramiko.common import linefeed_byte_value, crlf, cr_byte, linefeed_byte, \ - cr_byte_value +from paramiko.common import linefeed_byte_value, crlf, cr_byte, \ + linefeed_byte, cr_byte_value from paramiko.py3compat import BytesIO, PY2, u, b, bytes_types from paramiko.util import ClosingContextManager @@ -106,9 +106,9 @@ class BufferedFile (ClosingContextManager): else: def __next__(self): """ - Returns the next line from the input, or raises `.StopIteration` when - EOF is hit. Unlike python file objects, it's okay to mix calls to - `.next` and `.readline`. + Returns the next line from the input, or raises `.StopIteration` + when EOF is hit. Unlike python file objects, it's okay to mix + calls to `.next` and `.readline`. :raises StopIteration: when the end of the file is reached. @@ -163,9 +163,9 @@ class BufferedFile (ClosingContextManager): def read(self, size=None): """ - Read at most ``size`` bytes from the file (less if we hit the end of the - file first). If the ``size`` argument is negative or omitted, read all - the remaining data in the file. + Read at most ``size`` bytes from the file (less if we hit the end of + the file first). If the ``size`` argument is negative or omitted, + read all the remaining data in the file. .. note:: ``'b'`` mode flag is ignored (``self.FLAG_BINARY`` in @@ -250,7 +250,9 @@ class BufferedFile (ClosingContextManager): line = self._rbuffer truncated = False while True: - if self._at_trailing_cr and (self._flags & self.FLAG_UNIVERSAL_NEWLINE) and (len(line) > 0): + if self._at_trailing_cr and \ + (self._flags & self.FLAG_UNIVERSAL_NEWLINE) and \ + (len(line) > 0): # edge case: the newline may be '\r\n' and we may have read # only the first '\r' last time. if line[0] == linefeed_byte_value: @@ -271,7 +273,9 @@ class BufferedFile (ClosingContextManager): n = size - len(line) else: n = self._bufsize - if (linefeed_byte in line) or ((self._flags & self.FLAG_UNIVERSAL_NEWLINE) and (cr_byte in line)): + if (linefeed_byte in line) or \ + ((self._flags & self.FLAG_UNIVERSAL_NEWLINE) and + (cr_byte in line)): break try: new_data = self._read(n) @@ -294,12 +298,18 @@ class BufferedFile (ClosingContextManager): self._pos += len(line) return line if self._flags & self.FLAG_BINARY else u(line) xpos = pos + 1 - if (line[pos] == cr_byte_value) and (xpos < len(line)) and (line[xpos] == linefeed_byte_value): + if (line[pos] == cr_byte_value) and \ + (xpos < len(line)) and \ + (line[xpos] == linefeed_byte_value): xpos += 1 # if the string was truncated, _rbuffer needs to have the string after # the newline character plus the truncated part of the line we stored # earlier in _rbuffer - self._rbuffer = line[xpos:] + self._rbuffer if truncated else line[xpos:] + if truncated: + self._rbuffer = line[xpos:] + self._rbuffer + else: + self._rbuffer = line[xpos:] + lf = line[pos:xpos] line = line[:pos] + linefeed_byte if (len(self._rbuffer) == 0) and (lf == cr_byte): @@ -421,7 +431,7 @@ class BufferedFile (ClosingContextManager): def closed(self): return self._closed - ### overrides... + # ...overrides... def _read(self, size): """ @@ -449,7 +459,7 @@ class BufferedFile (ClosingContextManager): """ return 0 - ### internals... + # ...internals... def _set_mode(self, mode='r', bufsize=-1): """ @@ -513,7 +523,8 @@ class BufferedFile (ClosingContextManager): return if self.newlines is None: self.newlines = newline - elif self.newlines != newline and isinstance(self.newlines, bytes_types): + elif self.newlines != newline and \ + isinstance(self.newlines, bytes_types): self.newlines = (self.newlines, newline) elif newline not in self.newlines: self.newlines += (newline,) diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py index de7fc8ba..b2a80ca1 100644 --- a/paramiko/hostkeys.py +++ b/paramiko/hostkeys.py @@ -179,12 +179,18 @@ class HostKeys (MutableMapping): self._hostkeys._entries.append(e) def keys(self): - return [e.key.get_name() for e in self._entries if e.key is not None] + return [ + e.key.get_name() for e in self._entries + if e.key is not None + ] entries = [] for e in self._entries: for h in e.hostnames: - if h.startswith('|1|') and not hostname.startswith('|1|') and constant_time_bytes_eq(self.hash_host(hostname, h), h) or h == hostname: + if h.startswith('|1|') and not hostname.startswith('|1|') and \ + constant_time_bytes_eq( + self.hash_host(hostname, h), h) \ + or h == hostname: entries.append(e) if len(entries) == 0: return None @@ -235,7 +241,7 @@ class HostKeys (MutableMapping): for key_type in entry.keys(): found = False for e in self._entries: - if (hostname in e.hostnames) and (e.key.get_name() == key_type): + if (hostname in e.hostnames) and e.key.get_name() == key_type: # replace e.key = entry[key_type] found = True @@ -264,7 +270,8 @@ class HostKeys (MutableMapping): hashed hostnames in the known_hosts file. :param str hostname: the hostname to hash - :param str salt: optional salt to use when hashing (must be 20 bytes long) + :param str salt: optional salt to use when hashing + (must be 20 bytes long) :return: the hashed hostname as a `str` """ if salt is None: @@ -347,8 +354,10 @@ class HostKeyEntry: included. """ if self.valid: - return '%s %s %s\n' % (','.join(self.hostnames), self.key.get_name(), - self.key.get_base64()) + return '%s %s %s\n' % ( + ','.join(self.hostnames), + self.key.get_name(), + self.key.get_base64()) return None def __repr__(self): diff --git a/paramiko/message.py b/paramiko/message.py index bf4c6b95..309b8612 100644 --- a/paramiko/message.py +++ b/paramiko/message.py @@ -32,7 +32,7 @@ class Message (object): An SSH2 message is a stream of bytes that encodes some combination of strings, integers, bools, and infinite-precision integers (known in Python as longs). This class builds or breaks down such a byte stream. - + Normally you don't need to deal with anything this low-level, but it's exposed for people implementing custom extensions, or features that paramiko doesn't support yet. @@ -200,7 +200,7 @@ class Message (object): def get_list(self): """ Fetch a `list` of `strings ` from the stream. - + These are trivially encoded as comma-separated values in a string. """ return self.get_text().split(',') @@ -208,7 +208,7 @@ class Message (object): def add_bytes(self, b): """ Write bytes to the stream, without any formatting. - + :param str b: bytes to add """ self.packet.write(b) @@ -217,7 +217,7 @@ class Message (object): def add_byte(self, b): """ Write a single byte to the stream, without any formatting. - + :param str b: byte to add """ self.packet.write(b) @@ -226,7 +226,7 @@ class Message (object): def add_boolean(self, b): """ Add a boolean value to the stream. - + :param bool b: boolean value to add """ if b: @@ -234,20 +234,20 @@ class Message (object): else: self.packet.write(zero_byte) return self - + def add_int(self, n): """ Add an integer to the stream. - + :param int n: integer to add """ self.packet.write(struct.pack('>I', n)) return self - + def add_adaptive_int(self, n): """ Add an integer to the stream. - + :param int n: integer to add """ if n >= Message.big_int: @@ -270,7 +270,7 @@ class Message (object): """ Add a long int to the stream, encoded as an infinite-precision integer. This method only works on positive numbers. - + :param long z: long int to add """ self.add_string(util.deflate_long(z)) @@ -279,7 +279,7 @@ class Message (object): def add_string(self, s): """ Add a string to the stream. - + :param str s: string to add """ s = asbytes(s) @@ -292,12 +292,12 @@ class Message (object): Add a list of strings to the stream. They are encoded identically to a single string of values separated by commas. (Yes, really, that's how SSH2 does it.) - + :param list l: list of strings to add """ self.add_string(','.join(l)) return self - + def _add(self, i): if type(i) is bool: return self.add_boolean(i) @@ -315,7 +315,7 @@ class Message (object): .. warning:: Longs are encoded non-deterministically. Don't use this method. - + :param seq: the sequence of items """ for item in seq: diff --git a/setup.cfg b/setup.cfg index 7cf39d46..b4280a7e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -5,6 +5,6 @@ universal = 1 omit = paramiko/_winapi.py [flake8] -exclude = sites,.git,build,dist,alt_env,appveyor,demos,tests,test.py -ignore = E124,E125,E128,E261,E301,E302,E303 +exclude = sites,.git,build,dist,alt_env,appveyor,demos,tests,test.py,kex_gss.py,kex_gex.py +ignore = E124,E125,E128,E261,E301,E302,E303,E402 max-line-length = 79 -- cgit v1.2.3 From 28218bf90b97451d3aba9d2c9ee01d87349a8886 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Wed, 24 May 2017 16:42:23 -0700 Subject: More flake8 fixes and fix tests. --- paramiko/client.py | 60 ++++++++++++++++++++++++++++++++++++---------------- paramiko/common.py | 7 +++--- paramiko/hostkeys.py | 3 +++ paramiko/util.py | 26 +++++++++++++++-------- 4 files changed, 66 insertions(+), 30 deletions(-) diff --git a/paramiko/client.py b/paramiko/client.py index 18139416..3bb78e9f 100644 --- a/paramiko/client.py +++ b/paramiko/client.py @@ -197,14 +197,16 @@ class SSHClient (ClosingContextManager): :returns: Yields an iterable of ``(family, address)`` tuples """ guess = True - addrinfos = socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM) + addrinfos = socket.getaddrinfo( + hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM) for (family, socktype, proto, canonname, sockaddr) in addrinfos: if socktype == socket.SOCK_STREAM: yield family, sockaddr guess = False - # some OS like AIX don't indicate SOCK_STREAM support, so just guess. :( - # We only do this if we did not get a single result marked as socktype == SOCK_STREAM. + # some OS like AIX don't indicate SOCK_STREAM support, so just + # guess. :( We only do this if we did not get a single result marked + # as socktype == SOCK_STREAM. if guess: for family, _, _, _, sockaddr in addrinfos: yield family, sockaddr @@ -323,7 +325,8 @@ class SSHClient (ClosingContextManager): if len(errors) == len(to_try): raise NoValidConnectionsError(errors) - t = self._transport = Transport(sock, gss_kex=gss_kex, gss_deleg_creds=gss_deleg_creds) + t = self._transport = Transport( + sock, gss_kex=gss_kex, gss_deleg_creds=gss_deleg_creds) t.use_compression(compress=compress) if gss_kex and gss_host is None: t.set_gss_host(hostname) @@ -357,7 +360,8 @@ class SSHClient (ClosingContextManager): our_server_key = self._host_keys.get(server_hostkey_name, {}).get(keytype, None) if our_server_key is None: - # will raise exception if the key is rejected; let that fall out + # will raise exception if the key is rejected; + # let that fall out self._policy.missing_host_key(self, server_hostkey_name, server_key) # if the callback returns, assume the key is ok @@ -485,7 +489,7 @@ class SSHClient (ClosingContextManager): saved_exception = None two_factor = False allowed_types = set() - two_factor_types = set(['keyboard-interactive','password']) + two_factor_types = set(['keyboard-interactive', 'password']) # If GSS-API support and GSS-PI Key Exchange was performed, we attempt # authentication with gssapi-keyex. @@ -510,8 +514,11 @@ class SSHClient (ClosingContextManager): if pkey is not None: try: - self._log(DEBUG, 'Trying SSH key %s' % hexlify(pkey.get_fingerprint())) - allowed_types = set(self._transport.auth_publickey(username, pkey)) + self._log( + DEBUG, + 'Trying SSH key %s' % hexlify(pkey.get_fingerprint())) + allowed_types = set( + self._transport.auth_publickey(username, pkey)) two_factor = (allowed_types & two_factor_types) if not two_factor: return @@ -522,9 +529,14 @@ class SSHClient (ClosingContextManager): for key_filename in key_filenames: for pkey_class in (RSAKey, DSSKey, ECDSAKey): try: - key = pkey_class.from_private_key_file(key_filename, password) - self._log(DEBUG, 'Trying key %s from %s' % (hexlify(key.get_fingerprint()), key_filename)) - allowed_types = set(self._transport.auth_publickey(username, key)) + key = pkey_class.from_private_key_file( + key_filename, password) + self._log( + DEBUG, + 'Trying key %s from %s' % ( + hexlify(key.get_fingerprint()), key_filename)) + allowed_types = set( + self._transport.auth_publickey(username, key)) two_factor = (allowed_types & two_factor_types) if not two_factor: return @@ -538,9 +550,14 @@ class SSHClient (ClosingContextManager): for key in self._agent.get_keys(): try: - self._log(DEBUG, 'Trying SSH agent key %s' % hexlify(key.get_fingerprint())) - # for 2-factor auth a successfully auth'd key password will return an allowed 2fac auth method - allowed_types = set(self._transport.auth_publickey(username, key)) + self._log( + DEBUG, + 'Trying SSH agent key %s' % hexlify( + key.get_fingerprint())) + # for 2-factor auth a successfully auth'd key password + # will return an allowed 2fac auth method + allowed_types = set( + self._transport.auth_publickey(username, key)) two_factor = (allowed_types & two_factor_types) if not two_factor: return @@ -576,9 +593,15 @@ class SSHClient (ClosingContextManager): for pkey_class, filename in keyfiles: try: key = pkey_class.from_private_key_file(filename, password) - self._log(DEBUG, 'Trying discovered key %s in %s' % (hexlify(key.get_fingerprint()), filename)) - # for 2-factor auth a successfully auth'd key will result in ['password'] - allowed_types = set(self._transport.auth_publickey(username, key)) + self._log( + DEBUG, + 'Trying discovered key %s in %s' % ( + hexlify(key.get_fingerprint()), filename)) + + # for 2-factor auth a successfully auth'd key will result + # in ['password'] + allowed_types = set( + self._transport.auth_publickey(username, key)) two_factor = (allowed_types & two_factor_types) if not two_factor: return @@ -662,4 +685,5 @@ class WarningPolicy (MissingHostKeyPolicy): """ def missing_host_key(self, client, hostname, key): warnings.warn('Unknown %s host key for %s: %s' % - (key.get_name(), hostname, hexlify(key.get_fingerprint()))) + (key.get_name(), hostname, hexlify( + key.get_fingerprint()))) diff --git a/paramiko/common.py b/paramiko/common.py index 0b0cc2a7..3dc4421d 100644 --- a/paramiko/common.py +++ b/paramiko/common.py @@ -22,8 +22,8 @@ Common constants and global variables. import logging from paramiko.py3compat import byte_chr, PY2, bytes_types, string_types, b, long -MSG_DISCONNECT, MSG_IGNORE, MSG_UNIMPLEMENTED, MSG_DEBUG, MSG_SERVICE_REQUEST, \ - MSG_SERVICE_ACCEPT = range(1, 7) +MSG_DISCONNECT, MSG_IGNORE, MSG_UNIMPLEMENTED, MSG_DEBUG, \ + MSG_SERVICE_REQUEST, MSG_SERVICE_ACCEPT = range(1, 7) MSG_KEXINIT, MSG_NEWKEYS = range(20, 22) MSG_USERAUTH_REQUEST, MSG_USERAUTH_FAILURE, MSG_USERAUTH_SUCCESS, \ MSG_USERAUTH_BANNER = range(50, 54) @@ -55,7 +55,8 @@ cMSG_USERAUTH_INFO_REQUEST = byte_chr(MSG_USERAUTH_INFO_REQUEST) cMSG_USERAUTH_INFO_RESPONSE = byte_chr(MSG_USERAUTH_INFO_RESPONSE) cMSG_USERAUTH_GSSAPI_RESPONSE = byte_chr(MSG_USERAUTH_GSSAPI_RESPONSE) cMSG_USERAUTH_GSSAPI_TOKEN = byte_chr(MSG_USERAUTH_GSSAPI_TOKEN) -cMSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE = byte_chr(MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE) +cMSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE = \ + byte_chr(MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE) cMSG_USERAUTH_GSSAPI_ERROR = byte_chr(MSG_USERAUTH_GSSAPI_ERROR) cMSG_USERAUTH_GSSAPI_ERRTOK = byte_chr(MSG_USERAUTH_GSSAPI_ERRTOK) cMSG_USERAUTH_GSSAPI_MIC = byte_chr(MSG_USERAUTH_GSSAPI_MIC) diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py index b2a80ca1..001471ac 100644 --- a/paramiko/hostkeys.py +++ b/paramiko/hostkeys.py @@ -233,6 +233,9 @@ class HostKeys (MutableMapping): raise KeyError(key) return ret + def __delitem__(self, key): + pass # Needed for instantiating HostKeys. + def __setitem__(self, hostname, entry): # don't use this please. if len(entry) == 0: diff --git a/paramiko/util.py b/paramiko/util.py index b69e8882..f6885c60 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -35,7 +35,8 @@ from paramiko.config import SSHConfig def inflate_long(s, always_positive=False): - """turns a normalized byte string into a long-int (adapted from Crypto.Util.number)""" + """turns a normalized byte string into a long-int + (adapted from Crypto.Util.number)""" out = long(0) negative = 0 if not always_positive and (len(s) > 0) and (byte_ord(s[0]) >= 0x80): @@ -48,17 +49,19 @@ def inflate_long(s, always_positive=False): # noinspection PyAugmentAssignment s = filler * (4 - len(s) % 4) + s for i in range(0, len(s), 4): - out = (out << 32) + struct.unpack('>I', s[i:i+4])[0] + out = (out << 32) + struct.unpack('>I', s[i:i + 4])[0] if negative: out -= (long(1) << (8 * len(s))) return out + deflate_zero = zero_byte if PY2 else 0 deflate_ff = max_byte if PY2 else 0xff def deflate_long(n, add_sign_padding=True): - """turns a long-int into a normalized byte string (adapted from Crypto.Util.number)""" + """turns a long-int into a normalized byte string + (adapted from Crypto.Util.number)""" # after much testing, this algorithm was deemed to be the fastest s = bytes() n = long(n) @@ -91,7 +94,7 @@ def format_binary(data, prefix=''): x = 0 out = [] while len(data) > x + 16: - out.append(format_binary_line(data[x:x+16])) + out.append(format_binary_line(data[x:x + 16])) x += 16 if x < len(data): out.append(format_binary_line(data[x:])) @@ -100,7 +103,7 @@ def format_binary(data, prefix=''): def format_binary_line(data): left = ' '.join(['%02X' % byte_ord(c) for c in data]) - right = ''.join([('.%c..' % c)[(byte_ord(c)+63)//95] for c in data]) + right = ''.join([('.%c..' % c)[(byte_ord(c) + 63) // 95] for c in data]) return '%-50s %s' % (left, right) @@ -215,6 +218,7 @@ def mod_inverse(x, m): u2 += m return u2 + _g_thread_ids = {} _g_thread_counter = 0 _g_thread_lock = threading.Lock() @@ -236,15 +240,17 @@ def get_thread_id(): def log_to_file(filename, level=DEBUG): - """send paramiko logs to a logfile, if they're not already going somewhere""" + """send paramiko logs to a logfile, + if they're not already going somewhere""" l = logging.getLogger("paramiko") if len(l.handlers) > 0: return l.setLevel(level) f = open(filename, 'a') lh = logging.StreamHandler(f) - lh.setFormatter(logging.Formatter('%(levelname)-.3s [%(asctime)s.%(msecs)03d] thr=%(_threadid)-3d %(name)s: %(message)s', - '%Y%m%d-%H:%M:%S')) + frm = '%(levelname)-.3s [%(asctime)s.%(msecs)03d] thr=%(_threadid)-3d ' \ + '%(name)s: %(message)s' + lh.setFormatter(logging.Formatter(frm, '%Y%m%d-%H:%M:%S')) l.addHandler(lh) @@ -253,6 +259,8 @@ class PFilter (object): def filter(self, record): record._threadid = get_thread_id() return True + + _pfilter = PFilter() @@ -277,7 +285,7 @@ def constant_time_bytes_eq(a, b): return False res = 0 # noinspection PyUnresolvedReferences - for i in (xrange if PY2 else range)(len(a)): + for i in (xrange if PY2 else range)(len(a)): # noqa: F821 res |= byte_ord(a[i]) ^ byte_ord(b[i]) return res == 0 -- cgit v1.2.3 From fa96fe0609b335c2b97aa7ff8db0dc4fb6710ff1 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Wed, 24 May 2017 17:26:00 -0700 Subject: Even more flake8. --- paramiko/channel.py | 37 +++++++++++--------- paramiko/common.py | 8 +++-- paramiko/kex_gex.py | 16 ++++++--- paramiko/kex_group1.py | 17 +++++---- paramiko/kex_gss.py | 77 +++++++++++++++++++++++++---------------- paramiko/packet.py | 94 +++++++++++++++++++++++++++++++++----------------- paramiko/pipe.py | 30 ++++++++-------- setup.cfg | 2 +- 8 files changed, 172 insertions(+), 109 deletions(-) diff --git a/paramiko/channel.py b/paramiko/channel.py index 1b334264..f67ed7e2 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -50,12 +50,8 @@ def open_only(func): """ @wraps(func) def _check(self, *args, **kwds): - if ( - self.closed - or self.eof_received - or self.eof_sent - or not self.active - ): + if self.closed or self.eof_received or self.eof_sent or \ + not self.active: raise SSHException('Channel is not open') return func(self, *args, **kwds) return _check @@ -74,7 +70,7 @@ class Channel (ClosingContextManager): flow-controlled independently.) Similarly, if the server isn't reading data you send, calls to `send` may block, unless you set a timeout. This is exactly like a normal network socket, so it shouldn't be too surprising. - + Instances of this class may be used as context managers. """ @@ -157,7 +153,8 @@ class Channel (ClosingContextManager): It isn't necessary (or desirable) to call this method if you're going to execute a single command with `exec_command`. - :param str term: the terminal type to emulate (for example, ``'vt100'``) + :param str term: the terminal type to emulate + (for example, ``'vt100'``) :param int width: width (in characters) of the terminal screen :param int height: height (in characters) of the terminal screen :param int width_pixels: width (in pixels) of the terminal screen @@ -347,8 +344,14 @@ class Channel (ClosingContextManager): self.transport._send_user_message(m) @open_only - def request_x11(self, screen_number=0, auth_protocol=None, auth_cookie=None, - single_connection=False, handler=None): + def request_x11( + self, + screen_number=0, + auth_protocol=None, + auth_cookie=None, + single_connection=False, + handler=None + ): """ Request an x11 session on this channel. If the server allows it, further x11 requests can be made from the server to the client, @@ -364,7 +367,7 @@ class Channel (ClosingContextManager): generated, used, and returned. You will need to use this value to verify incoming x11 requests and replace them with the actual local x11 cookie (which requires some knowledge of the x11 protocol). - + If a handler is passed in, the handler is called from another thread whenever a new x11 connection arrives. The default handler queues up incoming x11 connections, which may be retrieved using @@ -497,16 +500,16 @@ class Channel (ClosingContextManager): self._feed(data) return old - ### socket API + # ...socket API... def settimeout(self, timeout): """ Set a timeout on blocking read/write operations. The ``timeout`` - argument can be a nonnegative float expressing seconds, or ``None``. If - a float is given, subsequent channel read/write operations will raise - a timeout exception if the timeout period value has elapsed before the - operation has completed. Setting a timeout of ``None`` disables - timeouts on socket operations. + argument can be a nonnegative float expressing seconds, or ``None``. + If a float is given, subsequent channel read/write operations will + raise a timeout exception if the timeout period value has elapsed + before the operation has completed. Setting a timeout of ``None`` + disables timeouts on socket operations. ``chan.settimeout(0.0)`` is equivalent to ``chan.setblocking(0)``; ``chan.settimeout(None)`` is equivalent to ``chan.setblocking(1)``. diff --git a/paramiko/common.py b/paramiko/common.py index 3dc4421d..a77e39b9 100644 --- a/paramiko/common.py +++ b/paramiko/common.py @@ -20,7 +20,8 @@ Common constants and global variables. """ import logging -from paramiko.py3compat import byte_chr, PY2, bytes_types, string_types, b, long +from paramiko.py3compat import byte_chr, PY2, bytes_types, string_types, b,\ + long MSG_DISCONNECT, MSG_IGNORE, MSG_UNIMPLEMENTED, MSG_DEBUG, \ MSG_SERVICE_REQUEST, MSG_SERVICE_ACCEPT = range(1, 7) @@ -31,7 +32,7 @@ MSG_USERAUTH_PK_OK = 60 MSG_USERAUTH_INFO_REQUEST, MSG_USERAUTH_INFO_RESPONSE = range(60, 62) MSG_USERAUTH_GSSAPI_RESPONSE, MSG_USERAUTH_GSSAPI_TOKEN = range(60, 62) MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, MSG_USERAUTH_GSSAPI_ERROR,\ -MSG_USERAUTH_GSSAPI_ERRTOK, MSG_USERAUTH_GSSAPI_MIC = range(63, 67) + MSG_USERAUTH_GSSAPI_ERRTOK, MSG_USERAUTH_GSSAPI_MIC = range(63, 67) MSG_GLOBAL_REQUEST, MSG_REQUEST_SUCCESS, MSG_REQUEST_FAILURE = range(80, 83) MSG_CHANNEL_OPEN, MSG_CHANNEL_OPEN_SUCCESS, MSG_CHANNEL_OPEN_FAILURE, \ MSG_CHANNEL_WINDOW_ADJUST, MSG_CHANNEL_DATA, MSG_CHANNEL_EXTENDED_DATA, \ @@ -171,6 +172,7 @@ def asbytes(s): raise Exception('Unknown type') return s + xffffffff = long(0xffffffff) x80000000 = long(0x80000000) o666 = 438 @@ -203,4 +205,4 @@ MIN_WINDOW_SIZE = 2 ** 15 MIN_PACKET_SIZE = 2 ** 12 # Max windows size according to http://www.ietf.org/rfc/rfc4254.txt -MAX_WINDOW_SIZE = 2**32 -1 +MAX_WINDOW_SIZE = 2 ** 32 - 1 diff --git a/paramiko/kex_gex.py b/paramiko/kex_gex.py index c407d9f1..ba45da18 100644 --- a/paramiko/kex_gex.py +++ b/paramiko/kex_gex.py @@ -137,7 +137,8 @@ class KexGex (object): # generate prime pack = self.transport._get_modulus_pack() if pack is None: - raise SSHException('Can\'t do server-side gex with no modulus pack') + raise SSHException( + 'Can\'t do server-side gex with no modulus pack') self.transport._log( DEBUG, 'Picking p (%d <= %d <= %d bits)' % ( @@ -162,7 +163,8 @@ class KexGex (object): # generate prime pack = self.transport._get_modulus_pack() if pack is None: - raise SSHException('Can\'t do server-side gex with no modulus pack') + raise SSHException( + 'Can\'t do server-side gex with no modulus pack') self.transport._log( DEBUG, 'Picking p (~ %d bits)' % (self.preferred_bits,)) self.g, self.p = pack.get_modulus( @@ -181,7 +183,9 @@ class KexGex (object): # reject if p's bit length < 1024 or > 8192 bitlen = util.bit_length(self.p) if (bitlen < 1024) or (bitlen > 8192): - raise SSHException('Server-generated gex p (don\'t ask) is out of range (%d bits)' % bitlen) + raise SSHException( + 'Server-generated gex p (don\'t ask) is out of range ' + '(%d bits)' % bitlen) self.transport._log(DEBUG, 'Got server p (%d bits)' % bitlen) self._generate_x() # now compute e = g^x mod p @@ -200,7 +204,8 @@ class KexGex (object): self.f = pow(self.g, self.x, self.p) K = pow(self.e, self.x, self.p) key = self.transport.get_server_key().asbytes() - # okay, build up the hash H of (V_C || V_S || I_C || I_S || K_S || min || n || max || p || g || e || f || K) + # okay, build up the hash H of + # (V_C || V_S || I_C || I_S || K_S || min || n || max || p || g || e || f || K) # noqa hm = Message() hm.add(self.transport.remote_version, self.transport.local_version, self.transport.remote_kex_init, self.transport.local_kex_init, @@ -235,7 +240,8 @@ class KexGex (object): if (self.f < 1) or (self.f > self.p - 1): raise SSHException('Server kex "f" is out of range') K = pow(self.f, self.x, self.p) - # okay, build up the hash H of (V_C || V_S || I_C || I_S || K_S || min || n || max || p || g || e || f || K) + # okay, build up the hash H of + # (V_C || V_S || I_C || I_S || K_S || min || n || max || p || g || e || f || K) # noqa hm = Message() hm.add(self.transport.local_version, self.transport.remote_version, self.transport.local_kex_init, self.transport.remote_kex_init, diff --git a/paramiko/kex_group1.py b/paramiko/kex_group1.py index 9eee066c..e8f042b1 100644 --- a/paramiko/kex_group1.py +++ b/paramiko/kex_group1.py @@ -41,7 +41,7 @@ b0000000000000000 = zero_byte * 8 class KexGroup1(object): # draft-ietf-secsh-transport-09.txt, page 17 - P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF + P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF # noqa G = 2 name = 'diffie-hellman-group1-sha1' @@ -75,14 +75,15 @@ class KexGroup1(object): return self._parse_kexdh_reply(m) raise SSHException('KexGroup1 asked to handle packet type %d' % ptype) - ### internals... + # ...internals... def _generate_x(self): # generate an "x" (1 < x < q), where q is (p-1)/2. - # p is a 128-byte (1024-bit) number, where the first 64 bits are 1. + # p is a 128-byte (1024-bit) number, where the first 64 bits are 1. # therefore q can be approximated as a 2^1023. we drop the subset of - # potential x where the first 63 bits are 1, because some of those will be - # larger than q (but this is a tiny tiny subset of potential x). + # potential x where the first 63 bits are 1, because some of those + # will be larger than q (but this is a tiny tiny subset of + # potential x). while 1: x_bytes = os.urandom(128) x_bytes = byte_mask(x_bytes[0], 0x7f) + x_bytes[1:] @@ -99,7 +100,8 @@ class KexGroup1(object): raise SSHException('Server kex "f" is out of range') sig = m.get_binary() K = pow(self.f, self.x, self.P) - # okay, build up the hash H of (V_C || V_S || I_C || I_S || K_S || e || f || K) + # okay, build up the hash H of + # (V_C || V_S || I_C || I_S || K_S || e || f || K) hm = Message() hm.add(self.transport.local_version, self.transport.remote_version, self.transport.local_kex_init, self.transport.remote_kex_init) @@ -118,7 +120,8 @@ class KexGroup1(object): raise SSHException('Client kex "e" is out of range') K = pow(self.e, self.x, self.P) key = self.transport.get_server_key().asbytes() - # okay, build up the hash H of (V_C || V_S || I_C || I_S || K_S || e || f || K) + # okay, build up the hash H of + # (V_C || V_S || I_C || I_S || K_S || e || f || K) hm = Message() hm.add(self.transport.remote_version, self.transport.local_version, self.transport.remote_kex_init, self.transport.local_kex_init) diff --git a/paramiko/kex_gss.py b/paramiko/kex_gss.py index 483c2dec..ccca8e9e 100644 --- a/paramiko/kex_gss.py +++ b/paramiko/kex_gss.py @@ -40,19 +40,23 @@ This module provides GSS-API / SSPI Key Exchange as defined in :rfc:`4462`. import os from hashlib import sha1 -from paramiko.common import * +from paramiko.common import * # noqa from paramiko import util from paramiko.message import Message -from paramiko.py3compat import byte_chr, long, byte_mask, byte_ord +from paramiko.py3compat import byte_chr, byte_mask, byte_ord from paramiko.ssh_exception import SSHException MSG_KEXGSS_INIT, MSG_KEXGSS_CONTINUE, MSG_KEXGSS_COMPLETE, MSG_KEXGSS_HOSTKEY,\ -MSG_KEXGSS_ERROR = range(30, 35) + MSG_KEXGSS_ERROR = range(30, 35) MSG_KEXGSS_GROUPREQ, MSG_KEXGSS_GROUP = range(40, 42) c_MSG_KEXGSS_INIT, c_MSG_KEXGSS_CONTINUE, c_MSG_KEXGSS_COMPLETE,\ -c_MSG_KEXGSS_HOSTKEY, c_MSG_KEXGSS_ERROR = [byte_chr(c) for c in range(30, 35)] -c_MSG_KEXGSS_GROUPREQ, c_MSG_KEXGSS_GROUP = [byte_chr(c) for c in range(40, 42)] + c_MSG_KEXGSS_HOSTKEY, c_MSG_KEXGSS_ERROR = [ + byte_chr(c) for c in range(30, 35) + ] +c_MSG_KEXGSS_GROUPREQ, c_MSG_KEXGSS_GROUP = [ + byte_chr(c) for c in range(40, 42) +] class KexGSSGroup1(object): @@ -61,10 +65,10 @@ class KexGSSGroup1(object): 4462 Section 2 `_ """ # draft-ietf-secsh-transport-09.txt, page 17 - P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF + P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF # noqa G = 2 - b7fffffffffffffff = byte_chr(0x7f) + max_byte * 7 - b0000000000000000 = zero_byte * 8 + b7fffffffffffffff = byte_chr(0x7f) + max_byte * 7 # noqa + b0000000000000000 = zero_byte * 8 # noqa NAME = "gss-group1-sha1-toWM5Slw5Ew8Mqkay+al2g==" def __init__(self, transport): @@ -127,14 +131,14 @@ class KexGSSGroup1(object): generate an "x" (1 < x < q), where q is (p-1)/2. p is a 128-byte (1024-bit) number, where the first 64 bits are 1. therefore q can be approximated as a 2^1023. we drop the subset of - potential x where the first 63 bits are 1, because some of those will be - larger than q (but this is a tiny tiny subset of potential x). + potential x where the first 63 bits are 1, because some of those will + be larger than q (but this is a tiny tiny subset of potential x). """ while 1: x_bytes = os.urandom(128) x_bytes = byte_mask(x_bytes[0], 0x7f) + x_bytes[1:] if (x_bytes[:8] != self.b7fffffffffffffff) and \ - (x_bytes[:8] != self.b0000000000000000): + (x_bytes[:8] != self.b0000000000000000): break self.x = util.inflate_long(x_bytes) @@ -156,18 +160,21 @@ class KexGSSGroup1(object): """ Parse the SSH2_MSG_KEXGSS_CONTINUE message. - :param `.Message` m: The content of the SSH2_MSG_KEXGSS_CONTINUE message + :param `.Message` m: The content of the SSH2_MSG_KEXGSS_CONTINUE + message """ if not self.transport.server_mode: srv_token = m.get_string() m = Message() m.add_byte(c_MSG_KEXGSS_CONTINUE) - m.add_string(self.kexgss.ssh_init_sec_context(target=self.gss_host, - recv_token=srv_token)) + m.add_string(self.kexgss.ssh_init_sec_context( + target=self.gss_host, recv_token=srv_token)) self.transport.send_message(m) - self.transport._expect_packet(MSG_KEXGSS_CONTINUE, - MSG_KEXGSS_COMPLETE, - MSG_KEXGSS_ERROR) + self.transport._expect_packet( + MSG_KEXGSS_CONTINUE, + MSG_KEXGSS_COMPLETE, + MSG_KEXGSS_ERROR + ) else: pass @@ -175,7 +182,8 @@ class KexGSSGroup1(object): """ Parse the SSH2_MSG_KEXGSS_COMPLETE message (client mode). - :param `.Message` m: The content of the SSH2_MSG_KEXGSS_COMPLETE message + :param `.Message` m: The content of the + SSH2_MSG_KEXGSS_COMPLETE message """ # client mode if self.transport.host_key is None: @@ -190,7 +198,8 @@ class KexGSSGroup1(object): if bool: srv_token = m.get_string() K = pow(self.f, self.x, self.P) - # okay, build up the hash H of (V_C || V_S || I_C || I_S || K_S || e || f || K) + # okay, build up the hash H of + # (V_C || V_S || I_C || I_S || K_S || e || f || K) hm = Message() hm.add(self.transport.local_version, self.transport.remote_version, self.transport.local_kex_init, self.transport.remote_kex_init) @@ -223,7 +232,8 @@ class KexGSSGroup1(object): K = pow(self.e, self.x, self.P) self.transport.host_key = NullHostKey() key = self.transport.host_key.__str__() - # okay, build up the hash H of (V_C || V_S || I_C || I_S || K_S || e || f || K) + # okay, build up the hash H of + # (V_C || V_S || I_C || I_S || K_S || e || f || K) hm = Message() hm.add(self.transport.remote_version, self.transport.local_version, self.transport.remote_kex_init, self.transport.local_kex_init) @@ -271,7 +281,7 @@ class KexGSSGroup1(object): maj_status = m.get_int() min_status = m.get_int() err_msg = m.get_string() - lang_tag = m.get_string() # we don't care about the language! + m.get_string() # we don't care about the language! raise SSHException("GSS-API Error:\nMajor Status: %s\nMinor Status: %s\ \nError Message: %s\n") % (str(maj_status), str(min_status), @@ -284,7 +294,7 @@ class KexGSSGroup14(KexGSSGroup1): in `RFC 4462 Section 2 `_ """ - P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF + P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF # noqa G = 2 NAME = "gss-group14-sha1-toWM5Slw5Ew8Mqkay+al2g==" @@ -378,7 +388,8 @@ class KexGSSGex(object): """ Parse the SSH2_MSG_KEXGSS_GROUPREQ message (server mode). - :param `.Message` m: The content of the SSH2_MSG_KEXGSS_GROUPREQ message + :param `.Message` m: The content of the + SSH2_MSG_KEXGSS_GROUPREQ message """ minbits = m.get_int() preferredbits = m.get_int() @@ -402,8 +413,12 @@ class KexGSSGex(object): # generate prime pack = self.transport._get_modulus_pack() if pack is None: - raise SSHException('Can\'t do server-side gex with no modulus pack') - self.transport._log(DEBUG, 'Picking p (%d <= %d <= %d bits)' % (minbits, preferredbits, maxbits)) + raise SSHException( + 'Can\'t do server-side gex with no modulus pack') + self.transport._log( + DEBUG, # noqa + 'Picking p (%d <= %d <= %d bits)' % ( + minbits, preferredbits, maxbits)) self.g, self.p = pack.get_modulus(minbits, preferredbits, maxbits) m = Message() m.add_byte(c_MSG_KEXGSS_GROUP) @@ -423,8 +438,10 @@ class KexGSSGex(object): # reject if p's bit length < 1024 or > 8192 bitlen = util.bit_length(self.p) if (bitlen < 1024) or (bitlen > 8192): - raise SSHException('Server-generated gex p (don\'t ask) is out of range (%d bits)' % bitlen) - self.transport._log(DEBUG, 'Got server p (%d bits)' % bitlen) + raise SSHException( + 'Server-generated gex p (don\'t ask) is out of range ' + '(%d bits)' % bitlen) + self.transport._log(DEBUG, 'Got server p (%d bits)' % bitlen) # noqa self._generate_x() # now compute e = g^x mod p self.e = pow(self.g, self.x, self.p) @@ -453,7 +470,8 @@ class KexGSSGex(object): K = pow(self.e, self.x, self.p) self.transport.host_key = NullHostKey() key = self.transport.host_key.__str__() - # okay, build up the hash H of (V_C || V_S || I_C || I_S || K_S || min || n || max || p || g || e || f || K) + # okay, build up the hash H of + # (V_C || V_S || I_C || I_S || K_S || min || n || max || p || g || e || f || K) # noqa hm = Message() hm.add(self.transport.remote_version, self.transport.local_version, self.transport.remote_kex_init, self.transport.local_kex_init, @@ -543,7 +561,8 @@ class KexGSSGex(object): if (self.f < 1) or (self.f > self.p - 1): raise SSHException('Server kex "f" is out of range') K = pow(self.f, self.x, self.p) - # okay, build up the hash H of (V_C || V_S || I_C || I_S || K_S || min || n || max || p || g || e || f || K) + # okay, build up the hash H of + # (V_C || V_S || I_C || I_S || K_S || min || n || max || p || g || e || f || K) # noqa hm = Message() hm.add(self.transport.local_version, self.transport.remote_version, self.transport.local_kex_init, self.transport.remote_kex_init, diff --git a/paramiko/packet.py b/paramiko/packet.py index c943fe3c..cae64727 100644 --- a/paramiko/packet.py +++ b/paramiko/packet.py @@ -54,8 +54,11 @@ class Packetizer (object): REKEY_PACKETS = pow(2, 29) REKEY_BYTES = pow(2, 29) - REKEY_PACKETS_OVERFLOW_MAX = pow(2, 29) # Allow receiving this many packets after a re-key request before terminating - REKEY_BYTES_OVERFLOW_MAX = pow(2, 29) # Allow receiving this many bytes after a re-key request before terminating + # Allow receiving this many packets after a re-key request before + # terminating + REKEY_PACKETS_OVERFLOW_MAX = pow(2, 29) + # Allow receiving this many bytes after a re-key request before terminating + REKEY_BYTES_OVERFLOW_MAX = pow(2, 29) def __init__(self, socket): self.__socket = socket @@ -113,7 +116,8 @@ class Packetizer (object): """ self.__logger = log - def set_outbound_cipher(self, block_engine, block_size, mac_engine, mac_size, mac_key, sdctr=False): + def set_outbound_cipher(self, block_engine, block_size, mac_engine, + mac_size, mac_key, sdctr=False): """ Switch outbound data cipher. """ @@ -125,13 +129,15 @@ class Packetizer (object): self.__mac_key_out = mac_key self.__sent_bytes = 0 self.__sent_packets = 0 - # wait until the reset happens in both directions before clearing rekey flag + # wait until the reset happens in both directions before clearing + # rekey flag self.__init_count |= 1 if self.__init_count == 3: self.__init_count = 0 self.__need_rekey = False - def set_inbound_cipher(self, block_engine, block_size, mac_engine, mac_size, mac_key): + def set_inbound_cipher( + self, block_engine, block_size, mac_engine, mac_size, mac_key): """ Switch inbound data cipher. """ @@ -144,7 +150,8 @@ class Packetizer (object): self.__received_packets = 0 self.__received_bytes_overflow = 0 self.__received_packets_overflow = 0 - # wait until the reset happens in both directions before clearing rekey flag + # wait until the reset happens in both directions before clearing + # rekey flag self.__init_count |= 2 if self.__init_count == 3: self.__init_count = 0 @@ -262,9 +269,11 @@ class Packetizer (object): # on Linux, sometimes instead of socket.timeout, we get # EAGAIN. this is a bug in recent (> 2.6.9) kernels but # we need to work around it. - if (type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EAGAIN): + if (type(e.args) is tuple) and (len(e.args) > 0) and \ + (e.args[0] == errno.EAGAIN): got_timeout = True - elif (type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EINTR): + elif (type(e.args) is tuple) and (len(e.args) > 0) and \ + (e.args[0] == errno.EINTR): # syscall interrupted; try again pass elif self.__closed: @@ -289,9 +298,11 @@ class Packetizer (object): except socket.timeout: retry_write = True except socket.error as e: - if (type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EAGAIN): + if (type(e.args) is tuple) and (len(e.args) > 0) and \ + (e.args[0] == errno.EAGAIN): retry_write = True - elif (type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EINTR): + elif (type(e.args) is tuple) and (len(e.args) > 0) and \ + (e.args[0] == errno.EINTR): # syscall interrupted; try again retry_write = True else: @@ -307,11 +318,11 @@ class Packetizer (object): n = -1 else: if n == 0 and iteration_with_zero_as_return_value > 10: - # We shouldn't retry the write, but we didn't - # manage to send anything over the socket. This might be an - # indication that we have lost contact with the remote side, - # but are yet to receive an EOFError or other socket errors. - # Let's give it some iteration to try and catch up. + # We shouldn't retry the write, but we didn't + # manage to send anything over the socket. This might be an + # indication that we have lost contact with the remote + # side, but are yet to receive an EOFError or other socket + # errors. Let's give it some iteration to try and catch up. n = -1 iteration_with_zero_as_return_value += 1 if n < 0: @@ -327,7 +338,7 @@ class Packetizer (object): line, so it's okay to attempt large reads. """ buf = self.__remainder - while not linefeed_byte in buf: + while linefeed_byte not in buf: buf += self._read_timeout(timeout) n = buf.index(linefeed_byte) self.__remainder = buf[n + 1:] @@ -354,7 +365,9 @@ class Packetizer (object): data = self.__compress_engine_out(data) packet = self._build_packet(data) if self.__dump_packets: - self._log(DEBUG, 'Write packet <%s>, length %d' % (cmd_name, orig_len)) + self._log( + DEBUG, + 'Write packet <%s>, length %d' % (cmd_name, orig_len)) self._log(DEBUG, util.format_binary(packet, 'OUT: ')) if self.__block_engine_out is not None: out = self.__block_engine_out.update(packet) @@ -362,14 +375,20 @@ class Packetizer (object): out = packet # + mac if self.__block_engine_out is not None: - payload = struct.pack('>I', self.__sequence_number_out) + packet - out += compute_hmac(self.__mac_key_out, payload, self.__mac_engine_out)[:self.__mac_size_out] - self.__sequence_number_out = (self.__sequence_number_out + 1) & xffffffff + payload = struct.pack( + '>I', self.__sequence_number_out) + packet + out += compute_hmac( + self.__mac_key_out, + payload, + self.__mac_engine_out)[:self.__mac_size_out] + self.__sequence_number_out = \ + (self.__sequence_number_out + 1) & xffffffff self.write_all(out) self.__sent_bytes += len(out) self.__sent_packets += 1 - if (self.__sent_packets >= self.REKEY_PACKETS or self.__sent_bytes >= self.REKEY_BYTES)\ + if (self.__sent_packets >= self.REKEY_PACKETS or + self.__sent_bytes >= self.REKEY_BYTES)\ and not self.__need_rekey: # only ask once for rekeying self._log(DEBUG, 'Rekeying (hit %d packets, %d bytes sent)' % @@ -410,15 +429,21 @@ class Packetizer (object): if self.__mac_size_in > 0: mac = post_packet[:self.__mac_size_in] - mac_payload = struct.pack('>II', self.__sequence_number_in, packet_size) + packet - my_mac = compute_hmac(self.__mac_key_in, mac_payload, self.__mac_engine_in)[:self.__mac_size_in] + mac_payload = struct.pack( + '>II', self.__sequence_number_in, packet_size) + packet + my_mac = compute_hmac( + self.__mac_key_in, + mac_payload, + self.__mac_engine_in)[:self.__mac_size_in] if not util.constant_time_bytes_eq(my_mac, mac): raise SSHException('Mismatched MAC') padding = byte_ord(packet[0]) payload = packet[1:packet_size - padding] if self.__dump_packets: - self._log(DEBUG, 'Got payload (%d bytes, %d padding)' % (packet_size, padding)) + self._log( + DEBUG, + 'Got payload (%d bytes, %d padding)' % (packet_size, padding)) if self.__compress_engine_in is not None: payload = self.__compress_engine_in(payload) @@ -436,9 +461,12 @@ class Packetizer (object): # dropping the connection self.__received_bytes_overflow += raw_packet_size self.__received_packets_overflow += 1 - if (self.__received_packets_overflow >= self.REKEY_PACKETS_OVERFLOW_MAX) or \ - (self.__received_bytes_overflow >= self.REKEY_BYTES_OVERFLOW_MAX): - raise SSHException('Remote transport is ignoring rekey requests') + if (self.__received_packets_overflow >= + self.REKEY_PACKETS_OVERFLOW_MAX) or \ + (self.__received_bytes_overflow >= + self.REKEY_BYTES_OVERFLOW_MAX): + raise SSHException( + 'Remote transport is ignoring rekey requests') elif (self.__received_packets >= self.REKEY_PACKETS) or \ (self.__received_bytes >= self.REKEY_BYTES): # only ask once for rekeying @@ -454,10 +482,12 @@ class Packetizer (object): else: cmd_name = '$%x' % cmd if self.__dump_packets: - self._log(DEBUG, 'Read packet <%s>, length %d' % (cmd_name, len(payload))) + self._log( + DEBUG, + 'Read packet <%s>, length %d' % (cmd_name, len(payload))) return cmd, msg - ########## protected + # ...protected... def _log(self, level, msg): if self.__logger is None: @@ -469,7 +499,8 @@ class Packetizer (object): self.__logger.log(level, msg) def _check_keepalive(self): - if (not self.__keepalive_interval) or (not self.__block_engine_out) or \ + if (not self.__keepalive_interval) or \ + (not self.__block_engine_out) or \ self.__need_rekey: # wait till we're encrypting, and not in the middle of rekeying return @@ -508,7 +539,8 @@ class Packetizer (object): packet = struct.pack('>IB', len(payload) + padding + 1, padding) packet += payload if self.__sdctr_out or self.__block_engine_out is None: - # cute trick i caught openssh doing: if we're not encrypting or SDCTR mode (RFC4344), + # cute trick i caught openssh doing: if we're not encrypting or + # SDCTR mode (RFC4344), # don't waste random bytes for the padding packet += (zero_byte * padding) else: diff --git a/paramiko/pipe.py b/paramiko/pipe.py index 4f62d7c5..6ca37703 100644 --- a/paramiko/pipe.py +++ b/paramiko/pipe.py @@ -28,7 +28,6 @@ will trigger as readable in `select `. import sys import os import socket -from paramiko.py3compat import b def make_pipe(): @@ -45,13 +44,13 @@ class PosixPipe (object): self._set = False self._forever = False self._closed = False - + def close(self): os.close(self._rfd) os.close(self._wfd) # used for unit tests: self._closed = True - + def fileno(self): return self._rfd @@ -60,13 +59,13 @@ class PosixPipe (object): return os.read(self._rfd, 1) self._set = False - + def set(self): if self._set or self._closed: return self._set = True os.write(self._wfd, b'*') - + def set_forever(self): self._forever = True self.set() @@ -81,39 +80,39 @@ class WindowsPipe (object): serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serv.bind(('127.0.0.1', 0)) serv.listen(1) - + # need to save sockets in _rsock/_wsock so they don't get closed self._rsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._rsock.connect(('127.0.0.1', serv.getsockname()[1])) - + self._wsock, addr = serv.accept() serv.close() self._set = False self._forever = False self._closed = False - + def close(self): self._rsock.close() self._wsock.close() # used for unit tests: self._closed = True - + def fileno(self): return self._rsock.fileno() - def clear (self): + def clear(self): if not self._set or self._forever: return self._rsock.recv(1) self._set = False - - def set (self): + + def set(self): if self._set or self._closed: return self._set = True self._wsock.send(b'*') - def set_forever (self): + def set_forever(self): self._forever = True self.set() @@ -123,12 +122,12 @@ class OrPipe (object): self._set = False self._partner = None self._pipe = pipe - + def set(self): self._set = True if not self._partner._set: self._pipe.set() - + def clear(self): self._set = False if not self._partner._set: @@ -146,4 +145,3 @@ def make_or_pipe(pipe): p1._partner = p2 p2._partner = p1 return p1, p2 - diff --git a/setup.cfg b/setup.cfg index b4280a7e..364c2f3f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -5,6 +5,6 @@ universal = 1 omit = paramiko/_winapi.py [flake8] -exclude = sites,.git,build,dist,alt_env,appveyor,demos,tests,test.py,kex_gss.py,kex_gex.py +exclude = sites,.git,build,dist,alt_env,appveyor,demos,tests,test.py ignore = E124,E125,E128,E261,E301,E302,E303,E402 max-line-length = 79 -- cgit v1.2.3 From bda557429547f4d5a4e81492c9754b3b4e1ae622 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Mon, 29 May 2017 17:21:36 -0400 Subject: Flake8 improvements for more of paramiko. --- paramiko/buffered_pipe.py | 20 +++++------ paramiko/channel.py | 53 +++++++++++++++++++---------- paramiko/client.py | 7 ++-- paramiko/kex_group14.py | 2 +- paramiko/rsakey.py | 5 +-- paramiko/server.py | 60 ++++++++++++++++++++------------- paramiko/sftp_handle.py | 15 +++++---- paramiko/transport.py | 86 +++++++++++++++++++++++++++++++++-------------- setup.cfg | 2 +- 9 files changed, 160 insertions(+), 90 deletions(-) diff --git a/paramiko/buffered_pipe.py b/paramiko/buffered_pipe.py index 605f51e9..9a65cd95 100644 --- a/paramiko/buffered_pipe.py +++ b/paramiko/buffered_pipe.py @@ -41,7 +41,7 @@ class BufferedPipe (object): file or socket, but is fed data from another thread. This is used by `.Channel`. """ - + def __init__(self): self._lock = threading.Lock() self._cv = threading.Condition(self._lock) @@ -67,7 +67,7 @@ class BufferedPipe (object): Set an event on this buffer. When data is ready to be read (or the buffer has been closed), the event will be set. When no data is ready, the event will be cleared. - + :param threading.Event event: the event to set/clear """ self._lock.acquire() @@ -84,12 +84,12 @@ class BufferedPipe (object): event.clear() finally: self._lock.release() - + def feed(self, data): """ Feed new data into this pipe. This method is assumed to be called from a separate thread, so synchronization is done. - + :param data: the data to add, as a `str` or `bytes` """ self._lock.acquire() @@ -106,7 +106,7 @@ class BufferedPipe (object): Returns true if data is buffered and ready to be read from this feeder. A ``False`` result does not mean that the feeder has closed; it means you may need to wait before more data arrives. - + :return: ``True`` if a `read` call would immediately return at least one byte; ``False`` otherwise. @@ -135,7 +135,7 @@ class BufferedPipe (object): :param float timeout: maximum seconds to wait (or ``None``, the default, to wait forever) :return: the read data, as a `bytes` - + :raises PipeTimeout: if a timeout was specified and no data was ready before that timeout @@ -172,11 +172,11 @@ class BufferedPipe (object): self._lock.release() return out - + def empty(self): """ Clear out the buffer and return all data that was in it. - + :return: any data that was in the buffer prior to clearing it out, as a `str` @@ -190,7 +190,7 @@ class BufferedPipe (object): return out finally: self._lock.release() - + def close(self): """ Close this pipe object. Future calls to `read` after the buffer @@ -208,7 +208,7 @@ class BufferedPipe (object): def __len__(self): """ Return the number of bytes buffered. - + :return: number (`int`) of bytes buffered """ self._lock.acquire() diff --git a/paramiko/channel.py b/paramiko/channel.py index f67ed7e2..ed03a813 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -419,7 +419,8 @@ class Channel (ClosingContextManager): :param function handler: a required handler to use for incoming SSH Agent connections - :return: True if we are ok, else False (at that time we always return ok) + :return: True if we are ok, else False + (at that time we always return ok) :raises: SSHException in case of channel problem. """ @@ -603,8 +604,8 @@ class Channel (ClosingContextManager): """ Receive data from the channel. The return value is a string representing the data received. The maximum amount of data to be - received at once is specified by ``nbytes``. If a string of length zero - is returned, the channel stream has closed. + received at once is specified by ``nbytes``. If a string of + length zero is returned, the channel stream has closed. :param int nbytes: maximum number of bytes to read. :return: received data, as a `bytes` @@ -754,7 +755,7 @@ class Channel (ClosingContextManager): if sending stalled for longer than the timeout set by `settimeout`. :raises socket.error: if an error occurred before the entire string was sent. - + .. note:: If the channel is closed while only part of the data has been sent, there is no way to determine how much data (if any) was sent. @@ -778,7 +779,7 @@ class Channel (ClosingContextManager): if sending stalled for longer than the timeout set by `settimeout`. :raises socket.error: if an error occurred before the entire string was sent. - + .. versionadded:: 1.1 """ while s: @@ -896,7 +897,7 @@ class Channel (ClosingContextManager): # attribute instead of a semipublic .closed attribute. return self.closed - ### calls from Transport + # ...calls from Transport def _set_transport(self, transport): self.transport = transport @@ -905,7 +906,8 @@ class Channel (ClosingContextManager): def _set_window(self, window_size, max_packet_size): self.in_window_size = window_size self.in_max_packet_size = max_packet_size - # threshold of bytes we receive before we bother to send a window update + # threshold of bytes we receive before we bother to send + # a window update self.in_window_threshold = window_size // 10 self.in_window_sofar = 0 self._log(DEBUG, 'Max packet in: %d bytes' % max_packet_size) @@ -946,7 +948,10 @@ class Channel (ClosingContextManager): code = m.get_int() s = m.get_binary() if code != 1: - self._log(ERROR, 'unknown extended_data type %d; discarding' % code) + self._log( + ERROR, + 'unknown extended_data type %d; discarding' % code + ) return if self.combine_stderr: self._feed(s) @@ -986,8 +991,15 @@ class Channel (ClosingContextManager): if server is None: ok = False else: - ok = server.check_channel_pty_request(self, term, width, height, pixelwidth, - pixelheight, modes) + ok = server.check_channel_pty_request( + self, + term, + width, + height, + pixelwidth, + pixelheight, + modes + ) elif key == 'shell': if server is None: ok = False @@ -1020,8 +1032,8 @@ class Channel (ClosingContextManager): if server is None: ok = False else: - ok = server.check_channel_window_change_request(self, width, height, pixelwidth, - pixelheight) + ok = server.check_channel_window_change_request( + self, width, height, pixelwidth, pixelheight) elif key == 'x11-req': single_connection = m.get_boolean() auth_proto = m.get_text() @@ -1030,8 +1042,13 @@ class Channel (ClosingContextManager): if server is None: ok = False else: - ok = server.check_channel_x11_request(self, single_connection, - auth_proto, auth_cookie, screen_number) + ok = server.check_channel_x11_request( + self, + single_connection, + auth_proto, + auth_cookie, + screen_number + ) elif key == 'auth-agent-req@openssh.com': if server is None: ok = False @@ -1073,7 +1090,7 @@ class Channel (ClosingContextManager): if m is not None: self.transport._send_user_message(m) - ### internals... + # ...internals... def _send(self, s, m): size = len(s) @@ -1149,7 +1166,8 @@ class Channel (ClosingContextManager): return m1, m2 def _unlink(self): - # server connection could die before we become active: still signal the close! + # server connection could die before we become active: + # still signal the close! if self.closed: return self.lock.acquire() @@ -1192,7 +1210,8 @@ class Channel (ClosingContextManager): # should we block? if self.timeout == 0.0: raise socket.timeout() - # loop here in case we get woken up but a different thread has filled the buffer + # loop here in case we get woken up but a different thread has + # filled the buffer timeout = self.timeout while self.out_window_size == 0: if self.closed or self.eof_sent: diff --git a/paramiko/client.py b/paramiko/client.py index 3bb78e9f..bbda1f80 100644 --- a/paramiko/client.py +++ b/paramiko/client.py @@ -142,7 +142,8 @@ class SSHClient (ClosingContextManager): with open(filename, 'w') as f: for hostname, keys in self._host_keys.items(): for keytype, key in keys.items(): - f.write('%s %s %s\n' % (hostname, keytype, key.get_base64())) + f.write('%s %s %s\n' % ( + hostname, keytype, key.get_base64())) def get_host_keys(self): """ @@ -354,8 +355,8 @@ class SSHClient (ClosingContextManager): # host key, because the host is authenticated via GSS-API / SSPI as # well as our client. if not self._transport.use_gss_kex: - our_server_key = self._system_host_keys.get(server_hostkey_name, - {}).get(keytype, None) + our_server_key = self._system_host_keys.get( + server_hostkey_name, {}).get(keytype) if our_server_key is None: our_server_key = self._host_keys.get(server_hostkey_name, {}).get(keytype, None) diff --git a/paramiko/kex_group14.py b/paramiko/kex_group14.py index 9f7dd216..22955e34 100644 --- a/paramiko/kex_group14.py +++ b/paramiko/kex_group14.py @@ -28,7 +28,7 @@ from hashlib import sha1 class KexGroup14(KexGroup1): # http://tools.ietf.org/html/rfc3526#section-3 - P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF + P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF # noqa G = 2 name = 'diffie-hellman-group14-sha1' diff --git a/paramiko/rsakey.py b/paramiko/rsakey.py index fa2b4c44..8ccf4c30 100644 --- a/paramiko/rsakey.py +++ b/paramiko/rsakey.py @@ -37,7 +37,8 @@ class RSAKey(PKey): data. """ - def __init__(self, msg=None, data=None, filename=None, password=None, key=None, file_obj=None): + def __init__(self, msg=None, data=None, filename=None, password=None, + key=None, file_obj=None): self.key = None if file_obj is not None: self._from_private_key(file_obj, password) @@ -167,7 +168,7 @@ class RSAKey(PKey): ) return RSAKey(key=key) - ### internals... + # ...internals... def _from_private_key_file(self, filename, password): data = self._read_private_key_file('RSA', filename, password) diff --git a/paramiko/server.py b/paramiko/server.py index bc4ac071..da4a1e1c 100644 --- a/paramiko/server.py +++ b/paramiko/server.py @@ -22,7 +22,8 @@ import threading from paramiko import util -from paramiko.common import DEBUG, ERROR, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED, AUTH_FAILED, AUTH_SUCCESSFUL +from paramiko.common import DEBUG, ERROR, \ + OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED, AUTH_FAILED, AUTH_SUCCESSFUL from paramiko.py3compat import string_types @@ -250,9 +251,10 @@ class ServerInterface (object): We don't check if the krb5 principal is allowed to log in on the server, because there is no way to do that in python. So if you develop your own SSH server with paramiko for a cetain - plattform like Linux, you should call C{krb5_kuserok()} in your - local kerberos library to make sure that the krb5_principal has - an account on the server and is allowed to log in as a user. + plattform like Linux, you should call C{krb5_kuserok()} in + your local kerberos library to make sure that the + krb5_principal has an account on the server and is allowed to + log in as a user. :see: `http://www.unix.com/man-page/all/3/krb5_kuserok/` """ if gss_authenticated == AUTH_SUCCESSFUL: @@ -281,9 +283,10 @@ class ServerInterface (object): We don't check if the krb5 principal is allowed to log in on the server, because there is no way to do that in python. So if you develop your own SSH server with paramiko for a cetain - plattform like Linux, you should call C{krb5_kuserok()} in your - local kerberos library to make sure that the krb5_principal has - an account on the server and is allowed to log in as a user. + plattform like Linux, you should call C{krb5_kuserok()} in + your local kerberos library to make sure that the + krb5_principal has an account on the server and is allowed + to log in as a user. :see: `http://www.unix.com/man-page/all/3/krb5_kuserok/` """ if gss_authenticated == AUTH_SUCCESSFUL: @@ -366,10 +369,11 @@ class ServerInterface (object): """ return False - ### Channel requests + # ...Channel requests... - def check_channel_pty_request(self, channel, term, width, height, pixelwidth, pixelheight, - modes): + def check_channel_pty_request( + self, channel, term, width, height, pixelwidth, pixelheight, + modes): """ Determine if a pseudo-terminal of the given dimensions (usually requested for shell access) can be provided on the given channel. @@ -447,14 +451,16 @@ class ServerInterface (object): ``True`` if this channel is now hooked up to the requested subsystem; ``False`` if that subsystem can't or won't be provided. """ - handler_class, larg, kwarg = channel.get_transport()._get_subsystem_handler(name) + handler_class, larg, kwarg = \ + channel.get_transport()._get_subsystem_handler(name) if handler_class is None: return False handler = handler_class(channel, name, self, *larg, **kwarg) handler.start() return True - def check_channel_window_change_request(self, channel, width, height, pixelwidth, pixelheight): + def check_channel_window_change_request( + self, channel, width, height, pixelwidth, pixelheight): """ Determine if the pseudo-terminal on the given channel can be resized. This only makes sense if a pty was previously allocated on it. @@ -472,7 +478,9 @@ class ServerInterface (object): """ return False - def check_channel_x11_request(self, channel, single_connection, auth_protocol, auth_cookie, screen_number): + def check_channel_x11_request( + self, channel, single_connection, auth_protocol, auth_cookie, + screen_number): """ Determine if the client will be provided with an X11 session. If this method returns ``True``, X11 applications should be routed through new @@ -588,12 +596,12 @@ class InteractiveQuery (object): self.add_prompt(x) else: self.add_prompt(x[0], x[1]) - + def add_prompt(self, prompt, echo=True): """ Add a prompt to this query. The prompt should be a (reasonably short) string. Multiple prompts can be added to the same query. - + :param str prompt: the user prompt :param bool echo: ``True`` (default) if the user's response should be echoed; @@ -621,10 +629,11 @@ class SubsystemHandler (threading.Thread): Create a new handler for a channel. This is used by `.ServerInterface` to start up a new handler when a channel requests this subsystem. You don't need to override this method, but if you do, be sure to pass the - ``channel`` and ``name`` parameters through to the original ``__init__`` - method here. + ``channel`` and ``name`` parameters through to the original + ``__init__`` method here. - :param .Channel channel: the channel associated with this subsystem request. + :param .Channel channel: the channel associated with this + subsystem request. :param str name: name of the requested subsystem. :param .ServerInterface server: the server object for the session that started this subsystem @@ -634,7 +643,7 @@ class SubsystemHandler (threading.Thread): self.__transport = channel.get_transport() self.__name = name self.__server = server - + def get_server(self): """ Return the `.ServerInterface` object associated with this channel and @@ -644,10 +653,12 @@ class SubsystemHandler (threading.Thread): def _run(self): try: - self.__transport._log(DEBUG, 'Starting handler for subsystem %s' % self.__name) + self.__transport._log( + DEBUG, 'Starting handler for subsystem %s' % self.__name) self.start_subsystem(self.__name, self.__transport, self.__channel) except Exception as e: - self.__transport._log(ERROR, 'Exception in subsystem handler for "%s": %s' % + self.__transport._log( + ERROR, 'Exception in subsystem handler for "%s": %s' % (self.__name, str(e))) self.__transport._log(ERROR, util.tb_strings()) try: @@ -663,8 +674,8 @@ class SubsystemHandler (threading.Thread): subsystem is finished, this method will return. After this method returns, the channel is closed. - The combination of ``transport`` and ``channel`` are unique; this handler - corresponds to exactly one `.Channel` on one `.Transport`. + The combination of ``transport`` and ``channel`` are unique; this + handler corresponds to exactly one `.Channel` on one `.Transport`. .. note:: It is the responsibility of this method to exit if the underlying @@ -676,7 +687,8 @@ class SubsystemHandler (threading.Thread): :param str name: name of the requested subsystem. :param .Transport transport: the server-mode `.Transport`. - :param .Channel channel: the channel associated with this subsystem request. + :param .Channel channel: the channel associated with this subsystem + request. """ pass diff --git a/paramiko/sftp_handle.py b/paramiko/sftp_handle.py index 05b5e904..bcb4bcba 100644 --- a/paramiko/sftp_handle.py +++ b/paramiko/sftp_handle.py @@ -30,10 +30,10 @@ class SFTPHandle (ClosingContextManager): Abstract object representing a handle to an open file (or folder) in an SFTP server implementation. Each handle has a string representation used by the client to refer to the underlying file. - + Server implementations can (and should) subclass SFTPHandle to implement features of a file handle, like `stat` or `chattr`. - + Instances of this class may be used as context managers. """ def __init__(self, flags=0): @@ -41,8 +41,9 @@ class SFTPHandle (ClosingContextManager): Create a new file handle representing a local file being served over SFTP. If ``flags`` is passed in, it's used to determine if the file is open in append mode. - - :param int flags: optional flags as passed to `.SFTPServerInterface.open` + + :param int flags: optional flags as passed to + `.SFTPServerInterface.open` """ self.__flags = flags self.__name = None @@ -55,7 +56,7 @@ class SFTPHandle (ClosingContextManager): When a client closes a file, this method is called on the handle. Normally you would use this method to close the underlying OS level file object(s). - + The default implementation checks for attributes on ``self`` named ``readfile`` and/or ``writefile``, and if either or both are present, their ``close()`` methods are called. This means that if you are @@ -117,7 +118,7 @@ class SFTPHandle (ClosingContextManager): differently from ``readfile`` to make it easy to implement read-only (or write-only) files, but if both attributes are present, they should refer to the same file. - + :param offset: position in the file to start reading from. :type offset: int or long :param str data: data to write into the file. @@ -167,7 +168,7 @@ class SFTPHandle (ClosingContextManager): """ return SFTP_OP_UNSUPPORTED - ### internals... + # ...internals... def _set_files(self, files): """ diff --git a/paramiko/transport.py b/paramiko/transport.py index 6d7dd0e8..914166d7 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -1806,7 +1806,6 @@ class Transport (threading.Thread, ClosingContextManager): self.saved_exception = e except EOFError as e: self._log(DEBUG, 'EOF in transport thread') - #self._log(DEBUG, util.tb_strings()) self.saved_exception = e except socket.error as e: if type(e.args) is tuple: @@ -2070,7 +2069,8 @@ class Transport (threading.Thread, ClosingContextManager): self.remote_kex_init = cMSG_KEXINIT + m.get_so_far() def _activate_inbound(self): - """switch on newly negotiated encryption parameters for inbound traffic""" + """switch on newly negotiated encryption parameters for + inbound traffic""" block_size = self._cipher_info[self.remote_cipher]['block-size'] if self.server_mode: IV_in = self._compute_key('A', block_size) @@ -2094,18 +2094,22 @@ class Transport (threading.Thread, ClosingContextManager): self.packetizer.set_inbound_compressor(compress_in()) def _activate_outbound(self): - """switch on newly negotiated encryption parameters for outbound traffic""" + """switch on newly negotiated encryption parameters for + outbound traffic""" m = Message() m.add_byte(cMSG_NEWKEYS) self._send_message(m) block_size = self._cipher_info[self.local_cipher]['block-size'] if self.server_mode: IV_out = self._compute_key('B', block_size) - key_out = self._compute_key('D', self._cipher_info[self.local_cipher]['key-size']) + key_out = self._compute_key( + 'D', self._cipher_info[self.local_cipher]['key-size']) else: IV_out = self._compute_key('A', block_size) - key_out = self._compute_key('C', self._cipher_info[self.local_cipher]['key-size']) - engine = self._get_cipher(self.local_cipher, key_out, IV_out, self._ENCRYPT) + key_out = self._compute_key( + 'C', self._cipher_info[self.local_cipher]['key-size']) + engine = self._get_cipher( + self.local_cipher, key_out, IV_out, self._ENCRYPT) mac_size = self._mac_info[self.local_mac]['size'] mac_engine = self._mac_info[self.local_mac]['class'] # initial mac keys are done in the hash's natural size (not the @@ -2115,9 +2119,11 @@ class Transport (threading.Thread, ClosingContextManager): else: mac_key = self._compute_key('E', mac_engine().digest_size) sdctr = self.local_cipher.endswith('-ctr') - self.packetizer.set_outbound_cipher(engine, block_size, mac_engine, mac_size, mac_key, sdctr) + self.packetizer.set_outbound_cipher( + engine, block_size, mac_engine, mac_size, mac_key, sdctr) compress_out = self._compression_info[self.local_compression][0] - if (compress_out is not None) and ((self.local_compression != 'zlib@openssh.com') or self.authenticated): + if (compress_out is not None) and \ + ((self.local_compression != 'zlib@openssh.com') or self.authenticated): self._log(DEBUG, 'Switching on outbound compression ...') self.packetizer.set_outbound_compressor(compress_out()) if not self.packetizer.need_rekey(): @@ -2173,7 +2179,10 @@ class Transport (threading.Thread, ClosingContextManager): self._log(DEBUG, 'Received global request "%s"' % kind) want_reply = m.get_boolean() if not self.server_mode: - self._log(DEBUG, 'Rejecting "%s" global request from server.' % kind) + self._log( + DEBUG, + 'Rejecting "%s" global request from server.' % kind + ) ok = False elif kind == 'tcpip-forward': address = m.get_text() @@ -2224,7 +2233,8 @@ class Transport (threading.Thread, ClosingContextManager): return self.lock.acquire() try: - chan._set_remote_channel(server_chanid, server_window_size, server_max_packet_size) + chan._set_remote_channel( + server_chanid, server_window_size, server_max_packet_size) self._log(DEBUG, 'Secsh channel %d opened.' % chanid) if chanid in self.channel_events: self.channel_events[chanid].set() @@ -2237,9 +2247,13 @@ class Transport (threading.Thread, ClosingContextManager): chanid = m.get_int() reason = m.get_int() reason_str = m.get_text() - lang = m.get_text() + m.get_text() # ignored language reason_text = CONNECTION_FAILED_CODE.get(reason, '(unknown code)') - self._log(ERROR, 'Secsh channel %d open FAILED: %s: %s' % (chanid, reason_str, reason_text)) + self._log( + ERROR, + 'Secsh channel %d open FAILED: %s: %s' % ( + chanid, reason_str, reason_text) + ) self.lock.acquire() try: self.saved_exception = ChannelException(reason, reason_text) @@ -2258,7 +2272,8 @@ class Transport (threading.Thread, ClosingContextManager): initial_window_size = m.get_int() max_packet_size = m.get_int() reject = False - if (kind == 'auth-agent@openssh.com') and (self._forward_agent_handler is not None): + if (kind == 'auth-agent@openssh.com') and \ + (self._forward_agent_handler is not None): self._log(DEBUG, 'Incoming forward agent connection') self.lock.acquire() try: @@ -2268,7 +2283,11 @@ class Transport (threading.Thread, ClosingContextManager): elif (kind == 'x11') and (self._x11_handler is not None): origin_addr = m.get_text() origin_port = m.get_int() - self._log(DEBUG, 'Incoming x11 connection from %s:%d' % (origin_addr, origin_port)) + self._log( + DEBUG, + 'Incoming x11 connection from %s:%d' % ( + origin_addr, origin_port) + ) self.lock.acquire() try: my_chanid = self._next_channel() @@ -2279,14 +2298,20 @@ class Transport (threading.Thread, ClosingContextManager): server_port = m.get_int() origin_addr = m.get_text() origin_port = m.get_int() - self._log(DEBUG, 'Incoming tcp forwarded connection from %s:%d' % (origin_addr, origin_port)) + self._log( + DEBUG, + 'Incoming tcp forwarded connection from %s:%d' % ( + origin_addr, origin_port) + ) self.lock.acquire() try: my_chanid = self._next_channel() finally: self.lock.release() elif not self.server_mode: - self._log(DEBUG, 'Rejecting "%s" channel request from server.' % kind) + self._log( + DEBUG, + 'Rejecting "%s" channel request from server.' % kind) reject = True reason = OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED else: @@ -2302,11 +2327,17 @@ class Transport (threading.Thread, ClosingContextManager): origin_addr = m.get_text() origin_port = m.get_int() reason = self.server_object.check_channel_direct_tcpip_request( - my_chanid, (origin_addr, origin_port), (dest_addr, dest_port)) + my_chanid, + (origin_addr, origin_port), + (dest_addr, dest_port) + ) else: - reason = self.server_object.check_channel_request(kind, my_chanid) + reason = self.server_object.check_channel_request( + kind, my_chanid) if reason != OPEN_SUCCEEDED: - self._log(DEBUG, 'Rejecting "%s" channel request from client.' % kind) + self._log( + DEBUG, + 'Rejecting "%s" channel request from client.' % kind) reject = True if reject: msg = Message() @@ -2324,8 +2355,10 @@ class Transport (threading.Thread, ClosingContextManager): self._channels.put(my_chanid, chan) self.channels_seen[my_chanid] = True chan._set_transport(self) - chan._set_window(self.default_window_size, self.default_max_packet_size) - chan._set_remote_channel(chanid, initial_window_size, max_packet_size) + chan._set_window( + self.default_window_size, self.default_max_packet_size) + chan._set_remote_channel( + chanid, initial_window_size, max_packet_size) finally: self.lock.release() m = Message() @@ -2342,14 +2375,18 @@ class Transport (threading.Thread, ClosingContextManager): self._x11_handler(chan, (origin_addr, origin_port)) elif kind == 'forwarded-tcpip': chan.origin_addr = (origin_addr, origin_port) - self._tcp_handler(chan, (origin_addr, origin_port), (server_addr, server_port)) + self._tcp_handler( + chan, + (origin_addr, origin_port), + (server_addr, server_port) + ) else: self._queue_incoming_channel(chan) def _parse_debug(self, m): - always_display = m.get_boolean() + m.get_boolean() # always_display msg = m.get_string() - lang = m.get_string() + m.get_string() # language self._log(DEBUG, 'Debug msg: {0}'.format(util.safe_string(msg))) def _get_subsystem_handler(self, name): @@ -2396,7 +2433,6 @@ class SecurityOptions (object): ``ValueError`` will be raised. If you try to assign something besides a tuple to one of the fields, ``TypeError`` will be raised. """ - #__slots__ = [ 'ciphers', 'digests', 'key_types', 'kex', 'compression', '_transport' ] __slots__ = '_transport' def __init__(self, transport): diff --git a/setup.cfg b/setup.cfg index 364c2f3f..764803e0 100644 --- a/setup.cfg +++ b/setup.cfg @@ -5,6 +5,6 @@ universal = 1 omit = paramiko/_winapi.py [flake8] -exclude = sites,.git,build,dist,alt_env,appveyor,demos,tests,test.py +exclude = sites,.git,build,dist,alt_env,appveyor,demos,tests,test.py,transport.py ignore = E124,E125,E128,E261,E301,E302,E303,E402 max-line-length = 79 -- cgit v1.2.3 From 98f682815cdd33a2f1999b931582e2467bb59754 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Tue, 30 May 2017 09:10:55 -0400 Subject: Additional PEP8 fixes. --- paramiko/agent.py | 15 +++++++++----- paramiko/pkey.py | 31 ++++++++++++++++++----------- paramiko/server.py | 58 +++++++++++++++++++++++++++--------------------------- 3 files changed, 59 insertions(+), 45 deletions(-) diff --git a/paramiko/agent.py b/paramiko/agent.py index c13810bb..c950ba5e 100644 --- a/paramiko/agent.py +++ b/paramiko/agent.py @@ -109,15 +109,19 @@ class AgentProxyThread(threading.Thread): def run(self): try: (r, addr) = self.get_connection() - # Found that r should be either a socket from the socket library or None + # Found that r should be either + # a socket from the socket library or None self.__inr = r - self.__addr = addr # This should be an IP address as a string? or None + # The address should be an IP address as a string? or None + self.__addr = addr self._agent.connect() - if not isinstance(self._agent, int) and (self._agent._conn is None or not hasattr(self._agent._conn, 'fileno')): + if not isinstance(self._agent, int) and \ + (self._agent._conn is None or + not hasattr(self._agent._conn, 'fileno')): raise AuthenticationException("Unable to connect to SSH agent") self._communicate() except: - #XXX Not sure what to do here ... raise or pass ? + # XXX Not sure what to do here ... raise or pass ? raise def _communicate(self): @@ -213,7 +217,8 @@ class AgentClientProxy(object): if ('SSH_AUTH_SOCK' in os.environ) and (sys.platform != 'win32'): conn = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: - retry_on_signal(lambda: conn.connect(os.environ['SSH_AUTH_SOCK'])) + retry_on_signal( + lambda: conn.connect(os.environ['SSH_AUTH_SOCK'])) except: # probably a dangling env var: the ssh agent is gone return diff --git a/paramiko/pkey.py b/paramiko/pkey.py index c87daaed..af9370fc 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -65,7 +65,8 @@ class PKey(object): :param .Message msg: an optional SSH `.Message` containing a public key of this type. - :param str data: an optional string containing a public key of this type + :param str data: an optional string containing a public key + of this type :raises SSHException: if a key cannot be created from the ``data`` or ``msg`` given, or @@ -85,6 +86,8 @@ class PKey(object): return self.asbytes() # noinspection PyUnresolvedReferences + # TODO: The comparison functions should be removed as per: + # https://docs.python.org/3.0/whatsnew/3.0.html#ordering-comparisons def __cmp__(self, other): """ Compare this key to another. Returns 0 if this key is equivalent to @@ -97,8 +100,8 @@ class PKey(object): hs = hash(self) ho = hash(other) if hs != ho: - return cmp(hs, ho) - return cmp(self.asbytes(), other.asbytes()) + return cmp(hs, ho) # noqa + return cmp(self.asbytes(), other.asbytes()) # noqa def __eq__(self, other): return hash(self) == hash(other) @@ -249,10 +252,11 @@ class PKey(object): Read an SSH2-format private key file, looking for a string of the type ``"BEGIN xxx PRIVATE KEY"`` for some ``xxx``, base64-decode the text we find, and return it as a string. If the private key is encrypted and - ``password`` is not ``None``, the given password will be used to decrypt - the key (otherwise `.PasswordRequiredException` is thrown). + ``password`` is not ``None``, the given password will be used to + decrypt the key (otherwise `.PasswordRequiredException` is thrown). - :param str tag: ``"RSA"`` or ``"DSA"``, the tag used to mark the data block. + :param str tag: ``"RSA"`` or ``"DSA"``, the tag used to mark the + data block. :param str filename: name of the file to read. :param str password: an optional password to use to decrypt the key file, if it's @@ -271,7 +275,8 @@ class PKey(object): def _read_private_key(self, tag, f, password=None): lines = f.readlines() start = 0 - while (start < len(lines)) and (lines[start].strip() != '-----BEGIN ' + tag + ' PRIVATE KEY-----'): + beginning_of_key = '-----BEGIN ' + tag + ' PRIVATE KEY-----' + while start < len(lines) and lines[start].strip() != beginning_of_key: start += 1 if start >= len(lines): raise SSHException('not a valid ' + tag + ' private key file') @@ -286,7 +291,8 @@ class PKey(object): start += 1 # find end end = start - while end < len(lines) and lines[end].strip() != '-----END ' + tag + ' PRIVATE KEY-----': + ending_of_key = '-----END ' + tag + ' PRIVATE KEY-----' + while end < len(lines) and lines[end].strip() != ending_of_key: end += 1 # if we trudged to the end of the file, just try to cope. try: @@ -298,14 +304,17 @@ class PKey(object): return data # encrypted keyfile: will need a password if headers['proc-type'] != '4,ENCRYPTED': - raise SSHException('Unknown private key structure "%s"' % headers['proc-type']) + raise SSHException( + 'Unknown private key structure "%s"' % headers['proc-type']) try: encryption_type, saltstr = headers['dek-info'].split(',') except: raise SSHException("Can't parse DEK-info in private key file") if encryption_type not in self._CIPHER_TABLE: - raise SSHException('Unknown private key cipher "%s"' % encryption_type) - # if no password was passed in, raise an exception pointing out that we need one + raise SSHException( + 'Unknown private key cipher "%s"' % encryption_type) + # if no password was passed in, + # raise an exception pointing out that we need one if password is None: raise PasswordRequiredException('Private key file is encrypted') cipher = self._CIPHER_TABLE[encryption_type]['cipher'] diff --git a/paramiko/server.py b/paramiko/server.py index da4a1e1c..bb244efe 100644 --- a/paramiko/server.py +++ b/paramiko/server.py @@ -70,7 +70,7 @@ class ServerInterface (object): - ``OPEN_FAILED_CONNECT_FAILED`` - ``OPEN_FAILED_UNKNOWN_CHANNEL_TYPE`` - ``OPEN_FAILED_RESOURCE_SHORTAGE`` - + The default implementation always returns ``OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED``. @@ -161,7 +161,7 @@ class ServerInterface (object): Note that you don't have to actually verify any key signtature here. If you're willing to accept the key, Paramiko will do the work of verifying the client's signature. - + The default implementation always returns `.AUTH_FAILED`. :param str username: the username of the authenticating client @@ -174,21 +174,21 @@ class ServerInterface (object): :rtype: int """ return AUTH_FAILED - + def check_auth_interactive(self, username, submethods): """ Begin an interactive authentication challenge, if supported. You should override this method in server mode if you want to support the ``"keyboard-interactive"`` auth type, which requires you to send a series of questions for the client to answer. - + Return `.AUTH_FAILED` if this auth method isn't supported. Otherwise, you should return an `.InteractiveQuery` object containing the prompts and instructions for the user. The response will be sent via a call to `check_auth_interactive_response`. - + The default implementation always returns `.AUTH_FAILED`. - + :param str username: the username of the authenticating client :param str submethods: a comma-separated list of methods preferred by the client (usually @@ -199,13 +199,13 @@ class ServerInterface (object): :rtype: int or `.InteractiveQuery` """ return AUTH_FAILED - + def check_auth_interactive_response(self, responses): """ Continue or finish an interactive authentication challenge, if supported. You should override this method in server mode if you want to support the ``"keyboard-interactive"`` auth type. - + Return `.AUTH_FAILED` if the responses are not accepted, `.AUTH_SUCCESSFUL` if the responses are accepted and complete the authentication, or `.AUTH_PARTIALLY_SUCCESSFUL` if your @@ -277,7 +277,7 @@ class ServerInterface (object): `.AUTH_SUCCESSFUL` :rtype: int :note: Kerberos credential delegation is not supported. - :see: `.ssh_gss` `.kex_gss` + :see: `.ssh_gss` `.kex_gss` :note: : We are just checking in L{AuthHandler} that the given user is a valid krb5 principal! We don't check if the krb5 principal is allowed to log in on @@ -304,9 +304,8 @@ class ServerInterface (object): :see: : `.ssh_gss` """ UseGSSAPI = False - GSSAPICleanupCredentials = False return UseGSSAPI - + def check_port_forward_request(self, address, port): """ Handle a request for port forwarding. The client is asking that @@ -315,11 +314,11 @@ class ServerInterface (object): address (any address associated with this server) and a port of ``0`` indicates that no specific port is requested (usually the OS will pick a port). - + The default implementation always returns ``False``, rejecting the port forwarding request. If the request is accepted, you should return the port opened for listening. - + :param str address: the requested address :param int port: the requested port :return: @@ -327,18 +326,18 @@ class ServerInterface (object): to reject """ return False - + def cancel_port_forward_request(self, address, port): """ The client would like to cancel a previous port-forwarding request. If the given address and port is being forwarded across this ssh connection, the port should be closed. - + :param str address: the forwarded address :param int port: the forwarded port """ pass - + def check_global_request(self, kind, msg): """ Handle a global request of the given ``kind``. This method is called @@ -357,7 +356,7 @@ class ServerInterface (object): The default implementation always returns ``False``, indicating that it does not support any global requests. - + .. note:: Port forwarding requests are handled separately, in `check_port_forward_request`. @@ -415,20 +414,20 @@ class ServerInterface (object): Determine if a shell command will be executed for the client. If this method returns ``True``, the channel should be connected to the stdin, stdout, and stderr of the shell command. - + The default implementation always returns ``False``. - + :param .Channel channel: the `.Channel` the request arrived on. :param str command: the command to execute. :return: ``True`` if this channel is now hooked up to the stdin, stdout, and stderr of the executing command; ``False`` if the command will not be executed. - + .. versionadded:: 1.1 """ return False - + def check_channel_subsystem_request(self, channel, name): """ Determine if a requested subsystem will be provided to the client on @@ -477,7 +476,7 @@ class ServerInterface (object): :return: ``True`` if the terminal was resized; ``False`` if not. """ return False - + def check_channel_x11_request( self, channel, single_connection, auth_protocol, auth_cookie, screen_number): @@ -485,9 +484,9 @@ class ServerInterface (object): Determine if the client will be provided with an X11 session. If this method returns ``True``, X11 applications should be routed through new SSH channels, using `.Transport.open_x11_channel`. - + The default implementation always returns ``False``. - + :param .Channel channel: the `.Channel` the X11 request arrived on :param bool single_connection: ``True`` if only a single X11 channel should be opened, else @@ -537,7 +536,7 @@ class ServerInterface (object): - ``OPEN_FAILED_CONNECT_FAILED`` - ``OPEN_FAILED_UNKNOWN_CHANNEL_TYPE`` - ``OPEN_FAILED_RESOURCE_SHORTAGE`` - + The default implementation always returns ``OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED``. @@ -575,14 +574,14 @@ class InteractiveQuery (object): """ A query (set of prompts) for a user during interactive authentication. """ - + def __init__(self, name='', instructions='', *prompts): """ Create a new interactive query to send to the client. The name and instructions are optional, but are generally displayed to the end user. A list of prompts may be included, or they may be added via the `add_prompt` method. - + :param str name: name of this query :param str instructions: user instructions (usually short) about this query @@ -658,8 +657,9 @@ class SubsystemHandler (threading.Thread): self.start_subsystem(self.__name, self.__transport, self.__channel) except Exception as e: self.__transport._log( - ERROR, 'Exception in subsystem handler for "%s": %s' % - (self.__name, str(e))) + ERROR, + 'Exception in subsystem handler for "{}": {}'.format( + self.__name, e)) self.__transport._log(ERROR, util.tb_strings()) try: self.finish_subsystem() -- cgit v1.2.3 From ecb297518267487853813109da946d7240d717e1 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Tue, 30 May 2017 12:02:17 -0400 Subject: Additional PEP8 fixes. --- paramiko/ber.py | 6 ++++-- paramiko/dsskey.py | 11 +++++++---- paramiko/ecdsakey.py | 4 ++-- paramiko/primes.py | 14 +++++++++----- paramiko/proxy.py | 1 - paramiko/py3compat.py | 28 +++++++++++++++------------- paramiko/resource.py | 16 ++++++++-------- paramiko/sftp_attr.py | 20 +++++++++++++------- paramiko/sftp_si.py | 22 +++++++++++----------- paramiko/win_pageant.py | 13 ++++++++----- 10 files changed, 77 insertions(+), 58 deletions(-) diff --git a/paramiko/ber.py b/paramiko/ber.py index a388df07..7725f944 100644 --- a/paramiko/ber.py +++ b/paramiko/ber.py @@ -71,7 +71,8 @@ class BER(object): t = size & 0x7f if self.idx + t > len(self.content): return None - size = util.inflate_long(self.content[self.idx: self.idx + t], True) + size = util.inflate_long( + self.content[self.idx: self.idx + t], True) self.idx += t if self.idx + size > len(self.content): # can't fit @@ -87,7 +88,8 @@ class BER(object): return util.inflate_long(data) else: # 1: boolean (00 false, otherwise true) - raise BERException('Unknown ber encoding type %d (robey is lazy)' % ident) + raise BERException( + 'Unknown ber encoding type %d (robey is lazy)' % ident) @staticmethod def decode_sequence(data): diff --git a/paramiko/dsskey.py b/paramiko/dsskey.py index 4644e9a6..c8452f23 100644 --- a/paramiko/dsskey.py +++ b/paramiko/dsskey.py @@ -42,7 +42,8 @@ class DSSKey(PKey): data. """ - def __init__(self, msg=None, data=None, filename=None, password=None, vals=None, file_obj=None): + def __init__(self, msg=None, data=None, filename=None, password=None, + vals=None, file_obj=None): self.p = None self.q = None self.g = None @@ -222,7 +223,7 @@ class DSSKey(PKey): key.x = numbers.x return key - ### internals... + # ...internals... def _from_private_key_file(self, filename, password): data = self._read_private_key_file('DSA', filename, password) @@ -239,8 +240,10 @@ class DSSKey(PKey): keylist = BER(data).decode() except BERException as e: raise SSHException('Unable to parse key file: ' + str(e)) - if (type(keylist) is not list) or (len(keylist) < 6) or (keylist[0] != 0): - raise SSHException('not a valid DSA private key file (bad ber encoding)') + if (type(keylist) is not list) or (len(keylist) < 6) or \ + (keylist[0] != 0): + raise SSHException( + 'not a valid DSA private key file (bad ber encoding)') self.p = keylist[1] self.q = keylist[2] self.g = keylist[3] diff --git a/paramiko/ecdsakey.py b/paramiko/ecdsakey.py index e4f74310..51f8d8ce 100644 --- a/paramiko/ecdsakey.py +++ b/paramiko/ecdsakey.py @@ -237,13 +237,13 @@ class ECDSAKey(PKey): if bits is not None: curve = cls._ECDSA_CURVES.get_by_key_length(bits) if curve is None: - raise ValueError("Unsupported key length: %d"%(bits)) + raise ValueError("Unsupported key length: %d" % bits) curve = curve.curve_class() private_key = ec.generate_private_key(curve, backend=default_backend()) return ECDSAKey(vals=(private_key, private_key.public_key())) - ### internals... + # ...internals... def _from_private_key_file(self, filename, password): data = self._read_private_key_file('EC', filename, password) diff --git a/paramiko/primes.py b/paramiko/primes.py index d0e17575..50100ad5 100644 --- a/paramiko/primes.py +++ b/paramiko/primes.py @@ -25,7 +25,7 @@ import os from paramiko import util from paramiko.py3compat import byte_mask, long from paramiko.ssh_exception import SSHException -from paramiko.common import * +from paramiko.common import * # noqa def _roll_random(n): @@ -62,7 +62,8 @@ class ModulusPack (object): self.discarded = [] def _parse_modulus(self, line): - timestamp, mod_type, tests, tries, size, generator, modulus = line.split() + timestamp, mod_type, tests, tries, size, generator, modulus = \ + line.split() mod_type = int(mod_type) tests = int(tests) tries = int(tries) @@ -74,8 +75,10 @@ class ModulusPack (object): # type 2 (meets basic structural requirements) # test 4 (more than just a small-prime sieve) # tries < 100 if test & 4 (at least 100 tries of miller-rabin) - if (mod_type < 2) or (tests < 4) or ((tests & 4) and (tests < 8) and (tries < 100)): - self.discarded.append((modulus, 'does not meet basic requirements')) + if (mod_type < 2) or (tests < 4) or \ + ((tests & 4) and (tests < 8) and (tries < 100)): + self.discarded.append( + (modulus, 'does not meet basic requirements')) return if generator == 0: generator = 2 @@ -85,7 +88,8 @@ class ModulusPack (object): # this is okay. bl = util.bit_length(modulus) if (bl != size) and (bl != size + 1): - self.discarded.append((modulus, 'incorrectly reported bit length %d' % size)) + self.discarded.append( + (modulus, 'incorrectly reported bit length %d' % size)) return if bl not in self.pack: self.pack[bl] = [] diff --git a/paramiko/proxy.py b/paramiko/proxy.py index 7d67680a..c4ec627c 100644 --- a/paramiko/proxy.py +++ b/paramiko/proxy.py @@ -17,7 +17,6 @@ # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. -from datetime import datetime import os from shlex import split as shlsplit import signal diff --git a/paramiko/py3compat.py b/paramiko/py3compat.py index 6fafc31d..095b0d09 100644 --- a/paramiko/py3compat.py +++ b/paramiko/py3compat.py @@ -1,20 +1,22 @@ import sys import base64 -__all__ = ['PY2', 'string_types', 'integer_types', 'text_type', 'bytes_types', 'bytes', 'long', 'input', - 'decodebytes', 'encodebytes', 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', - 'b', 'u', 'b2s', 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', 'next', 'builtins'] +__all__ = ['PY2', 'string_types', 'integer_types', 'text_type', 'bytes_types', + 'bytes', 'long', 'input', 'decodebytes', 'encodebytes', + 'bytestring', 'byte_ord', 'byte_chr', 'byte_mask', 'b', 'u', 'b2s', + 'StringIO', 'BytesIO', 'is_callable', 'MAXSIZE', + 'next', 'builtins'] PY2 = sys.version_info[0] < 3 if PY2: - string_types = basestring - text_type = unicode + string_types = basestring # NOQA + text_type = unicode # NOQA bytes_types = str bytes = str - integer_types = (int, long) - long = long - input = raw_input + integer_types = (int, long) # NOQA + long = long # NOQA + input = raw_input # NOQA decodebytes = base64.decodestring encodebytes = base64.encodestring @@ -22,7 +24,7 @@ if PY2: def bytestring(s): # NOQA - if isinstance(s, unicode): + if isinstance(s, unicode): # NOQA return s.encode('utf-8') return s @@ -39,9 +41,9 @@ if PY2: """cast unicode or bytes to bytes""" if isinstance(s, str): return s - elif isinstance(s, unicode): + elif isinstance(s, unicode): # NOQA return s.encode(encoding) - elif isinstance(s, buffer): + elif isinstance(s, buffer): # NOQA return s else: raise TypeError("Expected unicode or bytes, got %r" % s) @@ -51,9 +53,9 @@ if PY2: """cast bytes or unicode to unicode""" if isinstance(s, str): return s.decode(encoding) - elif isinstance(s, unicode): + elif isinstance(s, unicode): # NOQA return s - elif isinstance(s, buffer): + elif isinstance(s, buffer): # NOQA return s.decode(encoding) else: raise TypeError("Expected unicode or bytes, got %r" % s) diff --git a/paramiko/resource.py b/paramiko/resource.py index 9809afbe..5fed22ad 100644 --- a/paramiko/resource.py +++ b/paramiko/resource.py @@ -27,30 +27,30 @@ class ResourceManager (object): """ A registry of objects and resources that should be closed when those objects are deleted. - + This is meant to be a safer alternative to Python's ``__del__`` method, which can cause reference cycles to never be collected. Objects registered with the ResourceManager can be collected but still free resources when they die. - + Resources are registered using `register`, and when an object is garbage collected, each registered resource is closed by having its ``close()`` method called. Multiple resources may be registered per object, but a resource will only be closed once, even if multiple objects register it. (The last object to register it wins.) """ - + def __init__(self): self._table = {} - + def register(self, obj, resource): """ Register a resource to be closed with an object is collected. - + When the given ``obj`` is garbage-collected by the Python interpreter, - the ``resource`` will be closed by having its ``close()`` method called. - Any exceptions are ignored. - + the ``resource`` will be closed by having its ``close()`` method + called. Any exceptions are ignored. + :param object obj: the object to track :param object resource: the resource to close when the object is collected diff --git a/paramiko/sftp_attr.py b/paramiko/sftp_attr.py index 0eaca30b..5597948a 100644 --- a/paramiko/sftp_attr.py +++ b/paramiko/sftp_attr.py @@ -84,7 +84,7 @@ class SFTPAttributes (object): def __repr__(self): return '' % self._debug_str() - ### internals... + # ...internals... @classmethod def _from_msg(cls, msg, filename=None, longname=None): attr = cls() @@ -189,9 +189,12 @@ class SFTPAttributes (object): ks = 's' else: ks = '?' - ks += self._rwx((self.st_mode & o700) >> 6, self.st_mode & stat.S_ISUID) - ks += self._rwx((self.st_mode & o70) >> 3, self.st_mode & stat.S_ISGID) - ks += self._rwx(self.st_mode & 7, self.st_mode & stat.S_ISVTX, True) + ks += self._rwx( + (self.st_mode & o700) >> 6, self.st_mode & stat.S_ISUID) + ks += self._rwx( + (self.st_mode & o70) >> 3, self.st_mode & stat.S_ISGID) + ks += self._rwx( + self.st_mode & 7, self.st_mode & stat.S_ISVTX, True) else: ks = '?---------' # compute display date @@ -201,9 +204,11 @@ class SFTPAttributes (object): else: if abs(time.time() - self.st_mtime) > 15552000: # (15552000 = 6 months) - datestr = time.strftime('%d %b %Y', time.localtime(self.st_mtime)) + datestr = time.strftime( + '%d %b %Y', time.localtime(self.st_mtime)) else: - datestr = time.strftime('%d %b %H:%M', time.localtime(self.st_mtime)) + datestr = time.strftime( + '%d %b %H:%M', time.localtime(self.st_mtime)) filename = getattr(self, 'filename', '?') # not all servers support uid/gid @@ -217,7 +222,8 @@ class SFTPAttributes (object): if size is None: size = 0 - return '%s 1 %-8d %-8d %8d %-12s %s' % (ks, uid, gid, size, datestr, filename) + return '%s 1 %-8d %-8d %8d %-12s %s' % ( + ks, uid, gid, size, datestr, filename) def asbytes(self): return b(str(self)) diff --git a/paramiko/sftp_si.py b/paramiko/sftp_si.py index 7ab00ad7..c335eaec 100644 --- a/paramiko/sftp_si.py +++ b/paramiko/sftp_si.py @@ -35,16 +35,15 @@ class SFTPServerInterface (object): SFTP sessions). However, raising an exception will usually cause the SFTP session to abruptly end, so you will usually want to catch exceptions and return an appropriate error code. - + All paths are in string form instead of unicode because not all SFTP clients & servers obey the requirement that paths be encoded in UTF-8. """ - def __init__(self, server, *largs, **kwargs): """ Create a new SFTPServerInterface object. This method does nothing by default and is meant to be overridden by subclasses. - + :param .ServerInterface server: the server object associated with this channel and SFTP subsystem """ @@ -92,7 +91,7 @@ class SFTPServerInterface (object): The ``attr`` object contains requested attributes of the file if it has to be created. Some or all attribute fields may be missing if the client didn't specify them. - + .. note:: The SFTP protocol defines all files to be in "binary" mode. There is no equivalent to Python's "text" mode. @@ -123,11 +122,12 @@ class SFTPServerInterface (object): In case of an error, you should return one of the ``SFTP_*`` error codes, such as `.SFTP_PERMISSION_DENIED`. - :param str path: the requested path (relative or absolute) to be listed. + :param str path: the requested path (relative or absolute) to be + listed. :return: a list of the files in the given folder, using `.SFTPAttributes` objects. - + .. note:: You should normalize the given ``path`` first (see the `os.path` module) and check appropriate permissions before returning the list @@ -189,7 +189,7 @@ class SFTPServerInterface (object): and since there's no other (easy) way to move files via SFTP, it's probably a good idea to implement "move" in this method too, even for files that cross disk partition boundaries, if at all possible. - + .. note:: You should return an error if a file with the same name as ``newpath`` already exists. (The rename operation should be non-desctructive.) @@ -267,25 +267,25 @@ class SFTPServerInterface (object): # on windows, normalize backslashes to sftp/posix format out = out.replace('\\', '/') return out - + def readlink(self, path): """ Return the target of a symbolic link (or shortcut) on the server. If the specified path doesn't refer to a symbolic link, an error should be returned. - + :param str path: path (relative or absolute) of the symbolic link. :return: the target `str` path of the symbolic link, or an error code like `.SFTP_NO_SUCH_FILE`. """ return SFTP_OP_UNSUPPORTED - + def symlink(self, target_path, path): """ Create a symbolic link on the server, as new pathname ``path``, with ``target_path`` as the target of the link. - + :param str target_path: path (relative or absolute) of the target for this new symbolic link. diff --git a/paramiko/win_pageant.py b/paramiko/win_pageant.py index 4b482bee..c8c2c7bc 100644 --- a/paramiko/win_pageant.py +++ b/paramiko/win_pageant.py @@ -25,13 +25,13 @@ import array import ctypes.wintypes import platform import struct -from paramiko.util import * +from paramiko.util import * # noqa from paramiko.py3compat import b try: - import _thread as thread # Python 3.x + import _thread as thread # Python 3.x except ImportError: - import thread # Python 2.5-2.7 + import thread # Python 2.5-2.7 from . import _winapi @@ -57,7 +57,10 @@ def can_talk_to_agent(): return bool(_get_pageant_window_object()) -ULONG_PTR = ctypes.c_uint64 if platform.architecture()[0] == '64bit' else ctypes.c_uint32 +if platform.architecture()[0] == '64bit': + ULONG_PTR = ctypes.c_uint64 +else: + ULONG_PTR = ctypes.c_uint32 class COPYDATASTRUCT(ctypes.Structure): @@ -91,7 +94,7 @@ def _query_pageant(msg): with pymap: pymap.write(msg) # Create an array buffer containing the mapped filename - char_buffer = array.array("b", b(map_name) + zero_byte) + char_buffer = array.array("b", b(map_name) + zero_byte) # noqa char_buffer_address, char_buffer_size = char_buffer.buffer_info() # Create a string to use for the SendMessage function call cds = COPYDATASTRUCT(_AGENT_COPYDATA_ID, char_buffer_size, -- cgit v1.2.3 From ddf1e357b43a8ca69848d550021f269b48361214 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Tue, 30 May 2017 12:23:33 -0400 Subject: Additional PEP8 fixes. --- paramiko/sftp.py | 20 +++++------ paramiko/sftp_client.py | 79 ++++++++++++++++++++++++++---------------- paramiko/sftp_server.py | 92 ++++++++++++++++++++++++++++++++----------------- 3 files changed, 119 insertions(+), 72 deletions(-) diff --git a/paramiko/sftp.py b/paramiko/sftp.py index f44a804d..e6786d10 100644 --- a/paramiko/sftp.py +++ b/paramiko/sftp.py @@ -26,15 +26,17 @@ from paramiko.message import Message from paramiko.py3compat import byte_chr, byte_ord -CMD_INIT, CMD_VERSION, CMD_OPEN, CMD_CLOSE, CMD_READ, CMD_WRITE, CMD_LSTAT, CMD_FSTAT, \ - CMD_SETSTAT, CMD_FSETSTAT, CMD_OPENDIR, CMD_READDIR, CMD_REMOVE, CMD_MKDIR, \ - CMD_RMDIR, CMD_REALPATH, CMD_STAT, CMD_RENAME, CMD_READLINK, CMD_SYMLINK = range(1, 21) +CMD_INIT, CMD_VERSION, CMD_OPEN, CMD_CLOSE, CMD_READ, CMD_WRITE, CMD_LSTAT, \ + CMD_FSTAT, CMD_SETSTAT, CMD_FSETSTAT, CMD_OPENDIR, CMD_READDIR, \ + CMD_REMOVE, CMD_MKDIR, CMD_RMDIR, CMD_REALPATH, CMD_STAT, CMD_RENAME, \ + CMD_READLINK, CMD_SYMLINK = range(1, 21) CMD_STATUS, CMD_HANDLE, CMD_DATA, CMD_NAME, CMD_ATTRS = range(101, 106) CMD_EXTENDED, CMD_EXTENDED_REPLY = range(200, 202) SFTP_OK = 0 -SFTP_EOF, SFTP_NO_SUCH_FILE, SFTP_PERMISSION_DENIED, SFTP_FAILURE, SFTP_BAD_MESSAGE, \ - SFTP_NO_CONNECTION, SFTP_CONNECTION_LOST, SFTP_OP_UNSUPPORTED = range(1, 9) +SFTP_EOF, SFTP_NO_SUCH_FILE, SFTP_PERMISSION_DENIED, SFTP_FAILURE, \ + SFTP_BAD_MESSAGE, SFTP_NO_CONNECTION, SFTP_CONNECTION_LOST, \ + SFTP_OP_UNSUPPORTED = range(1, 9) SFTP_DESC = ['Success', 'End of file', @@ -98,7 +100,7 @@ class BaseSFTP (object): self.sock = None self.ultra_debug = False - ### internals... + # ...internals... def _send_version(self): self._send_packet(CMD_INIT, struct.pack('>I', _VERSION)) @@ -124,7 +126,7 @@ class BaseSFTP (object): msg.add(*extension_pairs) self._send_packet(CMD_VERSION, msg) return version - + def _log(self, level, msg, *args): self.logger.log(level, msg, *args) @@ -154,7 +156,7 @@ class BaseSFTP (object): break else: x = self.sock.recv(n) - + if len(x) == 0: raise EOFError() out += x @@ -162,7 +164,6 @@ class BaseSFTP (object): return out def _send_packet(self, t, packet): - #self._log(DEBUG2, 'write: %s (len=%d)' % (CMD_NAMES.get(t, '0x%02x' % t), len(packet))) packet = asbytes(packet) out = struct.pack('>I', len(packet) + 1) + byte_chr(t) + packet if self.ultra_debug: @@ -181,6 +182,5 @@ class BaseSFTP (object): self._log(DEBUG, util.format_binary(data, 'IN: ')) if size > 0: t = byte_ord(data[0]) - #self._log(DEBUG2, 'read: %s (len=%d)' % (CMD_NAMES.get(t), '0x%02x' % t, len(data)-1)) return t, data[1:] return 0, bytes() diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 12a9506f..245e5072 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -28,13 +28,15 @@ from paramiko import util from paramiko.channel import Channel from paramiko.message import Message from paramiko.common import INFO, DEBUG, o777 -from paramiko.py3compat import bytestring, b, u, long, string_types, bytes_types -from paramiko.sftp import BaseSFTP, CMD_OPENDIR, CMD_HANDLE, SFTPError, CMD_READDIR, \ - CMD_NAME, CMD_CLOSE, SFTP_FLAG_READ, SFTP_FLAG_WRITE, SFTP_FLAG_CREATE, \ - SFTP_FLAG_TRUNC, SFTP_FLAG_APPEND, SFTP_FLAG_EXCL, CMD_OPEN, CMD_REMOVE, \ - CMD_RENAME, CMD_MKDIR, CMD_RMDIR, CMD_STAT, CMD_ATTRS, CMD_LSTAT, \ - CMD_SYMLINK, CMD_SETSTAT, CMD_READLINK, CMD_REALPATH, CMD_STATUS, SFTP_OK, \ - SFTP_EOF, SFTP_NO_SUCH_FILE, SFTP_PERMISSION_DENIED +from paramiko.py3compat import bytestring, b, u, long, string_types, \ + bytes_types +from paramiko.sftp import BaseSFTP, CMD_OPENDIR, CMD_HANDLE, SFTPError, \ + CMD_READDIR, CMD_NAME, CMD_CLOSE, SFTP_FLAG_READ, SFTP_FLAG_WRITE, \ + SFTP_FLAG_CREATE, SFTP_FLAG_TRUNC, SFTP_FLAG_APPEND, SFTP_FLAG_EXCL, \ + CMD_OPEN, CMD_REMOVE, CMD_RENAME, CMD_MKDIR, CMD_RMDIR, CMD_STAT, \ + CMD_ATTRS, CMD_LSTAT, CMD_SYMLINK, CMD_SETSTAT, CMD_READLINK, \ + CMD_REALPATH, CMD_STATUS, SFTP_OK, SFTP_EOF, SFTP_NO_SUCH_FILE, \ + SFTP_PERMISSION_DENIED from paramiko.sftp_attr import SFTPAttributes from paramiko.ssh_exception import SSHException @@ -56,6 +58,7 @@ def _to_unicode(s): except UnicodeError: return s + b_slash = b'/' @@ -93,13 +96,16 @@ class SFTPClient(BaseSFTP, ClosingContextManager): if type(sock) is Channel: # override default logger transport = self.sock.get_transport() - self.logger = util.get_logger(transport.get_log_channel() + '.sftp') + self.logger = util.get_logger( + transport.get_log_channel() + '.sftp') self.ultra_debug = transport.get_hexdump() try: server_version = self._send_version() except EOFError: raise SSHException('EOF during negotiation') - self._log(INFO, 'Opened sftp connection (server version %d)' % server_version) + self._log( + INFO, + 'Opened sftp connection (server version %d)' % server_version) @classmethod def from_transport(cls, t, window_size=None, max_packet_size=None): @@ -111,7 +117,8 @@ class SFTPClient(BaseSFTP, ClosingContextManager): OpenSSH and should work adequately for both files transfers and interactive sessions. - :param .Transport t: an open `.Transport` which is already authenticated + :param .Transport t: an open `.Transport` which is already + authenticated :param int window_size: optional window size for the `.SFTPClient` session. :param int max_packet_size: @@ -136,9 +143,12 @@ class SFTPClient(BaseSFTP, ClosingContextManager): for m in msg: self._log(level, m, *args) else: - # escape '%' in msg (they could come from file or directory names) before logging - msg = msg.replace('%','%%') - super(SFTPClient, self)._log(level, "[chan %s] " + msg, *([self.sock.get_name()] + list(args))) + # escape '%' in msg (they could come from file or directory names) + # before logging + msg = msg.replace('%', '%%') + super(SFTPClient, self)._log( + level, + "[chan %s] " + msg, *([self.sock.get_name()] + list(args))) def close(self): """ @@ -160,7 +170,8 @@ class SFTPClient(BaseSFTP, ClosingContextManager): def listdir(self, path='.'): """ - Return a list containing the names of the entries in the given ``path``. + Return a list containing the names of the entries in the given + ``path``. The list is in arbitrary order. It does not include the special entries ``'.'`` and ``'..'`` even if they are present in the folder. @@ -328,7 +339,9 @@ class SFTPClient(BaseSFTP, ClosingContextManager): if t != CMD_HANDLE: raise SFTPError('Expected handle') handle = msg.get_binary() - self._log(DEBUG, 'open(%r, %r) -> %s' % (filename, mode, hexlify(handle))) + self._log( + DEBUG, + 'open(%r, %r) -> %s' % (filename, mode, hexlify(handle))) return SFTPFile(self, handle, mode, bufsize) # Python continues to vacillate about "open" vs "file"... @@ -480,12 +493,12 @@ class SFTPClient(BaseSFTP, ClosingContextManager): def utime(self, path, times): """ - Set the access and modified times of the file specified by ``path``. If - ``times`` is ``None``, then the file's access and modified times are set - to the current time. Otherwise, ``times`` must be a 2-tuple of numbers, - of the form ``(atime, mtime)``, which is used to set the access and - modified times, respectively. This bizarre API is mimicked from Python - for the sake of consistency -- I apologize. + Set the access and modified times of the file specified by ``path``. + If ``times`` is ``None``, then the file's access and modified times + are set to the current time. Otherwise, ``times`` must be a 2-tuple + of numbers, of the form ``(atime, mtime)``, which is used to set the + access and modified times, respectively. This bizarre API is mimicked + from Python for the sake of consistency -- I apologize. :param str path: path of the file to modify :param tuple times: @@ -578,7 +591,8 @@ class SFTPClient(BaseSFTP, ClosingContextManager): self._cwd = None return if not stat.S_ISDIR(self.stat(path).st_mode): - raise SFTPError(errno.ENOTDIR, "%s: %s" % (os.strerror(errno.ENOTDIR), path)) + raise SFTPError( + errno.ENOTDIR, "%s: %s" % (os.strerror(errno.ENOTDIR), path)) self._cwd = b(self.normalize(path)) def getcwd(self): @@ -639,7 +653,8 @@ class SFTPClient(BaseSFTP, ClosingContextManager): if confirm: s = self.stat(remotepath) if s.st_size != size: - raise IOError('size mismatch in put! %d != %d' % (s.st_size, size)) + raise IOError( + 'size mismatch in put! %d != %d' % (s.st_size, size)) else: s = SFTPAttributes() return s @@ -663,7 +678,8 @@ class SFTPClient(BaseSFTP, ClosingContextManager): whether to do a stat() on the file afterwards to confirm the file size - :return: an `.SFTPAttributes` object containing attributes about the given file + :return: an `.SFTPAttributes` object containing attributes about the + given file .. versionadded:: 1.4 .. versionchanged:: 1.7.4 @@ -699,7 +715,7 @@ class SFTPClient(BaseSFTP, ClosingContextManager): reader=fr, writer=fl, file_size=file_size, callback=callback ) - return size + return file_size def get(self, remotepath, localpath, callback=None): """ @@ -721,9 +737,10 @@ class SFTPClient(BaseSFTP, ClosingContextManager): size = self.getfo(remotepath, fl, callback) s = os.stat(localpath) if s.st_size != size: - raise IOError('size mismatch in get! %d != %d' % (s.st_size, size)) + raise IOError( + 'size mismatch in get! %d != %d' % (s.st_size, size)) - ### internals... + # ...internals... def _request(self, t, *arg): num = self._async_request(type(None), t, *arg) @@ -745,7 +762,8 @@ class SFTPClient(BaseSFTP, ClosingContextManager): elif isinstance(item, SFTPAttributes): item._pack(msg) else: - raise Exception('unknown type for %r type %r' % (item, type(item))) + raise Exception( + 'unknown type for %r type %r' % (item, type(item))) num = self.request_number self._expecting[num] = fileobj self.request_number += 1 @@ -765,7 +783,8 @@ class SFTPClient(BaseSFTP, ClosingContextManager): self._lock.acquire() try: if num not in self._expecting: - # might be response for a file that was closed before responses came back + # might be response for a file that was closed before + # responses came back self._log(DEBUG, 'Unexpected response #%d' % (num,)) if waitfor is None: # just doing a single check @@ -780,7 +799,7 @@ class SFTPClient(BaseSFTP, ClosingContextManager): if t == CMD_STATUS: self._convert_status(msg) return t, msg - if fileobj is not type(None): + if fileobj is not None: fileobj._async_response(t, msg, num) if waitfor is None: # just doing a single check diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index ce287e8f..6c59b2fa 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -36,13 +36,14 @@ from paramiko.server import SubsystemHandler # known hash algorithms for the "check-file" extension -from paramiko.sftp import CMD_HANDLE, SFTP_DESC, CMD_STATUS, SFTP_EOF, CMD_NAME, \ - SFTP_BAD_MESSAGE, CMD_EXTENDED_REPLY, SFTP_FLAG_READ, SFTP_FLAG_WRITE, \ - SFTP_FLAG_APPEND, SFTP_FLAG_CREATE, SFTP_FLAG_TRUNC, SFTP_FLAG_EXCL, \ - CMD_NAMES, CMD_OPEN, CMD_CLOSE, SFTP_OK, CMD_READ, CMD_DATA, CMD_WRITE, \ - CMD_REMOVE, CMD_RENAME, CMD_MKDIR, CMD_RMDIR, CMD_OPENDIR, CMD_READDIR, \ - CMD_STAT, CMD_ATTRS, CMD_LSTAT, CMD_FSTAT, CMD_SETSTAT, CMD_FSETSTAT, \ - CMD_READLINK, CMD_SYMLINK, CMD_REALPATH, CMD_EXTENDED, SFTP_OP_UNSUPPORTED +from paramiko.sftp import CMD_HANDLE, SFTP_DESC, CMD_STATUS, SFTP_EOF, \ + CMD_NAME, SFTP_BAD_MESSAGE, CMD_EXTENDED_REPLY, SFTP_FLAG_READ, \ + SFTP_FLAG_WRITE, SFTP_FLAG_APPEND, SFTP_FLAG_CREATE, SFTP_FLAG_TRUNC, \ + SFTP_FLAG_EXCL, CMD_NAMES, CMD_OPEN, CMD_CLOSE, SFTP_OK, CMD_READ, \ + CMD_DATA, CMD_WRITE, CMD_REMOVE, CMD_RENAME, CMD_MKDIR, CMD_RMDIR, \ + CMD_OPENDIR, CMD_READDIR, CMD_STAT, CMD_ATTRS, CMD_LSTAT, CMD_FSTAT, \ + CMD_SETSTAT, CMD_FSETSTAT, CMD_READLINK, CMD_SYMLINK, CMD_REALPATH, \ + CMD_EXTENDED, SFTP_OP_UNSUPPORTED _hash_class = { 'sha1': sha1, @@ -57,7 +58,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): Use `.Transport.set_subsystem_handler` to activate this class. """ - def __init__(self, channel, name, server, sftp_si=SFTPServerInterface, *largs, **kwargs): + def __init__(self, channel, name, server, sftp_si=SFTPServerInterface, + *largs, **kwargs): """ The constructor for SFTPServer is meant to be called from within the `.Transport` as a subsystem handler. ``server`` and any additional @@ -86,9 +88,13 @@ class SFTPServer (BaseSFTP, SubsystemHandler): def _log(self, level, msg): if issubclass(type(msg), list): for m in msg: - super(SFTPServer, self)._log(level, "[chan " + self.sock.get_name() + "] " + m) + super(SFTPServer, self)._log( + level, + "[chan " + self.sock.get_name() + "] " + m) else: - super(SFTPServer, self)._log(level, "[chan " + self.sock.get_name() + "] " + msg) + super(SFTPServer, self)._log( + level, + "[chan " + self.sock.get_name() + "] " + msg) def start_subsystem(self, name, transport, channel): self.sock = channel @@ -121,7 +127,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): def finish_subsystem(self): self.server.session_ended() super(SFTPServer, self).finish_subsystem() - # close any file handles that were left open (so we can return them to the OS quickly) + # close any file handles that were left open + # (so we can return them to the OS quickly) for f in self.file_table.values(): f.close() for f in self.folder_table.values(): @@ -175,7 +182,7 @@ class SFTPServer (BaseSFTP, SubsystemHandler): with open(filename, 'w+') as f: f.truncate(attr.st_size) - ### internals... + # ...internals... def _response(self, request_number, t, *arg): msg = Message() @@ -190,7 +197,9 @@ class SFTPServer (BaseSFTP, SubsystemHandler): elif type(item) is SFTPAttributes: item._pack(msg) else: - raise Exception('unknown type for ' + repr(item) + ' type ' + repr(type(item))) + raise Exception( + 'unknown type for {!r} type {!r}'.format( + item, type(item))) self._send_packet(t, msg) def _send_handle_response(self, request_number, handle, folder=False): @@ -212,7 +221,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): desc = SFTP_DESC[code] except IndexError: desc = 'Unknown' - # some clients expect a "langauge" tag at the end (but don't mind it being blank) + # some clients expect a "langauge" tag at the end + # (but don't mind it being blank) self._response(request_number, CMD_STATUS, code, desc, '') def _open_folder(self, request_number, path): @@ -251,7 +261,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): length = msg.get_int64() block_size = msg.get_int() if handle not in self.file_table: - self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') + self._send_status( + request_number, SFTP_BAD_MESSAGE, 'Invalid handle') return f = self.file_table[handle] for x in alg_list: @@ -260,7 +271,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): alg = _hash_class[x] break else: - self._send_status(request_number, SFTP_FAILURE, 'No supported hash types found') + self._send_status( + request_number, SFTP_FAILURE, 'No supported hash types found') return if length == 0: st = f.stat() @@ -271,7 +283,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): if block_size == 0: block_size = length if block_size < 256: - self._send_status(request_number, SFTP_FAILURE, 'Block size too small') + self._send_status( + request_number, SFTP_FAILURE, 'Block size too small') return sum_out = bytes() @@ -285,7 +298,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): while count < blocklen: data = f.read(offset, chunklen) if not isinstance(data, bytes_types): - self._send_status(request_number, data, 'Unable to hash file') + self._send_status( + request_number, data, 'Unable to hash file') return hash_obj.update(data) count += len(data) @@ -323,7 +337,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): path = msg.get_text() flags = self._convert_pflags(msg.get_int()) attr = SFTPAttributes._from_msg(msg) - self._send_handle_response(request_number, self.server.open(path, flags, attr)) + self._send_handle_response( + request_number, self.server.open(path, flags, attr)) elif t == CMD_CLOSE: handle = msg.get_binary() if handle in self.folder_table: @@ -335,13 +350,15 @@ class SFTPServer (BaseSFTP, SubsystemHandler): del self.file_table[handle] self._send_status(request_number, SFTP_OK) return - self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') + self._send_status( + request_number, SFTP_BAD_MESSAGE, 'Invalid handle') elif t == CMD_READ: handle = msg.get_binary() offset = msg.get_int64() length = msg.get_int() if handle not in self.file_table: - self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') + self._send_status( + request_number, SFTP_BAD_MESSAGE, 'Invalid handle') return data = self.file_table[handle].read(offset, length) if isinstance(data, (bytes_types, string_types)): @@ -356,16 +373,19 @@ class SFTPServer (BaseSFTP, SubsystemHandler): offset = msg.get_int64() data = msg.get_binary() if handle not in self.file_table: - self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') + self._send_status( + request_number, SFTP_BAD_MESSAGE, 'Invalid handle') return - self._send_status(request_number, self.file_table[handle].write(offset, data)) + self._send_status( + request_number, self.file_table[handle].write(offset, data)) elif t == CMD_REMOVE: path = msg.get_text() self._send_status(request_number, self.server.remove(path)) elif t == CMD_RENAME: oldpath = msg.get_text() newpath = msg.get_text() - self._send_status(request_number, self.server.rename(oldpath, newpath)) + self._send_status( + request_number, self.server.rename(oldpath, newpath)) elif t == CMD_MKDIR: path = msg.get_text() attr = SFTPAttributes._from_msg(msg) @@ -380,7 +400,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): elif t == CMD_READDIR: handle = msg.get_binary() if handle not in self.folder_table: - self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') + self._send_status( + request_number, SFTP_BAD_MESSAGE, 'Invalid handle') return folder = self.folder_table[handle] self._read_folder(request_number, folder) @@ -401,7 +422,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler): elif t == CMD_FSTAT: handle = msg.get_binary() if handle not in self.file_table: - self._send_status(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') + self._send_status( + request_number, SFTP_BAD_MESSAGE, 'Invalid handle') return resp = self.file_table[handle].stat() if issubclass(type(resp), SFTPAttributes): @@ -416,25 +438,31 @@ class SFTPServer (BaseSFTP, SubsystemHandler): handle = msg.get_binary() attr = SFTPAttributes._from_msg(msg) if handle not in self.file_table: - self._response(request_number, SFTP_BAD_MESSAGE, 'Invalid handle') + self._response( + request_number, SFTP_BAD_MESSAGE, 'Invalid handle') return - self._send_status(request_number, self.file_table[handle].chattr(attr)) + self._send_status( + request_number, self.file_table[handle].chattr(attr)) elif t == CMD_READLINK: path = msg.get_text() resp = self.server.readlink(path) if isinstance(resp, (bytes_types, string_types)): - self._response(request_number, CMD_NAME, 1, resp, '', SFTPAttributes()) + self._response( + request_number, CMD_NAME, 1, resp, '', SFTPAttributes()) else: self._send_status(request_number, resp) elif t == CMD_SYMLINK: - # the sftp 2 draft is incorrect here! path always follows target_path + # the sftp 2 draft is incorrect here! + # path always follows target_path target_path = msg.get_text() path = msg.get_text() - self._send_status(request_number, self.server.symlink(target_path, path)) + self._send_status( + request_number, self.server.symlink(target_path, path)) elif t == CMD_REALPATH: path = msg.get_text() rpath = self.server.canonicalize(path) - self._response(request_number, CMD_NAME, 1, rpath, '', SFTPAttributes()) + self._response( + request_number, CMD_NAME, 1, rpath, '', SFTPAttributes()) elif t == CMD_EXTENDED: tag = msg.get_text() if tag == 'check-file': -- cgit v1.2.3 From 991884faa5ad49ecd07472c67d9e49a8dd464f40 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Tue, 30 May 2017 12:26:26 -0400 Subject: Final paramiko flake8 fix. --- paramiko/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/paramiko/__init__.py b/paramiko/__init__.py index 5c1f38b3..3aca010f 100644 --- a/paramiko/__init__.py +++ b/paramiko/__init__.py @@ -16,6 +16,7 @@ # along with Paramiko; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. +# flake8: noqa import sys from paramiko._version import __version__, __version_info__ -- cgit v1.2.3 From 7fb89cf56da37d906595bb6dd9c88250fcfc6b25 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Tue, 30 May 2017 12:27:54 -0400 Subject: Final paramiko flake8 fix. --- paramiko/transport.py | 1 + setup.cfg | 2 +- test.py | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/paramiko/transport.py b/paramiko/transport.py index 914166d7..1b1ebec7 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -16,6 +16,7 @@ # along with Paramiko; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. +# flake8: noqa """ Core protocol implementation """ diff --git a/setup.cfg b/setup.cfg index 764803e0..ff099a69 100644 --- a/setup.cfg +++ b/setup.cfg @@ -5,6 +5,6 @@ universal = 1 omit = paramiko/_winapi.py [flake8] -exclude = sites,.git,build,dist,alt_env,appveyor,demos,tests,test.py,transport.py +exclude = sites,.git,build,dist,alt_env,appveyor,demos,tests ignore = E124,E125,E128,E261,E301,E302,E303,E402 max-line-length = 79 diff --git a/test.py b/test.py index a1f13d85..7849c149 100755 --- a/test.py +++ b/test.py @@ -22,6 +22,7 @@ do the unit tests! """ +# flake8: noqa import os import re import sys -- cgit v1.2.3 From 7edff286649906a64af97c9a38cdcc9240c86527 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Tue, 30 May 2017 13:38:40 -0400 Subject: Fix failing tests and explain the unusual code. --- paramiko/sftp_client.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 245e5072..719757c5 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -799,7 +799,10 @@ class SFTPClient(BaseSFTP, ClosingContextManager): if t == CMD_STATUS: self._convert_status(msg) return t, msg - if fileobj is not None: + + # can not rewrite this to deal with E721, either as a None check + # nor as not an instance of None or NoneType + if fileobj is not type(None): # noqa fileobj._async_response(t, msg, num) if waitfor is None: # just doing a single check -- cgit v1.2.3 From 5a9f4f71de9c0b6046fdf18c61dbb82ede199c66 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Tue, 30 May 2017 14:32:47 -0400 Subject: Fix documentation build failure. --- paramiko/server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paramiko/server.py b/paramiko/server.py index bb244efe..89278a82 100644 --- a/paramiko/server.py +++ b/paramiko/server.py @@ -688,7 +688,7 @@ class SubsystemHandler (threading.Thread): :param str name: name of the requested subsystem. :param .Transport transport: the server-mode `.Transport`. :param .Channel channel: the channel associated with this subsystem - request. + request. """ pass -- cgit v1.2.3 From b8ff9af8b3019ba1ba0c59a2c1414c818ea46e44 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Tue, 30 May 2017 14:37:02 -0400 Subject: Fix documentation build failure. --- paramiko/sftp_handle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paramiko/sftp_handle.py b/paramiko/sftp_handle.py index bcb4bcba..2d2e621c 100644 --- a/paramiko/sftp_handle.py +++ b/paramiko/sftp_handle.py @@ -43,7 +43,7 @@ class SFTPHandle (ClosingContextManager): is open in append mode. :param int flags: optional flags as passed to - `.SFTPServerInterface.open` + `.SFTPServerInterface.open` """ self.__flags = flags self.__name = None -- cgit v1.2.3 From e2497717d25280ab49d0ce6ce54b52cf47451923 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Wed, 31 May 2017 17:19:53 -0700 Subject: flake8 fixes for 2.0 branch --- paramiko/message.py | 1 - paramiko/util.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/paramiko/message.py b/paramiko/message.py index 309b8612..02af2526 100644 --- a/paramiko/message.py +++ b/paramiko/message.py @@ -184,7 +184,6 @@ class Message (object): @rtype: string """ return u(self.get_bytes(self.get_int())) - #return self.get_bytes(self.get_size()) def get_binary(self): """ diff --git a/paramiko/util.py b/paramiko/util.py index f6885c60..82157c24 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -98,7 +98,7 @@ def format_binary(data, prefix=''): x += 16 if x < len(data): out.append(format_binary_line(data[x:])) - return [prefix + x for x in out] + return [prefix + line for line in out] def format_binary_line(data): -- cgit v1.2.3 From 9d5760cf45619ce5aacb567fdc42849d678d93eb Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Wed, 31 May 2017 17:30:17 -0700 Subject: Additional house style formatting tweaks, mostly re: removal of line continuations --- paramiko/__init__.py | 31 +++++++++++++++++++------------ paramiko/auth_handler.py | 34 ++++++++++++++++++---------------- paramiko/channel.py | 17 +++++++++++------ paramiko/file.py | 5 +++-- paramiko/packet.py | 6 ++++-- paramiko/sftp_client.py | 20 +++++++++++--------- paramiko/sftp_file.py | 6 ++++-- paramiko/sftp_server.py | 22 ++++++++++++---------- paramiko/transport.py | 26 ++++++++++++++------------ tests/stub_sftp.py | 6 ++++-- tests/test_auth.py | 7 ++++--- tests/test_transport.py | 13 ++++++++----- tests/test_util.py | 7 ++++--- 13 files changed, 116 insertions(+), 84 deletions(-) diff --git a/paramiko/__init__.py b/paramiko/__init__.py index 3aca010f..197f519a 100644 --- a/paramiko/__init__.py +++ b/paramiko/__init__.py @@ -29,14 +29,18 @@ __license__ = "GNU Lesser General Public License (LGPL)" from paramiko.transport import SecurityOptions, Transport -from paramiko.client import SSHClient, MissingHostKeyPolicy, AutoAddPolicy, \ - RejectPolicy, WarningPolicy +from paramiko.client import ( + SSHClient, MissingHostKeyPolicy, AutoAddPolicy, RejectPolicy, + WarningPolicy, +) from paramiko.auth_handler import AuthHandler from paramiko.ssh_gss import GSSAuth, GSS_AUTH_AVAILABLE from paramiko.channel import Channel, ChannelFile -from paramiko.ssh_exception import SSHException, PasswordRequiredException, \ - BadAuthenticationType, ChannelException, BadHostKeyException, \ - AuthenticationException, ProxyCommandFailure +from paramiko.ssh_exception import ( + SSHException, PasswordRequiredException, BadAuthenticationType, + ChannelException, BadHostKeyException, AuthenticationException, + ProxyCommandFailure, +) from paramiko.server import ServerInterface, SubsystemHandler, InteractiveQuery from paramiko.rsakey import RSAKey from paramiko.dsskey import DSSKey @@ -57,14 +61,17 @@ from paramiko.hostkeys import HostKeys from paramiko.config import SSHConfig from paramiko.proxy import ProxyCommand -from paramiko.common import AUTH_SUCCESSFUL, AUTH_PARTIALLY_SUCCESSFUL, \ - AUTH_FAILED, OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED, \ - OPEN_FAILED_CONNECT_FAILED, OPEN_FAILED_UNKNOWN_CHANNEL_TYPE, \ - OPEN_FAILED_RESOURCE_SHORTAGE +from paramiko.common import ( + AUTH_SUCCESSFUL, AUTH_PARTIALLY_SUCCESSFUL, AUTH_FAILED, OPEN_SUCCEEDED, + OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED, OPEN_FAILED_CONNECT_FAILED, + OPEN_FAILED_UNKNOWN_CHANNEL_TYPE, OPEN_FAILED_RESOURCE_SHORTAGE, +) -from paramiko.sftp import SFTP_OK, SFTP_EOF, SFTP_NO_SUCH_FILE, \ - SFTP_PERMISSION_DENIED, SFTP_FAILURE, SFTP_BAD_MESSAGE, \ - SFTP_NO_CONNECTION, SFTP_CONNECTION_LOST, SFTP_OP_UNSUPPORTED +from paramiko.sftp import ( + SFTP_OK, SFTP_EOF, SFTP_NO_SUCH_FILE, SFTP_PERMISSION_DENIED, SFTP_FAILURE, + SFTP_BAD_MESSAGE, SFTP_NO_CONNECTION, SFTP_CONNECTION_LOST, + SFTP_OP_UNSUPPORTED, +) from paramiko.common import io_sleep diff --git a/paramiko/auth_handler.py b/paramiko/auth_handler.py index ace79638..83c5a575 100644 --- a/paramiko/auth_handler.py +++ b/paramiko/auth_handler.py @@ -21,25 +21,27 @@ """ import weakref -from paramiko.common import cMSG_SERVICE_REQUEST, cMSG_DISCONNECT, \ - DISCONNECT_SERVICE_NOT_AVAILABLE, \ - DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE, \ - cMSG_USERAUTH_REQUEST, cMSG_SERVICE_ACCEPT, DEBUG, AUTH_SUCCESSFUL, INFO, \ - cMSG_USERAUTH_SUCCESS, cMSG_USERAUTH_FAILURE, AUTH_PARTIALLY_SUCCESSFUL, \ - cMSG_USERAUTH_INFO_REQUEST, WARNING, AUTH_FAILED, cMSG_USERAUTH_PK_OK, \ - cMSG_USERAUTH_INFO_RESPONSE, MSG_SERVICE_REQUEST, MSG_SERVICE_ACCEPT, \ - MSG_USERAUTH_REQUEST, MSG_USERAUTH_SUCCESS, MSG_USERAUTH_FAILURE, \ - MSG_USERAUTH_BANNER, MSG_USERAUTH_INFO_REQUEST, \ - MSG_USERAUTH_INFO_RESPONSE, \ - cMSG_USERAUTH_GSSAPI_RESPONSE, cMSG_USERAUTH_GSSAPI_TOKEN, \ - cMSG_USERAUTH_GSSAPI_MIC, MSG_USERAUTH_GSSAPI_RESPONSE, \ - MSG_USERAUTH_GSSAPI_TOKEN, MSG_USERAUTH_GSSAPI_ERROR, \ - MSG_USERAUTH_GSSAPI_ERRTOK, MSG_USERAUTH_GSSAPI_MIC, MSG_NAMES +from paramiko.common import ( + cMSG_SERVICE_REQUEST, cMSG_DISCONNECT, DISCONNECT_SERVICE_NOT_AVAILABLE, + DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE, cMSG_USERAUTH_REQUEST, + cMSG_SERVICE_ACCEPT, DEBUG, AUTH_SUCCESSFUL, INFO, cMSG_USERAUTH_SUCCESS, + cMSG_USERAUTH_FAILURE, AUTH_PARTIALLY_SUCCESSFUL, + cMSG_USERAUTH_INFO_REQUEST, WARNING, AUTH_FAILED, cMSG_USERAUTH_PK_OK, + cMSG_USERAUTH_INFO_RESPONSE, MSG_SERVICE_REQUEST, MSG_SERVICE_ACCEPT, + MSG_USERAUTH_REQUEST, MSG_USERAUTH_SUCCESS, MSG_USERAUTH_FAILURE, + MSG_USERAUTH_BANNER, MSG_USERAUTH_INFO_REQUEST, MSG_USERAUTH_INFO_RESPONSE, + cMSG_USERAUTH_GSSAPI_RESPONSE, cMSG_USERAUTH_GSSAPI_TOKEN, + cMSG_USERAUTH_GSSAPI_MIC, MSG_USERAUTH_GSSAPI_RESPONSE, + MSG_USERAUTH_GSSAPI_TOKEN, MSG_USERAUTH_GSSAPI_ERROR, + MSG_USERAUTH_GSSAPI_ERRTOK, MSG_USERAUTH_GSSAPI_MIC, MSG_NAMES, +) from paramiko.message import Message from paramiko.py3compat import bytestring -from paramiko.ssh_exception import SSHException, AuthenticationException, \ - BadAuthenticationType, PartialAuthentication +from paramiko.ssh_exception import ( + SSHException, AuthenticationException, BadAuthenticationType, + PartialAuthentication, +) from paramiko.server import InteractiveQuery from paramiko.ssh_gss import GSSAuth diff --git a/paramiko/channel.py b/paramiko/channel.py index ed03a813..1bd7969d 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -28,10 +28,11 @@ import threading from functools import wraps from paramiko import util -from paramiko.common import cMSG_CHANNEL_REQUEST, cMSG_CHANNEL_WINDOW_ADJUST, \ - cMSG_CHANNEL_DATA, cMSG_CHANNEL_EXTENDED_DATA, DEBUG, ERROR, \ - cMSG_CHANNEL_SUCCESS, cMSG_CHANNEL_FAILURE, cMSG_CHANNEL_EOF, \ - cMSG_CHANNEL_CLOSE +from paramiko.common import ( + cMSG_CHANNEL_REQUEST, cMSG_CHANNEL_WINDOW_ADJUST, cMSG_CHANNEL_DATA, + cMSG_CHANNEL_EXTENDED_DATA, DEBUG, ERROR, cMSG_CHANNEL_SUCCESS, + cMSG_CHANNEL_FAILURE, cMSG_CHANNEL_EOF, cMSG_CHANNEL_CLOSE, +) from paramiko.message import Message from paramiko.py3compat import bytes_types from paramiko.ssh_exception import SSHException @@ -50,8 +51,12 @@ def open_only(func): """ @wraps(func) def _check(self, *args, **kwds): - if self.closed or self.eof_received or self.eof_sent or \ - not self.active: + if ( + self.closed or + self.eof_received or + self.eof_sent or + not self.active + ): raise SSHException('Channel is not open') return func(self, *args, **kwds) return _check diff --git a/paramiko/file.py b/paramiko/file.py index ab95c063..92bf1f14 100644 --- a/paramiko/file.py +++ b/paramiko/file.py @@ -15,8 +15,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with Paramiko; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. -from paramiko.common import linefeed_byte_value, crlf, cr_byte, \ - linefeed_byte, cr_byte_value +from paramiko.common import ( + linefeed_byte_value, crlf, cr_byte, linefeed_byte, cr_byte_value, +) from paramiko.py3compat import BytesIO, PY2, u, b, bytes_types from paramiko.util import ClosingContextManager diff --git a/paramiko/packet.py b/paramiko/packet.py index cae64727..2d36adc8 100644 --- a/paramiko/packet.py +++ b/paramiko/packet.py @@ -29,8 +29,10 @@ import time from hmac import HMAC from paramiko import util -from paramiko.common import linefeed_byte, cr_byte_value, asbytes, MSG_NAMES, \ - DEBUG, xffffffff, zero_byte +from paramiko.common import ( + linefeed_byte, cr_byte_value, asbytes, MSG_NAMES, DEBUG, xffffffff, + zero_byte, +) from paramiko.py3compat import u, byte_ord from paramiko.ssh_exception import SSHException, ProxyCommandFailure from paramiko.message import Message diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index 719757c5..f3895caf 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -28,15 +28,17 @@ from paramiko import util from paramiko.channel import Channel from paramiko.message import Message from paramiko.common import INFO, DEBUG, o777 -from paramiko.py3compat import bytestring, b, u, long, string_types, \ - bytes_types -from paramiko.sftp import BaseSFTP, CMD_OPENDIR, CMD_HANDLE, SFTPError, \ - CMD_READDIR, CMD_NAME, CMD_CLOSE, SFTP_FLAG_READ, SFTP_FLAG_WRITE, \ - SFTP_FLAG_CREATE, SFTP_FLAG_TRUNC, SFTP_FLAG_APPEND, SFTP_FLAG_EXCL, \ - CMD_OPEN, CMD_REMOVE, CMD_RENAME, CMD_MKDIR, CMD_RMDIR, CMD_STAT, \ - CMD_ATTRS, CMD_LSTAT, CMD_SYMLINK, CMD_SETSTAT, CMD_READLINK, \ - CMD_REALPATH, CMD_STATUS, SFTP_OK, SFTP_EOF, SFTP_NO_SUCH_FILE, \ - SFTP_PERMISSION_DENIED +from paramiko.py3compat import ( + bytestring, b, u, long, string_types, bytes_types, +) +from paramiko.sftp import ( + BaseSFTP, CMD_OPENDIR, CMD_HANDLE, SFTPError, CMD_READDIR, CMD_NAME, + CMD_CLOSE, SFTP_FLAG_READ, SFTP_FLAG_WRITE, SFTP_FLAG_CREATE, + SFTP_FLAG_TRUNC, SFTP_FLAG_APPEND, SFTP_FLAG_EXCL, CMD_OPEN, CMD_REMOVE, + CMD_RENAME, CMD_MKDIR, CMD_RMDIR, CMD_STAT, CMD_ATTRS, CMD_LSTAT, + CMD_SYMLINK, CMD_SETSTAT, CMD_READLINK, CMD_REALPATH, CMD_STATUS, SFTP_OK, + SFTP_EOF, SFTP_NO_SUCH_FILE, SFTP_PERMISSION_DENIED, +) from paramiko.sftp_attr import SFTPAttributes from paramiko.ssh_exception import SSHException diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py index 9ebaaf66..13e28e28 100644 --- a/paramiko/sftp_file.py +++ b/paramiko/sftp_file.py @@ -31,8 +31,10 @@ from paramiko.common import DEBUG from paramiko.file import BufferedFile from paramiko.py3compat import long -from paramiko.sftp import CMD_CLOSE, CMD_READ, CMD_DATA, SFTPError,\ - CMD_WRITE, CMD_STATUS, CMD_FSTAT, CMD_ATTRS, CMD_FSETSTAT, CMD_EXTENDED +from paramiko.sftp import ( + CMD_CLOSE, CMD_READ, CMD_DATA, SFTPError, CMD_WRITE, CMD_STATUS, CMD_FSTAT, + CMD_ATTRS, CMD_FSETSTAT, CMD_EXTENDED, +) from paramiko.sftp_attr import SFTPAttributes diff --git a/paramiko/sftp_server.py b/paramiko/sftp_server.py index 6c59b2fa..d7b46e41 100644 --- a/paramiko/sftp_server.py +++ b/paramiko/sftp_server.py @@ -26,8 +26,9 @@ import sys from hashlib import md5, sha1 from paramiko import util -from paramiko.sftp import BaseSFTP, Message, SFTP_FAILURE, \ - SFTP_PERMISSION_DENIED, SFTP_NO_SUCH_FILE +from paramiko.sftp import ( + BaseSFTP, Message, SFTP_FAILURE, SFTP_PERMISSION_DENIED, SFTP_NO_SUCH_FILE, +) from paramiko.sftp_si import SFTPServerInterface from paramiko.sftp_attr import SFTPAttributes from paramiko.common import DEBUG @@ -36,14 +37,15 @@ from paramiko.server import SubsystemHandler # known hash algorithms for the "check-file" extension -from paramiko.sftp import CMD_HANDLE, SFTP_DESC, CMD_STATUS, SFTP_EOF, \ - CMD_NAME, SFTP_BAD_MESSAGE, CMD_EXTENDED_REPLY, SFTP_FLAG_READ, \ - SFTP_FLAG_WRITE, SFTP_FLAG_APPEND, SFTP_FLAG_CREATE, SFTP_FLAG_TRUNC, \ - SFTP_FLAG_EXCL, CMD_NAMES, CMD_OPEN, CMD_CLOSE, SFTP_OK, CMD_READ, \ - CMD_DATA, CMD_WRITE, CMD_REMOVE, CMD_RENAME, CMD_MKDIR, CMD_RMDIR, \ - CMD_OPENDIR, CMD_READDIR, CMD_STAT, CMD_ATTRS, CMD_LSTAT, CMD_FSTAT, \ - CMD_SETSTAT, CMD_FSETSTAT, CMD_READLINK, CMD_SYMLINK, CMD_REALPATH, \ - CMD_EXTENDED, SFTP_OP_UNSUPPORTED +from paramiko.sftp import ( + CMD_HANDLE, SFTP_DESC, CMD_STATUS, SFTP_EOF, CMD_NAME, SFTP_BAD_MESSAGE, + CMD_EXTENDED_REPLY, SFTP_FLAG_READ, SFTP_FLAG_WRITE, SFTP_FLAG_APPEND, + SFTP_FLAG_CREATE, SFTP_FLAG_TRUNC, SFTP_FLAG_EXCL, CMD_NAMES, CMD_OPEN, + CMD_CLOSE, SFTP_OK, CMD_READ, CMD_DATA, CMD_WRITE, CMD_REMOVE, CMD_RENAME, + CMD_MKDIR, CMD_RMDIR, CMD_OPENDIR, CMD_READDIR, CMD_STAT, CMD_ATTRS, + CMD_LSTAT, CMD_FSTAT, CMD_SETSTAT, CMD_FSETSTAT, CMD_READLINK, CMD_SYMLINK, + CMD_REALPATH, CMD_EXTENDED, SFTP_OP_UNSUPPORTED, +) _hash_class = { 'sha1': sha1, diff --git a/paramiko/transport.py b/paramiko/transport.py index 1b1ebec7..0a570977 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -38,18 +38,20 @@ from paramiko import util from paramiko.auth_handler import AuthHandler from paramiko.ssh_gss import GSSAuth from paramiko.channel import Channel -from paramiko.common import xffffffff, cMSG_CHANNEL_OPEN, cMSG_IGNORE, \ - cMSG_GLOBAL_REQUEST, DEBUG, MSG_KEXINIT, MSG_IGNORE, MSG_DISCONNECT, \ - MSG_DEBUG, ERROR, WARNING, cMSG_UNIMPLEMENTED, INFO, cMSG_KEXINIT, \ - cMSG_NEWKEYS, MSG_NEWKEYS, cMSG_REQUEST_SUCCESS, cMSG_REQUEST_FAILURE, \ - CONNECTION_FAILED_CODE, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED, \ - OPEN_SUCCEEDED, cMSG_CHANNEL_OPEN_FAILURE, cMSG_CHANNEL_OPEN_SUCCESS, \ - MSG_GLOBAL_REQUEST, MSG_REQUEST_SUCCESS, MSG_REQUEST_FAILURE, \ - MSG_CHANNEL_OPEN_SUCCESS, MSG_CHANNEL_OPEN_FAILURE, MSG_CHANNEL_OPEN, \ - MSG_CHANNEL_SUCCESS, MSG_CHANNEL_FAILURE, MSG_CHANNEL_DATA, \ - MSG_CHANNEL_EXTENDED_DATA, MSG_CHANNEL_WINDOW_ADJUST, MSG_CHANNEL_REQUEST, \ - MSG_CHANNEL_EOF, MSG_CHANNEL_CLOSE, MIN_WINDOW_SIZE, MIN_PACKET_SIZE, \ - MAX_WINDOW_SIZE, DEFAULT_WINDOW_SIZE, DEFAULT_MAX_PACKET_SIZE +from paramiko.common import ( + xffffffff, cMSG_CHANNEL_OPEN, cMSG_IGNORE, cMSG_GLOBAL_REQUEST, DEBUG, + MSG_KEXINIT, MSG_IGNORE, MSG_DISCONNECT, MSG_DEBUG, ERROR, WARNING, + cMSG_UNIMPLEMENTED, INFO, cMSG_KEXINIT, cMSG_NEWKEYS, MSG_NEWKEYS, + cMSG_REQUEST_SUCCESS, cMSG_REQUEST_FAILURE, CONNECTION_FAILED_CODE, + OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED, OPEN_SUCCEEDED, + cMSG_CHANNEL_OPEN_FAILURE, cMSG_CHANNEL_OPEN_SUCCESS, MSG_GLOBAL_REQUEST, + MSG_REQUEST_SUCCESS, MSG_REQUEST_FAILURE, MSG_CHANNEL_OPEN_SUCCESS, + MSG_CHANNEL_OPEN_FAILURE, MSG_CHANNEL_OPEN, MSG_CHANNEL_SUCCESS, + MSG_CHANNEL_FAILURE, MSG_CHANNEL_DATA, MSG_CHANNEL_EXTENDED_DATA, + MSG_CHANNEL_WINDOW_ADJUST, MSG_CHANNEL_REQUEST, MSG_CHANNEL_EOF, + MSG_CHANNEL_CLOSE, MIN_WINDOW_SIZE, MIN_PACKET_SIZE, MAX_WINDOW_SIZE, + DEFAULT_WINDOW_SIZE, DEFAULT_MAX_PACKET_SIZE, +) from paramiko.compress import ZlibCompressor, ZlibDecompressor from paramiko.dsskey import DSSKey from paramiko.kex_gex import KexGex, KexGexSHA256 diff --git a/tests/stub_sftp.py b/tests/stub_sftp.py index 5fcca386..334af561 100644 --- a/tests/stub_sftp.py +++ b/tests/stub_sftp.py @@ -22,8 +22,10 @@ A stub SFTP server for loopback SFTP testing. import os import sys -from paramiko import ServerInterface, SFTPServerInterface, SFTPServer, SFTPAttributes, \ - SFTPHandle, SFTP_OK, AUTH_SUCCESSFUL, OPEN_SUCCEEDED +from paramiko import ( + ServerInterface, SFTPServerInterface, SFTPServer, SFTPAttributes, + SFTPHandle, SFTP_OK, AUTH_SUCCESSFUL, OPEN_SUCCEEDED, +) from paramiko.common import o666 diff --git a/tests/test_auth.py b/tests/test_auth.py index 23517790..96f7611c 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -24,9 +24,10 @@ import sys import threading import unittest -from paramiko import Transport, ServerInterface, RSAKey, DSSKey, \ - BadAuthenticationType, InteractiveQuery, \ - AuthenticationException +from paramiko import ( + Transport, ServerInterface, RSAKey, DSSKey, BadAuthenticationType, + InteractiveQuery, AuthenticationException, +) from paramiko import AUTH_FAILED, AUTH_PARTIALLY_SUCCESSFUL, AUTH_SUCCESSFUL from paramiko.py3compat import u from tests.loop import LoopSocket diff --git a/tests/test_transport.py b/tests/test_transport.py index d81ad8f3..2ebdf854 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -31,13 +31,16 @@ import random from hashlib import sha1 import unittest -from paramiko import Transport, SecurityOptions, ServerInterface, RSAKey, DSSKey, \ - SSHException, ChannelException, Packetizer +from paramiko import ( + Transport, SecurityOptions, ServerInterface, RSAKey, DSSKey, SSHException, + ChannelException, Packetizer, +) from paramiko import AUTH_FAILED, AUTH_SUCCESSFUL from paramiko import OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED -from paramiko.common import MSG_KEXINIT, cMSG_CHANNEL_WINDOW_ADJUST, \ - MIN_PACKET_SIZE, MIN_WINDOW_SIZE, MAX_WINDOW_SIZE, \ - DEFAULT_WINDOW_SIZE, DEFAULT_MAX_PACKET_SIZE +from paramiko.common import ( + MSG_KEXINIT, cMSG_CHANNEL_WINDOW_ADJUST, MIN_PACKET_SIZE, MIN_WINDOW_SIZE, + MAX_WINDOW_SIZE, DEFAULT_WINDOW_SIZE, DEFAULT_MAX_PACKET_SIZE, +) from paramiko.py3compat import bytes from paramiko.message import Message from tests.loop import LoopSocket diff --git a/tests/test_util.py b/tests/test_util.py index a31e4507..7880e156 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -475,9 +475,10 @@ Host param3 parara safe_has_bytes = safe_string(has_bytes) expected_bytes = b("has %07%03 bytes") err = "{0!r} != {1!r}" - assert safe_vanilla == vanilla, err.format(safe_vanilla, vanilla) - assert safe_has_bytes == expected_bytes, \ - err.format(safe_has_bytes, expected_bytes) + msg = err.format(safe_vanilla, vanilla) + assert safe_vanilla == vanilla, msg + msg = err.format(safe_has_bytes, expected_bytes) + assert safe_has_bytes == expected_bytes, msg def test_proxycommand_none_issue_418(self): test_config_file = """ -- cgit v1.2.3 From f95f0eb1fb82da7ead513dac352ec92712d68e24 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Wed, 31 May 2017 18:02:09 -0700 Subject: Seriously, folks? Refactor some silly crap. --- paramiko/packet.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/paramiko/packet.py b/paramiko/packet.py index 2d36adc8..6f3cd4e2 100644 --- a/paramiko/packet.py +++ b/paramiko/packet.py @@ -46,6 +46,13 @@ class NeedRekeyException (Exception): pass +def first_arg(e): + arg = None + if type(e.args) is tuple and len(e.args) > 0: + arg = e.args[0] + return arg + + class Packetizer (object): """ Implementation of the base SSH packet protocol. @@ -271,11 +278,10 @@ class Packetizer (object): # on Linux, sometimes instead of socket.timeout, we get # EAGAIN. this is a bug in recent (> 2.6.9) kernels but # we need to work around it. - if (type(e.args) is tuple) and (len(e.args) > 0) and \ - (e.args[0] == errno.EAGAIN): + arg = first_arg(e) + if arg == errno.EAGAIN: got_timeout = True - elif (type(e.args) is tuple) and (len(e.args) > 0) and \ - (e.args[0] == errno.EINTR): + elif arg == errno.EINTR: # syscall interrupted; try again pass elif self.__closed: @@ -300,11 +306,10 @@ class Packetizer (object): except socket.timeout: retry_write = True except socket.error as e: - if (type(e.args) is tuple) and (len(e.args) > 0) and \ - (e.args[0] == errno.EAGAIN): + arg = first_arg(e) + if arg == errno.EAGAIN: retry_write = True - elif (type(e.args) is tuple) and (len(e.args) > 0) and \ - (e.args[0] == errno.EINTR): + elif arg == errno.EINTR: # syscall interrupted; try again retry_write = True else: @@ -522,8 +527,7 @@ class Packetizer (object): except socket.timeout: pass except EnvironmentError as e: - if (type(e.args) is tuple and len(e.args) > 0 and - e.args[0] == errno.EINTR): + if first_arg(e) == errno.EINTR: pass else: raise -- cgit v1.2.3 From 25de2a02048897752df356189017b22a49d37f88 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Wed, 31 May 2017 18:03:12 -0700 Subject: More death to line continuations --- demos/forward.py | 2 +- demos/rforward.py | 2 +- paramiko/agent.py | 10 +++++++--- paramiko/auth_handler.py | 6 ++++-- paramiko/channel.py | 5 +++-- paramiko/common.py | 5 +++-- paramiko/config.py | 10 ++++++---- paramiko/dsskey.py | 7 +++++-- paramiko/file.py | 32 +++++++++++++++++++++----------- paramiko/hostkeys.py | 10 ++++++---- paramiko/kex_gss.py | 4 ++-- 11 files changed, 59 insertions(+), 34 deletions(-) diff --git a/demos/forward.py b/demos/forward.py index 96e1700d..c478e217 100644 --- a/demos/forward.py +++ b/demos/forward.py @@ -105,7 +105,7 @@ def verbose(s): print(s) -HELP = """\ +HELP = """ Set up a forward tunnel across an SSH server, using paramiko. A local port (given with -p) is forwarded across an SSH session to an address:port from the SSH server. This is similar to the openssh -L option. diff --git a/demos/rforward.py b/demos/rforward.py index ae70670c..d015cebb 100755 --- a/demos/rforward.py +++ b/demos/rforward.py @@ -85,7 +85,7 @@ def verbose(s): print(s) -HELP = """\ +HELP = """ Set up a reverse forwarding tunnel across an SSH server, using paramiko. A port on the SSH server (given with -p) is forwarded across an SSH session back to the local machine, and out to a remote site reachable from this diff --git a/paramiko/agent.py b/paramiko/agent.py index c950ba5e..a7cab4d8 100644 --- a/paramiko/agent.py +++ b/paramiko/agent.py @@ -115,9 +115,13 @@ class AgentProxyThread(threading.Thread): # The address should be an IP address as a string? or None self.__addr = addr self._agent.connect() - if not isinstance(self._agent, int) and \ - (self._agent._conn is None or - not hasattr(self._agent._conn, 'fileno')): + if ( + not isinstance(self._agent, int) and + ( + self._agent._conn is None or + not hasattr(self._agent._conn, 'fileno') + ) + ): raise AuthenticationException("Unable to connect to SSH agent") self._communicate() except: diff --git a/paramiko/auth_handler.py b/paramiko/auth_handler.py index 83c5a575..2de71241 100644 --- a/paramiko/auth_handler.py +++ b/paramiko/auth_handler.py @@ -313,8 +313,10 @@ class AuthHandler (object): else: raise SSHException( "Received Package: %s" % MSG_NAMES[ptype]) - elif self.auth_method == 'gssapi-keyex' and\ - self.transport.gss_kex_used: + elif ( + self.auth_method == 'gssapi-keyex' and + self.transport.gss_kex_used + ): kexgss = self.transport.kexgss_ctxt kexgss.set_username(self.username) mic_token = kexgss.ssh_get_mic(self.transport.session_id) diff --git a/paramiko/channel.py b/paramiko/channel.py index 1bd7969d..f2ecc4c0 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -920,8 +920,9 @@ class Channel (ClosingContextManager): def _set_remote_channel(self, chanid, window_size, max_packet_size): self.remote_chanid = chanid self.out_window_size = window_size - self.out_max_packet_size = self.transport. \ - _sanitize_packet_size(max_packet_size) + self.out_max_packet_size = self.transport._sanitize_packet_size( + max_packet_size + ) self.active = 1 self._log(DEBUG, 'Max packet out: %d bytes' % self.out_max_packet_size) diff --git a/paramiko/common.py b/paramiko/common.py index a77e39b9..556f046a 100644 --- a/paramiko/common.py +++ b/paramiko/common.py @@ -20,8 +20,9 @@ Common constants and global variables. """ import logging -from paramiko.py3compat import byte_chr, PY2, bytes_types, string_types, b,\ - long +from paramiko.py3compat import ( + byte_chr, PY2, bytes_types, string_types, b, long, +) MSG_DISCONNECT, MSG_IGNORE, MSG_UNIMPLEMENTED, MSG_DEBUG, \ MSG_SERVICE_REQUEST, MSG_SERVICE_ACCEPT = range(1, 7) diff --git a/paramiko/config.py b/paramiko/config.py index 196e32ba..073abb36 100644 --- a/paramiko/config.py +++ b/paramiko/config.py @@ -224,8 +224,9 @@ class SSHConfig (object): if isinstance(config[k], list): for item in range(len(config[k])): if find in config[k][item]: - config[k][item] = config[k][item].\ - replace(find, str(replace)) + config[k][item] = config[k][item].replace( + find, str(replace) + ) else: if find in config[k]: config[k] = config[k].replace(find, str(replace)) @@ -267,8 +268,9 @@ class LazyFqdn(object): address_family = self.config.get('addressfamily', 'any').lower() if address_family != 'any': try: - family = socket.AF_INET if address_family == 'inet' \ - else socket.AF_INET6 + family = socket.AF_INET6 + if address_family == 'inet': + socket.AF_INET results = socket.getaddrinfo( self.host, None, diff --git a/paramiko/dsskey.py b/paramiko/dsskey.py index c8452f23..ac6875bc 100644 --- a/paramiko/dsskey.py +++ b/paramiko/dsskey.py @@ -240,8 +240,11 @@ class DSSKey(PKey): keylist = BER(data).decode() except BERException as e: raise SSHException('Unable to parse key file: ' + str(e)) - if (type(keylist) is not list) or (len(keylist) < 6) or \ - (keylist[0] != 0): + if ( + type(keylist) is not list or + len(keylist) < 6 or + keylist[0] != 0 + ): raise SSHException( 'not a valid DSA private key file (bad ber encoding)') self.p = keylist[1] diff --git a/paramiko/file.py b/paramiko/file.py index 92bf1f14..e31ad9dd 100644 --- a/paramiko/file.py +++ b/paramiko/file.py @@ -251,9 +251,11 @@ class BufferedFile (ClosingContextManager): line = self._rbuffer truncated = False while True: - if self._at_trailing_cr and \ - (self._flags & self.FLAG_UNIVERSAL_NEWLINE) and \ - (len(line) > 0): + if ( + self._at_trailing_cr and + self._flags & self.FLAG_UNIVERSAL_NEWLINE and + len(line) > 0 + ): # edge case: the newline may be '\r\n' and we may have read # only the first '\r' last time. if line[0] == linefeed_byte_value: @@ -274,9 +276,13 @@ class BufferedFile (ClosingContextManager): n = size - len(line) else: n = self._bufsize - if (linefeed_byte in line) or \ - ((self._flags & self.FLAG_UNIVERSAL_NEWLINE) and - (cr_byte in line)): + if ( + linefeed_byte in line or + ( + self._flags & self.FLAG_UNIVERSAL_NEWLINE and + cr_byte in line + ) + ): break try: new_data = self._read(n) @@ -299,9 +305,11 @@ class BufferedFile (ClosingContextManager): self._pos += len(line) return line if self._flags & self.FLAG_BINARY else u(line) xpos = pos + 1 - if (line[pos] == cr_byte_value) and \ - (xpos < len(line)) and \ - (line[xpos] == linefeed_byte_value): + if ( + line[pos] == cr_byte_value and + xpos < len(line) and + line[xpos] == linefeed_byte_value + ): xpos += 1 # if the string was truncated, _rbuffer needs to have the string after # the newline character plus the truncated part of the line we stored @@ -524,8 +532,10 @@ class BufferedFile (ClosingContextManager): return if self.newlines is None: self.newlines = newline - elif self.newlines != newline and \ - isinstance(self.newlines, bytes_types): + elif ( + self.newlines != newline and + isinstance(self.newlines, bytes_types) + ): self.newlines = (self.newlines, newline) elif newline not in self.newlines: self.newlines += (newline,) diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py index 001471ac..f5c622ed 100644 --- a/paramiko/hostkeys.py +++ b/paramiko/hostkeys.py @@ -187,10 +187,12 @@ class HostKeys (MutableMapping): entries = [] for e in self._entries: for h in e.hostnames: - if h.startswith('|1|') and not hostname.startswith('|1|') and \ - constant_time_bytes_eq( - self.hash_host(hostname, h), h) \ - or h == hostname: + if ( + h == hostname or + h.startswith('|1|') and + not hostname.startswith('|1|') and + constant_time_bytes_eq(self.hash_host(hostname, h), h) + ): entries.append(e) if len(entries) == 0: return None diff --git a/paramiko/kex_gss.py b/paramiko/kex_gss.py index ccca8e9e..40ceb5cd 100644 --- a/paramiko/kex_gss.py +++ b/paramiko/kex_gss.py @@ -137,8 +137,8 @@ class KexGSSGroup1(object): while 1: x_bytes = os.urandom(128) x_bytes = byte_mask(x_bytes[0], 0x7f) + x_bytes[1:] - if (x_bytes[:8] != self.b7fffffffffffffff) and \ - (x_bytes[:8] != self.b0000000000000000): + first = x_bytes[:8] + if first not in (self.b7fffffffffffffff, self.b0000000000000000): break self.x = util.inflate_long(x_bytes) -- cgit v1.2.3 From 7a2c893d95651ad1f4667f7e4480da149069b7ff Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Wed, 31 May 2017 18:18:33 -0700 Subject: Even moar parentheses over backslashes --- paramiko/packet.py | 16 ++++++++++------ paramiko/primes.py | 7 +++++-- paramiko/server.py | 10 ++++++---- paramiko/sftp_file.py | 12 ++++++++---- paramiko/ssh_exception.py | 3 +-- paramiko/ssh_gss.py | 16 +++++++++++----- paramiko/transport.py | 15 +++++++++++---- paramiko/util.py | 3 +-- tests/test_gssapi.py | 8 +++++--- 9 files changed, 58 insertions(+), 32 deletions(-) diff --git a/paramiko/packet.py b/paramiko/packet.py index 6f3cd4e2..16288a0a 100644 --- a/paramiko/packet.py +++ b/paramiko/packet.py @@ -394,9 +394,11 @@ class Packetizer (object): self.__sent_bytes += len(out) self.__sent_packets += 1 - if (self.__sent_packets >= self.REKEY_PACKETS or - self.__sent_bytes >= self.REKEY_BYTES)\ - and not self.__need_rekey: + sent_too_much = ( + self.__sent_packets >= self.REKEY_PACKETS or + self.__sent_bytes >= self.REKEY_BYTES + ) + if sent_too_much and not self.__need_rekey: # only ask once for rekeying self._log(DEBUG, 'Rekeying (hit %d packets, %d bytes sent)' % (self.__sent_packets, self.__sent_bytes)) @@ -506,9 +508,11 @@ class Packetizer (object): self.__logger.log(level, msg) def _check_keepalive(self): - if (not self.__keepalive_interval) or \ - (not self.__block_engine_out) or \ - self.__need_rekey: + if ( + not self.__keepalive_interval or + not self.__block_engine_out or + self.__need_rekey + ): # wait till we're encrypting, and not in the middle of rekeying return now = time.time() diff --git a/paramiko/primes.py b/paramiko/primes.py index 50100ad5..48a34e53 100644 --- a/paramiko/primes.py +++ b/paramiko/primes.py @@ -75,8 +75,11 @@ class ModulusPack (object): # type 2 (meets basic structural requirements) # test 4 (more than just a small-prime sieve) # tries < 100 if test & 4 (at least 100 tries of miller-rabin) - if (mod_type < 2) or (tests < 4) or \ - ((tests & 4) and (tests < 8) and (tries < 100)): + if ( + mod_type < 2 or + tests < 4 or + (tests & 4 and tests < 8 and tries < 100) + ): self.discarded.append( (modulus, 'does not meet basic requirements')) return diff --git a/paramiko/server.py b/paramiko/server.py index 89278a82..953bb33f 100644 --- a/paramiko/server.py +++ b/paramiko/server.py @@ -22,8 +22,10 @@ import threading from paramiko import util -from paramiko.common import DEBUG, ERROR, \ - OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED, AUTH_FAILED, AUTH_SUCCESSFUL +from paramiko.common import ( + DEBUG, ERROR, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED, AUTH_FAILED, + AUTH_SUCCESSFUL, +) from paramiko.py3compat import string_types @@ -450,8 +452,8 @@ class ServerInterface (object): ``True`` if this channel is now hooked up to the requested subsystem; ``False`` if that subsystem can't or won't be provided. """ - handler_class, larg, kwarg = \ - channel.get_transport()._get_subsystem_handler(name) + transport = channel.get_transport() + handler_class, larg, kwarg = transport._get_subsystem_handler(name) if handler_class is None: return False handler = handler_class(channel, name, self, *larg, **kwarg) diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py index 13e28e28..58653c79 100644 --- a/paramiko/sftp_file.py +++ b/paramiko/sftp_file.py @@ -194,8 +194,10 @@ class SFTPFile (BufferedFile): data[:chunk] ) self._reqs.append(sftp_async_request) - if not self.pipelined or \ - (len(self._reqs) > 100 and self.sftp.sock.recv_ready()): + if ( + not self.pipelined or + (len(self._reqs) > 100 and self.sftp.sock.recv_ready()) + ): while len(self._reqs): req = self._reqs.popleft() t, msg = self.sftp._read_response(req) @@ -476,8 +478,10 @@ class SFTPFile (BufferedFile): read_chunks = [] for offset, size in chunks: # don't fetch data that's already in the prefetch buffer - if self._data_in_prefetch_buffers(offset) or \ - self._data_in_prefetch_requests(offset, size): + if ( + self._data_in_prefetch_buffers(offset) or + self._data_in_prefetch_requests(offset, size) + ): continue # break up anything larger than the max read size diff --git a/paramiko/ssh_exception.py b/paramiko/ssh_exception.py index eb63df4e..e3584d89 100644 --- a/paramiko/ssh_exception.py +++ b/paramiko/ssh_exception.py @@ -108,8 +108,7 @@ class BadHostKeyException (SSHException): .. versionadded:: 1.6 """ def __init__(self, hostname, got_key, expected_key): - message = 'Host key for server {} does not match : ' \ - 'got {} expected {}' + message = 'Host key for server {} does not match: got {} expected {}' message = message.format( hostname, got_key.get_base64(), expected_key.get_base64()) diff --git a/paramiko/ssh_gss.py b/paramiko/ssh_gss.py index 8bb0c2e0..9c88c6fc 100644 --- a/paramiko/ssh_gss.py +++ b/paramiko/ssh_gss.py @@ -405,12 +405,16 @@ class _SSH_SSPI(_SSH_GSSAuth): _SSH_GSSAuth.__init__(self, auth_method, gss_deleg_creds) if self._gss_deleg_creds: - self._gss_flags = \ - sspicon.ISC_REQ_INTEGRITY | sspicon.ISC_REQ_MUTUAL_AUTH | \ + self._gss_flags = ( + sspicon.ISC_REQ_INTEGRITY | + sspicon.ISC_REQ_MUTUAL_AUTH | sspicon.ISC_REQ_DELEGATE + ) else: - self._gss_flags = \ - sspicon.ISC_REQ_INTEGRITY | sspicon.ISC_REQ_MUTUAL_AUTH + self._gss_flags = ( + sspicon.ISC_REQ_INTEGRITY | + sspicon.ISC_REQ_MUTUAL_AUTH + ) def ssh_init_sec_context(self, target, desired_mech=None, username=None, recv_token=None): @@ -546,8 +550,10 @@ class _SSH_SSPI(_SSH_GSSAuth): :return: ``True`` if credentials are delegated, otherwise ``False`` :rtype: Boolean """ - return self._gss_flags & sspicon.ISC_REQ_DELEGATE and \ + return ( + self._gss_flags & sspicon.ISC_REQ_DELEGATE and (self._gss_srv_ctxt_status or self._gss_flags) + ) def save_client_creds(self, client_token): """ diff --git a/paramiko/transport.py b/paramiko/transport.py index 0a570977..129562d1 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -2125,8 +2125,13 @@ class Transport (threading.Thread, ClosingContextManager): self.packetizer.set_outbound_cipher( engine, block_size, mac_engine, mac_size, mac_key, sdctr) compress_out = self._compression_info[self.local_compression][0] - if (compress_out is not None) and \ - ((self.local_compression != 'zlib@openssh.com') or self.authenticated): + if ( + compress_out is not None and + ( + self.local_compression != 'zlib@openssh.com' or + self.authenticated + ) + ): self._log(DEBUG, 'Switching on outbound compression ...') self.packetizer.set_outbound_compressor(compress_out()) if not self.packetizer.need_rekey(): @@ -2275,8 +2280,10 @@ class Transport (threading.Thread, ClosingContextManager): initial_window_size = m.get_int() max_packet_size = m.get_int() reject = False - if (kind == 'auth-agent@openssh.com') and \ - (self._forward_agent_handler is not None): + if ( + kind == 'auth-agent@openssh.com' and + self._forward_agent_handler is not None + ): self._log(DEBUG, 'Incoming forward agent connection') self.lock.acquire() try: diff --git a/paramiko/util.py b/paramiko/util.py index 82157c24..de099c0c 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -248,8 +248,7 @@ def log_to_file(filename, level=DEBUG): l.setLevel(level) f = open(filename, 'a') lh = logging.StreamHandler(f) - frm = '%(levelname)-.3s [%(asctime)s.%(msecs)03d] thr=%(_threadid)-3d ' \ - '%(name)s: %(message)s' + frm = '%(levelname)-.3s [%(asctime)s.%(msecs)03d] thr=%(_threadid)-3d %(name)s: %(message)s' # noqa lh.setFormatter(logging.Formatter(frm, '%Y%m%d-%H:%M:%S')) l.addHandler(lh) diff --git a/tests/test_gssapi.py b/tests/test_gssapi.py index 96c268d9..bc220108 100644 --- a/tests/test_gssapi.py +++ b/tests/test_gssapi.py @@ -104,9 +104,11 @@ class GSSAPITest(unittest.TestCase): status = gss_srv_ctxt.verify_mic(mic_msg, mic_token) self.assertEquals(0, status) else: - gss_flags = sspicon.ISC_REQ_INTEGRITY |\ - sspicon.ISC_REQ_MUTUAL_AUTH |\ - sspicon.ISC_REQ_DELEGATE + gss_flags = ( + sspicon.ISC_REQ_INTEGRITY | + sspicon.ISC_REQ_MUTUAL_AUTH | + sspicon.ISC_REQ_DELEGATE + ) # Initialize a GSS-API context. target_name = "host/" + socket.getfqdn(targ_name) gss_ctxt = sspi.ClientAuth("Kerberos", -- cgit v1.2.3 From 14861487ec7633699fe8ea40eef6702d5ab771b5 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Wed, 31 May 2017 18:19:15 -0700 Subject: This previous tweak was overzealous. These backslashes are OK! --- demos/forward.py | 2 +- demos/rforward.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/demos/forward.py b/demos/forward.py index c478e217..96e1700d 100644 --- a/demos/forward.py +++ b/demos/forward.py @@ -105,7 +105,7 @@ def verbose(s): print(s) -HELP = """ +HELP = """\ Set up a forward tunnel across an SSH server, using paramiko. A local port (given with -p) is forwarded across an SSH session to an address:port from the SSH server. This is similar to the openssh -L option. diff --git a/demos/rforward.py b/demos/rforward.py index d015cebb..ae70670c 100755 --- a/demos/rforward.py +++ b/demos/rforward.py @@ -85,7 +85,7 @@ def verbose(s): print(s) -HELP = """ +HELP = """\ Set up a reverse forwarding tunnel across an SSH server, using paramiko. A port on the SSH server (given with -p) is forwarded across an SSH session back to the local machine, and out to a remote site reachable from this -- cgit v1.2.3 From 383e102b54464f2be8fe5aaa3b1e9bd94ad0b788 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Wed, 31 May 2017 18:21:02 -0700 Subject: Remove some Invoke-isms from the flake8 config --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index ff099a69..f2c1499d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -5,6 +5,6 @@ universal = 1 omit = paramiko/_winapi.py [flake8] -exclude = sites,.git,build,dist,alt_env,appveyor,demos,tests +exclude = sites,.git,build,dist,demos,tests ignore = E124,E125,E128,E261,E301,E302,E303,E402 max-line-length = 79 -- cgit v1.2.3