Skip to content

Commit eea555c

Browse files
Fixing review comments
Signed-off-by: Lalatendu Mohanty <lmohanty@redhat.com>
1 parent 49e7dbf commit eea555c

3 files changed

Lines changed: 65 additions & 81 deletions

File tree

src/fromager/bootstrapper.py

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ class SourceBuildResult:
5151
wheel_filename: pathlib.Path | None
5252
sdist_filename: pathlib.Path | None
5353
unpack_dir: pathlib.Path
54-
sdist_root_dir: pathlib.Path
55-
build_env: build_environment.BuildEnvironment
56-
source_url_type: str
54+
sdist_root_dir: pathlib.Path | None
55+
build_env: build_environment.BuildEnvironment | None
56+
source_type: SourceType
5757

5858

5959
class Bootstrapper:
@@ -190,17 +190,9 @@ def bootstrap(self, req: Requirement, req_type: RequirementType) -> Version:
190190
# we're given.
191191
self.why.append((req_type, req, resolved_version))
192192

193-
# for cleanup
194-
build_env: build_environment.BuildEnvironment | None = None
195-
sdist_root_dir: pathlib.Path | None = None
196193
cached_wheel_filename: pathlib.Path | None = None
197-
wheel_filename: pathlib.Path | None = None
198-
sdist_filename: pathlib.Path | None = None
199-
unpack_dir: pathlib.Path | None = None
200194
unpacked_cached_wheel: pathlib.Path | None = None
201195

202-
source_url_type = sources.get_source_type(self.ctx, req)
203-
204196
if pbi.pre_built:
205197
wheel_filename, unpack_dir = self._download_prebuilt(
206198
req=req,
@@ -209,10 +201,14 @@ def bootstrap(self, req: Requirement, req_type: RequirementType) -> Version:
209201
wheel_url=source_url,
210202
)
211203
# Remember that this is a prebuilt wheel, and where we got it.
212-
source_url_type = str(SourceType.PREBUILT)
213-
sdist_filename = None
214-
sdist_root_dir = None
215-
build_env = None
204+
build_result = SourceBuildResult(
205+
wheel_filename=wheel_filename,
206+
sdist_filename=None,
207+
unpack_dir=unpack_dir,
208+
sdist_root_dir=None,
209+
build_env=None,
210+
source_type=SourceType.PREBUILT,
211+
)
216212
else:
217213
# Look for an existing wheel in caches (3 levels: build, downloads,
218214
# cache server) before building from source.
@@ -230,31 +226,23 @@ def bootstrap(self, req: Requirement, req_type: RequirementType) -> Version:
230226
unpacked_cached_wheel=unpacked_cached_wheel,
231227
)
232228

233-
# Unpack result
234-
wheel_filename = build_result.wheel_filename
235-
sdist_filename = build_result.sdist_filename
236-
unpack_dir = build_result.unpack_dir
237-
sdist_root_dir = build_result.sdist_root_dir
238-
build_env = build_result.build_env
239-
source_url_type = build_result.source_url_type
240-
241229
hooks.run_post_bootstrap_hooks(
242230
ctx=self.ctx,
243231
req=req,
244232
dist_name=canonicalize_name(req.name),
245233
dist_version=str(resolved_version),
246-
sdist_filename=sdist_filename,
247-
wheel_filename=wheel_filename,
234+
sdist_filename=build_result.sdist_filename,
235+
wheel_filename=build_result.wheel_filename,
248236
)
249237

250238
install_dependencies = self._get_install_dependencies(
251239
req=req,
252240
resolved_version=resolved_version,
253-
wheel_filename=wheel_filename,
254-
sdist_filename=sdist_filename,
255-
sdist_root_dir=sdist_root_dir,
256-
build_env=build_env,
257-
unpack_dir=unpack_dir,
241+
wheel_filename=build_result.wheel_filename,
242+
sdist_filename=build_result.sdist_filename,
243+
sdist_root_dir=build_result.sdist_root_dir,
244+
build_env=build_result.build_env,
245+
unpack_dir=build_result.unpack_dir,
258246
)
259247

260248
logger.debug(
@@ -266,7 +254,7 @@ def bootstrap(self, req: Requirement, req_type: RequirementType) -> Version:
266254
req=req,
267255
version=resolved_version,
268256
source_url=source_url,
269-
source_url_type=source_url_type,
257+
source_type=build_result.source_type,
270258
prebuilt=pbi.pre_built,
271259
constraint=constraint,
272260
)
@@ -282,7 +270,7 @@ def bootstrap(self, req: Requirement, req_type: RequirementType) -> Version:
282270

283271
# we are done processing this req, so lets remove it from the why chain
284272
self.why.pop()
285-
self.ctx.clean_build_dirs(sdist_root_dir, build_env)
273+
self.ctx.clean_build_dirs(build_result.sdist_root_dir, build_result.build_env)
286274
return resolved_version
287275

288276
@property
@@ -606,15 +594,15 @@ def _build_from_source(
606594
req, resolved_version, sdist_root_dir, build_env
607595
)
608596

609-
source_url_type = sources.get_source_type(self.ctx, req)
597+
source_type = sources.get_source_type(self.ctx, req)
610598

611599
return SourceBuildResult(
612600
wheel_filename=wheel_filename,
613601
sdist_filename=sdist_filename,
614602
unpack_dir=unpack_dir,
615603
sdist_root_dir=sdist_root_dir,
616604
build_env=build_env,
617-
source_url_type=source_url_type,
605+
source_type=source_type,
618606
)
619607

620608
def _look_for_existing_wheel(
@@ -1108,7 +1096,7 @@ def _add_to_build_order(
11081096
req: Requirement,
11091097
version: Version,
11101098
source_url: str,
1111-
source_url_type: str,
1099+
source_type: SourceType,
11121100
prebuilt: bool = False,
11131101
constraint: Requirement | None = None,
11141102
) -> None:
@@ -1129,7 +1117,7 @@ def _add_to_build_order(
11291117
"version": str(version),
11301118
"prebuilt": prebuilt,
11311119
"source_url": source_url,
1132-
"source_url_type": source_url_type,
1120+
"source_url_type": str(source_type),
11331121
}
11341122
if req.url:
11351123
info["source_url"] = req.url

src/fromager/sources.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,33 @@
2727
overrides,
2828
packagesettings,
2929
pyproject,
30-
requirements_file,
3130
resolver,
3231
tarballs,
3332
vendor_rust,
3433
)
3534
from .http_retry import RETRYABLE_EXCEPTIONS, retry_on_exception
3635
from .request_session import session
37-
from .requirements_file import RequirementType
36+
from .requirements_file import RequirementType, SourceType
3837

3938
if typing.TYPE_CHECKING:
4039
from . import build_environment, context
4140

4241
logger = logging.getLogger(__name__)
4342

4443

45-
def get_source_type(ctx: context.WorkContext, req: Requirement) -> str:
46-
source_type = requirements_file.SourceType.SDIST
44+
def get_source_type(ctx: context.WorkContext, req: Requirement) -> SourceType:
45+
source_type = SourceType.SDIST
4746
if req.url:
48-
return requirements_file.SourceType.GIT
47+
return SourceType.GIT
4948
pbi = ctx.package_build_info(req)
5049
if (
5150
overrides.find_override_method(req.name, "download_source")
5251
or overrides.find_override_method(req.name, "resolve_source")
5352
or overrides.find_override_method(req.name, "get_resolver_provider")
5453
or pbi.download_source_url(resolve_template=False)
5554
):
56-
source_type = requirements_file.SourceType.OVERRIDE
57-
return str(source_type)
55+
source_type = SourceType.OVERRIDE
56+
return source_type
5857

5958

6059
@metrics.timeit(description="download source")

tests/test_bootstrapper.py

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from fromager import bootstrapper
1010
from fromager.context import WorkContext
1111
from fromager.dependency_graph import DependencyGraph
12-
from fromager.requirements_file import RequirementType
12+
from fromager.requirements_file import RequirementType, SourceType
1313

1414
old_graph = DependencyGraph()
1515

@@ -290,13 +290,13 @@ def test_build_order(tmp_context: WorkContext) -> None:
290290
req=Requirement("buildme>1.0"),
291291
version=Version("6.0"),
292292
source_url="url",
293-
source_url_type="sdist",
293+
source_type=SourceType.SDIST,
294294
)
295295
bt._add_to_build_order(
296296
req=Requirement("testdist>1.0"),
297297
version=Version("1.2"),
298298
source_url="url",
299-
source_url_type="sdist",
299+
source_type=SourceType.SDIST,
300300
)
301301
contents_str = bt._build_order_filename.read_text()
302302
contents = json.loads(contents_str)
@@ -329,19 +329,19 @@ def test_build_order_repeats(tmp_context: WorkContext) -> None:
329329
Requirement("buildme>1.0"),
330330
Version("6.0"),
331331
"url",
332-
"sdist",
332+
SourceType.SDIST,
333333
)
334334
bt._add_to_build_order(
335335
Requirement("buildme>1.0"),
336336
Version("6.0"),
337337
"url",
338-
"sdist",
338+
SourceType.SDIST,
339339
)
340340
bt._add_to_build_order(
341341
Requirement("buildme[extra]>1.0"),
342342
Version("6.0"),
343343
"url",
344-
"sdist",
344+
SourceType.SDIST,
345345
)
346346
contents_str = bt._build_order_filename.read_text()
347347
contents = json.loads(contents_str)
@@ -365,13 +365,13 @@ def test_build_order_name_canonicalization(tmp_context: WorkContext) -> None:
365365
Requirement("flit-core>1.0"),
366366
Version("3.9.0"),
367367
"url",
368-
"sdist",
368+
SourceType.SDIST,
369369
)
370370
bt._add_to_build_order(
371371
Requirement("flit_core>1.0"),
372372
Version("3.9.0"),
373373
"url",
374-
"sdist",
374+
SourceType.SDIST,
375375
)
376376
contents_str = bt._build_order_filename.read_text()
377377
contents = json.loads(contents_str)
@@ -447,50 +447,47 @@ def test_find_cached_wheel_returns_tuple(tmp_context: WorkContext) -> None:
447447
assert len(result) == 2
448448

449449

450-
def test_get_install_dependencies_returns_list(tmp_context: WorkContext) -> None:
450+
@patch("fromager.dependencies.get_install_dependencies_of_wheel", return_value=set())
451+
def test_get_install_dependencies_returns_list(
452+
mock_get_deps: Mock, tmp_context: WorkContext
453+
) -> None:
451454
"""Verify _get_install_dependencies returns list."""
452455
bt = bootstrapper.Bootstrapper(tmp_context)
453456

454457
# Create fake wheel file and mock dependencies
455458
wheel_file = pathlib.Path("/fake/package-1.0.0-py3-none-any.whl")
456459
unpack_dir = tmp_context.work_dir
457460

458-
# Mock the dependency extraction to return empty set
459-
import fromager.dependencies as deps
460-
461-
original_func = deps.get_install_dependencies_of_wheel
462-
deps.get_install_dependencies_of_wheel = Mock(return_value=set())
463-
464-
try:
465-
result = bt._get_install_dependencies(
466-
req=Requirement("test-package"),
467-
resolved_version=Version("1.0.0"),
468-
wheel_filename=wheel_file,
469-
sdist_filename=None,
470-
sdist_root_dir=None,
471-
build_env=None,
472-
unpack_dir=unpack_dir,
473-
)
461+
result = bt._get_install_dependencies(
462+
req=Requirement("test-package"),
463+
resolved_version=Version("1.0.0"),
464+
wheel_filename=wheel_file,
465+
sdist_filename=None,
466+
sdist_root_dir=None,
467+
build_env=None,
468+
unpack_dir=unpack_dir,
469+
)
474470

475-
# Verify return type is list
476-
assert isinstance(result, list)
477-
finally:
478-
deps.get_install_dependencies_of_wheel = original_func
471+
# Verify return type is list
472+
assert isinstance(result, list)
473+
# Verify the mocked function was called
474+
mock_get_deps.assert_called_once()
479475

480476

481477
def test_build_from_source_returns_dataclass(tmp_context: WorkContext) -> None:
482-
"""Verify _build_from_source returns SourceBuildResult."""
478+
"""Verify _build_from_source returns SourceBuildResult with correct values."""
483479
bt = bootstrapper.Bootstrapper(tmp_context)
484480

485481
mock_sdist_root = tmp_context.work_dir / "package-1.0.0" / "package-1.0.0"
486482
mock_sdist_root.parent.mkdir(parents=True, exist_ok=True)
487483
mock_source_file = tmp_context.work_dir / "package-1.0.0.tar.gz"
488484
mock_wheel = tmp_context.work_dir / "package-1.0.0-py3-none-any.whl"
485+
expected_unpack_dir = mock_sdist_root.parent
489486

490487
with (
491488
patch("fromager.sources.download_source", return_value=mock_source_file),
492489
patch("fromager.sources.prepare_source", return_value=mock_sdist_root),
493-
patch("fromager.sources.get_source_type", return_value="sdist"),
490+
patch("fromager.sources.get_source_type", return_value=SourceType.SDIST),
494491
patch.object(bt, "_prepare_build_dependencies"),
495492
patch.object(bt, "_build_wheel", return_value=(mock_wheel, None)),
496493
):
@@ -506,10 +503,10 @@ def test_build_from_source_returns_dataclass(tmp_context: WorkContext) -> None:
506503
# Verify return type is SourceBuildResult
507504
assert isinstance(result, bootstrapper.SourceBuildResult)
508505

509-
# Verify all expected fields are present
510-
assert hasattr(result, "wheel_filename")
511-
assert hasattr(result, "sdist_filename")
512-
assert hasattr(result, "unpack_dir")
513-
assert hasattr(result, "sdist_root_dir")
514-
assert hasattr(result, "build_env")
515-
assert hasattr(result, "source_url_type")
506+
# Verify all expected fields have correct values
507+
assert result.wheel_filename == mock_wheel
508+
assert result.sdist_filename is None
509+
assert result.unpack_dir == expected_unpack_dir
510+
assert result.sdist_root_dir == mock_sdist_root
511+
assert result.build_env is not None
512+
assert result.source_type == SourceType.SDIST

0 commit comments

Comments
 (0)