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

Function compute_path_depth

src/packagedcode/pypi.py:2919–2942  ·  view source on GitHub ↗

Return the depth of ``path`` below ``base`` as the number of path segments that ``path`` extends below ``base``. For example: >>> base = '/home/foo/bar' >>> compute_path_depth(base, '/home/foo/bar/baz') 1 >>> compute_path_depth(base, base) 0

(base, path)

Source from the content-addressed store, hash-verified

2917
2918
2919def compute_path_depth(base, path):
2920 """
2921 Return the depth of ``path`` below ``base`` as the number of path segments
2922 that ``path`` extends below ``base``.
2923 For example:
2924 >>> base = '/home/foo/bar'
2925 >>> compute_path_depth(base, '/home/foo/bar/baz')
2926 1
2927 >>> compute_path_depth(base, base)
2928 0
2929 """
2930 base = base.strip(os.path.sep)
2931 path = path.strip(os.path.sep)
2932
2933 assert path.startswith(base)
2934 subpath = path[len(base):].strip(os.path.sep)
2935 segments = [s for s in subpath.split(os.path.sep) if s]
2936 depth = len(segments)
2937 if TRACE:
2938 logger_debug(
2939 ' compute_path_depth:',
2940 'base:', base, 'path:', path, 'subpath:', subpath,
2941 'segments:', segments, 'depth:', depth,)
2942 return depth
2943
2944
2945def get_requirement_from_section(section, sub_section):

Callers 1

get_module_scriptsFunction · 0.85

Calls 2

logger_debugFunction · 0.70
splitMethod · 0.45

Tested by

no test coverage detected