Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap distutils.spawn.find_executable for python3 #56

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions ssh-ident
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/python3
# vim: tabstop=2 shiftwidth=2 expandtab
"""Start and use ssh-agent and load identities as necessary.

Expand Down Expand Up @@ -122,14 +122,14 @@ to solve the problem:
rsync -e '/path/to/ssh-ident' ...
scp -S '/path/to/ssh-ident' ...

4) Replace the real ssh on the system with ssh-ident, and set the
4) Replace the real ssh on the system with ssh-ident, and set the
BINARY_SSH configuration parameter to the original value.

On Debian based system, you can make this change in a way that
will survive automated upgrades and audits by running:

dpkg-divert --divert /usr/bin/ssh.ssh-ident --rename /usr/bin/ssh

After which, you will need to use:

BINARY_SSH="/usr/bin/ssh.ssh-ident"
Expand Down Expand Up @@ -313,7 +313,6 @@ CREDITS
from __future__ import print_function

import collections
import distutils.spawn
import errno
import fcntl
import getpass
Expand All @@ -326,6 +325,14 @@ import sys
import termios
import textwrap

if sys.version_info[0] == 3: # is python3?
import shutil
def find_executable(ex, path=None):
return shutil.which(ex, os.F_OK|os.X_OK, path)
else:
import distutils.spawn
find_executable = distutils.spawn.find_executable

# constants so noone has deal with cryptic numbers
LOG_CONSTANTS = {"LOG_ERROR": 1, "LOG_WARN": 2, "LOG_INFO": 3, "LOG_DEBUG": 4}
# load them directly into the global scope, for easy use
Expand Down Expand Up @@ -422,7 +429,7 @@ class Config(object):

# Complete path of full ssh binary to use. If not set, ssh-ident will
# try to find the correct binary in PATH.
"BINARY_SSH": None,
"BINARY_SSH": "/usr/bin/ssh",
"BINARY_DIR": None,

# Which identity to use by default if we cannot tell from
Expand Down Expand Up @@ -632,7 +639,7 @@ def GetSessionTty():
tell us anything about the session having a /dev/tty associated
or not.

For example, running
For example, running

ssh -t user@remotehost './test.sh < /dev/null > /dev/null'

Expand Down Expand Up @@ -890,7 +897,7 @@ def AutodetectBinary(argv, config):
# The logic here is pretty straightforward:
# - Try to eliminate the path of ssh-ident from PATH.
# - Search for a binary with the same name of ssh-ident to run.
#
#
# If this fails, we may end up in some sort of loop, where ssh-ident
# tries to run itself. This should normally be detected later on,
# where the code checks for the next binary to run.
Expand Down Expand Up @@ -928,10 +935,10 @@ def AutodetectBinary(argv, config):
p for p in normalized_path if p != ssh_ident_path])

# Find an executable with the desired name.
binary_path = distutils.spawn.find_executable(binary_name, search_path)
binary_path = find_executable(binary_name, search_path)
if not binary_path:
# Nothing found. Try to find something named 'ssh'.
binary_path = distutils.spawn.find_executable('ssh')
binary_path = find_executable('ssh')

if binary_path:
config.Set("BINARY_SSH", binary_path)
Expand All @@ -942,7 +949,7 @@ def AutodetectBinary(argv, config):
ssh-ident was invoked in place of the binary {0} (determined from argv[0]).
Neither this binary nor 'ssh' could be found in $PATH.

PATH="{1}"
PATH="{1}"

You need to adjust your setup for ssh-ident to work: consider setting
BINARY_SSH or BINARY_DIR in your config, or running ssh-ident some
Expand Down Expand Up @@ -997,15 +1004,15 @@ def main(argv):
# which is not always the case. argv[0] may also just have the binary
# name if found in a path.
binary_path = os.path.realpath(
distutils.spawn.find_executable(config.Get("BINARY_SSH")))
find_executable(config.Get("BINARY_SSH")))
ssh_ident_path = os.path.realpath(
distutils.spawn.find_executable(argv[0]))
find_executable(argv[0]))
if binary_path == ssh_ident_path:
message = textwrap.dedent("""\
ssh-ident found '{0}' as the next command to run.
Based on argv[0] ({1}), it seems like this will create a
loop.
loop.

Please use BINARY_SSH, BINARY_DIR, or change the way
ssh-ident is invoked (eg, a different argv[0]) to make
it work correctly.""")
Expand Down