Ejemplo n.º 1
0
    def is_manifest(cls, location):
        """
        Return True if the file at ``location`` is likely a manifest of this type.

        Sub-classes should override to implement their own manifest recognition.
        """
        if not filetype.is_file(location):
            return

        filename = file_name(location)

        file_patterns = cls.file_patterns
        if any(
                fnmatch.fnmatchcase(filename, metaf)
                for metaf in file_patterns):
            return True

        T = contenttype.get_type(location)
        ftype = T.filetype_file.lower()
        mtype = T.mimetype_file

        _base_name, extension = splitext_name(location, is_file=True)
        extension = extension.lower()

        if TRACE:
            logger_debug(
                'is_manifest: ftype:',
                ftype,
                'mtype:',
                mtype,
                'pygtype:',
                T.filetype_pygment,
                'fname:',
                filename,
                'ext:',
                extension,
            )

        type_matched = False
        if cls.filetypes:
            type_matched = any(t in ftype for t in cls.filetypes)

        mime_matched = False
        if cls.mimetypes:
            mime_matched = any(m in mtype for m in cls.mimetypes)

        extension_matched = False
        extensions = cls.extensions
        if extensions:
            extensions = (e.lower() for e in extensions)
            extension_matched = any(
                fnmatch.fnmatchcase(extension, ext_pat)
                for ext_pat in extensions)

        if type_matched and mime_matched and extension_matched:
            return True
Ejemplo n.º 2
0
def recognize_packages(location):
    """
    Return a list of Package object if any packages were recognized for this
    `location`, or None if there were no Packages found. Raises Exceptions on errors.
    """

    if not filetype.is_file(location):
        return

    T = contenttype.get_type(location)
    ftype = T.filetype_file.lower()
    mtype = T.mimetype_file

    _base_name, extension = splitext_name(location, is_file=True)
    filename = file_name(location)
    extension = extension.lower()

    if TRACE:
        logger_debug('recognize_packages: ftype:', ftype, 'mtype:', mtype,
                     'pygtype:', T.filetype_pygment, 'fname:', filename,
                     'ext:', extension)

    recognized_packages = []
    for package_type in PACKAGE_TYPES:
        # Note: default to True if there is nothing to match against
        metafiles = package_type.metafiles
        if on_linux and py2:
            metafiles = (fsencode(m) for m in metafiles)

        if any(fnmatch.fnmatchcase(filename, metaf) for metaf in metafiles):
            for recognized in package_type.recognize(location):
                if TRACE:
                    logger_debug(
                        'recognize_packages: metafile matching: recognized:',
                        recognized)
                if recognized and not recognized.license_expression:
                    # compute and set a normalized license expression
                    recognized.license_expression = recognized.compute_normalized_license(
                    )
                    if TRACE:
                        logger_debug(
                            'recognize_packages: recognized.license_expression:',
                            recognized.license_expression)
                recognized_packages.append(recognized)
            return recognized_packages

        type_matched = False
        if package_type.filetypes:
            type_matched = any(t in ftype for t in package_type.filetypes)

        mime_matched = False
        if package_type.mimetypes:
            mime_matched = any(m in mtype for m in package_type.mimetypes)

        extension_matched = False
        extensions = package_type.extensions
        if extensions:
            if on_linux and py2:
                extensions = (fsencode(e) for e in extensions)

            extensions = (e.lower() for e in extensions)
            extension_matched = any(
                fnmatch.fnmatchcase(extension, ext_pat)
                for ext_pat in extensions)

        if type_matched and mime_matched and extension_matched:
            if TRACE: logger_debug('recognize_packages: all matching')
            try:
                for recognized in package_type.recognize(location):
                    # compute and set a normalized license expression
                    if recognized and not recognized.license_expression:
                        recognized.license_expression = recognized.compute_normalized_license(
                        )
                    if TRACE:
                        logger_debug('recognize_packages: recognized',
                                     recognized)
                    recognized_packages.append(recognized)
            except NotImplementedError:
                # build a plain package if recognize is not yet implemented
                recognized = package_type()
                if TRACE:
                    logger_debug('recognize_packages: recognized', recognized)
                recognized_packages.append(recognized)
            return recognized_packages

        if TRACE:
            logger_debug('recognize_packages: no match for type:',
                         package_type)