mirror of
https://github.com/ARM-software/devlib.git
synced 2025-09-23 12:21:54 +01:00
escaping: Use pipes.quote instead of escape_*
pipes.quote (also known as shlex.quote in Python3) provides a robust implementation of quoting and escaping strings before passing them to POSIX-style shells. Use it instead of the escape_* functions to simplify quoting inside devlib.
This commit is contained in:
committed by
Marc Bonnici
parent
1f50b0ffc2
commit
389ec76c1e
@@ -28,10 +28,10 @@ import tempfile
|
||||
import subprocess
|
||||
from collections import defaultdict
|
||||
import pexpect
|
||||
from pipes import quote
|
||||
|
||||
from devlib.exception import TargetTransientError, TargetStableError, HostError
|
||||
from devlib.utils.misc import check_output, which, ABI_MAP
|
||||
from devlib.utils.misc import escape_single_quotes, escape_double_quotes
|
||||
|
||||
|
||||
logger = logging.getLogger('android')
|
||||
@@ -383,7 +383,7 @@ def adb_shell(device, command, timeout=None, check_exit_code=False,
|
||||
as_root=False, adb_server=None): # NOQA
|
||||
_check_env()
|
||||
if as_root:
|
||||
command = 'echo \'{}\' | su'.format(escape_single_quotes(command))
|
||||
command = 'echo {} | su'.format(quote(command))
|
||||
device_part = []
|
||||
if adb_server:
|
||||
device_part = ['-H', adb_server]
|
||||
@@ -443,9 +443,9 @@ def adb_background_shell(device, command,
|
||||
"""Runs the sepcified command in a subprocess, returning the the Popen object."""
|
||||
_check_env()
|
||||
if as_root:
|
||||
command = 'echo \'{}\' | su'.format(escape_single_quotes(command))
|
||||
command = 'echo {} | su'.format(quote(command))
|
||||
device_string = ' -s {}'.format(device) if device else ''
|
||||
full_command = 'adb{} shell "{}"'.format(device_string, escape_double_quotes(command))
|
||||
full_command = 'adb{} shell {}'.format(device_string, quote(command))
|
||||
logger.debug(full_command)
|
||||
return subprocess.Popen(full_command, stdout=stdout, stderr=stderr, shell=True)
|
||||
|
||||
|
@@ -429,22 +429,38 @@ def sanitize_cmd_template(cmd):
|
||||
return cmd
|
||||
|
||||
def escape_quotes(text):
|
||||
"""Escape quotes, and escaped quotes, in the specified text."""
|
||||
"""
|
||||
Escape quotes, and escaped quotes, in the specified text.
|
||||
|
||||
.. note:: :func:`pipes.quote` should be favored where possible.
|
||||
"""
|
||||
return re.sub(r'\\("|\')', r'\\\\\1', text).replace('\'', '\\\'').replace('\"', '\\\"')
|
||||
|
||||
|
||||
def escape_single_quotes(text):
|
||||
"""Escape single quotes, and escaped single quotes, in the specified text."""
|
||||
"""
|
||||
Escape single quotes, and escaped single quotes, in the specified text.
|
||||
|
||||
.. note:: :func:`pipes.quote` should be favored where possible.
|
||||
"""
|
||||
return re.sub(r'\\("|\')', r'\\\\\1', text).replace('\'', '\'\\\'\'')
|
||||
|
||||
|
||||
def escape_double_quotes(text):
|
||||
"""Escape double quotes, and escaped double quotes, in the specified text."""
|
||||
"""
|
||||
Escape double quotes, and escaped double quotes, in the specified text.
|
||||
|
||||
.. note:: :func:`pipes.quote` should be favored where possible.
|
||||
"""
|
||||
return re.sub(r'\\("|\')', r'\\\\\1', text).replace('\"', '\\\"')
|
||||
|
||||
|
||||
def escape_spaces(text):
|
||||
"""Escape spaces in the specified text"""
|
||||
"""
|
||||
Escape spaces in the specified text
|
||||
|
||||
.. note:: :func:`pipes.quote` should be favored where possible.
|
||||
"""
|
||||
return text.replace(' ', '\ ')
|
||||
|
||||
|
||||
|
@@ -41,8 +41,6 @@ from pexpect import EOF, TIMEOUT, spawn
|
||||
from devlib.exception import (HostError, TargetStableError, TargetNotRespondingError,
|
||||
TimeoutError, TargetTransientError)
|
||||
from devlib.utils.misc import which, strip_bash_colors, check_output, sanitize_cmd_template
|
||||
from devlib.utils.misc import (escape_single_quotes, escape_double_quotes,
|
||||
escape_spaces)
|
||||
from devlib.utils.types import boolean
|
||||
|
||||
|
||||
@@ -265,7 +263,7 @@ class SshConnection(object):
|
||||
# As we're already root, there is no need to use sudo.
|
||||
as_root = False
|
||||
if as_root:
|
||||
command = self.sudo_cmd.format(escape_single_quotes(command))
|
||||
command = self.sudo_cmd.format(quote(command))
|
||||
if log:
|
||||
logger.debug(command)
|
||||
self.conn.sendline(command)
|
||||
@@ -758,7 +756,7 @@ class Gem5Connection(TelnetConnection):
|
||||
gem5_logger.debug("gem5_shell command: {}".format(command))
|
||||
|
||||
if as_root:
|
||||
command = 'echo "{}" | su'.format(escape_double_quotes(command))
|
||||
command = 'echo {} | su'.format(quote(command))
|
||||
|
||||
# Send the actual command
|
||||
self.conn.send("{}\n".format(command))
|
||||
|
Reference in New Issue
Block a user