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
|
|
4183.7.1
by Sabin Iacob
update FSF mailing address |
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2052.3.1
by John Arbash Meinel
Add tests to cleanup the copyright of all source files |
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 |
|
4505.2.1
by Alexander Belchenko
Set hidden attribute on .bzr directory below unicode path should never fail with error. The operation should succeed even if bzr unable to set the attribute. (related to bug #335362). |
69 |
import pywintypes |
3023.1.2
by Alexander Belchenko
Martin's review. |
70 |
has_win32file = True |
71 |
except ImportError: |
|
72 |
has_win32file = False |
|
3626.1.2
by skip
win32utils.get_host_name() uses 'mbcs' encoding when decoding env vars |
73 |
try: |
74 |
import win32api |
|
75 |
has_win32api = True |
|
76 |
except ImportError: |
|
77 |
has_win32api = False |
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
78 |
|
3638.4.1
by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can |
79 |
# pulling in win32com.shell is a bit of overhead, and normally we don't need
|
80 |
# it as ctypes is preferred and common. lazy_imports and "optional"
|
|
81 |
# modules don't work well, so we do our own lazy thing...
|
|
82 |
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 |
83 |
|
84 |
# Special Win32 API constants
|
|
85 |
# Handles of std streams
|
|
1704.2.3
by Martin Pool
(win32) Detect terminal width using GetConsoleScreenBufferInfo (Alexander) |
86 |
WIN32_STDIN_HANDLE = -10 |
87 |
WIN32_STDOUT_HANDLE = -11 |
|
88 |
WIN32_STDERR_HANDLE = -12 |
|
89 |
||
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
90 |
# CSIDL constants (from MSDN 2003)
|
91 |
CSIDL_APPDATA = 0x001A # Application Data folder |
|
3638.4.10
by Aaron Bentley
Correct spelling of 'Application Data' |
92 |
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 |
93 |
CSIDL_PERSONAL = 0x0005 # My Documents folder |
94 |
||
95 |
# from winapi C headers
|
|
96 |
MAX_PATH = 260 |
|
97 |
UNLEN = 256 |
|
98 |
MAX_COMPUTERNAME_LENGTH = 31 |
|
99 |
||
4476.2.1
by Alexander Belchenko
win32utils.py: get_app_path() can read path for wordpad.exe (data type_id is REG_EXPAND_SZ). |
100 |
# Registry data type ids
|
101 |
REG_SZ = 1 |
|
102 |
REG_EXPAND_SZ = 2 |
|
103 |
||
1704.2.3
by Martin Pool
(win32) Detect terminal width using GetConsoleScreenBufferInfo (Alexander) |
104 |
|
4011.1.1
by John Arbash Meinel
Implement -Dmemory for win32 |
105 |
def debug_memory_win32api(message='', short=True): |
106 |
"""Use trace.note() to dump the running memory info."""
|
|
107 |
from bzrlib import trace |
|
4011.1.2
by John Arbash Meinel
Fix some small bugs, and prefer the ctypes form. |
108 |
if has_ctypes: |
4011.1.1
by John Arbash Meinel
Implement -Dmemory for win32 |
109 |
class PROCESS_MEMORY_COUNTERS_EX(ctypes.Structure): |
110 |
"""Used by GetProcessMemoryInfo"""
|
|
111 |
_fields_ = [('cb', ctypes.c_ulong), |
|
112 |
('PageFaultCount', ctypes.c_ulong), |
|
113 |
('PeakWorkingSetSize', ctypes.c_size_t), |
|
114 |
('WorkingSetSize', ctypes.c_size_t), |
|
115 |
('QuotaPeakPagedPoolUsage', ctypes.c_size_t), |
|
116 |
('QuotaPagedPoolUsage', ctypes.c_size_t), |
|
117 |
('QuotaPeakNonPagedPoolUsage', ctypes.c_size_t), |
|
118 |
('QuotaNonPagedPoolUsage', ctypes.c_size_t), |
|
119 |
('PagefileUsage', ctypes.c_size_t), |
|
120 |
('PeakPagefileUsage', ctypes.c_size_t), |
|
121 |
('PrivateUsage', ctypes.c_size_t), |
|
122 |
]
|
|
123 |
cur_process = ctypes.windll.kernel32.GetCurrentProcess() |
|
124 |
mem_struct = PROCESS_MEMORY_COUNTERS_EX() |
|
125 |
ret = ctypes.windll.psapi.GetProcessMemoryInfo(cur_process, |
|
126 |
ctypes.byref(mem_struct), |
|
127 |
ctypes.sizeof(mem_struct)) |
|
128 |
if not ret: |
|
129 |
trace.note('Failed to GetProcessMemoryInfo()') |
|
130 |
return
|
|
131 |
info = {'PageFaultCount': mem_struct.PageFaultCount, |
|
132 |
'PeakWorkingSetSize': mem_struct.PeakWorkingSetSize, |
|
133 |
'WorkingSetSize': mem_struct.WorkingSetSize, |
|
134 |
'QuotaPeakPagedPoolUsage': mem_struct.QuotaPeakPagedPoolUsage, |
|
135 |
'QuotaPagedPoolUsage': mem_struct.QuotaPagedPoolUsage, |
|
136 |
'QuotaPeakNonPagedPoolUsage': mem_struct.QuotaPeakNonPagedPoolUsage, |
|
137 |
'QuotaNonPagedPoolUsage': mem_struct.QuotaNonPagedPoolUsage, |
|
138 |
'PagefileUsage': mem_struct.PagefileUsage, |
|
139 |
'PeakPagefileUsage': mem_struct.PeakPagefileUsage, |
|
140 |
'PrivateUsage': mem_struct.PrivateUsage, |
|
141 |
}
|
|
4011.1.2
by John Arbash Meinel
Fix some small bugs, and prefer the ctypes form. |
142 |
elif has_win32api: |
143 |
import win32process |
|
144 |
# win32process does not return PrivateUsage, because it doesn't use
|
|
145 |
# PROCESS_MEMORY_COUNTERS_EX (it uses the one without _EX).
|
|
146 |
proc = win32process.GetCurrentProcess() |
|
147 |
info = win32process.GetProcessMemoryInfo(proc) |
|
4011.1.1
by John Arbash Meinel
Implement -Dmemory for win32 |
148 |
else: |
149 |
trace.note('Cannot debug memory on win32 without ctypes' |
|
150 |
' or win32process') |
|
151 |
return
|
|
4163.1.1
by John Arbash Meinel
Some small fixes for the win32 'trace.debug_mem()' code. |
152 |
if short: |
4163.1.2
by John Arbash Meinel
Merge bzr.dev, which changed kB => KB. |
153 |
trace.note('WorkingSize %7dKB' |
154 |
'\tPeakWorking %7dKB\t%s', |
|
4163.1.1
by John Arbash Meinel
Some small fixes for the win32 'trace.debug_mem()' code. |
155 |
info['WorkingSetSize'] / 1024, |
156 |
info['PeakWorkingSetSize'] / 1024, |
|
157 |
message) |
|
158 |
return
|
|
159 |
if message: |
|
160 |
trace.note('%s', message) |
|
4170.2.1
by Alexander Belchenko
Use KB not kB for 1024 bytes |
161 |
trace.note('WorkingSize %8d KB', info['WorkingSetSize'] / 1024) |
162 |
trace.note('PeakWorking %8d KB', info['PeakWorkingSetSize'] / 1024) |
|
163 |
trace.note('PagefileUsage %8d KB', info.get('PagefileUsage', 0) / 1024) |
|
164 |
trace.note('PeakPagefileUsage %8d KB', info.get('PeakPagefileUsage', 0) / 1024) |
|
165 |
trace.note('PrivateUsage %8d KB', info.get('PrivateUsage', 0) / 1024) |
|
4011.1.1
by John Arbash Meinel
Implement -Dmemory for win32 |
166 |
trace.note('PageFaultCount %8d', info.get('PageFaultCount', 0)) |
167 |
||
168 |
||
1185.16.86
by mbp at sourcefrog
- win32 get_console_size from Alexander |
169 |
def get_console_size(defaultx=80, defaulty=25): |
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
170 |
"""Return size of current console.
|
171 |
||
172 |
This function try to determine actual size of current working
|
|
173 |
console window and return tuple (sizex, sizey) if success,
|
|
174 |
or default size (defaultx, defaulty) otherwise.
|
|
175 |
"""
|
|
176 |
if not has_ctypes: |
|
177 |
# no ctypes is found
|
|
178 |
return (defaultx, defaulty) |
|
179 |
||
180 |
# To avoid problem with redirecting output via pipe
|
|
181 |
# need to use stderr instead of stdout
|
|
182 |
h = ctypes.windll.kernel32.GetStdHandle(WIN32_STDERR_HANDLE) |
|
183 |
csbi = ctypes.create_string_buffer(22) |
|
184 |
res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi) |
|
185 |
||
186 |
if res: |
|
187 |
(bufx, bufy, curx, cury, wattr, |
|
1185.16.86
by mbp at sourcefrog
- win32 get_console_size from Alexander |
188 |
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 |
189 |
sizex = right - left + 1 |
190 |
sizey = bottom - top + 1 |
|
191 |
return (sizex, sizey) |
|
192 |
else: |
|
193 |
return (defaultx, defaulty) |
|
194 |
||
195 |
||
3638.4.1
by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can |
196 |
def _get_sh_special_folder_path(csidl): |
197 |
"""Call SHGetSpecialFolderPathW if available, or return None.
|
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
198 |
|
3638.4.1
by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can |
199 |
Result is always unicode (or None).
|
200 |
"""
|
|
201 |
if has_ctypes: |
|
202 |
try: |
|
203 |
SHGetSpecialFolderPath = \ |
|
204 |
ctypes.windll.shell32.SHGetSpecialFolderPathW |
|
205 |
except AttributeError: |
|
206 |
pass
|
|
207 |
else: |
|
208 |
buf = ctypes.create_unicode_buffer(MAX_PATH) |
|
209 |
if SHGetSpecialFolderPath(None,buf,csidl,0): |
|
210 |
return buf.value |
|
211 |
||
212 |
global has_win32com_shell |
|
213 |
if has_win32com_shell is None: |
|
214 |
try: |
|
215 |
from win32com.shell import shell |
|
216 |
has_win32com_shell = True |
|
217 |
except ImportError: |
|
218 |
has_win32com_shell = False |
|
219 |
if has_win32com_shell: |
|
220 |
# still need to bind the name locally, but this is fast.
|
|
221 |
from win32com.shell import shell |
|
222 |
try: |
|
223 |
return shell.SHGetSpecialFolderPath(0, csidl, 0) |
|
224 |
except shell.error: |
|
225 |
# possibly E_NOTIMPL meaning we can't load the function pointer,
|
|
226 |
# or E_FAIL meaning the function failed - regardless, just ignore it
|
|
227 |
pass
|
|
228 |
return None |
|
229 |
||
230 |
||
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
231 |
def get_appdata_location(): |
232 |
"""Return Application Data location.
|
|
233 |
Return None if we cannot obtain location.
|
|
234 |
||
3638.4.1
by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can |
235 |
Windows defines two 'Application Data' folders per user - a 'roaming'
|
236 |
one that moves with the user as they logon to different machines, and
|
|
237 |
a 'local' one that stays local to the machine. This returns the 'roaming'
|
|
238 |
directory, and thus is suitable for storing user-preferences, etc.
|
|
239 |
||
240 |
Returned value can be unicode or plain string.
|
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
241 |
To convert plain string to unicode use
|
3224.5.4
by Andrew Bennetts
Fix test suite, mainly weeding out uses of bzrlib.user_encoding. |
242 |
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. |
243 |
(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 |
244 |
"""
|
3638.4.1
by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can |
245 |
appdata = _get_sh_special_folder_path(CSIDL_APPDATA) |
246 |
if appdata: |
|
247 |
return appdata |
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
248 |
# from env variable
|
249 |
appdata = os.environ.get('APPDATA') |
|
250 |
if appdata: |
|
251 |
return appdata |
|
252 |
# if we fall to this point we on win98
|
|
253 |
# at least try C:/WINDOWS/Application Data
|
|
254 |
windir = os.environ.get('windir') |
|
255 |
if windir: |
|
256 |
appdata = os.path.join(windir, 'Application Data') |
|
257 |
if os.path.isdir(appdata): |
|
258 |
return appdata |
|
259 |
# did not find anything
|
|
260 |
return None |
|
261 |
||
262 |
||
3638.4.1
by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can |
263 |
def get_local_appdata_location(): |
264 |
"""Return Local Application Data location.
|
|
265 |
Return the same as get_appdata_location() if we cannot obtain location.
|
|
266 |
||
267 |
Windows defines two 'Application Data' folders per user - a 'roaming'
|
|
268 |
one that moves with the user as they logon to different machines, and
|
|
269 |
a 'local' one that stays local to the machine. This returns the 'local'
|
|
270 |
directory, and thus is suitable for caches, temp files and other things
|
|
271 |
which don't need to move with the user.
|
|
272 |
||
273 |
Returned value can be unicode or plain string.
|
|
274 |
To convert plain string to unicode use
|
|
4385.4.1
by Alexander Belchenko
removed all references to bzrlib.user_encoding |
275 |
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. |
276 |
(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 |
277 |
"""
|
278 |
local = _get_sh_special_folder_path(CSIDL_LOCAL_APPDATA) |
|
279 |
if local: |
|
280 |
return local |
|
281 |
# Vista supplies LOCALAPPDATA, but XP and earlier do not.
|
|
282 |
local = os.environ.get('LOCALAPPDATA') |
|
283 |
if local: |
|
284 |
return local |
|
285 |
return get_appdata_location() |
|
286 |
||
287 |
||
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
288 |
def get_home_location(): |
289 |
"""Return user's home location.
|
|
290 |
Assume on win32 it's the <My Documents> folder.
|
|
291 |
If location cannot be obtained return system drive root,
|
|
292 |
i.e. C:\
|
|
293 |
||
4031.3.1
by Frank Aspell
Fixing various typos |
294 |
Returned value can be unicode or plain string.
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
295 |
To convert plain string to unicode use
|
3224.5.4
by Andrew Bennetts
Fix test suite, mainly weeding out uses of bzrlib.user_encoding. |
296 |
s.decode(osutils.get_user_encoding())
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
297 |
"""
|
3638.4.1
by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can |
298 |
home = _get_sh_special_folder_path(CSIDL_PERSONAL) |
299 |
if home: |
|
300 |
return home |
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
301 |
# try for HOME env variable
|
302 |
home = os.path.expanduser('~') |
|
303 |
if home != '~': |
|
304 |
return home |
|
305 |
# at least return windows root directory
|
|
306 |
windir = os.environ.get('windir') |
|
307 |
if windir: |
|
2610.1.1
by Martin Pool
Fix get_home_location on Win98 (gzlist,r=john,r=alexander) |
308 |
return os.path.splitdrive(windir)[0] + '/' |
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
309 |
# otherwise C:\ is good enough for 98% users
|
310 |
return 'C:/' |
|
311 |
||
312 |
||
313 |
def get_user_name(): |
|
314 |
"""Return user name as login name.
|
|
315 |
If name cannot be obtained return None.
|
|
316 |
||
4031.3.1
by Frank Aspell
Fixing various typos |
317 |
Returned value can be unicode or plain string.
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
318 |
To convert plain string to unicode use
|
3224.5.4
by Andrew Bennetts
Fix test suite, mainly weeding out uses of bzrlib.user_encoding. |
319 |
s.decode(osutils.get_user_encoding())
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
320 |
"""
|
321 |
if has_ctypes: |
|
322 |
try: |
|
323 |
advapi32 = ctypes.windll.advapi32 |
|
324 |
GetUserName = getattr(advapi32, 'GetUserName'+suffix) |
|
325 |
except AttributeError: |
|
326 |
pass
|
|
327 |
else: |
|
328 |
buf = create_buffer(UNLEN+1) |
|
329 |
n = ctypes.c_int(UNLEN+1) |
|
330 |
if GetUserName(buf, ctypes.byref(n)): |
|
331 |
return buf.value |
|
332 |
# otherwise try env variables
|
|
333 |
return os.environ.get('USERNAME', None) |
|
334 |
||
335 |
||
3626.1.3
by John Arbash Meinel
Use GetComputerNameEx from ctypes when available. |
336 |
# 1 == ComputerNameDnsHostname, which returns "The DNS host name of the local
|
337 |
# computer or the cluster associated with the local computer."
|
|
338 |
_WIN32_ComputerNameDnsHostname = 1 |
|
339 |
||
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
340 |
def get_host_name(): |
341 |
"""Return host machine name.
|
|
342 |
If name cannot be obtained return None.
|
|
343 |
||
3626.1.3
by John Arbash Meinel
Use GetComputerNameEx from ctypes when available. |
344 |
:return: A unicode string representing the host name. On win98, this may be
|
345 |
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 |
346 |
"""
|
3626.1.2
by skip
win32utils.get_host_name() uses 'mbcs' encoding when decoding env vars |
347 |
if has_win32api: |
348 |
try: |
|
3626.1.3
by John Arbash Meinel
Use GetComputerNameEx from ctypes when available. |
349 |
return win32api.GetComputerNameEx(_WIN32_ComputerNameDnsHostname) |
3626.1.2
by skip
win32utils.get_host_name() uses 'mbcs' encoding when decoding env vars |
350 |
except (NotImplementedError, win32api.error): |
351 |
# NotImplemented will happen on win9x...
|
|
352 |
pass
|
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
353 |
if has_ctypes: |
354 |
try: |
|
355 |
kernel32 = ctypes.windll.kernel32 |
|
356 |
except AttributeError: |
|
3626.1.3
by John Arbash Meinel
Use GetComputerNameEx from ctypes when available. |
357 |
pass # Missing the module we need |
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
358 |
else: |
359 |
buf = create_buffer(MAX_COMPUTERNAME_LENGTH+1) |
|
360 |
n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH+1) |
|
3626.1.3
by John Arbash Meinel
Use GetComputerNameEx from ctypes when available. |
361 |
|
362 |
# Try GetComputerNameEx which gives a proper Unicode hostname
|
|
363 |
GetComputerNameEx = getattr(kernel32, 'GetComputerNameEx'+suffix, |
|
364 |
None) |
|
365 |
if (GetComputerNameEx is not None |
|
366 |
and GetComputerNameEx(_WIN32_ComputerNameDnsHostname, |
|
367 |
buf, ctypes.byref(n))): |
|
368 |
return buf.value |
|
369 |
||
370 |
# Try GetComputerName in case GetComputerNameEx wasn't found
|
|
371 |
# It returns the NETBIOS name, which isn't as good, but still ok.
|
|
372 |
# The first GetComputerNameEx might have changed 'n', so reset it
|
|
373 |
n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH+1) |
|
374 |
GetComputerName = getattr(kernel32, 'GetComputerName'+suffix, |
|
375 |
None) |
|
376 |
if (GetComputerName is not None |
|
377 |
and GetComputerName(buf, ctypes.byref(n))): |
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
378 |
return buf.value |
3626.1.2
by skip
win32utils.get_host_name() uses 'mbcs' encoding when decoding env vars |
379 |
# otherwise try env variables, which will be 'mbcs' encoded
|
380 |
# on Windows (Python doesn't expose the native win32 unicode environment)
|
|
3626.1.3
by John Arbash Meinel
Use GetComputerNameEx from ctypes when available. |
381 |
# According to this:
|
382 |
# http://msdn.microsoft.com/en-us/library/aa246807.aspx
|
|
383 |
# environment variables should always be encoded in 'mbcs'.
|
|
3626.1.2
by skip
win32utils.get_host_name() uses 'mbcs' encoding when decoding env vars |
384 |
try: |
385 |
return os.environ['COMPUTERNAME'].decode("mbcs") |
|
386 |
except KeyError: |
|
387 |
return None |
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
388 |
|
389 |
||
390 |
def _ensure_unicode(s): |
|
3794.1.1
by Martin Pool
Update osutils imports to fix setup.py on Windows |
391 |
from bzrlib import osutils |
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
392 |
if s and type(s) != unicode: |
3788.1.1
by John Arbash Meinel
Fix a missing import |
393 |
from bzrlib import osutils |
3224.5.4
by Andrew Bennetts
Fix test suite, mainly weeding out uses of bzrlib.user_encoding. |
394 |
s = s.decode(osutils.get_user_encoding()) |
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
395 |
return s |
3626.1.3
by John Arbash Meinel
Use GetComputerNameEx from ctypes when available. |
396 |
|
2245.4.1
by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes |
397 |
|
398 |
def get_appdata_location_unicode(): |
|
399 |
return _ensure_unicode(get_appdata_location()) |
|
400 |
||
401 |
def get_home_location_unicode(): |
|
402 |
return _ensure_unicode(get_home_location()) |
|
403 |
||
404 |
def get_user_name_unicode(): |
|
405 |
return _ensure_unicode(get_user_name()) |
|
406 |
||
407 |
def get_host_name_unicode(): |
|
408 |
return _ensure_unicode(get_host_name()) |
|
2568.2.2
by Robert Collins
* New method ``_glob_expand_file_list_if_needed`` on the ``Command`` class |
409 |
|
410 |
||
2617.5.8
by Kuno Meyer
Extended tests for unicode chars outside of the iso-8859-* range |
411 |
def _ensure_with_dir(path): |
412 |
if not os.path.split(path)[0] or path.startswith(u'*') or path.startswith(u'?'): |
|
413 |
return u'./' + path, True |
|
414 |
else: |
|
415 |
return path, False |
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
416 |
|
2617.5.8
by Kuno Meyer
Extended tests for unicode chars outside of the iso-8859-* range |
417 |
def _undo_ensure_with_dir(path, corrected): |
418 |
if corrected: |
|
419 |
return path[2:] |
|
420 |
else: |
|
421 |
return path |
|
422 |
||
423 |
||
424 |
||
2598.3.1
by Kuno Meyer
fix method rename glob_expand_for_win32 -> win32utils.glob_expand |
425 |
def glob_expand(file_list): |
2568.2.2
by Robert Collins
* New method ``_glob_expand_file_list_if_needed`` on the ``Command`` class |
426 |
"""Replacement for glob expansion by the shell.
|
427 |
||
428 |
Win32's cmd.exe does not do glob expansion (eg ``*.py``), so we do our own
|
|
429 |
here.
|
|
430 |
||
431 |
:param file_list: A list of filenames which may include shell globs.
|
|
432 |
:return: An expanded list of filenames.
|
|
433 |
||
434 |
Introduced in bzrlib 0.18.
|
|
435 |
"""
|
|
436 |
if not file_list: |
|
437 |
return [] |
|
438 |
import glob |
|
439 |
expanded_file_list = [] |
|
440 |
for possible_glob in file_list: |
|
2617.5.8
by Kuno Meyer
Extended tests for unicode chars outside of the iso-8859-* range |
441 |
# work around bugs in glob.glob()
|
442 |
# - Python bug #1001604 ("glob doesn't return unicode with ...")
|
|
443 |
# - failing expansion for */* with non-iso-8859-* chars
|
|
444 |
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 |
445 |
glob_files = glob.glob(possible_glob) |
446 |
||
447 |
if glob_files == []: |
|
448 |
# special case to let the normal code path handle
|
|
449 |
# files that do not exists
|
|
2617.5.8
by Kuno Meyer
Extended tests for unicode chars outside of the iso-8859-* range |
450 |
expanded_file_list.append( |
451 |
_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 |
452 |
else: |
2617.5.8
by Kuno Meyer
Extended tests for unicode chars outside of the iso-8859-* range |
453 |
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 |
454 |
expanded_file_list += glob_files |
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
455 |
|
456 |
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 |
457 |
|
458 |
||
459 |
def get_app_path(appname): |
|
460 |
"""Look up in Windows registry for full path to application executable.
|
|
4031.3.1
by Frank Aspell
Fixing various typos |
461 |
Typically, applications create subkey with their basename
|
2681.4.1
by Alexander Belchenko
win32: looking for full path of mail client executable in registry |
462 |
in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
|
463 |
||
464 |
:param appname: name of application (if no filename extension
|
|
465 |
is specified, .exe used)
|
|
466 |
:return: full path to aplication executable from registry,
|
|
467 |
or appname itself if nothing found.
|
|
468 |
"""
|
|
2681.4.3
by Alexander Belchenko
move import _winreg into function get_app_path to avoid ImportError on non-win32 platforms |
469 |
import _winreg |
4476.2.1
by Alexander Belchenko
win32utils.py: get_app_path() can read path for wordpad.exe (data type_id is REG_EXPAND_SZ). |
470 |
|
471 |
basename = appname |
|
472 |
if not os.path.splitext(basename)[1]: |
|
473 |
basename = appname + '.exe' |
|
474 |
||
2681.4.1
by Alexander Belchenko
win32: looking for full path of mail client executable in registry |
475 |
try: |
476 |
hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, |
|
4476.2.1
by Alexander Belchenko
win32utils.py: get_app_path() can read path for wordpad.exe (data type_id is REG_EXPAND_SZ). |
477 |
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\' + |
478 |
basename) |
|
2681.4.1
by Alexander Belchenko
win32: looking for full path of mail client executable in registry |
479 |
except EnvironmentError: |
480 |
return appname |
|
481 |
||
482 |
try: |
|
483 |
try: |
|
4476.2.1
by Alexander Belchenko
win32utils.py: get_app_path() can read path for wordpad.exe (data type_id is REG_EXPAND_SZ). |
484 |
path, type_id = _winreg.QueryValueEx(hkey, '') |
2681.4.1
by Alexander Belchenko
win32: looking for full path of mail client executable in registry |
485 |
except WindowsError: |
4476.2.1
by Alexander Belchenko
win32utils.py: get_app_path() can read path for wordpad.exe (data type_id is REG_EXPAND_SZ). |
486 |
return appname |
2681.4.1
by Alexander Belchenko
win32: looking for full path of mail client executable in registry |
487 |
finally: |
488 |
_winreg.CloseKey(hkey) |
|
489 |
||
4476.2.1
by Alexander Belchenko
win32utils.py: get_app_path() can read path for wordpad.exe (data type_id is REG_EXPAND_SZ). |
490 |
if type_id == REG_SZ: |
491 |
return path |
|
492 |
if type_id == REG_EXPAND_SZ and has_win32api: |
|
493 |
fullpath = win32api.ExpandEnvironmentStrings(path) |
|
4476.2.2
by Alexander Belchenko
remove quotes around value only if there is pair of quotes (igc review) |
494 |
if len(fullpath) > 1 and fullpath[0] == '"' and fullpath[-1] == '"': |
4476.2.1
by Alexander Belchenko
win32utils.py: get_app_path() can read path for wordpad.exe (data type_id is REG_EXPAND_SZ). |
495 |
fullpath = fullpath[1:-1] # remove quotes around value |
496 |
return fullpath |
|
497 |
return appname |
|
3023.1.2
by Alexander Belchenko
Martin's review. |
498 |
|
499 |
||
500 |
def set_file_attr_hidden(path): |
|
501 |
"""Set file attributes to hidden if possible"""
|
|
502 |
if has_win32file: |
|
4505.2.1
by Alexander Belchenko
Set hidden attribute on .bzr directory below unicode path should never fail with error. The operation should succeed even if bzr unable to set the attribute. (related to bug #335362). |
503 |
if winver != 'Windows 98': |
504 |
SetFileAttributes = win32file.SetFileAttributesW |
|
505 |
else: |
|
506 |
SetFileAttributes = win32file.SetFileAttributes |
|
507 |
try: |
|
508 |
SetFileAttributes(path, win32file.FILE_ATTRIBUTE_HIDDEN) |
|
509 |
except pywintypes.error, e: |
|
4505.2.2
by Alexander Belchenko
forgotten import |
510 |
from bzrlib import trace |
4505.2.1
by Alexander Belchenko
Set hidden attribute on .bzr directory below unicode path should never fail with error. The operation should succeed even if bzr unable to set the attribute. (related to bug #335362). |
511 |
trace.mutter('Unable to set hidden attribute on %r: %s', path, e) |
4355.2.1
by Alexander Belchenko
Using unicode Windows API to obtain command-line arguments. |
512 |
|
513 |
||
514 |
if has_ctypes and winver != 'Windows 98': |
|
515 |
def get_unicode_argv(): |
|
516 |
LPCWSTR = ctypes.c_wchar_p |
|
517 |
INT = ctypes.c_int |
|
518 |
POINTER = ctypes.POINTER |
|
519 |
prototype = ctypes.WINFUNCTYPE(LPCWSTR) |
|
520 |
GetCommandLine = prototype(("GetCommandLineW", |
|
521 |
ctypes.windll.kernel32)) |
|
522 |
prototype = ctypes.WINFUNCTYPE(POINTER(LPCWSTR), LPCWSTR, POINTER(INT)) |
|
523 |
CommandLineToArgv = prototype(("CommandLineToArgvW", |
|
524 |
ctypes.windll.shell32)) |
|
525 |
c = INT(0) |
|
526 |
pargv = CommandLineToArgv(GetCommandLine(), ctypes.byref(c)) |
|
4355.2.4
by Alexander Belchenko
win32utils.py: get_unicode_argv: get bzr options as tail of argv list based on the number of items in sys.argv[1:] list. |
527 |
# Skip the first argument, since we only care about parameters
|
4355.2.1
by Alexander Belchenko
Using unicode Windows API to obtain command-line arguments. |
528 |
argv = [pargv[i] for i in range(1, c.value)] |
529 |
if getattr(sys, 'frozen', None) is None: |
|
4355.2.4
by Alexander Belchenko
win32utils.py: get_unicode_argv: get bzr options as tail of argv list based on the number of items in sys.argv[1:] list. |
530 |
# Invoked via 'python.exe' which takes the form:
|
531 |
# python.exe [PYTHON_OPTIONS] C:\Path\bzr [BZR_OPTIONS]
|
|
532 |
# we need to get only BZR_OPTIONS part,
|
|
4355.2.5
by Alexander Belchenko
improve comment |
533 |
# so let's using sys.argv[1:] as reference to get the tail
|
534 |
# of unicode argv
|
|
4355.2.4
by Alexander Belchenko
win32utils.py: get_unicode_argv: get bzr options as tail of argv list based on the number of items in sys.argv[1:] list. |
535 |
tail_len = len(sys.argv[1:]) |
536 |
ix = len(argv) - tail_len |
|
537 |
argv = argv[ix:] |
|
4355.2.1
by Alexander Belchenko
Using unicode Windows API to obtain command-line arguments. |
538 |
return argv |
4355.2.2
by Alexander Belchenko
osutils.py: get_unicode_argv function (to obtain unicode command line arguments from sys.argv) moved to the beginning of module based on suggestions from review of John Meinel. |
539 |
else: |
540 |
get_unicode_argv = None |