File tree 2 files changed +31
-0
lines changed
2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 27
27
import shutil
28
28
import uuid
29
29
from collections import Counter
30
+ from collections import deque
30
31
from collections import defaultdict
31
32
from contextlib import suppress
32
33
from itertools import groupby
@@ -2875,6 +2876,24 @@ def descendants(self):
2875
2876
"""
2876
2877
return self .project .codebaseresources .filter (path__startswith = f"{ self .path } /" )
2877
2878
2879
+ def ancestors (self ):
2880
+ """
2881
+ Return a QuerySet of ancestors CodebaseResource objects using a database query
2882
+ on the current CodebaseResource `path`. The current CodebaseResource is not included
2883
+ """
2884
+
2885
+ if not self .has_parent ():
2886
+ return []
2887
+ anscesotrs = deque ()
2888
+ current = self .parent ()
2889
+ anscesotrs_appendleft = anscesotrs .appendleft
2890
+
2891
+ while current :
2892
+ anscesotrs_appendleft (current )
2893
+ current = current .parent ()
2894
+
2895
+ return list (anscesotrs )
2896
+
2878
2897
def children (self , codebase = None ):
2879
2898
"""
2880
2899
Return a QuerySet of direct children CodebaseResource objects using a
Original file line number Diff line number Diff line change @@ -1905,6 +1905,18 @@ def test_scanpipe_codebase_resource_descendants(self):
1905
1905
"asgiref-3.3.0-py3-none-any.whl-extract/asgiref/wsgi.py" ,
1906
1906
]
1907
1907
self .assertEqual (expected , sorted ([resource .path for resource in descendants ]))
1908
+
1909
+ def test_scanpipe_codebase_resource_ancestors (self ):
1910
+ path = "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/__init__.py"
1911
+ resource = self .project_asgiref .codebaseresources .get (path = path )
1912
+ ancestors = list (resource .ancestors ())
1913
+ self .assertEqual (2 , len (ancestors ))
1914
+ self .assertNotIn (resource .path , ancestors )
1915
+ expected = [
1916
+ "asgiref-3.3.0-py3-none-any.whl-extract" ,
1917
+ "asgiref-3.3.0-py3-none-any.whl-extract/asgiref" ,
1918
+ ]
1919
+ self .assertEqual (expected , [resource .path for resource in ancestors ])
1908
1920
1909
1921
def test_scanpipe_codebase_resource_children (self ):
1910
1922
path = "asgiref-3.3.0-py3-none-any.whl-extract"
You can’t perform that action at this time.
0 commit comments