From 5c68f3db41766e24d5fe6f7adbfd713bc83786a3 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 19 Oct 2014 12:52:08 +0100 Subject: The name win32 is a misnomer. Use 'windows_support' instead. --- setuptools/windows_support.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 setuptools/windows_support.py (limited to 'setuptools/windows_support.py') diff --git a/setuptools/windows_support.py b/setuptools/windows_support.py new file mode 100644 index 00000000..fd373009 --- /dev/null +++ b/setuptools/windows_support.py @@ -0,0 +1,19 @@ +# From http://stackoverflow.com/questions/19622133/python-set-hide-attribute-on-folders-in-windows-os + +import ctypes + + +def hide_file(path): + """Sets the hidden attribute on a file or directory + + `path` must be unicode; be careful that you escape backslashes or use raw + string literals - e.g.: `u'G:\\Dir\\folder1'` or `ur'G:\Dir\folder1'`. + """ + + SetFileAttributesW = ctypes.windll.kernel32.SetFileAttributesW + + FILE_ATTRIBUTE_HIDDEN = 0x02 + + ret = SetFileAttributesW(path, FILE_ATTRIBUTE_HIDDEN) + if not ret: + raise ctypes.WinError() -- cgit v1.2.1 From 3d532f52850f0c860f0031519a25231797320976 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 19 Oct 2014 12:55:43 +0100 Subject: Update docstring to be imperative and incorporate comment. Omit lessons about string literals. --- setuptools/windows_support.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'setuptools/windows_support.py') diff --git a/setuptools/windows_support.py b/setuptools/windows_support.py index fd373009..8da71243 100644 --- a/setuptools/windows_support.py +++ b/setuptools/windows_support.py @@ -1,13 +1,13 @@ -# From http://stackoverflow.com/questions/19622133/python-set-hide-attribute-on-folders-in-windows-os - import ctypes def hide_file(path): - """Sets the hidden attribute on a file or directory + """ + Set the hidden attribute on a file or directory. + + From http://stackoverflow.com/questions/19622133/ - `path` must be unicode; be careful that you escape backslashes or use raw - string literals - e.g.: `u'G:\\Dir\\folder1'` or `ur'G:\Dir\folder1'`. + `path` must be text. """ SetFileAttributesW = ctypes.windll.kernel32.SetFileAttributesW -- cgit v1.2.1 From ae6eb3131a935824d1aca43c6ac5ac6bb4907078 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 19 Oct 2014 13:02:37 +0100 Subject: Declare argtypes and restype on SetFileAttributesW so that it will cast Python 2 bytestrings to Unicode automatically. --- setuptools/windows_support.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'setuptools/windows_support.py') diff --git a/setuptools/windows_support.py b/setuptools/windows_support.py index 8da71243..df35a5f4 100644 --- a/setuptools/windows_support.py +++ b/setuptools/windows_support.py @@ -1,4 +1,4 @@ -import ctypes +import ctypes.wintypes def hide_file(path): @@ -9,11 +9,12 @@ def hide_file(path): `path` must be text. """ - - SetFileAttributesW = ctypes.windll.kernel32.SetFileAttributesW + SetFileAttributes = ctypes.windll.kernel32.SetFileAttributesW + SetFileAttributes.argtypes = ctypes.wintypes.LPWSTR, ctypes.wintypes.DWORD + SetFileAttributes.restype = ctypes.wintypes.BOOL FILE_ATTRIBUTE_HIDDEN = 0x02 - ret = SetFileAttributesW(path, FILE_ATTRIBUTE_HIDDEN) + ret = SetFileAttributes(path, FILE_ATTRIBUTE_HIDDEN) if not ret: raise ctypes.WinError() -- cgit v1.2.1 From 81b4d929152eb7119f89a06294e2b656d36c3484 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 19 Oct 2014 13:07:48 +0100 Subject: Decorate hide_file to only run on Windows. --- setuptools/windows_support.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'setuptools/windows_support.py') diff --git a/setuptools/windows_support.py b/setuptools/windows_support.py index df35a5f4..8e67e41a 100644 --- a/setuptools/windows_support.py +++ b/setuptools/windows_support.py @@ -1,6 +1,14 @@ +import platform import ctypes.wintypes +def windows_only(func): + if platform.system() != 'Windows': + return lambda *args, **kwargs: None + return func + + +@windows_only def hide_file(path): """ Set the hidden attribute on a file or directory. -- cgit v1.2.1 From 1fab75aaec4ada030ca2777e26edbf5eb76d9802 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 19 Oct 2014 13:14:06 +0100 Subject: Defer importing of wintypes because it doesn't import nicely. See Python issue 16396. --- setuptools/windows_support.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'setuptools/windows_support.py') diff --git a/setuptools/windows_support.py b/setuptools/windows_support.py index 8e67e41a..cb977cff 100644 --- a/setuptools/windows_support.py +++ b/setuptools/windows_support.py @@ -1,5 +1,5 @@ import platform -import ctypes.wintypes +import ctypes def windows_only(func): @@ -17,6 +17,7 @@ def hide_file(path): `path` must be text. """ + __import__('ctypes.wintypes') SetFileAttributes = ctypes.windll.kernel32.SetFileAttributesW SetFileAttributes.argtypes = ctypes.wintypes.LPWSTR, ctypes.wintypes.DWORD SetFileAttributes.restype = ctypes.wintypes.BOOL -- cgit v1.2.1