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

Method _get_or_create_resource

src/commoncode/resource.py:718–774  ·  view source on GitHub ↗

Create and return a new codebase Resource with ``path`` and ``location``.

(
        self,
        name,
        parent,
        is_file=False,
        path=None,
    )

Source from the content-addressed store, hash-verified

716 return root
717
718 def _get_or_create_resource(
719 self,
720 name,
721 parent,
722 is_file=False,
723 path=None,
724 ):
725 """
726 Create and return a new codebase Resource with ``path`` and ``location``.
727 """
728 if not parent:
729 raise TypeError(
730 f"Cannot create resource without parent: name: {name!r}, path: {path!r}"
731 )
732
733 # If the codebase is virtual, we provide the path
734 if not path:
735 path = posixpath_join(parent.path, name)
736 path = clean_path(path)
737
738 existing = self.get_resource(path)
739 if existing:
740 if TRACE:
741 logger_debug(" Codebase._get_or_create_resource: path already exists:", path)
742 return existing
743
744 if self._use_disk_cache_for_resource():
745 cache_location = self._get_resource_cache_location(path=path, create_dirs=True)
746 else:
747 cache_location = None
748
749 # NOTE: If the codebase is virtual, then there is no location
750 parent_location = parent.location
751 if parent_location:
752 location = join(parent_location, name)
753 else:
754 location = None
755
756 if TRACE:
757 logger_debug(
758 f" Codebase._get_or_create_resource: with path: {path}\n"
759 f" name={name}, is_file={is_file}"
760 )
761
762 child = self.resource_class(
763 name=name,
764 location=location,
765 path=path,
766 cache_location=cache_location,
767 is_file=is_file,
768 )
769 self.resources_count += 1
770
771 parent.children_names.append(name)
772 self.save_resource(parent)
773 self.save_resource(child)
774 return child
775

Calls 7

get_resourceMethod · 0.95
save_resourceMethod · 0.95
clean_pathFunction · 0.85
logger_debugFunction · 0.70
appendMethod · 0.45