def runAsEffectiveUser(euid, egid, function, *args, **kwargs):
 (uid, gid) = (os.geteuid(), os.getegid())
 if ((uid == euid) and (gid == egid)):
  return function(*args, **kwargs)
 else:
  if ((uid != 0) and ((uid != euid) or (gid != egid))):
   os.seteuid(0)
  if (gid != egid):
   os.setegid(egid)
  if ((euid != 0) and ((euid != uid) or (gid != egid))):
   os.seteuid(euid)
  try:
   return function(*args, **kwargs)
  finally:
   if ((euid != 0) and ((uid != euid) or (gid != egid))):
    os.seteuid(0)
   if (gid != egid):
    os.setegid(gid)
   if ((uid != 0) and ((uid != euid) or (gid != egid))):
    os.seteuid(uid)

def runAsEffectiveUser ( euid , egid , function , * args , ** kwargs ) : ( uid , gid ) = ( os . geteuid ( ) , os . getegid ( ) ) if ( ( uid == euid ) and ( gid == egid ) ) : return function ( * args , ** kwargs ) else : if ( ( uid != 0 ) and ( ( uid != euid ) or ( gid != egid ) ) ) : os . seteuid ( 0 ) if ( gid != egid ) : os . setegid ( egid ) if ( ( euid != 0 ) and ( ( euid != uid ) or ( gid != egid ) ) ) : os . seteuid ( euid ) try : return function ( * args , ** kwargs ) finally : if ( ( euid != 0 ) and ( ( uid != euid ) or ( gid != egid ) ) ) : os . seteuid ( 0 ) if ( gid != egid ) : os . setegid ( gid ) if ( ( uid != 0 ) and ( ( uid != euid ) or ( gid != egid ) ) ) : os . seteuid ( uid )
run the given function wrapped with seteuid/setegid calls .