Skip to content

Commit 9934325

Browse files
committed
refactor: improve performance of .railsignore handling
fix
1 parent 16d06c9 commit 9934325

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

nemoguardrails/utils.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515
import asyncio
1616
import dataclasses
17+
import fnmatch
1718
import importlib.resources as pkg_resources
1819
import json
1920
import os
@@ -24,7 +25,7 @@
2425
from datetime import datetime, timezone
2526
from enum import Enum
2627
from pathlib import Path
27-
from typing import Any, Dict, Tuple
28+
from typing import Any, Dict, Optional, Set, Tuple
2829

2930
import yaml
3031
from rich.console import Console
@@ -315,31 +316,43 @@ def snake_to_camelcase(name: str) -> str:
315316
return "".join(n.capitalize() for n in name.split("_"))
316317

317318

318-
def get_railsignore_path() -> Path:
319+
def get_railsignore_path(path: Optional[str] = None) -> Optional[Path]:
319320
"""Helper to get railsignore path.
320321
322+
Args:
323+
path (Optional[str]): The starting path to search for the .railsignore file.
324+
321325
Returns:
322-
Path: The.railsignore file path.
323-
"""
324-
current_path = Path(__file__).resolve()
326+
Path: The .railsignore file path, if found.
325327
326-
# Navigate to the root directory by going up 4 levels
327-
root_dir = current_path.parents[1]
328+
Raises:
329+
FileNotFoundError: If the .railsignore file is not found.
330+
"""
331+
current_path = Path(path) if path else Path.cwd()
328332

329-
file_path = root_dir / ".railsignore"
333+
while True:
334+
railsignore_file = current_path / ".railsignore"
335+
if railsignore_file.exists() and railsignore_file.is_file():
336+
return railsignore_file
337+
if current_path == current_path.parent:
338+
break
339+
current_path = current_path.parent
330340

331-
return file_path
341+
return None
332342

333343

334-
def get_railsignore_patterns() -> set[str]:
344+
def get_railsignore_patterns(railsignore_path) -> Set[str]:
335345
"""
336346
Helper to retrieve all specified patterns in railsignore.
337347
Returns:
338348
Set[str]: The set of filenames or glob patterns in railsignore
339349
"""
340350
ignored_patterns = set()
341351

342-
railsignore_path = get_railsignore_path()
352+
if railsignore_path is None:
353+
return ignored_patterns
354+
355+
# railsignore_path = get_railsignore_path()
343356

344357
# File doesn't exist or is empty
345358
if not railsignore_path.exists() or not os.path.getsize(railsignore_path):
@@ -362,3 +375,15 @@ def get_railsignore_patterns() -> set[str]:
362375
except FileNotFoundError:
363376
print(f"No {railsignore_path} found in the current directory.")
364377
return ignored_patterns
378+
379+
380+
def is_ignored_by_railsignore(filename, ignore_patterns) -> bool:
381+
ignore = False
382+
383+
# Load candidate patterns from railsignore
384+
385+
for pattern in ignore_patterns:
386+
if fnmatch.fnmatch(filename, pattern):
387+
ignore = True
388+
389+
return ignore

0 commit comments

Comments
 (0)