Skip to content
Snippets Groups Projects
Unverified Commit bd153fd5 authored by Simon Arlott's avatar Simon Arlott
Browse files

Handle access errors scanning directories

Trying to read a directory that is visible but not accessible, or a
symlink to a file in a directory that is not accessible will raise a
PermissionError. Output these and then continue.

If os.scandir() raises an exception then the finally block accesses
"scanner" before it is assigned, raising an UnboundLocalError.
parent fc9c2b4a
Branches
Tags
No related merge requests found
Pipeline #11130 failed
...@@ -3,6 +3,7 @@ import datetime ...@@ -3,6 +3,7 @@ import datetime
import itertools import itertools
import os import os
import queue import queue
import sys
import threading import threading
import time import time
import urllib.parse import urllib.parse
...@@ -29,7 +30,15 @@ def crawl_dir(dir, extensions, recursive=True, ignored=[]): ...@@ -29,7 +30,15 @@ def crawl_dir(dir, extensions, recursive=True, ignored=[]):
return return
try: try:
scanner = os.scandir(dir) scanner = os.scandir(dir)
except Exception as e:
m = "Error while reading {}: {} {}\n".format(
dir, e.__class__.__name__, e
)
sys.stderr.write(m)
return
try:
for entry in scanner: for entry in scanner:
try:
if entry.is_file(): if entry.is_file():
for e in extensions: for e in extensions:
if entry.name.lower().endswith(".{}".format(e.lower())): if entry.name.lower().endswith(".{}".format(e.lower())):
...@@ -39,6 +48,11 @@ def crawl_dir(dir, extensions, recursive=True, ignored=[]): ...@@ -39,6 +48,11 @@ def crawl_dir(dir, extensions, recursive=True, ignored=[]):
yield from crawl_dir( yield from crawl_dir(
entry.path, extensions, recursive=recursive, ignored=ignored entry.path, extensions, recursive=recursive, ignored=ignored
) )
except Exception as e:
m = "Error while reading {}: {} {}\n".format(
entry.name, e.__class__.__name__, e
)
sys.stderr.write(m)
finally: finally:
if hasattr(scanner, "close"): if hasattr(scanner, "close"):
scanner.close() scanner.close()
......
Handle access errors scanning directories when importing files
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment