Python os.getlogin problem

gistart picture gistart · Dec 9, 2010 · Viewed 11k times · Source

If i create a file like:

import os
print os.getlogin()

and run it with cron, I get an exception

print os.getlogin()
OSError: [Errno 22] Invalid argument

If I run it manually in shell -- it works.

Problem is, GitPython 0.3.1 in commit() uses this function, and i need to use it.

Is there any workaround?

I've tested it on Ubuntu10.10/python2.6.6 and Debian5.0.6/python2.5.2.

Answer

kindall picture kindall · Dec 9, 2010

From the os.getlogin() docs: "Returns the user logged in to the controlling terminal of the process." Your script does not have a controlling terminal when run from cron. The docs go on to suggest: "For most purposes, it is more useful to use the environment variable LOGNAME to find out who the user is, or pwd.getpwuid(os.getuid())[0] to get the login name of the currently effective user id."

Since you don't want to modify GitPython, you could write a script that does this:

import os, pwd

os.getlogin = lambda: pwd.getpwuid(os.getuid())[0]

import git

# do whatever you need to do with GitPython here

I would suggest filing a bug (or better yet, submitting a patch) with GitPython, though.