Improve subdirectory check
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import sys
|
||||
__version__ = "0.0.5.1"
|
||||
__version__ = "0.0.5.2"
|
||||
|
||||
def main(argv=sys.argv):
|
||||
from .cli import main
|
||||
|
@@ -130,20 +130,12 @@ def is_subdir(path, directory):
|
||||
Returns true if *path* in a subdirectory of *directory*.
|
||||
"""
|
||||
import os
|
||||
from os.path import normpath, normcase
|
||||
from os.path import normpath, normcase, sep
|
||||
path = normpath(normcase(path))
|
||||
directory = normpath(normcase(directory))
|
||||
|
||||
if isinstance(directory, bytes):
|
||||
sep_i = ord(os.sep)
|
||||
sep = os.sep.encode('ascii')
|
||||
else:
|
||||
sep_i = os.sep
|
||||
sep = os.sep
|
||||
|
||||
directory = directory.rstrip(sep)
|
||||
if len(path) > len(directory):
|
||||
if path.startswith(directory):
|
||||
return (path[len(directory)] == sep_i)
|
||||
sep = sep.encode('ascii') if isinstance(directory, bytes) else sep
|
||||
if path.startswith(directory.rstrip(sep) + sep):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
Reference in New Issue
Block a user