Created
December 4, 2020 17:19
-
-
Save nikolay-n/3e00a6f9b47a8fda24734e065894c909 to your computer and use it in GitHub Desktop.
Wiggle permissions error fix "wiggle/agent/scanner.py"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pathlib | |
import logging | |
from agent.importer import add | |
class Scanner(object): | |
def __init__(self, directories, model, sysroot='/', block_list=None): | |
self.directories = map(pathlib.Path, directories) | |
self.sysroot = pathlib.Path(sysroot) | |
self.model = model | |
if block_list == None: | |
self.block_list = [] | |
else: | |
self.block_list = set(map(pathlib.Path, block_list)) | |
def run(self): | |
for path in self.directories: | |
try: | |
self.scan(path) | |
except (PermissionError): | |
logging.warning('!!Unable to read directory %s', path) | |
except FileNotFoundError as e: | |
logging.warning('!!Not found %s', path) | |
def scan(self, cwd): | |
from django.db import IntegrityError | |
for blocked in self.block_list: | |
if blocked == cwd or blocked in cwd.parents: | |
return | |
for child in cwd.iterdir(): | |
try: | |
if child.is_dir(): | |
try: | |
self.scan(child) | |
except PermissionError: | |
logging.warning('!Unable to read directory %s', child) | |
elif child.is_symlink(): | |
# TODO: better solution for symlink | |
continue | |
elif child.is_file(): | |
try: | |
add(child.resolve(), self) | |
except IntegrityError as e: | |
logging.error('Path %s may already exists\n(%s)', child, e) | |
except PermissionError as e: | |
logging.warning('Unable to read file %s\n(%s)', child, e) | |
except NotImplementedError as e: | |
logging.error('Unsupported file %s\n(%s)', child, e) | |
except PermissionError as e: | |
logging.warning('Unable to read path %s\n(%s)', child, e) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment