Skip to content

Commit ad5936c

Browse files
authored
Merge pull request #1924 from daspecster/fix-gae-default-project-id
Resolve issues with GAE and os.path.expanduser()
2 parents 95f2a20 + b1d56a5 commit ad5936c

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

gcloud/_helpers.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,18 @@ 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+
search_paths.append(os.path.expanduser(DEFAULT_CONFIGURATION_PATH))
195+
except ImportError:
196+
pass
192197
win32_config_path = os.path.join(os.getenv('APPDATA', ''),
193198
'gcloud', 'configurations',
194199
'config_default')
200+
search_paths.append(win32_config_path)
195201
config = configparser.RawConfigParser()
196-
config.read([full_config_path, win32_config_path])
202+
config.read(search_paths)
197203

198204
if config.has_section('core'):
199205
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+
try:
228+
sys.modules['pwd'] = None # Blocks pwd from being imported.
229+
project_id = self.callFUT('test-project-id')
230+
self.assertEqual(None, project_id)
231+
finally:
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)