Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions jupyter_notebook/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

import ctypes
import os

import nose.tools as nt

from traitlets.tests.utils import check_help_all_output
from jupyter_notebook.utils import url_escape, url_unescape, is_hidden
from ipython_genutils.py3compat import cast_unicode
from ipython_genutils.tempdir import TemporaryDirectory
from ipython_genutils.testing.decorators import skip_if_not_win32


def test_help_output():
Expand Down Expand Up @@ -64,3 +67,15 @@ def test_is_hidden():
os.makedirs(subdir34)
nt.assert_equal(is_hidden(subdir34, root), True)
nt.assert_equal(is_hidden(subdir34), True)

@skip_if_not_win32
def test_is_hidden_win32():
with TemporaryDirectory() as root:
root = cast_unicode(root)
subdir1 = os.path.join(root, u'subdir')
os.makedirs(subdir1)
assert not is_hidden(subdir1, root)
r = ctypes.windll.kernel32.SetFileAttributesW(subdir1, 0x02)
print(r)
assert is_hidden(subdir1, root)

12 changes: 12 additions & 0 deletions jupyter_notebook/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from __future__ import print_function

import ctypes
import errno
import os
import stat
Expand Down Expand Up @@ -71,6 +72,8 @@ def url_unescape(path):
for p in py3compat.unicode_to_str(path, encoding='utf8').split('/')
])

_win32_FILE_ATTRIBUTE_HIDDEN = 0x02

def is_hidden(abs_path, abs_root=''):
"""Is a file hidden or contained in a hidden directory?

Expand Down Expand Up @@ -115,6 +118,15 @@ def is_hidden(abs_path, abs_root=''):
if getattr(st, 'st_flags', 0) & UF_HIDDEN:
return True
path = os.path.dirname(path)

if sys.platform == 'win32':
try:
attrs = ctypes.windll.kernel32.GetFileAttributesW(py3compat.cast_unicode(path))
except AttributeError:
pass
else:
if attrs > 0 and attrs & _win32_FILE_ATTRIBUTE_HIDDEN:
return True

return False

Expand Down