6060from commoncode .fileutils import splitext_name
6161
6262from commoncode import ignore
63+ from commoncode import paths
6364
6465"""
6566This module provides Codebase and Resource objects as an abstraction for files
@@ -1054,6 +1055,12 @@ def type(self, value):
10541055 else :
10551056 self .is_file = False
10561057
1058+ def get_path (self , strip_root = False ):
1059+ if strip_root :
1060+ return strip_first_path_segment (self .path )
1061+ else :
1062+ return self .path
1063+
10571064 @property
10581065 def is_dir (self ):
10591066 # note: we only store is_file
@@ -1354,6 +1361,29 @@ def get_path(root_location, location, full_root=False, strip_root=False):
13541361 return posix_loc .replace (posix_root_loc , '' , 1 )
13551362
13561363
1364+ def strip_first_path_segment (path ):
1365+ """
1366+ Return a POSIX path stripped from its first path segment.
1367+
1368+ For example::
1369+ >>> strip_first_path_segment('')
1370+ ''
1371+ >>> strip_first_path_segment('foo')
1372+ 'foo'
1373+ >>> strip_first_path_segment('foo/bar/baz')
1374+ 'bar/baz'
1375+ >>> strip_first_path_segment('/foo/bar/baz/')
1376+ 'bar/baz'
1377+ >>> strip_first_path_segment('foo/')
1378+ 'foo/'
1379+ """
1380+ segments = paths .split (path )
1381+ if not segments or len (segments ) == 1 :
1382+ return path
1383+ stripped = segments [1 :]
1384+ return '/' .join (stripped )
1385+
1386+
13571387def get_codebase_cache_dir (temp_dir ):
13581388 """
13591389 Return a new, created and unique per-run cache storage directory path rooted
@@ -1506,20 +1536,27 @@ def _get_or_create_parent(self, path, parent_by_path):
15061536 """
15071537 Return a parent resource for a given `path` from `parent_by_path`.
15081538
1509- If a parent resource for a `path` does not exist in `parent_by_path`, it is created recursively.
1539+ If a parent resource for a `path` does not exist in `parent_by_path`, it
1540+ is created recursively.
15101541
1511- Note: the root path and root Resource must already be in `parent_by_path` or else this
1512- function does not work.
1542+ Note: the root path and root Resource must already be in
1543+ `parent_by_path` or else this function does not work.
15131544 """
1514- parent_path = parent_directory (path ).rstrip ('/' ).rstrip ('\\ ' )
1545+ parent_path = parent_directory (path ).rstrip ('/' ).rstrip ('\\ ' ). lstrip ( "/" )
15151546 existing_parent = parent_by_path .get (parent_path )
15161547 if existing_parent :
15171548 return existing_parent
15181549 parent_parent = self ._get_or_create_parent (parent_path , parent_by_path )
15191550 parent_name = file_base_name (parent_path )
15201551 parent_is_file = False
15211552 parent_resource_data = self ._create_empty_resource_data ()
1522- parent_resource = self ._create_resource (parent_name , parent_parent , parent_is_file , parent_path , parent_resource_data )
1553+ parent_resource = self ._create_resource (
1554+ parent_name ,
1555+ parent_parent ,
1556+ parent_is_file ,
1557+ parent_path ,
1558+ parent_resource_data ,
1559+ )
15231560 parent_by_path [parent_path ] = parent_resource
15241561 return parent_resource
15251562
@@ -1620,7 +1657,12 @@ def _populate(self, scan_data):
16201657 root_name = root_path
16211658 root_is_file = False
16221659 root_data = self ._create_empty_resource_data ()
1623- root_resource = self ._create_root_resource (root_name , root_path , root_is_file , root_data )
1660+ root_resource = self ._create_root_resource (
1661+ name = root_name ,
1662+ path = root_path ,
1663+ is_file = root_is_file ,
1664+ root_data = root_data ,
1665+ )
16241666
16251667 # To help recreate the resource tree we keep a mapping by path of any
16261668 # parent resource
@@ -1630,24 +1672,32 @@ def _populate(self, scan_data):
16301672 path = resource_data .get ('path' )
16311673 # Append virtual_root path to imported Resource path if we are merging multiple scans
16321674 if multiple_input :
1633- path = os .path .join (root_path , path )
1675+ path = posixpath .join (root_path , path )
1676+
16341677 name = resource_data .get ('name' , None )
16351678 if not name :
16361679 name = file_name (path )
1680+
16371681 is_file = resource_data .get ('type' , 'file' ) == 'file'
16381682
16391683 existing_parent = parent_by_path .get (path )
16401684 if existing_parent :
1641- # We update the empty parent Resouorce we in _get_or_create_parent() with the data
1642- # from the scan
1685+ # We update the empty parent Resouorce we in
1686+ # _get_or_create_parent() with the data from the scan
16431687 for k , v in resource_data .items ():
16441688 setattr (existing_parent , k , v )
16451689 self .save_resource (existing_parent )
16461690 else :
1647- # Note: `root_path`: `root_resource` must be in `parent_by_path` in order for
1648- # `_get_or_create_parent` to work
1691+ # Note: `root_path`: `root_resource` must be in `parent_by_path`
1692+ # in order for `_get_or_create_parent` to work
16491693 parent = self ._get_or_create_parent (path , parent_by_path )
1650- resource = self ._create_resource (name , parent , is_file , path , resource_data )
1694+ resource = self ._create_resource (
1695+ name = name ,
1696+ parent = parent ,
1697+ is_file = is_file ,
1698+ path = path ,
1699+ resource_data = resource_data ,
1700+ )
16511701
16521702 # Files are not parents (for now), so we do not need to add this
16531703 # to the parent_by_path mapping
@@ -1665,7 +1715,14 @@ def _create_root_resource(self, name, path, is_file, root_data):
16651715 if root_data :
16661716 root_data = remove_properties_and_basics (root_data )
16671717 root = self .resource_class (
1668- name = name , location = None , path = path , rid = 0 , pid = None , is_file = is_file , ** root_data )
1718+ name = name ,
1719+ location = None ,
1720+ path = path ,
1721+ rid = 0 ,
1722+ pid = None ,
1723+ is_file = is_file ,
1724+ ** root_data ,
1725+ )
16691726
16701727 self .resource_ids .add (0 )
16711728 self .resources [0 ] = root
0 commit comments