##// END OF EJS Templates
Small refactoring of is_hidden to take root as default kwarg.
Brian E. Granger -
Show More
@@ -265,7 +265,7 b' class AuthenticatedFileHandler(IPythonHandler, web.StaticFileHandler):'
265 """
265 """
266 abs_path = super(AuthenticatedFileHandler, self).validate_absolute_path(root, absolute_path)
266 abs_path = super(AuthenticatedFileHandler, self).validate_absolute_path(root, absolute_path)
267 abs_root = os.path.abspath(root)
267 abs_root = os.path.abspath(root)
268 if is_hidden(abs_root, abs_path):
268 if is_hidden(abs_path, abs_root):
269 raise web.HTTPError(404)
269 raise web.HTTPError(404)
270 return abs_path
270 return abs_path
271
271
@@ -127,7 +127,7 b' class FileNotebookManager(NotebookManager):'
127 """
127 """
128 path = path.strip('/')
128 path = path.strip('/')
129 os_path = self.get_os_path(path=path)
129 os_path = self.get_os_path(path=path)
130 return is_hidden(self.notebook_dir, os_path)
130 return is_hidden(os_path, self.notebook_dir)
131
131
132 def get_os_path(self, name=None, path=''):
132 def get_os_path(self, name=None, path=''):
133 """Given a notebook name and a URL path, return its file system
133 """Given a notebook name and a URL path, return its file system
@@ -179,13 +179,13 b' class FileNotebookManager(NotebookManager):'
179 """List the directories for a given API style path."""
179 """List the directories for a given API style path."""
180 path = path.strip('/')
180 path = path.strip('/')
181 os_path = self.get_os_path('', path)
181 os_path = self.get_os_path('', path)
182 if not os.path.isdir(os_path) or is_hidden(self.notebook_dir, os_path):
182 if not os.path.isdir(os_path) or is_hidden(os_path, self.notebook_dir):
183 raise web.HTTPError(404, u'directory does not exist: %r' % os_path)
183 raise web.HTTPError(404, u'directory does not exist: %r' % os_path)
184 dir_names = os.listdir(os_path)
184 dir_names = os.listdir(os_path)
185 dirs = []
185 dirs = []
186 for name in dir_names:
186 for name in dir_names:
187 os_path = self.get_os_path(name, path)
187 os_path = self.get_os_path(name, path)
188 if os.path.isdir(os_path) and not is_hidden(self.notebook_dir, os_path):
188 if os.path.isdir(os_path) and not is_hidden(os_path, self.notebook_dir):
189 try:
189 try:
190 model = self.get_dir_model(name, path)
190 model = self.get_dir_model(name, path)
191 except IOError:
191 except IOError:
@@ -66,10 +66,11 b' def test_is_hidden():'
66 with TemporaryDirectory() as root:
66 with TemporaryDirectory() as root:
67 subdir1 = os.path.join(root, 'subdir')
67 subdir1 = os.path.join(root, 'subdir')
68 os.makedirs(subdir1)
68 os.makedirs(subdir1)
69 nt.assert_equal(is_hidden(root, subdir1), False)
69 nt.assert_equal(is_hidden(subdir1, root), False)
70 subdir2 = os.path.join(root, '.subdir2')
70 subdir2 = os.path.join(root, '.subdir2')
71 os.makedirs(subdir2)
71 os.makedirs(subdir2)
72 nt.assert_equal(is_hidden(root, subdir2), True)
72 nt.assert_equal(is_hidden(subdir2, root), True)
73 subdir34 = os.path.join(root, 'subdir3', '.subdir4')
73 subdir34 = os.path.join(root, 'subdir3', '.subdir4')
74 os.makedirs(subdir34)
74 os.makedirs(subdir34)
75 nt.assert_equal(is_hidden(root, subdir34), True)
75 nt.assert_equal(is_hidden(subdir34, root), True)
76 nt.assert_equal(is_hidden(subdir34), True)
@@ -80,24 +80,31 b' def url_unescape(path):'
80 for p in py3compat.unicode_to_str(path).split('/')
80 for p in py3compat.unicode_to_str(path).split('/')
81 ])
81 ])
82
82
83 def is_hidden(absolute_root, absolute_path):
83 def is_hidden(abs_path, abs_root=''):
84 """Is a file is hidden or contained in a hidden directory.
84 """Is a file is hidden or contained in a hidden directory.
85
85
86 Hidden is determined by either name starting with '.' or the UF_HIDDEN
86 This will start with the rightmost path element and work backwards to the
87 flag as reported by stat.
87 given root to see if a path is hidden or in a hidden directory. Hidden is
88 determined by either name starting with '.' or the UF_HIDDEN flag as
89 reported by stat.
88
90
89 Parameters
91 Parameters
90 ----------
92 ----------
91 absolute_root : unicode
93 abs_path : unicode
92 absolute_path : unicode
94 The absolute path to check for hidden directories.
95 abs_root : unicode
96 The absolute path of the root directory in which hidden directories
97 should be check for.
93 """
98 """
94 inside_root = absolute_path[len(absolute_root):]
99 if not abs_root:
100 abs_root = abs_path.split(os.sep, 1)[0] + os.sep
101 inside_root = abs_path[len(abs_root):]
95 if any(part.startswith('.') for part in inside_root.split(os.sep)):
102 if any(part.startswith('.') for part in inside_root.split(os.sep)):
96 return True
103 return True
97
104
98 # check UF_HIDDEN on any location up to root
105 # check UF_HIDDEN on any location up to root
99 path = absolute_path
106 path = abs_path
100 while path and path.startswith(absolute_root) and path != absolute_root:
107 while path and path.startswith(abs_root) and path != abs_root:
101 st = os.stat(path)
108 st = os.stat(path)
102 if getattr(st, 'st_flags', 0) & UF_HIDDEN:
109 if getattr(st, 'st_flags', 0) & UF_HIDDEN:
103 return True
110 return True
General Comments 0
You need to be logged in to leave comments. Login now