Return true if child is subdirectory of parent. Assumes both paths are absolute and don't contain symlinks.
(parent, child)
| 7838 | |
| 7839 | |
| 7840 | def _IsParentOrSame(parent, child): |
| 7841 | """Return true if child is subdirectory of parent. |
| 7842 | Assumes both paths are absolute and don't contain symlinks. |
| 7843 | """ |
| 7844 | parent = os.path.normpath(parent) |
| 7845 | child = os.path.normpath(child) |
| 7846 | if parent == child: |
| 7847 | return True |
| 7848 | |
| 7849 | prefix = os.path.commonprefix([parent, child]) |
| 7850 | if prefix != parent: |
| 7851 | return False |
| 7852 | # Note: os.path.commonprefix operates on character basis, so |
| 7853 | # take extra care of situations like '/foo/ba' and '/foo/bar/baz' |
| 7854 | child_suffix = child[len(prefix) :] |
| 7855 | child_suffix = child_suffix.lstrip(os.sep) |
| 7856 | return child == os.path.join(prefix, child_suffix) |
| 7857 | |
| 7858 | |
| 7859 | def main(): |
no outgoing calls
no test coverage detected