Exemple #1
0
def extract_files(
        location,
        kinds=extractcode.default_kinds,
        recurse=False,
        ignore_pattern=(),
):
    """
    Extract the files found at `location`.

    Extract only archives of a kind listed in the `kinds` kind tuple.

    If `recurse` is True, extract recursively archives nested inside other
    archives. If `recurse` is false, then do not extract further an already
    extracted archive identified by the corresponding extract suffix location.
    """
    ignored = partial(ignore.is_ignored,
                      ignores=ignore.default_ignores,
                      unignores={})
    if TRACE:
        logger.debug('extract:start: %(location)r  recurse: %(recurse)r\n' %
                     locals())

    abs_location = abspath(expanduser(location))
    for top, dirs, files in fileutils.walk(abs_location, ignored):
        if TRACE:
            logger.debug(
                'extract:walk: top:  %(top)r dirs: %(dirs)r files: r(files)r' %
                locals())

        if not recurse:
            if TRACE:
                drs = set(dirs)
            for d in dirs[:]:
                if extractcode.is_extraction_path(d):
                    dirs.remove(d)
            if TRACE:
                logger.debug('extract:walk: not recurse: removed dirs:' +
                             repr(drs.symmetric_difference(set(dirs))))

        for f in files:
            loc = join(top, f)
            if not recurse and extractcode.is_extraction_path(loc):
                if TRACE:
                    logger.debug(
                        'extract:walk not recurse: skipped  file: %(loc)r' %
                        locals())
                continue

            if not archive.should_extract(loc, kinds, ignore_pattern):
                if TRACE:
                    logger.debug(
                        'extract:walk: skipped file: not should_extract: %(loc)r'
                        % locals())
                continue

            target = join(abspath(top), extractcode.get_extraction_path(loc))
            if TRACE:
                logger.debug('extract:target: %(target)r' % locals())

            # extract proper
            for xevent in extract_file(loc, target, kinds):
                if TRACE:
                    logger.debug('extract:walk:extraction event: %(xevent)r' %
                                 locals())
                yield xevent

            if recurse:
                if TRACE:
                    logger.debug(
                        'extract:walk: recursing on target: %(target)r' %
                        locals())
                for xevent in extract(
                        location=target,
                        kinds=kinds,
                        recurse=recurse,
                        ignore_pattern=ignore_pattern,
                ):
                    if TRACE:
                        logger.debug(
                            'extract:walk:recurse:extraction event: %(xevent)r'
                            % locals())
                    yield xevent
Exemple #2
0
def extract(location, kinds=extractcode.default_kinds, recurse=False):
    """
    Walk and extract any archives found at `location` (either a file or
    directory). Extract only archives of a kind listed in the `kinds` kind tuple.

    Return an iterable of ExtractEvent tuples for each extracted archive. This
    can be used to track extraction progress:

     - one event is emitted just before extracting an archive. The ExtractEvent
       warnings and errors are empty. The `done` flag is False.

     - one event is emitted right after extracting an archive. The ExtractEvent
       warnings and errors contains warnings and errors if any. The `done` flag
       is True.

    If `recurse` is True, extract recursively archives nested inside other
    archives If `recurse` is false, then do not extract further an already
    extracted archive identified by the corresponding extract suffix location.

    Note that while the original file system is walked top-down, breadth-first,
    if recurse and a nested archive is found, it is extracted to full depth
    first before resuming the file system walk.
    """
    ignored = partial(ignore.is_ignored,
                      ignores=ignore.default_ignores,
                      unignores={})
    if TRACE:
        logger.debug('extract:start: %(location)r  recurse: %(recurse)r\n' %
                     locals())
    abs_location = abspath(expanduser(location))
    for top, dirs, files in fileutils.walk(abs_location, ignored):
        if TRACE:
            logger.debug(
                'extract:walk: top:  %(top)r dirs: %(dirs)r files: r(files)r' %
                locals())

        if not recurse:
            if TRACE:
                drs = set(dirs)
            for d in dirs[:]:
                if extractcode.is_extraction_path(d):
                    dirs.remove(d)
            if TRACE:
                logger.debug('extract:walk: not recurse: removed dirs:' +
                             repr(drs.symmetric_difference(set(dirs))))
        for f in files:
            loc = join(top, f)
            if not recurse and extractcode.is_extraction_path(loc):
                if TRACE:
                    logger.debug(
                        'extract:walk not recurse: skipped  file: %(loc)r' %
                        locals())
                continue

            if not archive.should_extract(loc, kinds):
                if TRACE:
                    logger.debug(
                        'extract:walk: skipped file: not should_extract: %(loc)r'
                        % locals())
                continue

            target = join(abspath(top), extractcode.get_extraction_path(loc))
            if TRACE:
                logger.debug('extract:target: %(target)r' % locals())
            for xevent in extract_file(loc, target, kinds):
                if TRACE:
                    logger.debug('extract:walk:extraction event: %(xevent)r' %
                                 locals())
                yield xevent

            if recurse:
                if TRACE:
                    logger.debug(
                        'extract:walk: recursing on target: %(target)r' %
                        locals())
                for xevent in extract(target, kinds, recurse):
                    if TRACE:
                        logger.debug(
                            'extract:walk:recurse:extraction event: %(xevent)r'
                            % locals())
                    yield xevent
def extract(location, kinds=extractcode.default_kinds, recurse=False):
    """
    Walk and extract any archives found at `location` (either a file or
    directory). Extract only archives of a kind listed in the `kinds` kind tuple.

    Return an iterable of ExtractEvent tuples for each extracted archive. This
    can be used to track extraction progress:

     - one event is emitted just before extracting an archive. The ExtractEvent
       warnings and errors are empty. The `done` flag is False.

     - one event is emitted right after extracting an archive. The ExtractEvent
       warnings and errors contains warnings and errors if any. The `done` flag
       is True.

    If `recurse` is True, extract recursively archives nested inside other
    archives If `recurse` is false, then do not extract further an already
    extracted archive identified by the corresponding extract suffix location.

    Note that while the original file system is walked top-down, breadth-first,
    if recurse and a nested archive is found, it is extracted to full depth
    first before resuming the file system walk.
    """
    ignored = partial(ignore.is_ignored, ignores=ignore.default_ignores, unignores={})
    if TRACE:
        logger.debug('extract:start: %(location)r  recurse: %(recurse)r\n' % locals())
    abs_location = abspath(expanduser(location))
    for top, dirs, files in fileutils.walk(abs_location, ignored):
        if TRACE:
            logger.debug('extract:walk: top:  %(top)r dirs: %(dirs)r files: r(files)r' % locals())

        if not recurse:
            if TRACE:
                drs = set(dirs)
            for d in dirs[:]:
                if extractcode.is_extraction_path(d):
                    dirs.remove(d)
            if TRACE:
                logger.debug('extract:walk: not recurse: removed dirs:' + repr(drs.symmetric_difference(set(dirs))))
        for f in files:
            loc = join(top, f)
            if not recurse and extractcode.is_extraction_path(loc):
                if TRACE:
                    logger.debug('extract:walk not recurse: skipped  file: %(loc)r' % locals())
                continue

            if not archive.should_extract(loc, kinds):
                if TRACE:
                    logger.debug('extract:walk: skipped file: not should_extract: %(loc)r' % locals())
                continue

            target = join(abspath(top), extractcode.get_extraction_path(loc))
            if TRACE:
                logger.debug('extract:target: %(target)r' % locals())
            for xevent in extract_file(loc, target, kinds):
                if TRACE:
                    logger.debug('extract:walk:extraction event: %(xevent)r' % locals())
                yield xevent

            if recurse:
                if TRACE:
                    logger.debug('extract:walk: recursing on target: %(target)r' % locals())
                for xevent in extract(target, kinds, recurse):
                    if TRACE:
                        logger.debug('extract:walk:recurse:extraction event: %(xevent)r' % locals())
                    yield xevent