Skip to content

Commit 84ac521

Browse files
committed
Closes #1922, resolves issues with GAE and expanduser().
1 parent 95f2a20 commit 84ac521

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

gcloud/_helpers.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,19 @@ def _default_service_project_id():
188188
:rtype: str or ``NoneType``
189189
:returns: Project-ID from default configuration file else ``None``
190190
"""
191-
full_config_path = os.path.expanduser(DEFAULT_CONFIGURATION_PATH)
191+
search_paths = []
192+
# Workaround for GAE not supporting pwd which is used by expanduser.
193+
try:
194+
full_config_path = os.path.expanduser(DEFAULT_CONFIGURATION_PATH)
195+
search_paths.append(full_config_path)
196+
except ImportError:
197+
full_config_path = ''
192198
win32_config_path = os.path.join(os.getenv('APPDATA', ''),
193199
'gcloud', 'configurations',
194200
'config_default')
201+
search_paths.append(win32_config_path)
195202
config = configparser.RawConfigParser()
196-
config.read([full_config_path, win32_config_path])
203+
config.read(search_paths)
197204

198205
if config.has_section('core'):
199206
return config.get('core', 'project')

gcloud/test__helpers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ def callFUT(self, project_id=None):
211211

212212
def mock_expanduser(path=''):
213213
if project_id and path.startswith('~'):
214+
__import__('pwd') # Simulate actual expanduser imports.
214215
return self.temp_config_file
215216
return ''
216217

@@ -221,6 +222,15 @@ def test_read_from_cli_info(self):
221222
project_id = self.callFUT('test-project-id')
222223
self.assertEqual('test-project-id', project_id)
223224

225+
def test_gae_without_expanduser(self):
226+
import sys
227+
import pwd
228+
del pwd
229+
sys.modules['pwd'] = None # Blocks pwd from being imported.
230+
project_id = self.callFUT('test-project-id')
231+
self.assertEqual(None, project_id)
232+
del sys.modules['pwd'] # Unblocks importing of pwd.
233+
224234
def test_info_value_not_present(self):
225235
project_id = self.callFUT()
226236
self.assertEqual(None, project_id)

0 commit comments

Comments
 (0)