Skip to content

Instantly share code, notes, and snippets.

@jgstew
Last active November 11, 2015 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgstew/9b0a5d94e19516515c9c to your computer and use it in GitHub Desktop.
Save jgstew/9b0a5d94e19516515c9c to your computer and use it in GitHub Desktop.
A utility function to grab usernames and other identifying info from the current user to be used as potential identification options.
import os
def getLoggedOnUserName():
return os.getenv( "LOGNAME", os.getenv("USERNAME") )
# another UNIX option: pwd.getpwuid(os.getuid())[0]
# http://stackoverflow.com/questions/12821946/how-can-i-remove-the-white-characters-from-configuration-file
def getStrippedFile(filename):
import StringIO
return StringIO.StringIO('\n'.join(line.strip() for line in open( filename )))
def getGitUserName():
import ConfigParser
config = ConfigParser.ConfigParser()
config.readfp( getStrippedFile( os.path.expanduser('~/.gitconfig') ) )
return config.get('user','name')
def getConfigUserName( filename ):
import ConfigParser
config = ConfigParser.ConfigParser()
config.readfp( getStrippedFile( filename ) )
return config.get('user','name')
def main():
print getLoggedOnUserName()
print getGitUserName()
if __name__ == '__main__':
main()
@jgstew
Copy link
Author

jgstew commented Nov 11, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment