MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / _populate

Method _populate

src/commoncode/resource.py:1914–2120  ·  view source on GitHub ↗

Populate this codebase with Resource objects. The actual subclass of Resource objects used in this codebase will be created as a side effect. Population is done by loading JSON scan results and creating new Resources for each files mappings.

(self, scan_data)

Source from the content-addressed store, hash-verified

1912 )
1913
1914 def _populate(self, scan_data):
1915 """
1916 Populate this codebase with Resource objects.
1917
1918 The actual subclass of Resource objects used in this codebase will be
1919 created as a side effect.
1920
1921 Population is done by loading JSON scan results and creating new
1922 Resources for each files mappings.
1923 """
1924 # Collect headers
1925 ##########################################################
1926 headers = scan_data.get("headers") or []
1927 headers = [Header.from_dict(**hle) for hle in headers]
1928 self.headers = headers
1929
1930 # Collect codebase-level attributes and build a class, then load
1931 ##########################################################
1932 # Codebase attributes to use. Configured with scan_data and plugin
1933 # attributes if present.
1934 self.codebase_attributes = self._collect_codebase_attributes(scan_data)
1935 cbac = _CodebaseAttributes.from_attributes(attributes=self.codebase_attributes)
1936 self.attributes = cbac()
1937
1938 # now populate top level codebase attributes
1939 ##########################################################
1940 for attr_name in self.codebase_attributes:
1941 value = scan_data.get(attr_name)
1942 if not value:
1943 continue
1944 setattr(self.attributes, attr_name, value)
1945
1946 ##########################################################
1947 files_data = scan_data.get("files")
1948 if not files_data:
1949 raise Exception('Input has no "files" top-level scan results.')
1950
1951 if len(files_data) == 1:
1952 # we will shortcut to populate the codebase with a single root resource
1953 self.has_single_resource = True
1954 root_is_file = files_data[0].get("type") == "file"
1955 else:
1956 root_is_file = False
1957
1958 # Create a virtual root if we are merging multiple input scans together
1959 location = self.location
1960 multiple_inputs = (
1961 isinstance(
1962 location,
1963 (
1964 list,
1965 tuple,
1966 ),
1967 )
1968 and len(location) > 1
1969 )
1970
1971 # Iterate through all Resources to collect any attribute in any resource

Callers 1

__init__Method · 0.95

Calls 15

_build_resource_classMethod · 0.95
_create_root_resourceMethod · 0.95
_get_parent_directoryMethod · 0.95
file_nameFunction · 0.90
get_ancestor_pathsFunction · 0.85
from_attributesMethod · 0.80
save_resourceMethod · 0.80
addMethod · 0.80

Tested by

no test coverage detected