Beispiel #1
0
def make_codebase_resource(project, location, rootfs_path=None):
    """
    Create a CodebaseResource with the `location` absolute path for the `project`.

    The `location` of this Resource must be rooted in `project.codebase_path`.

    `rootfs_path` is an optional path relative to a rootfs root within an
    Image/VM filesystem context. e.g.: "/var/log/file.log"

    All paths use the POSIX separators.

    If a CodebaseResource already exists in the `project` for with the same path,
    the error raised on save() is not stored in the database and the creation is
    skipped.
    """
    resource_location = location.rstrip("/")
    codebase_dir = str(project.codebase_path)

    assert resource_location.startswith(
        codebase_dir
    ), f"Location: {resource_location} is not under project/codebase/: {codebase_dir}"

    resource_data = scancode.get_resource_info(location=resource_location)

    if rootfs_path:
        resource_data["rootfs_path"] = rootfs_path

    codebase_resource = CodebaseResource(
        project=project,
        path=resource_location.replace(codebase_dir, ""),
        **resource_data,
    )
    codebase_resource.save(save_error=False)
Beispiel #2
0
def make_codebase_resource(project, location, rootfs_path=None):
    """
    Get or create and return a CodebaseResource with `location` absolute path
    for the `project` Project.

    The `location` of this Resource must be rooted in `project.codebase_path`.

    `rootfs_path` is an optional path relative to a rootfs root within an
    Image/VM filesystem context. e.g.: "/var/log/file.log"

    All paths use the POSIX separators.
    """
    location = location.rstrip("/")
    codebase_path = str(project.codebase_path)
    assert location.startswith(
        codebase_path
    ), f"Location: {location} is not under project/codebase: {codebase_path}"

    path = location.replace(codebase_path, "")

    resource_defaults = {}
    if rootfs_path:
        resource_defaults["rootfs_path"] = rootfs_path
    resource_defaults.update(scancode.get_resource_info(location=location))

    codebase_resource, _created = CodebaseResource.objects.get_or_create(
        project=project,
        path=path,
        defaults=resource_defaults,
    )
    return codebase_resource
Beispiel #3
0
 def test_scanpipe_pipes_scancode_get_resource_info(self):
     input_location = str(self.data_location / "notice.NOTICE")
     sha256 = "b323607418a36b5bd700fcf52ae9ca49f82ec6359bc4b89b1b2d73cf75321757"
     expected = {
         "type": CodebaseResource.Type.FILE,
         "name": "notice",
         "extension": ".NOTICE",
         "size": 1178,
         "sha1": "4bd631df28995c332bf69d9d4f0f74d7ee089598",
         "md5": "90cd416fd24df31f608249b77bae80f1",
         "sha256": sha256,
         "mime_type": "text/plain",
         "file_type": "ASCII text",
     }
     self.assertEqual(expected, scancode.get_resource_info(input_location))