Skip to content

Commit

Permalink
feat: add support for pyproject.toml and env.yml/env.yaml files
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanpulver committed Jul 9, 2024
1 parent fcd8102 commit 9bb547a
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions safety/scan/finder/file_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def should_exclude(excludes: Set[Path], to_analyze: Path) -> bool:
return True
except ValueError:
pass

return False


Expand All @@ -37,19 +37,19 @@ class FileFinder():
find depending on the language type.
"""

def __init__(self, max_level: int, ecosystems: List[Ecosystem], target: Path,
def __init__(self, max_level: int, ecosystems: List[Ecosystem], target: Path,
console, live_status=None,
exclude: Optional[List[str]] = None,
exclude: Optional[List[str]] = None,
include_files: Optional[Dict[FileType, List[Path]]] = None,
handlers: Optional[Set[FileHandler]] = None) -> None:
self.max_level = max_level
self.target = target
self.include_files = include_files

if not handlers:
handlers = set(ECOSYSTEM_HANDLER_MAPPING[ecosystem]()
handlers = set(ECOSYSTEM_HANDLER_MAPPING[ecosystem]()
for ecosystem in ecosystems)

self.handlers = handlers
self.file_count = 0
self.exclude_dirs: Set[Path] = set()
Expand All @@ -65,7 +65,7 @@ def __init__(self, max_level: int, ecosystems: List[Ecosystem], target: Path,

self.console = console
self.live_status = live_status

def process_directory(self, dir_path, max_deep: Optional[int]=None) -> Tuple[str, Dict[str, Set[Path]]]:
files: Dict[str, Set[Path]] = {}
level : int = 0
Expand All @@ -79,10 +79,10 @@ def process_directory(self, dir_path, max_deep: Optional[int]=None) -> Tuple[str

dirs[:] = [d for d in dirs if not should_exclude(excludes=self.exclude_dirs,
to_analyze=(root_path / Path(d)))]

if dirs:
LOG.info(f"Directories to inspect -> {', '.join(dirs)}")

LOG.info(f"Current -> {root}")
if self.live_status:
self.live_status.update(f":mag: Scanning {root}")
Expand All @@ -92,7 +92,7 @@ def process_directory(self, dir_path, max_deep: Optional[int]=None) -> Tuple[str
del dirs[:]

filenames[:] = [f for f in filenames if not should_exclude(
excludes=self.exclude_files,
excludes=self.exclude_files,
to_analyze=Path(f))]

self.file_count += len(filenames)
Expand All @@ -106,6 +106,14 @@ def process_directory(self, dir_path, max_deep: Optional[int]=None) -> Tuple[str
files[file_type.value] = set()
files[file_type.value].add(inspectable_file)
break

special_files = {'pyproject.toml', 'env.yml', 'env.yaml'}
if file_name in special_files:
file_type = FileType(file_name)
inspectable_file = Path(root, file_name)
if file_type.value not in files or not files[file_type.value]:
files[file_type.value] = set()
files[file_type.value].add(inspectable_file)
level += 1

return dir_path, files
Expand Down

0 comments on commit 9bb547a

Please sign in to comment.