import os
from contextlib import contextmanager


@contextmanager
def effective_user(uid, gid):
    """
    A ContextManager that executes code in the with block with effective uid / gid given
    """
    original_uid = os.geteuid()
    original_gid = os.getegid()
    os.setegid(gid)
    os.seteuid(uid)
    yield
    os.setegid(original_gid)
    os.setuid(original_uid)

import os
from contextlib import contextmanager


@contextmanager
def effective_user(uid, gid):
    """
    A ContextManager that executes code in the with block with effective uid / gid given
    """
    original_uid = os.geteuid()
    original_gid = os.getegid()
    os.setegid(gid)
    os.seteuid(uid)
    try:
        yield
    finally:
        os.setegid(original_gid)
        os.setuid(original_uid)

Make effective_user handle exceptions properly
Make effective_user handle exceptions properly

Right now the context manager is just syntactic sugar - setegid
and seteuid aren't called if there's an exception.

Change-Id: I9e2f1d0ada00b03099fe60a8735db1caef8527e9
