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)
def test_scanpipe_codebase_resource_model_unique_license_expressions(self): resource = CodebaseResource(project=self.project1) resource.license_expressions = [ "mit", "apache-2.0", "apache-2.0", "mit AND apache-2.0", "gpl-3.0", ] expected = ["apache-2.0", "gpl-3.0", "mit", "mit AND apache-2.0"] self.assertEqual(expected, resource.unique_license_expressions)
def test_scanpipe_pipes_rootfs_has_hash_diff(self): install_file = mock.Mock(sha256="else", md5="md5") codebase_resource = CodebaseResource(sha256="sha256", md5="md5") self.assertTrue(rootfs.has_hash_diff(install_file, codebase_resource)) install_file = mock.Mock(sha512="sha512", md5="md5") codebase_resource = CodebaseResource(sha512="sha512", md5="else") self.assertTrue(rootfs.has_hash_diff(install_file, codebase_resource)) install_file = mock.Mock(sha256="sha256", md5="md5") codebase_resource = CodebaseResource(sha256="sha256", md5="md5") self.assertFalse(rootfs.has_hash_diff(install_file, codebase_resource))
def test_scanpipe_scan_fields_model_mixin_methods(self): expected = [ "copyrights", "holders", "authors", "licenses", "license_expressions", "emails", "urls", ] self.assertEqual(expected, CodebaseResource.scan_fields()) resource = CodebaseResource.objects.create(project=self.project1, path="filename.ext") scan_results = { "license_expressions": ["mit"], "name": "name", "non_resource_field": "value", } resource.set_scan_results(scan_results, save=True) resource.refresh_from_db() self.assertEqual("", resource.name) self.assertEqual(["mit"], resource.license_expressions) resource2 = CodebaseResource.objects.create(project=self.project1, path="file2") resource2.copy_scan_results(from_instance=resource, save=True) resource.refresh_from_db() self.assertEqual(["mit"], resource2.license_expressions)
def test_scanpipe_pipes_codebase_resources_inject_licenses_policy(self): resource = CodebaseResource(licenses=[ { "key": "mit" }, { "key": "apache-2.0" }, { "key": "gpl-3.0" }, ]) expected = [ { "key": "mit", "policy": None }, { "key": "apache-2.0", "policy": { "color_code": "#008000", "compliance_alert": "", "label": "Approved License", "license_key": "apache-2.0", }, }, { "key": "gpl-3.0", "policy": { "color_code": "#c83025", "compliance_alert": "error", "label": "Prohibited License", "license_key": "gpl-3.0", }, }, ] resource.inject_licenses_policy(license_policies_index) self.assertEqual(expected, resource.licenses)