some code cleanup (lint errors and escape_argument as _cmd_quote)

This commit is contained in:
William Villeneuve 2018-01-28 12:42:33 -05:00
parent 1c29bc5a3d
commit 217791079b
3 changed files with 11 additions and 11 deletions

View File

@ -36,7 +36,6 @@ from salt.exceptions import CommandExecutionError, TimedProcTimeoutError, \
SaltInvocationError
from salt.log import LOG_LEVELS
from salt.ext.six.moves import range, zip
from salt.ext.six.moves import shlex_quote as _cmd_quote
from salt.utils.locales import sdecode
# Only available on POSIX systems, nonfatal on windows
@ -47,9 +46,10 @@ except ImportError:
if salt.utils.is_windows():
from salt.utils.win_runas import runas as win_runas
from salt.utils.win_functions import escape_argument as win_cmd_quote
from salt.utils.win_functions import escape_argument as _cmd_quote
HAS_WIN_RUNAS = True
else:
from salt.ext.six.moves import shlex_quote as _cmd_quote
HAS_WIN_RUNAS = False
__proxyenabled__ = ['*']
@ -2148,10 +2148,7 @@ def script(source,
os.chmod(path, 320)
os.chown(path, __salt__['file.user_to_uid'](runas), -1)
if salt.utils.is_windows():
path = win_cmd_quote(path)
else:
path = _cmd_quote(path)
path = _cmd_quote(path)
ret = _run(path + ' ' + str(args) if args else path,
cwd=cwd,

View File

@ -161,11 +161,12 @@ def get_sam_name(username):
username, domain, _ = win32security.LookupAccountSid(None, sid_obj)
return '\\'.join([domain, username])
def escape_argument(arg):
'''
Escape the argument for the cmd.exe shell.
See http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/everyone-quotes-arguments-the-wrong-way.aspx
First we escape the quote chars to produce a argument suitable for
CommandLineToArgvW. We don't need to do this for simple arguments.
@ -180,19 +181,20 @@ def escape_argument(arg):
return escape_for_cmd_exe(arg)
def escape_for_cmd_exe(arg):
'''
Escape an argument string to be suitable to be passed to
cmd.exe on Windows
This method takes an argument that is expected to already be properly
escaped for the receiving program to be properly parsed. This argument
will be further escaped to pass the interpolation performed by cmd.exe
unchanged.
Any meta-characters will be escaped, removing the ability to e.g. use
redirects or variables.
Args:
arg (str): a single command line argument to escape for cmd.exe
@ -201,7 +203,7 @@ def escape_for_cmd_exe(arg):
'''
meta_chars = '()%!^"<>&|'
meta_re = re.compile('(' + '|'.join(re.escape(char) for char in list(meta_chars)) + ')')
meta_map = { char: "^%s" % char for char in meta_chars }
meta_map = {char: "^{0}".format(char) for char in meta_chars}
def escape_meta_chars(m):
char = m.group(1)

View File

@ -13,6 +13,7 @@ from tests.support.mock import (
# Import Salt Libs
import salt.utils.win_functions as win_functions
@skipIf(NO_MOCK, NO_MOCK_REASON)
class WinFunctionsTestCase(TestCase):
'''