Ejemplo n.º 1
0
def collect_files(directory, include, exclude):
    """
    Collect files from directory applying given include and exclude patterns.

    :param str directory: Directory to search for files.
    :param list include: List of include patterns.
    :param list exclude: List of exclude patterns.

    :return: List of collected relative filenames from directory.
    :rtype: list
    """
    files_collected = []

    for root, directories, files in walk(directory):
        relative = Path(root).relative_to(directory)

        directories[:] = [
            subdir
            for subdir, relsubdir in joiner(relative, directories)
            if is_wanted(relsubdir, include, exclude)
        ]

        files_collected.extend(
            relfname
            for _, relfname in joiner(relative, files)
            if is_wanted(relfname, include, exclude)
        )

    files_collected.sort()
    return files_collected
Ejemplo n.º 2
0
    def collect(self):
        from os import environ

        include = self.config.include.value
        exclude = self.config.exclude.value

        data = {}

        for env, value in environ.items():
            if is_wanted(env, include, exclude):

                key = env if not self.config.lowercase.value else env.lower()
                data[key] = value

        return data
Ejemplo n.º 3
0
    def collect(self):
        from os import environ

        include = self.config.include.value
        exclude = self.config.exclude.value
        lowercase = self.config.lowercase.value
        types = self.config.types.value or {}

        data = {}

        for env, value in environ.items():
            if is_wanted(env, include, exclude):

                if env in types:
                    value = TYPE_PARSERS[types[env]](value)

                key = env if not lowercase else env.lower()
                data[key] = value

        return data
Ejemplo n.º 4
0
    def collect(self):
        from pycobertura import Cobertura

        # Check if file exists
        infile = Path(self.config.xmlpath.value)
        if not infile.is_file():
            raise FileNotFoundError('No such file {}'.format(infile))

        # Parse file
        cobertura = Cobertura(str(infile))

        # Filter files
        include = self.config.include.value
        exclude = self.config.exclude.value

        original = cobertura.files()
        relevant = [
            filename for filename in original
            if is_wanted(filename, include, exclude)
        ]

        ignored = sorted(set(original) - set(relevant))
        if ignored:
            log.info('{} files ignored from total coverage'.format(
                len(ignored)))

        # Get files coverage data
        total = {
            'total_statements': 0,
            'total_misses': 0,
            'total_hits': 0,
            'branch_rate': 0.0,
            'line_rate': 0.0,
        }

        files = {
            filename:
            {key: getattr(cobertura, key)(filename=filename)
             for key in total}
            for filename in relevant
        }

        # Calculate total
        def reducer(accumulator, element):
            for key in ['total_statements', 'total_misses', 'total_hits']:
                accumulator[key] = accumulator.get(key, 0) + element[key]
            return accumulator

        total = reduce(reducer, files.values(), total)

        # Re-calculate total statements
        if total['total_statements']:
            total['line_rate'] = 1.0 - (total['total_misses'] /
                                        total['total_statements'])

        # Assign branch-rate
        total['branch_rate'] = cobertura.branch_rate()
        if include != ['*'] or exclude:
            log.warning(
                'Branch rate cannot be re-calculated when filtering files as '
                'the cobertura XML doesn\'t include the raw number of '
                'branches hit and total branches.')

        return {
            'files': files,
            'total': total,
            'ignored': ignored,
        }