From 5ba96d660457219f40b2dbc21cf57c4477f921a1 Mon Sep 17 00:00:00 2001 From: Min RK Date: Thu, 30 Apr 2015 10:12:48 -0700 Subject: [PATCH] check FILE_ATTRIBUTE_HIDDEN on Windows in is_hidden --- jupyter_notebook/tests/test_utils.py | 15 +++++++++++++++ jupyter_notebook/utils.py | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/jupyter_notebook/tests/test_utils.py b/jupyter_notebook/tests/test_utils.py index 4aae725915..7389c26cba 100644 --- a/jupyter_notebook/tests/test_utils.py +++ b/jupyter_notebook/tests/test_utils.py @@ -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(): @@ -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) + diff --git a/jupyter_notebook/utils.py b/jupyter_notebook/utils.py index 75fd1ac924..9ba424ccf3 100644 --- a/jupyter_notebook/utils.py +++ b/jupyter_notebook/utils.py @@ -5,6 +5,7 @@ from __future__ import print_function +import ctypes import errno import os import stat @@ -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? @@ -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