Skip to content

Commit 32478d2

Browse files
authored
Make cache_id method more portable (#332)
1 parent 83db49f commit 32478d2

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

openshift/dynamic/discovery.py

+26-7
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,37 @@ def __init__(self, client, cache_file):
3131
self.__init_cache()
3232

3333
def __get_default_cache_id(self):
34-
user = ""
35-
for func in [os.getlogin, os.getuid]:
36-
try:
37-
user = str(func())
38-
except OSError:
39-
pass
34+
user = self.__get_user()
35+
if user:
36+
cache_id = "{0}-{1}".format(self.client.configuration.host, user)
37+
else:
38+
cache_id = self.client.configuration.host
4039

41-
cache_id = "{0}-{1}".format(self.client.configuration.host, user)
4240
if six.PY3:
4341
return cache_id.encode('utf-8')
42+
4443
return cache_id
4544

45+
def __get_user(self):
46+
if hasattr(os, 'getlogin'):
47+
try:
48+
user = os.getlogin()
49+
if user:
50+
return str(user)
51+
except OSError:
52+
pass
53+
if hasattr(os, 'getuid'):
54+
try:
55+
user = os.getuid()
56+
if user:
57+
return str(user)
58+
except OSError:
59+
pass
60+
user = os.environ.get("USERNAME")
61+
if user:
62+
return str(user)
63+
return None
64+
4665
def __init_cache(self, refresh=False):
4766
if refresh or not os.path.exists(self.__cache_file):
4867
self._cache = {'library_version': __version__}

0 commit comments

Comments
 (0)