Skip to content

Commit 598cd1d

Browse files
committed
Simplified get_user_id() and fixed possible python3 compatiblity issue.
Changed motivated by #52
1 parent fbd0968 commit 598cd1d

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

git/util.py

+15-31
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,20 @@
1010
import time
1111
import stat
1212
import shutil
13-
import tempfile
1413
import platform
14+
import getpass
1515

16+
# NOTE: Some of the unused imports might be used/imported by others.
17+
# Handle once test-cases are back up and running.
1618
from gitdb.util import (
17-
make_sha,
18-
LockedFD,
19-
file_contents_ro,
20-
LazyMixin,
21-
to_hex_sha,
22-
to_bin_sha
23-
)
24-
25-
# Import the user database on unix based systems
26-
if os.name == "posix":
27-
import pwd
28-
19+
make_sha,
20+
LockedFD,
21+
file_contents_ro,
22+
LazyMixin,
23+
to_hex_sha,
24+
to_bin_sha
25+
)
26+
2927
__all__ = ( "stream_copy", "join_path", "to_native_path_windows", "to_native_path_linux",
3028
"join_path_native", "Stats", "IndexFileSHA1Writer", "Iterable", "IterableList",
3129
"BlockingLockFile", "LockFile", 'Actor', 'get_user_id', 'assure_directory_exists',
@@ -116,19 +114,8 @@ def assure_directory_exists(path, is_file=False):
116114
return False
117115

118116
def get_user_id():
119-
""":return: string identifying the currently active system user as name@node
120-
:note: user can be set with the 'USER' environment variable, usually set on windows
121-
:note: on unix based systems you can use the password database
122-
to get the login name of the effective process user"""
123-
if os.name == "posix":
124-
username = pwd.getpwuid(os.geteuid()).pw_name
125-
else:
126-
ukn = 'UNKNOWN'
127-
username = os.environ.get('USER', os.environ.get('USERNAME', ukn))
128-
if username == ukn and hasattr(os, 'getlogin'):
129-
username = os.getlogin()
130-
# END get username from login
131-
return "%s@%s" % (username, platform.node())
117+
""":return: string identifying the currently active system user as name@node"""
118+
return "%s@%s" % (getpass.getuser(), platform.node())
132119

133120
#} END utilities
134121

@@ -492,7 +479,7 @@ def _obtain_lock_or_raise(self):
492479
try:
493480
fd = os.open(lock_file, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0)
494481
os.close(fd)
495-
except OSError,e:
482+
except OSError as e:
496483
raise IOError(str(e))
497484

498485
self._owns_lock = True
@@ -514,7 +501,7 @@ def _release_lock(self):
514501
# on bloody windows, the file needs write permissions to be removable.
515502
# Why ...
516503
if os.name == 'nt':
517-
os.chmod(lfp, 0777)
504+
os.chmod(lfp, int("0777", 8))
518505
# END handle win32
519506
os.remove(lfp)
520507
except OSError:
@@ -593,9 +580,6 @@ def __new__(cls, id_attr, prefix=''):
593580
def __init__(self, id_attr, prefix=''):
594581
self._id_attr = id_attr
595582
self._prefix = prefix
596-
if not isinstance(id_attr, basestring):
597-
raise ValueError("First parameter must be a string identifying the name-property. Extend the list after initialization")
598-
# END help debugging !
599583

600584
def __contains__(self, attr):
601585
# first try identy match for performance

0 commit comments

Comments
 (0)