Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _get_filtered_filepaths(dirpath, pattern_filepath):
with io.open(pattern_filepath, "r", encoding=pg.TEXT_FILE_ENCODING) as file_:
spec_obj = pathspec.PathSpec.from_lines(
pathspec.patterns.gitwildmatch.GitWildMatchPattern, file_)
return [os.path.join(dirpath, match) for match in spec_obj.match_tree(dirpath)]
def find_imports(
path: str,
files: List[str] = None,
exclude_patterns: List[str] = None,
encoding='utf-8'
):
res = []
raw_imports = []
files = files if files is not None \
else glob(os.path.join(path, '**', '*.py'), recursive=True)
exclude_patterns = exclude_patterns \
if exclude_patterns is not None else []
spec = pathspec.PathSpec.from_lines(
pathspec.patterns.GitWildMatchPattern, exclude_patterns
)
for file in files:
if not file.endswith('.py'):
continue
file_rel = os.path.relpath(file, path)
if spec.match_file(file_rel):
continue
with open(file, 'r', encoding=encoding) as f:
content = f.read()
try:
tree = ast.parse(content)
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for subnode in node.names:
def _build_spec(self, folder: str):
ignore_file = os.path.join(folder, 'file.ignore.txt')
if not os.path.exists(ignore_file):
ignore_patterns = []
else:
ignore_patterns = read_lines(ignore_file)
ignore_patterns.extend(['log', 'data', 'models', '__pycache__'])
return pathspec.PathSpec.from_lines(
pathspec.patterns.GitWildMatchPattern, ignore_patterns
)
def determine_files_to_sync(
local_path, excludes=[], gitignore=False,
):
if isinstance(excludes, str):
excludes = [excludes]
gitignore_patterns = list(map(pathspec.patterns.GitWildMatchPattern, excludes))
svc_directories = [".git", ".svn"]
if gitignore:
gitignores = []
if os.path.exists(".gitignore"):
gitignores.append(".gitignore")
for root, dir_names, file_names in os.walk(local_path):
for dir_name in dir_names:
if dir_name in svc_directories:
continue
dir_name = os.path.join(root, dir_name)
gitignore_path = os.path.join(dir_name, ".gitignore")
if os.path.exists(gitignore_path):
gitignores.append(gitignore_path)
for file_name in file_names:
if file_name == ".gitignore":
gitignore_path = os.path.join(root, file_name)
def traverse(path, ignore_files=None):
if not os.path.exists(path):
return
if ignore_files is None:
ignore_files = []
for item in scandir(path):
full_path = os.path.join(path, item.name)
spec = pathspec.PathSpec.from_lines(
pathspec.patterns.GitWildMatchPattern, ignore_files
)
if spec.match_file(full_path):
logger.debug("Ignoring %s", item)
continue
if item.is_dir():
for result in traverse(item.path, ignore_files):
yield os.path.join(item.name, result)
else:
yield item.name