Skip to content

Commit 7c2fbc7

Browse files
committed
Make get_platform_installation_path() return full path
The previous functionality of providing the path split into the "base" and "platform" components was unused and thus added unnecessary complexity to the code. This is purely a refactoring.
1 parent a52a021 commit 7c2fbc7

File tree

2 files changed

+29
-77
lines changed

2 files changed

+29
-77
lines changed

compilesketches/compilesketches.py

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -422,37 +422,23 @@ def install_platforms_from_path(self, platform_list):
422422

423423
platform_installation_path = self.get_platform_installation_path(platform=platform)
424424

425-
# Create the parent path if it doesn't exist already. This must be the immediate parent, whereas
426-
# get_platform_installation_path().platform will be multiple nested folders under the base path
427-
platform_installation_path_parent = (
428-
pathlib.Path(platform_installation_path.base, platform_installation_path.platform).parent
429-
)
430-
platform_installation_path_parent.mkdir(parents=True, exist_ok=True)
425+
# Create the parent path if it doesn't exist already.
426+
platform_installation_path.parent.mkdir(parents=True, exist_ok=True)
431427

432428
# Install the platform by creating a symlink
433-
destination_path = platform_installation_path.base.joinpath(platform_installation_path.platform)
434-
destination_path.symlink_to(target=source_path, target_is_directory=True)
429+
platform_installation_path.symlink_to(target=source_path, target_is_directory=True)
435430

436431
def get_platform_installation_path(self, platform):
437432
"""Return the correct installation path for the given platform
438433
439434
Keyword arguments:
440435
platform -- dictionary defining the platform dependency
441436
"""
442-
443-
class PlatformInstallationPath:
444-
def __init__(self):
445-
self.base = pathlib.PurePath()
446-
self.platform = pathlib.PurePath()
447-
448-
platform_installation_path = PlatformInstallationPath()
449-
450437
platform_vendor = platform[self.dependency_name_key].split(sep=":")[0]
451438
platform_architecture = platform[self.dependency_name_key].rsplit(sep=":", maxsplit=1)[1]
452439

453440
# Default to installing to the sketchbook
454-
platform_installation_path.base = self.user_platforms_path
455-
platform_installation_path.platform = pathlib.PurePath(platform_vendor, platform_architecture)
441+
platform_installation_path = self.user_platforms_path.joinpath(platform_vendor, platform_architecture)
456442

457443
# I have no clue why this is needed, but arduino-cli core list fails if this isn't done first. The 3rd party
458444
# platforms are still shown in the list even if their index URLs are not specified to the command via the
@@ -464,16 +450,15 @@ def __init__(self):
464450
for installed_platform in installed_platform_list:
465451
if installed_platform["ID"] == platform[self.dependency_name_key]:
466452
# The platform has been installed via Board Manager, so do an overwrite
467-
platform_installation_path.base = self.board_manager_platforms_path
468-
platform_installation_path.platform = (
469-
pathlib.PurePath(platform_vendor,
470-
"hardware",
471-
platform_architecture,
472-
installed_platform["Installed"])
453+
platform_installation_path = (
454+
self.board_manager_platforms_path.joinpath(platform_vendor,
455+
"hardware",
456+
platform_architecture,
457+
installed_platform["Installed"])
473458
)
474459

475460
# Remove the existing installation so it can be replaced by the installation function
476-
shutil.rmtree(path=platform_installation_path.base.joinpath(platform_installation_path.platform))
461+
shutil.rmtree(path=platform_installation_path)
477462

478463
break
479464

@@ -500,8 +485,8 @@ def install_platforms_from_repository(self, platform_list):
500485
self.install_from_repository(url=platform[self.dependency_source_url_key],
501486
git_ref=git_ref,
502487
source_path=source_path,
503-
destination_parent_path=destination_path.base,
504-
destination_name=destination_path.platform)
488+
destination_parent_path=destination_path.parent,
489+
destination_name=destination_path.name)
505490

506491
def get_repository_dependency_ref(self, dependency):
507492
"""Return the appropriate git ref value for a repository dependency
@@ -591,8 +576,8 @@ def install_platforms_from_download(self, platform_list):
591576

592577
install_from_download(url=platform[self.dependency_source_url_key],
593578
source_path=source_path,
594-
destination_parent_path=destination_path.base,
595-
destination_name=destination_path.platform)
579+
destination_parent_path=destination_path.parent,
580+
destination_name=destination_path.name)
596581

597582
def install_libraries(self):
598583
"""Install Arduino libraries."""

compilesketches/tests/test_compilesketches.py

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -630,15 +630,7 @@ def test_get_manager_dependency_name(dependency, expected_name):
630630
(True, [{compilesketches.CompileSketches.dependency_source_path_key: pathlib.Path("Foo")}])]
631631
)
632632
def test_install_platforms_from_path(capsys, mocker, path_exists, platform_list):
633-
class PlatformInstallationPath:
634-
def __init__(self):
635-
self.parent = pathlib.PurePath()
636-
self.name = pathlib.PurePath()
637-
638-
platform_installation_path = PlatformInstallationPath()
639-
platform_installation_path.base = pathlib.Path("/foo/PlatformInstallationPathParent")
640-
platform_installation_path.platform = pathlib.Path("PlatformInstallationPathName")
641-
symlink_source_path = pathlib.Path("/foo/SymlinkSourcePath")
633+
platform_installation_path = pathlib.Path("/foo/PlatformInstallationPathParent/PlatformInstallationPathName")
642634

643635
compile_sketches = get_compilesketches_object()
644636

@@ -647,7 +639,6 @@ def __init__(self):
647639
autospec=True,
648640
return_value=platform_installation_path)
649641
mocker.patch.object(pathlib.Path, "mkdir", autospec=True)
650-
mocker.patch.object(pathlib.Path, "joinpath", autospec=True, return_value=symlink_source_path)
651642
mocker.patch.object(pathlib.Path, "symlink_to", autospec=True)
652643

653644
if not path_exists:
@@ -664,17 +655,14 @@ def __init__(self):
664655
compile_sketches.install_platforms_from_path(platform_list=platform_list)
665656

666657
get_platform_installation_path_calls = []
667-
joinpath_calls = []
668658
mkdir_calls = []
669659
symlink_to_calls = []
670660
for platform in platform_list:
671661
get_platform_installation_path_calls.append(unittest.mock.call(compile_sketches, platform=platform))
672-
mkdir_calls.append(unittest.mock.call(platform_installation_path.base, parents=True, exist_ok=True))
673-
joinpath_calls.append(unittest.mock.call(platform_installation_path.base,
674-
platform_installation_path.platform))
662+
mkdir_calls.append(unittest.mock.call(platform_installation_path.parent, parents=True, exist_ok=True))
675663
symlink_to_calls.append(
676664
unittest.mock.call(
677-
symlink_source_path,
665+
platform_installation_path,
678666
target=compilesketches.absolute_path(
679667
platform[compilesketches.CompileSketches.dependency_source_path_key]
680668
),
@@ -684,35 +672,29 @@ def __init__(self):
684672

685673
# noinspection PyUnresolvedReferences
686674
pathlib.Path.mkdir.assert_has_calls(calls=mkdir_calls)
687-
# noinspection PyUnresolvedReferences
688-
pathlib.Path.joinpath.assert_has_calls(calls=joinpath_calls)
689675
pathlib.Path.symlink_to.assert_has_calls(calls=symlink_to_calls)
690676

691677

692678
@pytest.mark.parametrize(
693679
"platform,"
694680
"command_data_stdout,"
695-
"expected_installation_path_base,"
696-
"expected_installation_path_platform,"
681+
"expected_installation_path,"
697682
"expected_rmtree",
698683
# No match to previously installed platforms
699684
[({compilesketches.CompileSketches.dependency_name_key: "foo:bar"},
700685
"[{\"ID\": \"asdf:zxcv\"}]",
701-
pathlib.PurePath("/foo/UserPlatformsPath"),
702-
pathlib.PurePath("foo/bar"),
686+
pathlib.PurePath("/foo/UserPlatformsPath/foo/bar"),
703687
False),
704688
# Match with previously installed platform
705689
({compilesketches.CompileSketches.dependency_name_key: "foo:bar"},
706690
"[{\"ID\": \"foo:bar\", \"Installed\": \"1.2.3\"}]",
707-
pathlib.PurePath("/foo/BoardManagerPlatformsPath"),
708-
pathlib.PurePath("foo/hardware/bar/1.2.3"),
691+
pathlib.PurePath("/foo/BoardManagerPlatformsPath/foo/hardware/bar/1.2.3"),
709692
True)]
710693
)
711694
def test_get_platform_installation_path(mocker,
712695
platform,
713696
command_data_stdout,
714-
expected_installation_path_base,
715-
expected_installation_path_platform,
697+
expected_installation_path,
716698
expected_rmtree):
717699
class CommandData:
718700
def __init__(self, stdout):
@@ -728,8 +710,7 @@ def __init__(self, stdout):
728710
compile_sketches.board_manager_platforms_path = pathlib.PurePath("/foo/BoardManagerPlatformsPath")
729711

730712
platform_installation_path = compile_sketches.get_platform_installation_path(platform=platform)
731-
assert platform_installation_path.base == expected_installation_path_base
732-
assert platform_installation_path.platform == expected_installation_path_platform
713+
assert platform_installation_path == expected_installation_path
733714

734715
run_arduino_cli_command_calls = [unittest.mock.call(compile_sketches, command=["core", "update-index"]),
735716
unittest.mock.call(compile_sketches, command=["core", "list", "--format", "json"])]
@@ -739,7 +720,7 @@ def __init__(self, stdout):
739720
if expected_rmtree is True:
740721
# noinspection PyUnresolvedReferences
741722
shutil.rmtree.assert_called_once_with(
742-
path=platform_installation_path.base.joinpath(platform_installation_path.platform)
723+
path=platform_installation_path
743724
)
744725
else:
745726
# noinspection PyUnresolvedReferences
@@ -756,14 +737,7 @@ def test_install_platforms_from_repository(mocker):
756737

757738
git_ref = unittest.mock.sentinel.git_ref
758739

759-
class PlatformInstallationPath:
760-
def __init__(self):
761-
self.base = pathlib.PurePath()
762-
self.platform = pathlib.PurePath()
763-
764-
platform_installation_path = PlatformInstallationPath()
765-
platform_installation_path.base = pathlib.Path("/foo/PlatformInstallationPathParent")
766-
platform_installation_path.platform = pathlib.Path("PlatformInstallationPathName")
740+
platform_installation_path = pathlib.Path("/foo/PlatformInstallationPathParent/PlatformInstallationPathName")
767741

768742
expected_source_path_list = [unittest.mock.sentinel.source_path, "."]
769743
expected_destination_name_list = [unittest.mock.sentinel.destination_name, None]
@@ -791,8 +765,8 @@ def __init__(self):
791765
url=platform[compilesketches.CompileSketches.dependency_source_url_key],
792766
git_ref=git_ref,
793767
source_path=expected_source_path,
794-
destination_parent_path=platform_installation_path.base,
795-
destination_name=platform_installation_path.platform)
768+
destination_parent_path=platform_installation_path.parent,
769+
destination_name=platform_installation_path.name)
796770
)
797771

798772
compile_sketches.get_repository_dependency_ref.assert_has_calls(calls=get_repository_dependency_ref_calls)
@@ -817,14 +791,7 @@ def test_install_platforms_from_download(mocker):
817791
{compilesketches.CompileSketches.dependency_source_url_key: unittest.mock.sentinel.source_url2}
818792
]
819793

820-
class PlatformInstallationPath:
821-
def __init__(self):
822-
self.base = pathlib.PurePath()
823-
self.platform = pathlib.PurePath()
824-
825-
platform_installation_path = PlatformInstallationPath()
826-
platform_installation_path.parent = pathlib.Path("/foo/PlatformInstallationPathParent")
827-
platform_installation_path.name = pathlib.Path("PlatformInstallationPathName")
794+
platform_installation_path = pathlib.Path("/foo/PlatformInstallationPathParent/PlatformInstallationPathName")
828795

829796
expected_source_path_list = [unittest.mock.sentinel.source_path, "."]
830797

@@ -844,8 +811,8 @@ def __init__(self):
844811
install_platforms_from_download_calls.append(
845812
unittest.mock.call(url=platform[compilesketches.CompileSketches.dependency_source_url_key],
846813
source_path=expected_source_path,
847-
destination_parent_path=platform_installation_path.base,
848-
destination_name=platform_installation_path.platform)
814+
destination_parent_path=platform_installation_path.parent,
815+
destination_name=platform_installation_path.name)
849816
)
850817
compilesketches.install_from_download.assert_has_calls(calls=install_platforms_from_download_calls)
851818

0 commit comments

Comments
 (0)