getpass.getuser() returns the value in the LOGNAME environment variable, so this doesn't work under sudo:

$ sudo LOGNAME=foo python 
Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48) 
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import getpass
>>> getpass.getuser()
'foo'

  This should be based off of os.geteuid()

$ sudo LOGNAME=foo python 
Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48) 
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import pwd
>>> pwd.getpwuid(os.geteuid()).pw_name
'root'