Skip to content

Commit 2df156e

Browse files
committed
exclude local modules from scan command
1 parent be37ab4 commit 2df156e

2 files changed

Lines changed: 74 additions & 2 deletions

File tree

src/datacustomcode/scan.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,27 @@ def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
258258

259259

260260
def scan_file_for_imports(file_path: str) -> Set[str]:
261-
"""Scan a Python file for external package imports."""
261+
"""Scan a Python file for external package imports.
262+
263+
Excludes local modules (Python files in the same directory).
264+
"""
262265
with open(file_path, "r") as f:
263266
code = f.read()
264267
tree = ast.parse(code)
265268
visitor = ImportVisitor()
266269
visitor.visit(tree)
267-
return visitor.imports
270+
271+
# Filter out local modules (files in the same directory)
272+
file_dir = os.path.dirname(file_path)
273+
filtered_imports = set()
274+
for package in visitor.imports:
275+
# Check if this is a local module (a .py file exists in the same directory)
276+
local_module_path = os.path.join(file_dir, f"{package}.py")
277+
if not os.path.exists(local_module_path):
278+
# Not a local module, keep it in the imports
279+
filtered_imports.add(package)
280+
281+
return filtered_imports
268282

269283

270284
def write_requirements_file(file_path: str) -> str:

tests/test_scan.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,3 +927,61 @@ def test_excluded_packages(self):
927927
assert "pyspark" not in imports
928928
finally:
929929
os.unlink(temp_path)
930+
931+
def test_local_module_exclusion(self):
932+
"""Test that local modules (files in the same directory) are excluded."""
933+
# Create a temporary directory with multiple Python files
934+
temp_dir = tempfile.mkdtemp()
935+
936+
try:
937+
# Create a local module file
938+
utility_path = os.path.join(temp_dir, "utility.py")
939+
with open(utility_path, "w") as f:
940+
f.write(textwrap.dedent(
941+
"""
942+
def helper_function():
943+
return "helper"
944+
"""
945+
))
946+
947+
# Create another local module
948+
helpers_path = os.path.join(temp_dir, "helpers.py")
949+
with open(helpers_path, "w") as f:
950+
f.write(textwrap.dedent(
951+
"""
952+
def another_helper():
953+
return "another"
954+
"""
955+
))
956+
957+
# Create the main script that imports both local modules and external packages
958+
main_content = textwrap.dedent(
959+
"""
960+
from utility import helper_function
961+
from helpers import another_helper
962+
import pandas as pd
963+
import numpy as np
964+
"""
965+
)
966+
main_path = os.path.join(temp_dir, "main.py")
967+
with open(main_path, "w") as f:
968+
f.write(main_content)
969+
970+
# Scan for imports
971+
imports = scan_file_for_imports(main_path)
972+
973+
# External packages should be included
974+
assert "pandas" in imports
975+
assert "numpy" in imports
976+
977+
# Local modules should be excluded
978+
assert "utility" not in imports
979+
assert "helpers" not in imports
980+
981+
finally:
982+
# Clean up
983+
for file in ["utility.py", "helpers.py", "main.py"]:
984+
file_path = os.path.join(temp_dir, file)
985+
if os.path.exists(file_path):
986+
os.unlink(file_path)
987+
os.rmdir(temp_dir)

0 commit comments

Comments
 (0)