

@contextmanager
def as_user(self, username=None):
    'Run nested commands as the given user. For example::\n\n            head = local["head"]\n            head("-n1", "/dev/sda1")    # this will fail...\n            with local.as_user():\n                head("-n1", "/dev/sda1")\n\n        :param username: The user to run commands as. If not given, root (or Administrator) is assumed\n        '
    if IS_WIN32:
        if (username is None):
            username = 'Administrator'
        self._as_user_stack.append((lambda argv: (['runas', '/savecred', ('/user:%s' % (username,)), (('"' + ' '.join((str(a) for a in argv))) + '"')], self.which('runas'))))
    elif (username is None):
        self._as_user_stack.append((lambda argv: ((['sudo'] + list(argv)), self.which('sudo'))))
    else:
        self._as_user_stack.append((lambda argv: ((['sudo', '-u', username] + list(argv)), self.which('sudo'))))
    try:
        (yield)
    finally:
        self._as_user_stack.pop((- 1))

dataset/ETHPy150Open tomerfiliba/plumbum/plumbum/machines/local.py LocalMachine.as_user/original