Skip to content

Commit

Permalink
Add is_root property to CodebaseResource #180
Browse files Browse the repository at this point in the history
    * Update docstrings
    * Update expected test results

Signed-off-by: Jono Yang <[email protected]>
  • Loading branch information
JonoYang committed May 11, 2022
1 parent 06acd33 commit a9410f8
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 185 deletions.
31 changes: 28 additions & 3 deletions scanpipe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
scanpipe_app = apps.get_app_config("scanpipe")


ROOT_SYMBOL = "."


class RunInProgressError(Exception):
"""Run are in progress or queued on this project."""

Expand Down Expand Up @@ -1551,6 +1554,16 @@ def is_symlink(self):
"""
return self.type == self.Type.SYMLINK

@property
def is_root(self):
"""
Returns True, if the resource is the root of the codebase.
We consider the root resource of the codebase to be the resource whose
path is equal to ROOT_SYMBOL.
"""
return self.path == ROOT_SYMBOL

def compute_compliance_alert(self):
"""
Computes and returns the compliance_alert value from the `licenses` policies.
Expand Down Expand Up @@ -1591,9 +1604,17 @@ def descendants(self):
Returns a QuerySet of descendant CodebaseResource objects using a
database query on the current CodebaseResource `path`.
The current CodebaseResource is not included.
In the case where we are at the root CodebaseResource of a codebase, we
return all CodebaseResources, excluding the root CodebaseResource, whose
path is equal to ROOT_SYMBOL.
"""
if self.path == ".":
return self.project.codebaseresources.exclude(path=".")
if self.is_root:
# We use a different query in the case we are at the root
# CodebaseResource because its path is equal to ROOT_SYMBOL, and no
# other CodebaseResource start with ROOT_SYMBOL. Using the other query
# would result in no CodebaseResources being returned.
return self.project.codebaseresources.exclude(path=ROOT_SYMBOL)
else:
return self.project.codebaseresources.filter(
path__startswith=f"{self.path}/"
Expand All @@ -1611,7 +1632,11 @@ def children(self, codebase=None):
`codebase` is not used in this context but required for compatibility
with the commoncode.resource.Codebase class API.
"""
if self.path == ".":
if self.is_root:
# The root CodebaseResource has a path of ROOT_SYMBOL, and no other
# CodebaseResource paths starts with ROOT_SYMBOL. Because of this,
# we cannot use the path value of the root as part of the query, as
# no other CodebaseResources would be found.
unnested_file_or_directory = "^[^/]+$"
children_regex = rf"{unnested_file_or_directory}"
else:
Expand Down
10 changes: 5 additions & 5 deletions scanpipe/pipes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@

from django.db.models import Count

from packageurl import normalize_qualifiers

from scanpipe.models import CodebaseResource
from scanpipe.models import DiscoveredPackage
from scanpipe.models import ROOT_SYMBOL
from scanpipe.pipes import scancode

logger = logging.getLogger("scanpipe.pipes")
Expand Down Expand Up @@ -64,10 +63,11 @@ def make_codebase_resource(project, location, rootfs_path=None):

if location_path == project.codebase_path:
# If we are making a CodebaseResource for the project codebase/
# directory, `relative_path` will be ".". However, when we scan
# directory, `relative_path` will be ROOT_SYMBOL. However, when we scan
# `location`, the `name` field will be "codebase". We have to overwrite
# the `name` field with "." to be consistant with `relative_path`
resource_data["name"] = "."
# the `name` field with ROOT_SYMBOL to be consistant with
# `relative_path`
resource_data["name"] = ROOT_SYMBOL

if rootfs_path:
resource_data["rootfs_path"] = rootfs_path
Expand Down
3 changes: 2 additions & 1 deletion scanpipe/pipes/codebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from django.core.exceptions import ObjectDoesNotExist

from scanpipe.models import Project
from scanpipe.models import ROOT_SYMBOL


def sort_by_lower_name(resource):
Expand Down Expand Up @@ -70,7 +71,7 @@ def __init__(self, project):
@property
def root(self):
try:
return self.project.codebaseresources.get(path=".")
return self.project.codebaseresources.get(path=ROOT_SYMBOL)
except ObjectDoesNotExist:
raise AttributeError("Codebase root cannot be determined.")

Expand Down
Loading

0 comments on commit a9410f8

Please sign in to comment.