Patch-Source: https://github.com/fabric/fabric/commit/7fa309db9dd5dac69079c1fb83b6f579555f7d53 -- From 7fa309db9dd5dac69079c1fb83b6f579555f7d53 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Fri, 3 Apr 2026 15:40:03 -0400 Subject: [PATCH] Update some classes to handle Invoke 3.x changes --- fabric/connection.py | 32 +++++++++++++++----------------- fabric/tasks.py | 15 +++++++++------ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/fabric/connection.py b/fabric/connection.py index 4d4c49b75..83f44b02e 100644 --- a/fabric/connection.py +++ b/fabric/connection.py @@ -1,20 +1,20 @@ +import socket from contextlib import contextmanager from io import StringIO from threading import Event -import socket from decorator import decorator from invoke import Context from invoke.exceptions import ThreadException from paramiko.agent import AgentRequestHandler -from paramiko.client import SSHClient, AutoAddPolicy +from paramiko.client import AutoAddPolicy, SSHClient from paramiko.config import SSHConfig from paramiko.proxy import ProxyCommand from .config import Config from .exceptions import InvalidV1Env from .transfer import Transfer -from .tunnels import TunnelManager, Tunnel +from .tunnels import Tunnel, TunnelManager @decorator @@ -217,6 +217,7 @@ def __init__( connect_timeout=None, connect_kwargs=None, inline_ssh_env=None, + remainder=None, ): """ Set up a new object representing a server connection. @@ -353,6 +354,12 @@ def __init__( affects remote commands, and thus, methods like `.run` and `.sudo`. + :param str remainder: + Mirrors the same parameter from `invoke.context.Context` - is a + method of informing called tasks that the CLI parser saw an + explicitly marked "don't parse me" segment of text. See + :ref:`remainder`. + :raises ValueError: if user or port values are given via both ``host`` shorthand *and* their own arguments. (We `refuse the temptation to guess`_). @@ -372,7 +379,7 @@ def __init__( # NOTE: parent __init__ sets self._config; for now we simply overwrite # that below. If it's somehow problematic we would want to break parent # __init__ up in a manner that is more cleanly overrideable. - super().__init__(config=config) + super().__init__(config=config, remainder=remainder) #: The .Config object referenced when handling default values (for e.g. #: user or port, when not explicitly given) or deciding how to behave. @@ -548,9 +555,7 @@ def __repr__(self): if isinstance(self.gateway, str): val = "proxycommand" bits.append(("gw", val)) - return "".format( - " ".join("{}={}".format(*x) for x in bits) - ) + return "".format(" ".join("{}={}".format(*x) for x in bits)) def _identity(self): # TODO: consider including gateway and maybe even other init kwargs? @@ -622,10 +627,7 @@ def open(self): if key in self.connect_kwargs: raise ValueError(err.format(key)) # These may be given one way or the other, but not both - if ( - "timeout" in self.connect_kwargs - and self.connect_timeout is not None - ): + if "timeout" in self.connect_kwargs and self.connect_timeout is not None: raise ValueError(err.format("timeout")) # No conflicts -> merge 'em together kwargs = dict( @@ -741,9 +743,7 @@ def create_session(self): return channel def _remote_runner(self): - return self.config.runners.remote( - context=self, inline_env=self.inline_ssh_env - ) + return self.config.runners.remote(context=self, inline_env=self.inline_ssh_env) @opens def run(self, command, **kwargs): @@ -1110,6 +1110,4 @@ def callback(channel, src_addr_tup, dst_addr_tup): for tunnel in tunnels: tunnel.finished.set() tunnel.join() - self.transport.cancel_port_forward( - address=remote_host, port=remote_port - ) + self.transport.cancel_port_forward(address=remote_host, port=remote_port) diff --git a/fabric/tasks.py b/fabric/tasks.py index acf89d0cb..a93f5f7af 100644 --- a/fabric/tasks.py +++ b/fabric/tasks.py @@ -101,12 +101,15 @@ def clone_kwargs(self): kwargs["init_kwargs"] = self.init_kwargs return kwargs - def make_context(self, config): - kwargs = self.init_kwargs - # TODO: what about corner case of a decorator giving config in a hosts - # kwarg member?! For now let's stomp on it, and then if somebody runs - # into it, we can identify the use case & decide how best to deal. - kwargs["config"] = config + def make_context(self, config, core_parse_result): + # TODO: updating this for Invoke 3 exposes that we probs need to add + # another intermediary subroutine so we can phrase our logic as + # "parent's context-making kwargs fed to our context subclass". + # TODO: or does it make sense to fold this into init_kwargs? I don't + # entirely recall why Fabric grew that but Invoke itself did not. + kwargs = dict( + self.init_kwargs, config=config, remainder=core_parse_result.remainder + ) return Connection(**kwargs) def __repr__(self):