How to use the commoncode.filetype.is_file function in commoncode

To help you get started, we’ve selected a few commoncode examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nexB / scancode-toolkit / tests / extractcode / extractcode_assert_utils.py View on Github external
if filetype.is_file(test_dir):
        test_dir = fileutils.parent_directory(test_dir)

    test_dir_path = fileutils.as_posixpath(test_dir)
    for top, _, files in os.walk(test_dir):
        for f in files:
            location = os.path.join(top, f)
            locs.append(location)
            path = fileutils.as_posixpath(location)
            path = path.replace(test_dir_path, '').strip('/')
            result.append(path)

    assert sorted(expected) == sorted(result)

    for location in locs:
        assert filetype.is_file(location)
        assert not filetype.is_special(location)
        assert filetype.is_readable(location)
github nexB / scancode-toolkit / src / packagedcode / cargo.py View on Github external
def is_cargo_toml(location):
    return (filetype.is_file(location) and fileutils.file_name(location).lower() == 'cargo.toml')
github nexB / scancode-toolkit / src / commoncode / hash.py View on Github external
def checksum(location, name, base64=False):
    """
    Return a checksum of `bitsize` length from the content of the file at
    `location`. The checksum is a hexdigest or base64-encoded is `base64` is
    True.
    """
    if not filetype.is_file(location):
        return
    hasher = _hashmodules_by_name[name]

    # fixme: we should read in chunks?
    with open(location, 'rb') as f:
        hashable = f.read()

    hashed = hasher(hashable)
    if base64:
        return hashed.b64digest()

    return hashed.hexdigest()
github nexB / scancode-toolkit / src / packagedcode / freebsd.py View on Github external
def is_freebsd_manifest(location):
    return (filetype.is_file(location)
            and fileutils.file_name(location).lower() == '+compact_manifest')
github nexB / scancode-toolkit / src / packagedcode / build.py View on Github external
def _is_build_manifest(cls, location):
        if not filetype.is_file(location):
            return False
        fn = fileutils.file_name(location)
        return any(fn == mf for mf in cls.metafiles)
github nexB / scancode-toolkit / src / packagedcode / recognize.py View on Github external
def recognize_package(location):
    """
    Return a Package object if one was recognized or None for this `location`.
    """

    if not filetype.is_file(location):
        return

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

    for package_type in PACKAGE_TYPES:
        # Note: default to True if there is nothing to match against
        metafiles = package_type.metafiles
        if on_linux:
            metafiles = (fsencode(m) for m in metafiles)
        if location.endswith(tuple(metafiles)):
            logger_debug('metafile matching: package_type is of type:', package_type)
            return package_type.recognize(location)

        if package_type.filetypes:
github nexB / scancode-toolkit / src / extractcode / archive.py View on Github external
def get_best_handler(location, kinds=all_kinds):
    """
    Return the best handler of None for the file at location.
    """
    if on_linux and py2:
        location = fileutils.fsencode(location)
    location = os.path.abspath(os.path.expanduser(location))
    if not filetype.is_file(location):
        return
    handlers = list(get_handlers(location))
    if TRACE_DEEP:
        logger.debug('get_best_handler: handlers: %(handlers)r ' % locals())

    if handlers:
        candidates = score_handlers(handlers)
        return candidates and pick_best_handler(candidates, kinds)
github nexB / scancode-toolkit / src / typecode / contenttype.py View on Github external
def is_data(location, definitions=DATA_TYPE_DEFINITIONS):
    """
    Return True isthe file at `location` is a data file.
    """
    if on_linux and py2:
        location = fileutils.fsencode(location)

    if not filetype.is_file(location):
        return False

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

    for ddef in definitions:
        type_matched = ddef.filetypes and any(t in ftype for t in ddef.filetypes)
        mime_matched = ddef.mimetypes and any(m in mtype for m in ddef.mimetypes)

        exts = ddef.extensions
        if exts:
            extension_matched = exts and location.lower().endswith(exts)

        if TRACE:
            logger_debug('is_data: considering def: %(ddef)r for %(location)s' % locals())
github nexB / scancode-toolkit / src / packagedcode / haxe.py View on Github external
def is_haxelib_json(location):
    return (filetype.is_file(location)
            and fileutils.file_name(location).lower() == 'haxelib.json')