Skip to content

Commit

Permalink
Merge pull request #1327 from doronz88/bugfix/ignore-all-completion-i…
Browse files Browse the repository at this point in the history
…nvocations

cli: prevent device connection on all autocomplete scripts
  • Loading branch information
doronz88 authored Jan 2, 2025
2 parents 5a8d3d1 + 12e3c85 commit f4f4293
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
12 changes: 10 additions & 2 deletions pymobiledevice3/cli/cli_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ def wrap_callback_calling(**kwargs: dict) -> None:
return wrap_callback_calling


def is_invoked_for_completion() -> bool:
""" Returns True if the command is ivoked for autocompletion. """
for env in os.environ.keys():
if env.startswith('_') and env.endswith('_COMPLETE'):
return True
return False


class BaseCommand(click.Command):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -198,7 +206,7 @@ def usbmux(self, ctx, param: str, value: Optional[str] = None) -> None:
self.usbmux_address = value

def udid(self, ctx, param: str, value: str) -> Optional[LockdownClient]:
if '_PYMOBILEDEVICE3_COMPLETE' in os.environ:
if is_invoked_for_completion():
# prevent lockdown connection establishment when in autocomplete mode
return

Expand Down Expand Up @@ -284,7 +292,7 @@ def __init__(self, *args, **kwargs):
class CommandWithoutAutopair(Command):
@staticmethod
def udid(ctx, param, value):
if '_PYMOBILEDEVICE3_COMPLETE' in os.environ:
if is_invoked_for_completion():
# prevent lockdown connection establishment when in autocomplete mode
return
return create_using_usbmux(serial=value, autopair=False)
Expand Down
5 changes: 2 additions & 3 deletions pymobiledevice3/cli/restore.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio
import contextlib
import logging
import os
import plistlib
import tempfile
import traceback
Expand All @@ -16,7 +15,7 @@
from pygments import formatters, highlight, lexers

from pymobiledevice3 import usbmux
from pymobiledevice3.cli.cli_common import print_json, prompt_selection, set_verbosity
from pymobiledevice3.cli.cli_common import is_invoked_for_completion, print_json, prompt_selection, set_verbosity
from pymobiledevice3.exceptions import ConnectionFailedError, ConnectionFailedToUsbmuxdError, IncorrectModeError
from pymobiledevice3.irecv import IRecv
from pymobiledevice3.lockdown import LockdownClient, create_using_usbmux
Expand Down Expand Up @@ -46,7 +45,7 @@ def __init__(self, *args, **kwargs):

@staticmethod
def device(ctx, param, value) -> Optional[Union[LockdownClient, IRecv]]:
if '_PYMOBILEDEVICE3_COMPLETE' in os.environ:
if is_invoked_for_completion():
# prevent lockdown connection establishment when in autocomplete mode
return

Expand Down

0 comments on commit f4f4293

Please sign in to comment.