Skip to content

Commit f2eccab

Browse files
authored
Merge pull request #253 from FoamyGuy/autofile_outside_root_subimports
fix subimport finding for auto-files that are not in the root.
2 parents d1bfdbb + 18a4687 commit f2eccab

File tree

7 files changed

+71
-10
lines changed

7 files changed

+71
-10
lines changed

circup/command_utils.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -673,14 +673,15 @@ def imports_from_code(full_content):
673673
return sorted(imports)
674674

675675

676-
def get_all_imports(
677-
backend, auto_file_content, mod_names, current_module, visited=None
676+
def get_all_imports( # pylint: disable=too-many-arguments,too-many-locals, too-many-branches
677+
backend, auto_file_content, auto_file_path, mod_names, current_module, visited=None
678678
):
679679
"""
680680
Recursively retrieve imports from files on the backend
681681
682682
:param Backend backend: The current backend object
683683
:param str auto_file_content: Content of the python file to analyse
684+
:param str auto_file_path: Path to the python file to analyse
684685
:param list mod_names: Lits of supported bundle mod names
685686
:param str current_module: Name of the call context module if recursive call
686687
:param set visited: Modules previously visited
@@ -714,18 +715,37 @@ def get_all_imports(
714715
install_module = install
715716
# possible files for the module: .py or __init__.py (if directory)
716717
file_name = os.path.join(*install_module.split(".")) + ".py"
717-
exists = backend.file_exists(file_name)
718+
try:
719+
file_location = os.path.join(
720+
*auto_file_path.replace(str(backend.device_location), "").split(
721+
"/"
722+
)[:-1]
723+
)
724+
725+
full_location = os.path.join(file_location, file_name)
726+
727+
except TypeError:
728+
# file is in root of CIRCUITPY
729+
full_location = file_name
730+
731+
exists = backend.file_exists(full_location)
718732
if not exists:
719733
file_name = os.path.join(*install_module.split("."), "__init__.py")
720-
exists = backend.file_exists(file_name)
734+
full_location = file_name
735+
exists = backend.file_exists(full_location)
721736
if not exists:
722737
continue
723738
install_module += ".__init__"
724739
# get the content and parse it recursively
725-
auto_file_content = backend.get_file_content(file_name)
740+
auto_file_content = backend.get_file_content(full_location)
726741
if auto_file_content:
727742
sub_imports = get_all_imports(
728-
backend, auto_file_content, mod_names, install_module, visited
743+
backend,
744+
auto_file_content,
745+
auto_file_path,
746+
mod_names,
747+
install_module,
748+
visited,
729749
)
730750
requested_installs.extend(sub_imports)
731751

@@ -775,7 +795,9 @@ def libraries_from_auto_file(backend, auto_file, mod_names):
775795
# from file name to module name (in case it's in a subpackage)
776796
click.secho(f"Finding imports from: {auto_file}", fg="green")
777797
current_module = auto_file.rstrip(".py").replace(os.path.sep, ".")
778-
return get_all_imports(backend, auto_file_content, mod_names, current_module)
798+
return get_all_imports(
799+
backend, auto_file_content, auto_file, mod_names, current_module
800+
)
779801

780802

781803
def get_device_path(host, port, password, path):
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-FileCopyrightText: 2021 Jeff Epler for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
# pylint: disable=all
5+
import os, sys
6+
import adafruit_bus_device
7+
from adafruit_button import Button
8+
from adafruit_esp32spi import adafruit_esp32spi_socketpool
9+
from adafruit_display_text import wrap_text_to_pixels, wrap_text_to_lines
10+
import adafruit_hid.consumer_control
11+
import import_styles_sub
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-FileCopyrightText: 2025 Neradoc
2+
#
3+
# SPDX-License-Identifier: MIT
4+
# pylint: disable=all
5+
import adafruit_ntp

tests/mock_device/import_styles.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-FileCopyrightText: 2021 Jeff Epler for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
# pylint: disable=all
5+
import os, sys
6+
import adafruit_bus_device
7+
from adafruit_button import Button
8+
from adafruit_esp32spi import adafruit_esp32spi_socketpool
9+
from adafruit_display_text import wrap_text_to_pixels, wrap_text_to_lines
10+
import adafruit_hid.consumer_control
11+
import import_styles_sub

tests/mock_device_2/boot_out.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Adafruit CircuitPython 8.1.0 on 2019-08-02; Adafruit CircuitPlayground Express with samd21g18
1+
Adafruit CircuitPython 9.0.0 on 2019-08-02; Adafruit CircuitPlayground Express with samd21g18
22
Board ID:this_is_a_board
33
UID:AAAABBBBCCCC
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-FileCopyrightText: 2025 Neradoc
2+
#
3+
# SPDX-License-Identifier: MIT
4+
# pylint: disable=all
5+
import adafruit_ntp

tests/test_circup.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,13 @@ def test_get_all_imports():
11851185
with open(test_file, "r", encoding="utf8") as fp:
11861186
test_data = fp.read()
11871187

1188-
result = get_all_imports(backend, test_data, mod_names, current_module="")
1188+
result = get_all_imports(
1189+
backend,
1190+
test_data,
1191+
os.path.join(backend.device_location, "import_styles.py"),
1192+
mod_names,
1193+
current_module="",
1194+
)
11891195

11901196
assert result == [
11911197
"adafruit_bus_device",
@@ -1213,7 +1219,7 @@ def test_libraries_from_auto_file_local():
12131219
"adafruit_ntp",
12141220
]
12151221

1216-
auto_file = "./tests/import_styles.py"
1222+
auto_file = "apps/test_app/import_styles.py"
12171223

12181224
with mock.patch("circup.logger.info") as mock_logger, mock.patch(
12191225
"circup.os.path.isfile", return_value=True
@@ -1265,6 +1271,7 @@ def test_libraries_from_auto_file_board():
12651271
result = libraries_from_auto_file(backend, auto_file, mod_names)
12661272

12671273
assert result == [
1274+
"adafruit_ntp",
12681275
"adafruit_spd1608",
12691276
"adafruit_spd1656",
12701277
"adafruit_ssd1675",

0 commit comments

Comments
 (0)