Python os.DirEntry() Examples

The following are 15 code examples of os.DirEntry(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module os , or try the search function .
Example #1
Source File: output_report.py    From flan with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def main(dirname: str, output_file: str, ip_file: str, report_type: str = 'tex'):
    nmap_command = ''
    start_date = ''
    builder = create_report_builder(report_type)
    parser = FlanXmlParser()

    for entry in os.scandir(dirname):  # type: os.DirEntry
        if not (entry.is_file() and entry.name.endswith('.xml')):
            continue
        data = parser.read_xml_file(entry.path)
        parser.parse(data)
        nmap_command = parse_nmap_command(data['nmaprun']['@args'])
        start_date = data['nmaprun']['@startstr']

    with open(output_file, 'w+') as output, open(ip_file) as ip_source:
        create_report(parser, builder, nmap_command, start_date, output, ip_source) 
Example #2
Source File: util.py    From chi with MIT License 6 votes vote down vote up
def __init__(self, paths):
    super().__init__()
    self.watches = set()

    for p in paths:
      p = os.path.expanduser(p)
      logger.debug('watch '+p)
      self.watches.add(Repo.observer.schedule(self, p))
      for f in os.scandir(p):
        isinstance(f, os.DirEntry)
        self.on_found(f.is_dir, f.path) 
Example #3
Source File: cascade.py    From batch-shipyard with MIT License 5 votes vote down vote up
def scantree(path):
    """Recursively scan a directory tree
    :param str path: path to scan
    :rtype: os.DirEntry
    :return: DirEntry via generator
    """
    for entry in os.scandir(path):
        yield entry
        if entry.is_dir(follow_symlinks=False):
            yield from scantree(entry.path) 
Example #4
Source File: util.py    From batch-shipyard with MIT License 5 votes vote down vote up
def scantree(path):
    # type: (str) -> os.DirEntry
    """Recursively scan a directory tree
    :param str path: path to scan
    :rtype: DirEntry
    :return: DirEntry via generator
    """
    for entry in scandir(path):
        if entry.is_dir(follow_symlinks=True):
            # due to python2 compat, cannot use yield from here
            for t in scantree(entry.path):
                yield t
        else:
            yield entry 
Example #5
Source File: cascade.py    From cortana-intelligence-inventory-optimization with MIT License 5 votes vote down vote up
def scantree(path):
    """Recursively scan a directory tree
    :param str path: path to scan
    :rtype: os.DirEntry
    :return: DirEntry via generator
    """
    for entry in os.scandir(path):
        if entry.is_dir(follow_symlinks=False):
            yield from scantree(entry.path)
        else:
            yield entry 
Example #6
Source File: complete.py    From kitty with GNU General Public License v3.0 5 votes vote down vote up
def path_completion(prefix: str = '') -> Tuple[List[str], List[str]]:
    prefix = prefix.replace(r'\ ', ' ')
    dirs, files = [], []
    base = '.'
    if prefix.endswith('/'):
        base = prefix
    elif '/' in prefix:
        base = os.path.dirname(prefix)
    src = os.path.expandvars(os.path.expanduser(base))
    src_prefix = os.path.abspath(os.path.expandvars(os.path.expanduser(prefix))) if prefix else ''
    try:
        items: Iterable[os.DirEntry] = os.scandir(src)
    except FileNotFoundError:
        items = ()
    for x in items:
        abspath = os.path.abspath(x.path)
        if prefix and not abspath.startswith(src_prefix):
            continue
        if prefix:
            q = prefix + abspath[len(src_prefix):].lstrip(os.sep)
            q = os.path.expandvars(os.path.expanduser(q))
        else:
            q = os.path.relpath(abspath)
        if x.is_dir():
            dirs.append(q.rstrip(os.sep) + os.sep)
        else:
            files.append(q)
    return dirs, files 
Example #7
Source File: dagster_module_publisher.py    From dagster with Apache License 2.0 5 votes vote down vote up
def get_core_module_directories():
    '''List core module directories (not including libraries) under python_modules.

    Returns:
        List(os.DirEntry): List of core module directories
    '''
    core_module_root_dir = os.path.join(git_repo_root(), 'python_modules')
    module_directories = [
        dir_
        for dir_ in os.scandir(core_module_root_dir)
        if dir_.is_dir() and not dir_.name.startswith('.')
    ]
    return module_directories 
Example #8
Source File: dagster_module_publisher.py    From dagster with Apache License 2.0 5 votes vote down vote up
def get_library_module_directories():
    '''List library module directories under python_modules/libraries.

    Returns:
        List(os.DirEntry): List of core module directories
    '''
    library_module_root_dir = os.path.join(git_repo_root(), 'python_modules', 'libraries')
    library_directories = [
        dir_
        for dir_ in os.scandir(library_module_root_dir)
        if dir_.is_dir() and not dir_.name.startswith('.')
    ]
    return library_directories 
Example #9
Source File: check_library_docs.py    From dagster with Apache License 2.0 5 votes vote down vote up
def get_library_module_directories():
    '''List library module directories under python_modules/libraries.

    Returns:
        List(os.DirEntry): List of core module directories
    '''
    library_module_root_dir = os.path.join(git_repo_root(), 'python_modules', 'libraries')
    library_directories = [
        dir_.name
        for dir_ in os.scandir(library_module_root_dir)
        if dir_.is_dir() and not dir_.name.startswith('.')
    ]
    return library_directories 
Example #10
Source File: user.py    From exbert with Apache License 2.0 5 votes vote down vote up
def walk_dir(self, rel_path):
        """
        Recursively list all files in a folder.
        """
        entries: List[os.DirEntry] = list(os.scandir(rel_path))
        files = [(os.path.join(os.getcwd(), f.path), f.path) for f in entries if f.is_file()]  # (filepath, filename)
        for f in entries:
            if f.is_dir():
                files += self.walk_dir(f.path)
        return files 
Example #11
Source File: series_data.py    From flask-react-spa with MIT License 5 votes vote down vote up
def load_series_datas(dir_path, default_author, last_updated):
    for dir_entry in os.scandir(dir_path):  # type: os.DirEntry
        is_dir = dir_entry.is_dir()
        if is_dir and os.path.exists(os.path.join(dir_entry.path,
                                                  SERIES_FILENAME)):
            is_updated = dir_entry.stat().st_mtime > last_updated
            if is_updated:
                yield from load_series_datas(dir_entry.path,
                                             default_author,
                                             last_updated)

        if dir_entry.name == SERIES_FILENAME:
            yield SeriesData(dir_entry, default_author, last_updated) 
Example #12
Source File: article_data.py    From flask-react-spa with MIT License 5 votes vote down vote up
def load_article_datas(dir_path, default_author, last_updated, series_data=None):
    for dir_entry in os.scandir(dir_path):  # type: os.DirEntry
        is_dir = dir_entry.is_dir()
        if is_dir and os.path.exists(os.path.join(dir_entry.path,
                                                  ARTICLE_FILENAME)):
            yield from load_article_datas(dir_entry.path, default_author, last_updated, series_data)
            continue

        is_markdown = dir_entry.is_file() and dir_entry.name.endswith('.md')
        is_updated = dir_entry.stat().st_mtime > last_updated
        if is_updated and is_markdown and dir_entry.name != SERIES_FILENAME:
            yield ArticleData(dir_entry, default_author, series_data) 
Example #13
Source File: file_data.py    From flask-react-spa with MIT License 5 votes vote down vote up
def __init__(self, dir_entry: os.DirEntry):
        self.file_path = dir_entry.path
        self.file_name = dir_entry.name
        self.is_dir = self.file_name in [ARTICLE_FILENAME, SERIES_FILENAME]
        self.dir_path = os.path.dirname(self.file_path) \
            if self.is_dir else None
        self.dir_name = self.dir_path.rsplit(os.path.sep, 1)[1] \
            if self.is_dir else None
        self.last_updated = timestamp_to_datetime(dir_entry.stat().st_mtime,
                                                  tzlocal.get_localzone())
        with open(self.file_path) as f:
            data = frontmatter.load(f)
        self.frontmatter = data.metadata
        self.markdown = data.content 
Example #14
Source File: util.py    From cortana-intelligence-inventory-optimization with MIT License 4 votes vote down vote up
def scantree(path):
    # type: (str) -> os.DirEntry
    """Recursively scan a directory tree
    :param str path: path to scan
    :rtype: DirEntry
    :return: DirEntry via generator
    """
    for entry in scandir(path):
        if entry.is_dir(follow_symlinks=True):
            # due to python2 compat, cannot use yield from here
            for t in scantree(entry.path):
                yield t
        else:
            yield entry 
Example #15
Source File: chroot.py    From cjworkbench with GNU Affero General Public License v3.0 4 votes vote down vote up
def _walk_and_delete_upper_files(
    chroot: Chroot, should_delete: Callable[[Path, Path], bool] = lambda root_path: True
) -> None:
    """
    Delete files and directories from `root` that are in `upper` but not `base`.

    Ignore files and directories for which `should_delete(root_path) == False`.
    """
    # lightweight recursion: (dirpath, scandir iterator) at each
    # level of nesting
    #
    # We scandir *chroot.upper*. This is where all the _changes_ are
    # recorded; so we want it to appear empty (except for maybe a few
    # empty directories that mirror directories in chroot.base).
    #
    # DO NOT edit chroot.upper directly: that gives undefined behavior.
    # Delete from chroot.root.
    stack: List[Tuple[Path, Iterator[os.DirEntry]]] = [
        (chroot.upper, os.scandir(str(chroot.upper)))
    ]

    while stack:
        cur_dir, cur_scandir = stack[-1]
        try:
            entry: os.DirEntry = next(cur_scandir)
        except StopIteration:
            stack.pop()
            if cur_dir != chroot.upper:  # we're done
                # Delete the directory itself, unless it's in base layer or
                # we didn't delete its children.
                relative_path = Path(cur_dir).relative_to(chroot.upper)
                base_path = chroot.base / relative_path
                root_path = chroot.root / relative_path
                if not base_path.exists() and should_delete(root_path):
                    try:
                        root_path.rmdir()
                    except OSError as err:
                        if err.errno == errno.ENOTEMPTY:
                            pass
                        else:
                            raise
            continue

        relative_path = Path(entry.path).relative_to(chroot.upper)
        root_path = chroot.root / relative_path
        if entry.is_dir(follow_symlinks=False):
            if root_path.is_mount():
                # Don't follow mountpoints. /root/.local/share/virtualenvs
                # is a mountpoint in dev mode, to cache installed Python
                # modules.
                continue
            stack.append((Path(entry.path), os.scandir(entry.path)))
        elif should_delete(root_path):
            root_path.unlink()