Esempio n. 1
0
 def test_get_file_info_include_size(self):
     # note the test file is EMPTY on purpose to generate all False is_* flags
     test_dir = self.get_test_loc('api/info/test.txt')
     info = api.get_file_info(test_dir)
     expected = [('size', 0), ('sha1', None), ('md5', None),
                 ('sha256', None), ('mime_type', 'inode/x-empty'),
                 ('file_type', 'empty'), ('programming_language', None),
                 ('is_binary', False), ('is_text', True),
                 ('is_archive', False), ('is_media', False),
                 ('is_source', False), ('is_script', False)]
     assert expected == [(k, v) for k, v in info.items() if k != 'date']
Esempio n. 2
0
def get_resource_info(location):
    """
    Return a mapping suitable for the creation of a new CodebaseResource.
    """
    file_info = {}

    location_path = Path(location)
    is_symlink = location_path.is_symlink()
    is_file = location_path.is_file()

    if is_symlink:
        resource_type = CodebaseResource.Type.SYMLINK
        file_info["status"] = "symlink"
    elif is_file:
        resource_type = CodebaseResource.Type.FILE
    else:
        resource_type = CodebaseResource.Type.DIRECTORY

    file_info.update(
        {
            "type": resource_type,
            "name": fileutils.file_base_name(location),
            "extension": fileutils.file_extension(location),
        }
    )

    if is_symlink:
        return file_info

    # Missing fields on CodebaseResource model returned by `get_file_info`.
    unsupported_fields = [
        "is_binary",
        "is_text",
        "is_archive",
        "is_media",
        "is_source",
        "is_script",
        "date",
    ]

    other_info = scancode_api.get_file_info(location)

    # Skip unsupported_fields
    # Skip empty values to avoid null vs. '' conflicts
    other_info = {
        field_name: value
        for field_name, value in other_info.items()
        if field_name not in unsupported_fields and value
    }

    file_info.update(other_info)

    return file_info