本文整理汇总了Python中invoke.Context.sudo方法的典型用法代码示例。如果您正苦于以下问题:Python Context.sudo方法的具体用法?Python Context.sudo怎么用?Python Context.sudo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类invoke.Context
的用法示例。
在下文中一共展示了Context.sudo方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _expect_responses
# 需要导入模块: from invoke import Context [as 别名]
# 或者: from invoke.Context import sudo [as 别名]
def _expect_responses(self,
expected, Local, getpass,
config=None, kwargs=None, getpass_reply=None,
):
"""
Execute mocked sudo(), expecting watchers= kwarg in its run().
* expected: list of 2-tuples of FailingResponder prompt/response
* config: Config object, if an overridden one is needed
* kwargs: sudo() kwargs, if needed
* getpass_reply: return value of getpass.getpass, if needed
(Local and getpass are just mock injections.)
"""
if kwargs is None:
kwargs = {}
getpass.getpass.return_value = getpass_reply
runner = Local.return_value
context = Context(config=config) if config else Context()
context.sudo('whoami', **kwargs)
# Tease out the interesting bits - pattern/response - ignoring the
# sentinel, etc for now.
prompt_responses = [
(watcher.pattern, watcher.response)
for watcher in runner.run.call_args[1]['watchers']
]
eq_(prompt_responses, expected)
示例2: prefixes_should_apply_to_sudo
# 需要导入模块: from invoke import Context [as 别名]
# 或者: from invoke.Context import sudo [as 别名]
def prefixes_should_apply_to_sudo(self, Local):
runner = Local.return_value
c = Context()
with c.prefix("cd foo"):
c.sudo("whoami")
cmd = "sudo -S -p '[sudo] password: ' cd foo && whoami"
assert runner.run.called, "sudo() never called runner.run()!"
assert runner.run.call_args[0][0] == cmd
示例3: prefixes_should_apply_to_sudo
# 需要导入模块: from invoke import Context [as 别名]
# 或者: from invoke.Context import sudo [as 别名]
def prefixes_should_apply_to_sudo(self, Local):
runner = Local.return_value
ctx = Context()
with ctx.prefix('cd foo'):
ctx.sudo('whoami')
cmd = "sudo -S -p '[sudo] password: ' cd foo && whoami"
ok_(runner.run.called, "sudo() never called runner.run()!")
eq_(runner.run.call_args[0][0], cmd)
示例4: cd_should_apply_to_sudo
# 需要导入模块: from invoke import Context [as 别名]
# 或者: from invoke.Context import sudo [as 别名]
def cd_should_apply_to_sudo(self, Local):
runner = Local.return_value
c = Context()
with c.cd('foo'):
c.sudo('whoami')
cmd = "sudo -S -p '[sudo] password: ' cd foo && whoami"
assert runner.run.called, "sudo() never called runner.run()!"
assert runner.run.call_args[0][0] == cmd
示例5: kwarg_only_adds_to_kwarg
# 需要导入模块: from invoke import Context [as 别名]
# 或者: from invoke.Context import sudo [as 别名]
def kwarg_only_adds_to_kwarg(self, Local):
runner = Local.return_value
context = Context()
watcher = self.watcher_klass()
context.sudo("whoami", watchers=[watcher])
# When sudo() called w/ user-specified watchers, we add ours to
# that list
watchers = runner.run.call_args[1]["watchers"]
# Will raise ValueError if not in the list
watchers.remove(watcher)
# Only remaining item in list should be our sudo responder
assert len(watchers) == 1
assert isinstance(watchers[0], FailingResponder)
assert watchers[0].pattern == self.escaped_prompt
示例6: _expect_responses
# 需要导入模块: from invoke import Context [as 别名]
# 或者: from invoke.Context import sudo [as 别名]
def _expect_responses(self, expected, config=None, kwargs=None):
"""
Execute mocked sudo(), expecting watchers= kwarg in its run().
* expected: list of 2-tuples of FailingResponder prompt/response
* config: Config object, if an overridden one is needed
* kwargs: sudo() kwargs, if needed
"""
if kwargs is None:
kwargs = {}
Local = Mock()
runner = Local.return_value
context = Context(config=config) if config else Context()
context.config.runners.local = Local
context.sudo("whoami", **kwargs)
# Tease out the interesting bits - pattern/response - ignoring the
# sentinel, etc for now.
prompt_responses = [
(watcher.pattern, watcher.response)
for watcher in runner.run.call_args[1]["watchers"]
]
assert prompt_responses == expected