Skip to content

Commit b244022

Browse files
committed
Profile Handling
1 parent 1237e03 commit b244022

File tree

2 files changed

+49
-59
lines changed

2 files changed

+49
-59
lines changed

cppython/plugins/conan/plugin.py

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,37 @@ def _install_dependencies(self, *, update: bool = False) -> None:
108108
all_remotes = conan_api.remotes.list()
109109
logger.debug('Available remotes: %s', [remote.name for remote in all_remotes])
110110

111-
# Get default profiles, handle case when no default profile exists
112-
profile_host, profile_build = self._resolve_profiles(conan_api)
111+
# Get profiles with fallback to auto-detection
112+
try:
113+
profile_host_path = conan_api.profiles.get_default_host()
114+
profile_build_path = conan_api.profiles.get_default_build()
115+
116+
# Check if profile paths exist
117+
if profile_host_path is None or profile_build_path is None:
118+
logger.warning('Default profile paths not available, using auto-detection')
119+
raise ValueError('Profile paths are None')
120+
121+
# Load the actual profile objects
122+
profile_host = conan_api.profiles.get_profile([profile_host_path])
123+
profile_build = conan_api.profiles.get_profile([profile_build_path])
113124

114-
logger.debug('Using profiles: host=%s, build=%s', profile_host, profile_build)
125+
# Check if profiles are valid (not None)
126+
if profile_host is None or profile_build is None:
127+
logger.warning('Default profiles are corrupted/invalid, using auto-detection')
128+
raise ValueError('Profile loading returned None')
129+
130+
logger.debug('Using existing default profiles')
131+
except Exception as e:
132+
logger.warning('Default profiles not available (%s), using auto-detection', str(e))
133+
# Use Conan's auto-detection to create minimal profiles
134+
profile_host = conan_api.profiles.detect()
135+
profile_build = conan_api.profiles.detect()
115136

116-
# Build dependency graph - prepare parameters
117-
# According to Conan docs, if profiles not specified, it will use defaults
118137
path = str(conanfile_path)
119138
remotes = all_remotes
120139
update_flag = None if not update else True
121140
check_updates_flag = update
122-
141+
123142
deps_graph = conan_api.graph.load_graph_consumer(
124143
path=path,
125144
name=None,
@@ -252,13 +271,32 @@ def publish(self) -> None:
252271
remotes=all_remotes, # Use all remotes for dependency resolution during export
253272
)
254273

255-
# Step 2: Get default profiles, handle case when no default profile exists
256-
profile_host, profile_build = self._resolve_profiles(conan_api)
274+
# Step 2: Get profiles with fallback to auto-detection
275+
try:
276+
profile_host_path = conan_api.profiles.get_default_host()
277+
profile_build_path = conan_api.profiles.get_default_build()
278+
279+
# Check if profile paths exist
280+
if profile_host_path is None or profile_build_path is None:
281+
raise ValueError('Profile paths are None')
282+
283+
# Load the actual profile objects
284+
profile_host = conan_api.profiles.get_profile([profile_host_path])
285+
profile_build = conan_api.profiles.get_profile([profile_build_path])
286+
287+
# Check if profiles are valid
288+
if profile_host is None or profile_build is None:
289+
raise ValueError('Profile loading returned None')
290+
291+
except Exception:
292+
# Use Conan's auto-detection to create minimal profiles
293+
profile_host = conan_api.profiles.detect()
294+
profile_build = conan_api.profiles.detect()
257295

258296
# Step 3: Build dependency graph for the package - prepare parameters
259297
path = str(conanfile_path)
260298
remotes = all_remotes # Use all remotes for dependency resolution
261-
299+
262300
deps_graph = conan_api.graph.load_graph_consumer(
263301
path=path,
264302
name=None,
@@ -308,51 +346,3 @@ def publish(self) -> None:
308346
)
309347
else:
310348
raise ProviderInstallationError('conan', 'No packages found to upload')
311-
312-
def _resolve_profiles(self, conan_api) -> tuple:
313-
"""Resolve host and build profiles with robust fallback handling.
314-
315-
Args:
316-
conan_api: The Conan API instance
317-
318-
Returns:
319-
Tuple of (profile_host, profile_build)
320-
"""
321-
try:
322-
profile_host_path = conan_api.profiles.get_default_host()
323-
profile_build_path = conan_api.profiles.get_default_build()
324-
325-
# Try to load profiles from paths if they exist
326-
if profile_host_path is None:
327-
profile_host = conan_api.profiles.detect()
328-
else:
329-
profile_host = conan_api.profiles.get_profile([profile_host_path])
330-
if profile_host is None:
331-
profile_host = conan_api.profiles.detect()
332-
333-
if profile_build_path is None:
334-
profile_build = conan_api.profiles.detect()
335-
else:
336-
profile_build = conan_api.profiles.get_profile([profile_build_path])
337-
if profile_build is None:
338-
profile_build = conan_api.profiles.detect()
339-
340-
return profile_host, profile_build
341-
342-
except Exception:
343-
# If profile operations fail with other exceptions, let's try detect() as fallback
344-
logger = logging.getLogger('cppython.conan')
345-
logger.warning('Profile resolution failed, attempting detect() fallback')
346-
347-
try:
348-
# Try to use detect() as fallback
349-
profile_host = conan_api.profiles.detect()
350-
profile_build = conan_api.profiles.detect()
351-
352-
# If detect returns None, that's okay - we'll let Conan handle None gracefully
353-
return profile_host, profile_build
354-
355-
except Exception as detect_error:
356-
logger.warning('Profile detect() also failed: %s', str(detect_error))
357-
# Return None for both profiles - Conan should handle this according to docs
358-
return None, None

tests/unit/plugins/conan/test_install.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def test_install_with_profile_exception_fallback_to_detect_works(
358358
"""
359359
# Configure the API mock to throw exception on profile calls but detect() works
360360
conan_mock_api.profiles.get_default_host.side_effect = Exception('Profile not found')
361-
361+
362362
# Setup dependencies
363363
plugin.core_data.cppython_data.dependencies = conan_mock_dependencies
364364

@@ -368,7 +368,7 @@ def test_install_with_profile_exception_fallback_to_detect_works(
368368
# Verify that the fallback was used
369369
conan_setup_mocks['conan_api_constructor'].assert_called_once()
370370
conan_mock_api.profiles.get_default_host.assert_called_once()
371-
371+
372372
# Verify detect was called for fallback (should be called twice for fallback)
373373
assert conan_mock_api.profiles.detect.call_count >= EXPECTED_PROFILE_CALLS
374374

0 commit comments

Comments
 (0)