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