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)
| 2917 | |
| 2918 | |
| 2919 | def 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 | |
| 2945 | def get_requirement_from_section(section, sub_section): |
no test coverage detected