bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
1  | 
# Copyright (C) 2006, 2007 Canonical Ltd
 | 
2  | 
#
 | 
|
| 
2052.3.1
by John Arbash Meinel
 Add tests to cleanup the copyright of all source files  | 
3  | 
# This program is free software; you can redistribute it and/or modify
 | 
4  | 
# it under the terms of the GNU General Public License as published by
 | 
|
5  | 
# the Free Software Foundation; either version 2 of the License, or
 | 
|
6  | 
# (at your option) any later version.
 | 
|
7  | 
#
 | 
|
8  | 
# This program is distributed in the hope that it will be useful,
 | 
|
9  | 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|
10  | 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|
11  | 
# GNU General Public License for more details.
 | 
|
12  | 
#
 | 
|
13  | 
# You should have received a copy of the GNU General Public License
 | 
|
14  | 
# along with this program; if not, write to the Free Software
 | 
|
15  | 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 | 
|
16  | 
||
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
17  | 
"""Win32-specific helper functions
 | 
18  | 
||
19  | 
Only one dependency: ctypes should be installed.
 | 
|
20  | 
"""
 | 
|
21  | 
||
22  | 
import os  | 
|
| 
1185.16.86
by mbp at sourcefrog
 - win32 get_console_size from Alexander  | 
23  | 
import struct  | 
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
24  | 
import sys  | 
25  | 
||
26  | 
||
27  | 
# Windows version
 | 
|
28  | 
if sys.platform == 'win32':  | 
|
29  | 
_major,_minor,_build,_platform,_text = sys.getwindowsversion()  | 
|
| 
2245.4.11
by Alexander Belchenko
 Small fixes after John's review; added NEWS entry  | 
30  | 
    # from MSDN:
 | 
31  | 
    # dwPlatformId
 | 
|
32  | 
    #   The operating system platform.
 | 
|
33  | 
    #   This member can be one of the following values.
 | 
|
34  | 
    #   ==========================  ======================================
 | 
|
35  | 
    #   Value                       Meaning
 | 
|
36  | 
    #   --------------------------  --------------------------------------
 | 
|
37  | 
    #   VER_PLATFORM_WIN32_NT       The operating system is Windows Vista,
 | 
|
38  | 
    #   2                           Windows Server "Longhorn",
 | 
|
39  | 
    #                               Windows Server 2003, Windows XP,
 | 
|
40  | 
    #                               Windows 2000, or Windows NT.
 | 
|
41  | 
    #
 | 
|
42  | 
    #   VER_PLATFORM_WIN32_WINDOWS  The operating system is Windows Me,
 | 
|
43  | 
    #   1                           Windows 98, or Windows 95.
 | 
|
44  | 
    #   ==========================  ======================================
 | 
|
45  | 
if _platform == 2:  | 
|
46  | 
winver = 'Windows NT'  | 
|
47  | 
else:  | 
|
48  | 
        # don't care about real Windows name, just to force safe operations
 | 
|
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
49  | 
winver = 'Windows 98'  | 
50  | 
else:  | 
|
51  | 
winver = None  | 
|
52  | 
||
| 
1185.16.86
by mbp at sourcefrog
 - win32 get_console_size from Alexander  | 
53  | 
|
| 
1773.4.1
by Martin Pool
 Add pyflakes makefile target; fix many warnings  | 
54  | 
# We can cope without it; use a separate variable to help pyflakes
 | 
| 
1185.16.86
by mbp at sourcefrog
 - win32 get_console_size from Alexander  | 
55  | 
try:  | 
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
56  | 
import ctypes  | 
57  | 
has_ctypes = True  | 
|
| 
1185.16.86
by mbp at sourcefrog
 - win32 get_console_size from Alexander  | 
58  | 
except ImportError:  | 
| 
1773.4.1
by Martin Pool
 Add pyflakes makefile target; fix many warnings  | 
59  | 
has_ctypes = False  | 
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
60  | 
else:  | 
61  | 
if winver == 'Windows 98':  | 
|
62  | 
create_buffer = ctypes.create_string_buffer  | 
|
63  | 
suffix = 'A'  | 
|
64  | 
else:  | 
|
65  | 
create_buffer = ctypes.create_unicode_buffer  | 
|
66  | 
suffix = 'W'  | 
|
| 
3023.1.2
by Alexander Belchenko
 Martin's review.  | 
67  | 
try:  | 
68  | 
import win32file  | 
|
69  | 
has_win32file = True  | 
|
70  | 
except ImportError:  | 
|
71  | 
has_win32file = False  | 
|
| 
3626.1.2
by skip
 win32utils.get_host_name() uses 'mbcs' encoding when decoding env vars  | 
72  | 
try:  | 
73  | 
import win32api  | 
|
74  | 
has_win32api = True  | 
|
75  | 
except ImportError:  | 
|
76  | 
has_win32api = False  | 
|
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
77  | 
|
| 
3638.4.1
by Mark Hammond
 Add win32utils.get_local_appdata_location() so bzr and plugins can  | 
78  | 
# pulling in win32com.shell is a bit of overhead, and normally we don't need
 | 
79  | 
# it as ctypes is preferred and common.  lazy_imports and "optional"
 | 
|
80  | 
# modules don't work well, so we do our own lazy thing...
 | 
|
81  | 
has_win32com_shell = None # Set to True or False once we know for sure...  | 
|
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
82  | 
|
83  | 
# Special Win32 API constants
 | 
|
84  | 
# Handles of std streams
 | 
|
| 
1704.2.3
by Martin Pool
 (win32) Detect terminal width using GetConsoleScreenBufferInfo (Alexander)  | 
85  | 
WIN32_STDIN_HANDLE = -10  | 
86  | 
WIN32_STDOUT_HANDLE = -11  | 
|
87  | 
WIN32_STDERR_HANDLE = -12  | 
|
88  | 
||
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
89  | 
# CSIDL constants (from MSDN 2003)
 | 
90  | 
CSIDL_APPDATA = 0x001A # Application Data folder  | 
|
| 
3638.4.10
by Aaron Bentley
 Correct spelling of 'Application Data'  | 
91  | 
CSIDL_LOCAL_APPDATA = 0x001c# <user name>\Local Settings\Application Data (non roaming)  | 
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
92  | 
CSIDL_PERSONAL = 0x0005 # My Documents folder  | 
93  | 
||
94  | 
# from winapi C headers
 | 
|
95  | 
MAX_PATH = 260  | 
|
96  | 
UNLEN = 256  | 
|
97  | 
MAX_COMPUTERNAME_LENGTH = 31  | 
|
98  | 
||
| 
1704.2.3
by Martin Pool
 (win32) Detect terminal width using GetConsoleScreenBufferInfo (Alexander)  | 
99  | 
|
| 
4011.1.1
by John Arbash Meinel
 Implement -Dmemory for win32  | 
100  | 
def debug_memory_win32api(message='', short=True):  | 
101  | 
"""Use trace.note() to dump the running memory info."""  | 
|
102  | 
from bzrlib import trace  | 
|
| 
4011.1.2
by John Arbash Meinel
 Fix some small bugs, and prefer the ctypes form.  | 
103  | 
if has_ctypes:  | 
| 
4011.1.1
by John Arbash Meinel
 Implement -Dmemory for win32  | 
104  | 
class PROCESS_MEMORY_COUNTERS_EX(ctypes.Structure):  | 
105  | 
"""Used by GetProcessMemoryInfo"""  | 
|
106  | 
_fields_ = [('cb', ctypes.c_ulong),  | 
|
107  | 
('PageFaultCount', ctypes.c_ulong),  | 
|
108  | 
('PeakWorkingSetSize', ctypes.c_size_t),  | 
|
109  | 
('WorkingSetSize', ctypes.c_size_t),  | 
|
110  | 
('QuotaPeakPagedPoolUsage', ctypes.c_size_t),  | 
|
111  | 
('QuotaPagedPoolUsage', ctypes.c_size_t),  | 
|
112  | 
('QuotaPeakNonPagedPoolUsage', ctypes.c_size_t),  | 
|
113  | 
('QuotaNonPagedPoolUsage', ctypes.c_size_t),  | 
|
114  | 
('PagefileUsage', ctypes.c_size_t),  | 
|
115  | 
('PeakPagefileUsage', ctypes.c_size_t),  | 
|
116  | 
('PrivateUsage', ctypes.c_size_t),  | 
|
117  | 
                       ]
 | 
|
118  | 
cur_process = ctypes.windll.kernel32.GetCurrentProcess()  | 
|
119  | 
mem_struct = PROCESS_MEMORY_COUNTERS_EX()  | 
|
120  | 
ret = ctypes.windll.psapi.GetProcessMemoryInfo(cur_process,  | 
|
121  | 
ctypes.byref(mem_struct),  | 
|
122  | 
ctypes.sizeof(mem_struct))  | 
|
123  | 
if not ret:  | 
|
124  | 
trace.note('Failed to GetProcessMemoryInfo()')  | 
|
125  | 
            return
 | 
|
126  | 
info = {'PageFaultCount': mem_struct.PageFaultCount,  | 
|
127  | 
'PeakWorkingSetSize': mem_struct.PeakWorkingSetSize,  | 
|
128  | 
'WorkingSetSize': mem_struct.WorkingSetSize,  | 
|
129  | 
'QuotaPeakPagedPoolUsage': mem_struct.QuotaPeakPagedPoolUsage,  | 
|
130  | 
'QuotaPagedPoolUsage': mem_struct.QuotaPagedPoolUsage,  | 
|
131  | 
'QuotaPeakNonPagedPoolUsage': mem_struct.QuotaPeakNonPagedPoolUsage,  | 
|
132  | 
'QuotaNonPagedPoolUsage': mem_struct.QuotaNonPagedPoolUsage,  | 
|
133  | 
'PagefileUsage': mem_struct.PagefileUsage,  | 
|
134  | 
'PeakPagefileUsage': mem_struct.PeakPagefileUsage,  | 
|
135  | 
'PrivateUsage': mem_struct.PrivateUsage,  | 
|
136  | 
               }
 | 
|
| 
4011.1.2
by John Arbash Meinel
 Fix some small bugs, and prefer the ctypes form.  | 
137  | 
elif has_win32api:  | 
138  | 
import win32process  | 
|
139  | 
        # win32process does not return PrivateUsage, because it doesn't use
 | 
|
140  | 
        # PROCESS_MEMORY_COUNTERS_EX (it uses the one without _EX).
 | 
|
141  | 
proc = win32process.GetCurrentProcess()  | 
|
142  | 
info = win32process.GetProcessMemoryInfo(proc)  | 
|
| 
4011.1.1
by John Arbash Meinel
 Implement -Dmemory for win32  | 
143  | 
else:  | 
144  | 
trace.note('Cannot debug memory on win32 without ctypes'  | 
|
145  | 
' or win32process')  | 
|
146  | 
        return
 | 
|
147  | 
trace.note('WorkingSize %8d kB', info['WorkingSetSize'] / 1024)  | 
|
148  | 
trace.note('PeakWorking %8d kB', info['PeakWorkingSetSize'] / 1024)  | 
|
149  | 
if short:  | 
|
150  | 
        return
 | 
|
151  | 
trace.note('PagefileUsage %8d kB', info.get('PagefileUsage', 0) / 1024)  | 
|
152  | 
trace.note('PeakPagefileUsage %8d kB', info.get('PeakPagefileUsage', 0) / 1024)  | 
|
| 
4011.1.2
by John Arbash Meinel
 Fix some small bugs, and prefer the ctypes form.  | 
153  | 
trace.note('PrivateUsage %8d kB', info.get('PrivateUsage', 0) / 1024)  | 
| 
4011.1.1
by John Arbash Meinel
 Implement -Dmemory for win32  | 
154  | 
trace.note('PageFaultCount %8d', info.get('PageFaultCount', 0))  | 
155  | 
||
156  | 
||
| 
1185.16.86
by mbp at sourcefrog
 - win32 get_console_size from Alexander  | 
157  | 
def get_console_size(defaultx=80, defaulty=25):  | 
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
158  | 
"""Return size of current console.  | 
159  | 
||
160  | 
    This function try to determine actual size of current working
 | 
|
161  | 
    console window and return tuple (sizex, sizey) if success,
 | 
|
162  | 
    or default size (defaultx, defaulty) otherwise.
 | 
|
163  | 
    """
 | 
|
164  | 
if not has_ctypes:  | 
|
165  | 
        # no ctypes is found
 | 
|
166  | 
return (defaultx, defaulty)  | 
|
167  | 
||
168  | 
    # To avoid problem with redirecting output via pipe
 | 
|
169  | 
    # need to use stderr instead of stdout
 | 
|
170  | 
h = ctypes.windll.kernel32.GetStdHandle(WIN32_STDERR_HANDLE)  | 
|
171  | 
csbi = ctypes.create_string_buffer(22)  | 
|
172  | 
res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)  | 
|
173  | 
||
174  | 
if res:  | 
|
175  | 
(bufx, bufy, curx, cury, wattr,  | 
|
| 
1185.16.86
by mbp at sourcefrog
 - win32 get_console_size from Alexander  | 
176  | 
left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)  | 
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
177  | 
sizex = right - left + 1  | 
178  | 
sizey = bottom - top + 1  | 
|
179  | 
return (sizex, sizey)  | 
|
180  | 
else:  | 
|
181  | 
return (defaultx, defaulty)  | 
|
182  | 
||
183  | 
||
| 
3638.4.1
by Mark Hammond
 Add win32utils.get_local_appdata_location() so bzr and plugins can  | 
184  | 
def _get_sh_special_folder_path(csidl):  | 
185  | 
"""Call SHGetSpecialFolderPathW if available, or return None.  | 
|
| 
3943.8.1
by Marius Kruger
 remove all trailing whitespace from bzr source  | 
186  | 
|
| 
3638.4.1
by Mark Hammond
 Add win32utils.get_local_appdata_location() so bzr and plugins can  | 
187  | 
    Result is always unicode (or None).
 | 
188  | 
    """
 | 
|
189  | 
if has_ctypes:  | 
|
190  | 
try:  | 
|
191  | 
SHGetSpecialFolderPath = \  | 
|
192  | 
ctypes.windll.shell32.SHGetSpecialFolderPathW  | 
|
193  | 
except AttributeError:  | 
|
194  | 
            pass
 | 
|
195  | 
else:  | 
|
196  | 
buf = ctypes.create_unicode_buffer(MAX_PATH)  | 
|
197  | 
if SHGetSpecialFolderPath(None,buf,csidl,0):  | 
|
198  | 
return buf.value  | 
|
199  | 
||
200  | 
global has_win32com_shell  | 
|
201  | 
if has_win32com_shell is None:  | 
|
202  | 
try:  | 
|
203  | 
from win32com.shell import shell  | 
|
204  | 
has_win32com_shell = True  | 
|
205  | 
except ImportError:  | 
|
206  | 
has_win32com_shell = False  | 
|
207  | 
if has_win32com_shell:  | 
|
208  | 
        # still need to bind the name locally, but this is fast.
 | 
|
209  | 
from win32com.shell import shell  | 
|
210  | 
try:  | 
|
211  | 
return shell.SHGetSpecialFolderPath(0, csidl, 0)  | 
|
212  | 
except shell.error:  | 
|
213  | 
            # possibly E_NOTIMPL meaning we can't load the function pointer,
 | 
|
214  | 
            # or E_FAIL meaning the function failed - regardless, just ignore it
 | 
|
215  | 
            pass
 | 
|
216  | 
return None  | 
|
217  | 
||
218  | 
||
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
219  | 
def get_appdata_location():  | 
220  | 
"""Return Application Data location.  | 
|
221  | 
    Return None if we cannot obtain location.
 | 
|
222  | 
||
| 
3638.4.1
by Mark Hammond
 Add win32utils.get_local_appdata_location() so bzr and plugins can  | 
223  | 
    Windows defines two 'Application Data' folders per user - a 'roaming'
 | 
224  | 
    one that moves with the user as they logon to different machines, and
 | 
|
225  | 
    a 'local' one that stays local to the machine.  This returns the 'roaming'
 | 
|
226  | 
    directory, and thus is suitable for storing user-preferences, etc.
 | 
|
227  | 
||
228  | 
    Returned value can be unicode or plain string.
 | 
|
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
229  | 
    To convert plain string to unicode use
 | 
| 
3224.5.4
by Andrew Bennetts
 Fix test suite, mainly weeding out uses of bzrlib.user_encoding.  | 
230  | 
    s.decode(osutils.get_user_encoding())
 | 
| 
3638.4.2
by Mark Hammond
 Add a reference to bug 262874 noting 'mbcs' may be the correct encoding.  | 
231  | 
    (XXX - but see bug 262874, which asserts the correct encoding is 'mbcs')
 | 
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
232  | 
    """
 | 
| 
3638.4.1
by Mark Hammond
 Add win32utils.get_local_appdata_location() so bzr and plugins can  | 
233  | 
appdata = _get_sh_special_folder_path(CSIDL_APPDATA)  | 
234  | 
if appdata:  | 
|
235  | 
return appdata  | 
|
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
236  | 
    # from env variable
 | 
237  | 
appdata = os.environ.get('APPDATA')  | 
|
238  | 
if appdata:  | 
|
239  | 
return appdata  | 
|
240  | 
    # if we fall to this point we on win98
 | 
|
241  | 
    # at least try C:/WINDOWS/Application Data
 | 
|
242  | 
windir = os.environ.get('windir')  | 
|
243  | 
if windir:  | 
|
244  | 
appdata = os.path.join(windir, 'Application Data')  | 
|
245  | 
if os.path.isdir(appdata):  | 
|
246  | 
return appdata  | 
|
247  | 
    # did not find anything
 | 
|
248  | 
return None  | 
|
249  | 
||
250  | 
||
| 
3638.4.1
by Mark Hammond
 Add win32utils.get_local_appdata_location() so bzr and plugins can  | 
251  | 
def get_local_appdata_location():  | 
252  | 
"""Return Local Application Data location.  | 
|
253  | 
    Return the same as get_appdata_location() if we cannot obtain location.
 | 
|
254  | 
||
255  | 
    Windows defines two 'Application Data' folders per user - a 'roaming'
 | 
|
256  | 
    one that moves with the user as they logon to different machines, and
 | 
|
257  | 
    a 'local' one that stays local to the machine.  This returns the 'local'
 | 
|
258  | 
    directory, and thus is suitable for caches, temp files and other things
 | 
|
259  | 
    which don't need to move with the user.
 | 
|
260  | 
||
261  | 
    Returned value can be unicode or plain string.
 | 
|
262  | 
    To convert plain string to unicode use
 | 
|
263  | 
    s.decode(bzrlib.user_encoding)
 | 
|
| 
3638.4.2
by Mark Hammond
 Add a reference to bug 262874 noting 'mbcs' may be the correct encoding.  | 
264  | 
    (XXX - but see bug 262874, which asserts the correct encoding is 'mbcs')
 | 
| 
3638.4.1
by Mark Hammond
 Add win32utils.get_local_appdata_location() so bzr and plugins can  | 
265  | 
    """
 | 
266  | 
local = _get_sh_special_folder_path(CSIDL_LOCAL_APPDATA)  | 
|
267  | 
if local:  | 
|
268  | 
return local  | 
|
269  | 
    # Vista supplies LOCALAPPDATA, but XP and earlier do not.
 | 
|
270  | 
local = os.environ.get('LOCALAPPDATA')  | 
|
271  | 
if local:  | 
|
272  | 
return local  | 
|
273  | 
return get_appdata_location()  | 
|
274  | 
||
275  | 
||
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
276  | 
def get_home_location():  | 
277  | 
"""Return user's home location.  | 
|
278  | 
    Assume on win32 it's the <My Documents> folder.
 | 
|
279  | 
    If location cannot be obtained return system drive root,
 | 
|
280  | 
    i.e. C:\
 | 
|
281  | 
||
282  | 
    Returned value can be unicode or plain sring.
 | 
|
283  | 
    To convert plain string to unicode use
 | 
|
| 
3224.5.4
by Andrew Bennetts
 Fix test suite, mainly weeding out uses of bzrlib.user_encoding.  | 
284  | 
    s.decode(osutils.get_user_encoding())
 | 
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
285  | 
    """
 | 
| 
3638.4.1
by Mark Hammond
 Add win32utils.get_local_appdata_location() so bzr and plugins can  | 
286  | 
home = _get_sh_special_folder_path(CSIDL_PERSONAL)  | 
287  | 
if home:  | 
|
288  | 
return home  | 
|
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
289  | 
    # try for HOME env variable
 | 
290  | 
home = os.path.expanduser('~')  | 
|
291  | 
if home != '~':  | 
|
292  | 
return home  | 
|
293  | 
    # at least return windows root directory
 | 
|
294  | 
windir = os.environ.get('windir')  | 
|
295  | 
if windir:  | 
|
| 
2610.1.1
by Martin Pool
 Fix get_home_location on Win98 (gzlist,r=john,r=alexander)  | 
296  | 
return os.path.splitdrive(windir)[0] + '/'  | 
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
297  | 
    # otherwise C:\ is good enough for 98% users
 | 
298  | 
return 'C:/'  | 
|
299  | 
||
300  | 
||
301  | 
def get_user_name():  | 
|
302  | 
"""Return user name as login name.  | 
|
303  | 
    If name cannot be obtained return None.
 | 
|
304  | 
||
305  | 
    Returned value can be unicode or plain sring.
 | 
|
306  | 
    To convert plain string to unicode use
 | 
|
| 
3224.5.4
by Andrew Bennetts
 Fix test suite, mainly weeding out uses of bzrlib.user_encoding.  | 
307  | 
    s.decode(osutils.get_user_encoding())
 | 
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
308  | 
    """
 | 
309  | 
if has_ctypes:  | 
|
310  | 
try:  | 
|
311  | 
advapi32 = ctypes.windll.advapi32  | 
|
312  | 
GetUserName = getattr(advapi32, 'GetUserName'+suffix)  | 
|
313  | 
except AttributeError:  | 
|
314  | 
            pass
 | 
|
315  | 
else:  | 
|
316  | 
buf = create_buffer(UNLEN+1)  | 
|
317  | 
n = ctypes.c_int(UNLEN+1)  | 
|
318  | 
if GetUserName(buf, ctypes.byref(n)):  | 
|
319  | 
return buf.value  | 
|
320  | 
    # otherwise try env variables
 | 
|
321  | 
return os.environ.get('USERNAME', None)  | 
|
322  | 
||
323  | 
||
| 
3626.1.3
by John Arbash Meinel
 Use GetComputerNameEx from ctypes when available.  | 
324  | 
# 1 == ComputerNameDnsHostname, which returns "The DNS host name of the local
 | 
325  | 
# computer or the cluster associated with the local computer."
 | 
|
326  | 
_WIN32_ComputerNameDnsHostname = 1  | 
|
327  | 
||
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
328  | 
def get_host_name():  | 
329  | 
"""Return host machine name.  | 
|
330  | 
    If name cannot be obtained return None.
 | 
|
331  | 
||
| 
3626.1.3
by John Arbash Meinel
 Use GetComputerNameEx from ctypes when available.  | 
332  | 
    :return: A unicode string representing the host name. On win98, this may be
 | 
333  | 
        a plain string as win32 api doesn't support unicode.
 | 
|
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
334  | 
    """
 | 
| 
3626.1.2
by skip
 win32utils.get_host_name() uses 'mbcs' encoding when decoding env vars  | 
335  | 
if has_win32api:  | 
336  | 
try:  | 
|
| 
3626.1.3
by John Arbash Meinel
 Use GetComputerNameEx from ctypes when available.  | 
337  | 
return win32api.GetComputerNameEx(_WIN32_ComputerNameDnsHostname)  | 
| 
3626.1.2
by skip
 win32utils.get_host_name() uses 'mbcs' encoding when decoding env vars  | 
338  | 
except (NotImplementedError, win32api.error):  | 
339  | 
            # NotImplemented will happen on win9x...
 | 
|
340  | 
            pass
 | 
|
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
341  | 
if has_ctypes:  | 
342  | 
try:  | 
|
343  | 
kernel32 = ctypes.windll.kernel32  | 
|
344  | 
except AttributeError:  | 
|
| 
3626.1.3
by John Arbash Meinel
 Use GetComputerNameEx from ctypes when available.  | 
345  | 
pass # Missing the module we need  | 
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
346  | 
else:  | 
347  | 
buf = create_buffer(MAX_COMPUTERNAME_LENGTH+1)  | 
|
348  | 
n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH+1)  | 
|
| 
3626.1.3
by John Arbash Meinel
 Use GetComputerNameEx from ctypes when available.  | 
349  | 
|
350  | 
            # Try GetComputerNameEx which gives a proper Unicode hostname
 | 
|
351  | 
GetComputerNameEx = getattr(kernel32, 'GetComputerNameEx'+suffix,  | 
|
352  | 
None)  | 
|
353  | 
if (GetComputerNameEx is not None  | 
|
354  | 
and GetComputerNameEx(_WIN32_ComputerNameDnsHostname,  | 
|
355  | 
buf, ctypes.byref(n))):  | 
|
356  | 
return buf.value  | 
|
357  | 
||
358  | 
            # Try GetComputerName in case GetComputerNameEx wasn't found
 | 
|
359  | 
            # It returns the NETBIOS name, which isn't as good, but still ok.
 | 
|
360  | 
            # The first GetComputerNameEx might have changed 'n', so reset it
 | 
|
361  | 
n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH+1)  | 
|
362  | 
GetComputerName = getattr(kernel32, 'GetComputerName'+suffix,  | 
|
363  | 
None)  | 
|
364  | 
if (GetComputerName is not None  | 
|
365  | 
and GetComputerName(buf, ctypes.byref(n))):  | 
|
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
366  | 
return buf.value  | 
| 
3626.1.2
by skip
 win32utils.get_host_name() uses 'mbcs' encoding when decoding env vars  | 
367  | 
    # otherwise try env variables, which will be 'mbcs' encoded
 | 
368  | 
    # on Windows (Python doesn't expose the native win32 unicode environment)
 | 
|
| 
3626.1.3
by John Arbash Meinel
 Use GetComputerNameEx from ctypes when available.  | 
369  | 
    # According to this:
 | 
370  | 
    # http://msdn.microsoft.com/en-us/library/aa246807.aspx
 | 
|
371  | 
    # environment variables should always be encoded in 'mbcs'.
 | 
|
| 
3626.1.2
by skip
 win32utils.get_host_name() uses 'mbcs' encoding when decoding env vars  | 
372  | 
try:  | 
373  | 
return os.environ['COMPUTERNAME'].decode("mbcs")  | 
|
374  | 
except KeyError:  | 
|
375  | 
return None  | 
|
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
376  | 
|
377  | 
||
378  | 
def _ensure_unicode(s):  | 
|
| 
3794.1.1
by Martin Pool
 Update osutils imports to fix setup.py on Windows  | 
379  | 
from bzrlib import osutils  | 
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
380  | 
if s and type(s) != unicode:  | 
| 
3788.1.1
by John Arbash Meinel
 Fix a missing import  | 
381  | 
from bzrlib import osutils  | 
| 
3224.5.4
by Andrew Bennetts
 Fix test suite, mainly weeding out uses of bzrlib.user_encoding.  | 
382  | 
s = s.decode(osutils.get_user_encoding())  | 
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
383  | 
return s  | 
| 
3626.1.3
by John Arbash Meinel
 Use GetComputerNameEx from ctypes when available.  | 
384  | 
|
| 
2245.4.1
by Alexander Belchenko
 win32utils: Windows-specific functions that use Win32 API via ctypes  | 
385  | 
|
386  | 
def get_appdata_location_unicode():  | 
|
387  | 
return _ensure_unicode(get_appdata_location())  | 
|
388  | 
||
389  | 
def get_home_location_unicode():  | 
|
390  | 
return _ensure_unicode(get_home_location())  | 
|
391  | 
||
392  | 
def get_user_name_unicode():  | 
|
393  | 
return _ensure_unicode(get_user_name())  | 
|
394  | 
||
395  | 
def get_host_name_unicode():  | 
|
396  | 
return _ensure_unicode(get_host_name())  | 
|
| 
2568.2.2
by Robert Collins
 * New method ``_glob_expand_file_list_if_needed`` on the ``Command`` class  | 
397  | 
|
398  | 
||
| 
2617.5.8
by Kuno Meyer
 Extended tests for unicode chars outside of the iso-8859-* range  | 
399  | 
def _ensure_with_dir(path):  | 
400  | 
if not os.path.split(path)[0] or path.startswith(u'*') or path.startswith(u'?'):  | 
|
401  | 
return u'./' + path, True  | 
|
402  | 
else:  | 
|
403  | 
return path, False  | 
|
| 
3943.8.1
by Marius Kruger
 remove all trailing whitespace from bzr source  | 
404  | 
|
| 
2617.5.8
by Kuno Meyer
 Extended tests for unicode chars outside of the iso-8859-* range  | 
405  | 
def _undo_ensure_with_dir(path, corrected):  | 
406  | 
if corrected:  | 
|
407  | 
return path[2:]  | 
|
408  | 
else:  | 
|
409  | 
return path  | 
|
410  | 
||
411  | 
||
412  | 
||
| 
2598.3.1
by Kuno Meyer
 fix method rename glob_expand_for_win32 -> win32utils.glob_expand  | 
413  | 
def glob_expand(file_list):  | 
| 
2568.2.2
by Robert Collins
 * New method ``_glob_expand_file_list_if_needed`` on the ``Command`` class  | 
414  | 
"""Replacement for glob expansion by the shell.  | 
415  | 
||
416  | 
    Win32's cmd.exe does not do glob expansion (eg ``*.py``), so we do our own
 | 
|
417  | 
    here.
 | 
|
418  | 
||
419  | 
    :param file_list: A list of filenames which may include shell globs.
 | 
|
420  | 
    :return: An expanded list of filenames.
 | 
|
421  | 
||
422  | 
    Introduced in bzrlib 0.18.
 | 
|
423  | 
    """
 | 
|
424  | 
if not file_list:  | 
|
425  | 
return []  | 
|
426  | 
import glob  | 
|
427  | 
expanded_file_list = []  | 
|
428  | 
for possible_glob in file_list:  | 
|
| 
3943.8.1
by Marius Kruger
 remove all trailing whitespace from bzr source  | 
429  | 
|
| 
2617.5.8
by Kuno Meyer
 Extended tests for unicode chars outside of the iso-8859-* range  | 
430  | 
        # work around bugs in glob.glob()
 | 
431  | 
        # - Python bug #1001604 ("glob doesn't return unicode with ...")
 | 
|
432  | 
        # - failing expansion for */* with non-iso-8859-* chars
 | 
|
433  | 
possible_glob, corrected = _ensure_with_dir(possible_glob)  | 
|
| 
2568.2.2
by Robert Collins
 * New method ``_glob_expand_file_list_if_needed`` on the ``Command`` class  | 
434  | 
glob_files = glob.glob(possible_glob)  | 
435  | 
||
436  | 
if glob_files == []:  | 
|
437  | 
            # special case to let the normal code path handle
 | 
|
438  | 
            # files that do not exists
 | 
|
| 
2617.5.8
by Kuno Meyer
 Extended tests for unicode chars outside of the iso-8859-* range  | 
439  | 
expanded_file_list.append(  | 
440  | 
_undo_ensure_with_dir(possible_glob, corrected))  | 
|
| 
2568.2.2
by Robert Collins
 * New method ``_glob_expand_file_list_if_needed`` on the ``Command`` class  | 
441  | 
else:  | 
| 
2617.5.8
by Kuno Meyer
 Extended tests for unicode chars outside of the iso-8859-* range  | 
442  | 
glob_files = [_undo_ensure_with_dir(elem, corrected) for elem in glob_files]  | 
| 
2568.2.2
by Robert Collins
 * New method ``_glob_expand_file_list_if_needed`` on the ``Command`` class  | 
443  | 
expanded_file_list += glob_files  | 
| 
3943.8.1
by Marius Kruger
 remove all trailing whitespace from bzr source  | 
444  | 
|
445  | 
return [elem.replace(u'\\', u'/') for elem in expanded_file_list]  | 
|
| 
2681.4.1
by Alexander Belchenko
 win32: looking for full path of mail client executable in registry  | 
446  | 
|
447  | 
||
448  | 
def get_app_path(appname):  | 
|
449  | 
"""Look up in Windows registry for full path to application executable.  | 
|
450  | 
    Typicaly, applications create subkey with their basename
 | 
|
451  | 
    in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
 | 
|
452  | 
||
453  | 
    :param  appname:    name of application (if no filename extension
 | 
|
454  | 
                        is specified, .exe used)
 | 
|
455  | 
    :return:    full path to aplication executable from registry,
 | 
|
456  | 
                or appname itself if nothing found.
 | 
|
457  | 
    """
 | 
|
| 
2681.4.3
by Alexander Belchenko
 move import _winreg into function get_app_path to avoid ImportError on non-win32 platforms  | 
458  | 
import _winreg  | 
| 
2681.4.1
by Alexander Belchenko
 win32: looking for full path of mail client executable in registry  | 
459  | 
try:  | 
460  | 
hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,  | 
|
461  | 
r'SOFTWARE\Microsoft\Windows'  | 
|
462  | 
r'\CurrentVersion\App Paths')  | 
|
463  | 
except EnvironmentError:  | 
|
464  | 
return appname  | 
|
465  | 
||
466  | 
basename = appname  | 
|
467  | 
if not os.path.splitext(basename)[1]:  | 
|
468  | 
basename = appname + '.exe'  | 
|
469  | 
try:  | 
|
470  | 
try:  | 
|
471  | 
fullpath = _winreg.QueryValue(hkey, basename)  | 
|
472  | 
except WindowsError:  | 
|
473  | 
fullpath = appname  | 
|
474  | 
finally:  | 
|
475  | 
_winreg.CloseKey(hkey)  | 
|
476  | 
||
477  | 
return fullpath  | 
|
| 
3023.1.2
by Alexander Belchenko
 Martin's review.  | 
478  | 
|
479  | 
||
480  | 
def set_file_attr_hidden(path):  | 
|
481  | 
"""Set file attributes to hidden if possible"""  | 
|
482  | 
if has_win32file:  | 
|
483  | 
win32file.SetFileAttributes(path, win32file.FILE_ATTRIBUTE_HIDDEN)  |