/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4988.10.3 by John Arbash Meinel
Merge bzr.dev 5007, resolve conflict, update NEWS
1
# Copyright (C) 2005-2010 Canonical Ltd
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
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
4786.1.1 by John Arbash Meinel
Work on doing globbing, etc for all commands on Windows.
22
import glob
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
23
import os
1185.16.86 by mbp at sourcefrog
- win32 get_console_size from Alexander
24
import struct
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
25
import sys
26
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
27
from breezy import (
6362.2.2 by Martin Packman
Use get_environ_unicode throughout win32utils and always return unicode paths
28
    cmdline,
29
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
30
from breezy.i18n import gettext
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
31
7341.1.1 by Martin
Remove win32 code using pywin32 library
32
has_ctypes_win32 = False
33
if sys.platform == 'win32':
34
    try:
35
        import ctypes
36
    except ImportError:
37
        has_ctypes_win32 = False
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
38
39
40
# Special Win32 API constants
41
# Handles of std streams
1704.2.3 by Martin Pool
(win32) Detect terminal width using GetConsoleScreenBufferInfo (Alexander)
42
WIN32_STDIN_HANDLE = -10
43
WIN32_STDOUT_HANDLE = -11
44
WIN32_STDERR_HANDLE = -12
45
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
46
# CSIDL constants (from MSDN 2003)
47
CSIDL_APPDATA = 0x001A      # Application Data folder
7143.15.2 by Jelmer Vernooij
Run autopep8.
48
# <user name>\Local Settings\Application Data (non roaming)
49
CSIDL_LOCAL_APPDATA = 0x001c
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
50
CSIDL_PERSONAL = 0x0005     # My Documents folder
51
52
# from winapi C headers
53
MAX_PATH = 260
54
UNLEN = 256
55
MAX_COMPUTERNAME_LENGTH = 31
56
4476.2.1 by Alexander Belchenko
win32utils.py: get_app_path() can read path for wordpad.exe (data type_id is REG_EXPAND_SZ).
57
# Registry data type ids
58
REG_SZ = 1
59
REG_EXPAND_SZ = 2
60
1704.2.3 by Martin Pool
(win32) Detect terminal width using GetConsoleScreenBufferInfo (Alexander)
61
4011.1.1 by John Arbash Meinel
Implement -Dmemory for win32
62
def debug_memory_win32api(message='', short=True):
63
    """Use trace.note() to dump the running memory info."""
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
64
    from breezy import trace
7341.1.1 by Martin
Remove win32 code using pywin32 library
65
    if has_ctypes_win32:
4011.1.1 by John Arbash Meinel
Implement -Dmemory for win32
66
        class PROCESS_MEMORY_COUNTERS_EX(ctypes.Structure):
67
            """Used by GetProcessMemoryInfo"""
68
            _fields_ = [('cb', ctypes.c_ulong),
69
                        ('PageFaultCount', ctypes.c_ulong),
70
                        ('PeakWorkingSetSize', ctypes.c_size_t),
71
                        ('WorkingSetSize', ctypes.c_size_t),
72
                        ('QuotaPeakPagedPoolUsage', ctypes.c_size_t),
73
                        ('QuotaPagedPoolUsage', ctypes.c_size_t),
74
                        ('QuotaPeakNonPagedPoolUsage', ctypes.c_size_t),
75
                        ('QuotaNonPagedPoolUsage', ctypes.c_size_t),
76
                        ('PagefileUsage', ctypes.c_size_t),
77
                        ('PeakPagefileUsage', ctypes.c_size_t),
78
                        ('PrivateUsage', ctypes.c_size_t),
7143.15.2 by Jelmer Vernooij
Run autopep8.
79
                        ]
4011.1.1 by John Arbash Meinel
Implement -Dmemory for win32
80
        cur_process = ctypes.windll.kernel32.GetCurrentProcess()
81
        mem_struct = PROCESS_MEMORY_COUNTERS_EX()
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
82
        ret = ctypes.windll.psapi.GetProcessMemoryInfo(
83
            cur_process, ctypes.byref(mem_struct), ctypes.sizeof(mem_struct))
4011.1.1 by John Arbash Meinel
Implement -Dmemory for win32
84
        if not ret:
6138.3.4 by Jonathan Riddell
add gettext() to uses of trace.note()
85
            trace.note(gettext('Failed to GetProcessMemoryInfo()'))
4011.1.1 by John Arbash Meinel
Implement -Dmemory for win32
86
            return
87
        info = {'PageFaultCount': mem_struct.PageFaultCount,
88
                'PeakWorkingSetSize': mem_struct.PeakWorkingSetSize,
89
                'WorkingSetSize': mem_struct.WorkingSetSize,
90
                'QuotaPeakPagedPoolUsage': mem_struct.QuotaPeakPagedPoolUsage,
91
                'QuotaPagedPoolUsage': mem_struct.QuotaPagedPoolUsage,
4989.1.6 by Vincent Ladeuil
Add comments and update HACKING.txt about which units should be used.
92
                'QuotaPeakNonPagedPoolUsage':
93
                    mem_struct.QuotaPeakNonPagedPoolUsage,
4011.1.1 by John Arbash Meinel
Implement -Dmemory for win32
94
                'QuotaNonPagedPoolUsage': mem_struct.QuotaNonPagedPoolUsage,
95
                'PagefileUsage': mem_struct.PagefileUsage,
96
                'PeakPagefileUsage': mem_struct.PeakPagefileUsage,
97
                'PrivateUsage': mem_struct.PrivateUsage,
7143.15.2 by Jelmer Vernooij
Run autopep8.
98
                }
4011.1.1 by John Arbash Meinel
Implement -Dmemory for win32
99
    else:
6138.3.4 by Jonathan Riddell
add gettext() to uses of trace.note()
100
        trace.note(gettext('Cannot debug memory on win32 without ctypes'
7143.15.2 by Jelmer Vernooij
Run autopep8.
101
                           ' or win32process'))
4011.1.1 by John Arbash Meinel
Implement -Dmemory for win32
102
        return
4163.1.1 by John Arbash Meinel
Some small fixes for the win32 'trace.debug_mem()' code.
103
    if short:
4989.1.6 by Vincent Ladeuil
Add comments and update HACKING.txt about which units should be used.
104
        # using base-2 units (see HACKING.txt).
6147.1.1 by Jonathan Riddell
use .format() instead of % for string formatting where there are multiple formats in one string to allow for translations
105
        trace.note(gettext('WorkingSize {0:>7}KiB'
7143.15.2 by Jelmer Vernooij
Run autopep8.
106
                           '\tPeakWorking {1:>7}KiB\t{2}').format(
4163.1.1 by John Arbash Meinel
Some small fixes for the win32 'trace.debug_mem()' code.
107
                   info['WorkingSetSize'] / 1024,
108
                   info['PeakWorkingSetSize'] / 1024,
6147.1.1 by Jonathan Riddell
use .format() instead of % for string formatting where there are multiple formats in one string to allow for translations
109
                   message))
4163.1.1 by John Arbash Meinel
Some small fixes for the win32 'trace.debug_mem()' code.
110
        return
111
    if message:
112
        trace.note('%s', message)
7143.15.2 by Jelmer Vernooij
Run autopep8.
113
    trace.note(gettext('WorkingSize       %8d KiB'),
114
               info['WorkingSetSize'] / 1024)
115
    trace.note(gettext('PeakWorking       %8d KiB'),
116
               info['PeakWorkingSetSize'] / 1024)
117
    trace.note(gettext('PagefileUsage     %8d KiB'),
118
               info.get('PagefileUsage', 0) / 1024)
6138.3.4 by Jonathan Riddell
add gettext() to uses of trace.note()
119
    trace.note(gettext('PeakPagefileUsage %8d KiB'),
4989.1.6 by Vincent Ladeuil
Add comments and update HACKING.txt about which units should be used.
120
               info.get('PeakPagefileUsage', 0) / 1024)
7143.15.2 by Jelmer Vernooij
Run autopep8.
121
    trace.note(gettext('PrivateUsage      %8d KiB'),
122
               info.get('PrivateUsage', 0) / 1024)
6138.3.4 by Jonathan Riddell
add gettext() to uses of trace.note()
123
    trace.note(gettext('PageFaultCount    %8d'), info.get('PageFaultCount', 0))
4011.1.1 by John Arbash Meinel
Implement -Dmemory for win32
124
125
1185.16.86 by mbp at sourcefrog
- win32 get_console_size from Alexander
126
def get_console_size(defaultx=80, defaulty=25):
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
127
    """Return size of current console.
128
129
    This function try to determine actual size of current working
130
    console window and return tuple (sizex, sizey) if success,
131
    or default size (defaultx, defaulty) otherwise.
132
    """
7341.1.1 by Martin
Remove win32 code using pywin32 library
133
    if not has_ctypes_win32:
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
134
        # no ctypes is found
135
        return (defaultx, defaulty)
136
137
    # To avoid problem with redirecting output via pipe
4747.3.6 by Vincent Ladeuil
terminal_width can now returns None.
138
    # we need to use stderr instead of stdout
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
139
    h = ctypes.windll.kernel32.GetStdHandle(WIN32_STDERR_HANDLE)
140
    csbi = ctypes.create_string_buffer(22)
141
    res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
142
143
    if res:
144
        (bufx, bufy, curx, cury, wattr,
7143.15.2 by Jelmer Vernooij
Run autopep8.
145
         left, top, right, bottom, maxx, maxy) = struct.unpack(
4989.1.6 by Vincent Ladeuil
Add comments and update HACKING.txt about which units should be used.
146
            "hhhhHhhhhhh", csbi.raw)
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
147
        sizex = right - left + 1
148
        sizey = bottom - top + 1
149
        return (sizex, sizey)
150
    else:
151
        return (defaultx, defaulty)
152
153
3638.4.1 by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can
154
def _get_sh_special_folder_path(csidl):
155
    """Call SHGetSpecialFolderPathW if available, or return None.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
156
3638.4.1 by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can
157
    Result is always unicode (or None).
158
    """
7341.1.1 by Martin
Remove win32 code using pywin32 library
159
    if has_ctypes_win32:
3638.4.1 by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can
160
        try:
161
            SHGetSpecialFolderPath = \
162
                ctypes.windll.shell32.SHGetSpecialFolderPathW
163
        except AttributeError:
164
            pass
165
        else:
166
            buf = ctypes.create_unicode_buffer(MAX_PATH)
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
167
            if SHGetSpecialFolderPath(None, buf, csidl, 0):
3638.4.1 by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can
168
                return buf.value
169
    return None
170
171
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
172
def get_appdata_location():
173
    """Return Application Data location.
174
    Return None if we cannot obtain location.
175
3638.4.1 by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can
176
    Windows defines two 'Application Data' folders per user - a 'roaming'
177
    one that moves with the user as they logon to different machines, and
178
    a 'local' one that stays local to the machine.  This returns the 'roaming'
179
    directory, and thus is suitable for storing user-preferences, etc.
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
180
    """
3638.4.1 by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can
181
    appdata = _get_sh_special_folder_path(CSIDL_APPDATA)
182
    if appdata:
183
        return appdata
6362.2.2 by Martin Packman
Use get_environ_unicode throughout win32utils and always return unicode paths
184
    # Use APPDATA if defined, will return None if not
185
    return get_environ_unicode('APPDATA')
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
186
187
3638.4.1 by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can
188
def get_local_appdata_location():
189
    """Return Local Application Data location.
190
    Return the same as get_appdata_location() if we cannot obtain location.
191
192
    Windows defines two 'Application Data' folders per user - a 'roaming'
193
    one that moves with the user as they logon to different machines, and
194
    a 'local' one that stays local to the machine.  This returns the 'local'
195
    directory, and thus is suitable for caches, temp files and other things
196
    which don't need to move with the user.
197
    """
198
    local = _get_sh_special_folder_path(CSIDL_LOCAL_APPDATA)
199
    if local:
200
        return local
201
    # Vista supplies LOCALAPPDATA, but XP and earlier do not.
6362.2.2 by Martin Packman
Use get_environ_unicode throughout win32utils and always return unicode paths
202
    local = get_environ_unicode('LOCALAPPDATA')
3638.4.1 by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can
203
    if local:
204
        return local
205
    return get_appdata_location()
206
207
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
208
def get_home_location():
209
    """Return user's home location.
210
    Assume on win32 it's the <My Documents> folder.
211
    If location cannot be obtained return system drive root,
212
    i.e. C:\
213
    """
3638.4.1 by Mark Hammond
Add win32utils.get_local_appdata_location() so bzr and plugins can
214
    home = _get_sh_special_folder_path(CSIDL_PERSONAL)
215
    if home:
216
        return home
6362.2.2 by Martin Packman
Use get_environ_unicode throughout win32utils and always return unicode paths
217
    home = get_environ_unicode('HOME')
218
    if home is not None:
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
219
        return home
6362.2.2 by Martin Packman
Use get_environ_unicode throughout win32utils and always return unicode paths
220
    homepath = get_environ_unicode('HOMEPATH')
221
    if homepath is not None:
222
        return os.path.join(get_environ_unicode('HOMEDIR', ''), home)
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
223
    # at least return windows root directory
6362.2.2 by Martin Packman
Use get_environ_unicode throughout win32utils and always return unicode paths
224
    windir = get_environ_unicode('WINDIR')
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
225
    if windir:
2610.1.1 by Martin Pool
Fix get_home_location on Win98 (gzlist,r=john,r=alexander)
226
        return os.path.splitdrive(windir)[0] + '/'
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
227
    # otherwise C:\ is good enough for 98% users
6973.6.4 by Jelmer Vernooij
Avoid text_type()
228
    return u'C:/'
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
229
230
231
def get_user_name():
232
    """Return user name as login name.
233
    If name cannot be obtained return None.
234
    """
7341.1.1 by Martin
Remove win32 code using pywin32 library
235
    if has_ctypes_win32:
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
236
        try:
237
            advapi32 = ctypes.windll.advapi32
7341.1.1 by Martin
Remove win32 code using pywin32 library
238
            GetUserName = getattr(advapi32, 'GetUserNameW')
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
239
        except AttributeError:
240
            pass
241
        else:
7341.1.1 by Martin
Remove win32 code using pywin32 library
242
            buf = ctypes.create_unicode_buffer(UNLEN + 1)
7143.15.2 by Jelmer Vernooij
Run autopep8.
243
            n = ctypes.c_int(UNLEN + 1)
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
244
            if GetUserName(buf, ctypes.byref(n)):
7341.1.1 by Martin
Remove win32 code using pywin32 library
245
                return buf.value
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
246
    # otherwise try env variables
6362.2.2 by Martin Packman
Use get_environ_unicode throughout win32utils and always return unicode paths
247
    return get_environ_unicode('USERNAME')
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
248
249
3626.1.3 by John Arbash Meinel
Use GetComputerNameEx from ctypes when available.
250
# 1 == ComputerNameDnsHostname, which returns "The DNS host name of the local
251
# computer or the cluster associated with the local computer."
252
_WIN32_ComputerNameDnsHostname = 1
253
7143.15.2 by Jelmer Vernooij
Run autopep8.
254
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
255
def get_host_name():
256
    """Return host machine name.
257
    If name cannot be obtained return None.
258
6362.2.3 by Martin Packman
Always return username and hostname as unicode in win32utils
259
    :return: A unicode string representing the host name.
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
260
    """
7341.1.1 by Martin
Remove win32 code using pywin32 library
261
    if has_ctypes_win32:
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
262
        try:
263
            kernel32 = ctypes.windll.kernel32
264
        except AttributeError:
7143.15.2 by Jelmer Vernooij
Run autopep8.
265
            pass  # Missing the module we need
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
266
        else:
7341.1.1 by Martin
Remove win32 code using pywin32 library
267
            buf = ctypes.create_unicode_buffer(MAX_COMPUTERNAME_LENGTH + 1)
7143.15.2 by Jelmer Vernooij
Run autopep8.
268
            n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH + 1)
3626.1.3 by John Arbash Meinel
Use GetComputerNameEx from ctypes when available.
269
270
            # Try GetComputerNameEx which gives a proper Unicode hostname
7341.1.1 by Martin
Remove win32 code using pywin32 library
271
            GetComputerNameEx = getattr(kernel32, 'GetComputerNameExW', None)
3626.1.3 by John Arbash Meinel
Use GetComputerNameEx from ctypes when available.
272
            if (GetComputerNameEx is not None
273
                and GetComputerNameEx(_WIN32_ComputerNameDnsHostname,
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
274
                                      buf, ctypes.byref(n))):
7341.1.1 by Martin
Remove win32 code using pywin32 library
275
                return buf.value
6362.2.2 by Martin Packman
Use get_environ_unicode throughout win32utils and always return unicode paths
276
    return get_environ_unicode('COMPUTERNAME')
2245.4.1 by Alexander Belchenko
win32utils: Windows-specific functions that use Win32 API via ctypes
277
278
2617.5.8 by Kuno Meyer
Extended tests for unicode chars outside of the iso-8859-* range
279
def _ensure_with_dir(path):
4989.1.6 by Vincent Ladeuil
Add comments and update HACKING.txt about which units should be used.
280
    if (not os.path.split(path)[0] or path.startswith(u'*')
7143.15.2 by Jelmer Vernooij
Run autopep8.
281
            or path.startswith(u'?')):
2617.5.8 by Kuno Meyer
Extended tests for unicode chars outside of the iso-8859-* range
282
        return u'./' + path, True
283
    else:
284
        return path, False
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
285
7143.15.2 by Jelmer Vernooij
Run autopep8.
286
2617.5.8 by Kuno Meyer
Extended tests for unicode chars outside of the iso-8859-* range
287
def _undo_ensure_with_dir(path, corrected):
288
    if corrected:
289
        return path[2:]
290
    else:
291
        return path
292
293
4786.1.2 by John Arbash Meinel
Refactor the glob_expand code a bit, making the inner function more reusable.
294
def glob_one(possible_glob):
295
    """Same as glob.glob().
296
297
    work around bugs in glob.glob()
298
    - Python bug #1001604 ("glob doesn't return unicode with ...")
299
    - failing expansion for */* with non-iso-8859-* chars
300
    """
301
    corrected_glob, corrected = _ensure_with_dir(possible_glob)
302
    glob_files = glob.glob(corrected_glob)
303
304
    if not glob_files:
305
        # special case to let the normal code path handle
306
        # files that do not exist, etc.
307
        glob_files = [possible_glob]
308
    elif corrected:
309
        glob_files = [_undo_ensure_with_dir(elem, corrected)
310
                      for elem in glob_files]
311
    return [elem.replace(u'\\', u'/') for elem in glob_files]
312
313
2598.3.1 by Kuno Meyer
fix method rename glob_expand_for_win32 -> win32utils.glob_expand
314
def glob_expand(file_list):
2568.2.2 by Robert Collins
* New method ``_glob_expand_file_list_if_needed`` on the ``Command`` class
315
    """Replacement for glob expansion by the shell.
316
317
    Win32's cmd.exe does not do glob expansion (eg ``*.py``), so we do our own
318
    here.
319
320
    :param file_list: A list of filenames which may include shell globs.
321
    :return: An expanded list of filenames.
322
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
323
    Introduced in breezy 0.18.
2568.2.2 by Robert Collins
* New method ``_glob_expand_file_list_if_needed`` on the ``Command`` class
324
    """
325
    if not file_list:
326
        return []
327
    expanded_file_list = []
328
    for possible_glob in file_list:
4786.1.2 by John Arbash Meinel
Refactor the glob_expand code a bit, making the inner function more reusable.
329
        expanded_file_list.extend(glob_one(possible_glob))
330
    return expanded_file_list
2681.4.1 by Alexander Belchenko
win32: looking for full path of mail client executable in registry
331
332
333
def get_app_path(appname):
5891.1.2 by Andrew Bennetts
Fix a bunch of docstring formatting nits, making pydoctor a bit happier.
334
    r"""Look up in Windows registry for full path to application executable.
4031.3.1 by Frank Aspell
Fixing various typos
335
    Typically, applications create subkey with their basename
2681.4.1 by Alexander Belchenko
win32: looking for full path of mail client executable in registry
336
    in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
337
338
    :param  appname:    name of application (if no filename extension
339
                        is specified, .exe used)
340
    :return:    full path to aplication executable from registry,
341
                or appname itself if nothing found.
342
    """
2681.4.3 by Alexander Belchenko
move import _winreg into function get_app_path to avoid ImportError on non-win32 platforms
343
    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).
344
345
    basename = appname
346
    if not os.path.splitext(basename)[1]:
347
        basename = appname + '.exe'
348
2681.4.1 by Alexander Belchenko
win32: looking for full path of mail client executable in registry
349
    try:
350
        hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
7143.15.2 by Jelmer Vernooij
Run autopep8.
351
                               'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\' +
352
                               basename)
2681.4.1 by Alexander Belchenko
win32: looking for full path of mail client executable in registry
353
    except EnvironmentError:
354
        return appname
355
356
    try:
357
        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).
358
            path, type_id = _winreg.QueryValueEx(hkey, '')
2681.4.1 by Alexander Belchenko
win32: looking for full path of mail client executable in registry
359
        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).
360
            return appname
2681.4.1 by Alexander Belchenko
win32: looking for full path of mail client executable in registry
361
    finally:
362
        _winreg.CloseKey(hkey)
363
4476.2.1 by Alexander Belchenko
win32utils.py: get_app_path() can read path for wordpad.exe (data type_id is REG_EXPAND_SZ).
364
    if type_id == REG_SZ:
365
        return path
366
    if type_id == REG_EXPAND_SZ and has_win32api:
367
        fullpath = win32api.ExpandEnvironmentStrings(path)
4476.2.2 by Alexander Belchenko
remove quotes around value only if there is pair of quotes (igc review)
368
        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).
369
            fullpath = fullpath[1:-1]   # remove quotes around value
370
        return fullpath
371
    return appname
3023.1.2 by Alexander Belchenko
Martin's review.
372
373
374
def set_file_attr_hidden(path):
375
    """Set file attributes to hidden if possible"""
7341.1.1 by Martin
Remove win32 code using pywin32 library
376
    if not has_ctypes_win32:
377
        return
378
    from ctypes.wintypes import BOOL, DWORD, LPCWSTR
379
    _kernel32 = ctypes.windll.kernel32
380
    # <https://docs.microsoft.com/windows/desktop/api/fileapi/nf-fileapi-setfileattributesw>
381
    _SetFileAttributesW = ctypes.WINFUNCTYPE(BOOL, LPCWSTR, DWORD)(
382
        ("SetFileAttributesW", _kernel32))
383
    FILE_ATTRIBUTE_HIDDEN = 2
384
    if not SetFileAttributes(path, FILE_ATTRIBUTE_HIDDEN):
385
        e = ctypes.WinError()
386
        from . import trace
387
        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.
388
389
5274.4.12 by Martin
Change interface of _command_line_to_argv so old tests can still be used with new stripping logic
390
def _command_line_to_argv(command_line, argv, single_quotes_allowed=False):
4913.5.2 by Gordon Tyler
Changed shlex_split_unicode to prevent wildcard expansion in the win32 codepath.
391
    """Convert a Unicode command line into a list of argv arguments.
392
4913.5.25 by Gordon Tyler
Simplified win32utils.command_line_to_argv and made it private since it's no longer used outside of the module.
393
    It performs wildcard expansion to make wildcards act closer to how they
394
    work in posix shells, versus how they work by default on Windows. Quoted
395
    arguments are left untouched.
4913.5.2 by Gordon Tyler
Changed shlex_split_unicode to prevent wildcard expansion in the win32 codepath.
396
397
    :param command_line: The unicode string to split into an arg list.
4913.5.11 by Gordon Tyler
Added optional single quote support to UnicodeShlex and thus command_line_to_argv (defaults to disabled).
398
    :param single_quotes_allowed: Whether single quotes are accepted as quoting
399
                                  characters like double quotes. False by
400
                                  default.
4913.5.2 by Gordon Tyler
Changed shlex_split_unicode to prevent wildcard expansion in the win32 codepath.
401
    :return: A list of unicode strings.
4786.1.1 by John Arbash Meinel
Work on doing globbing, etc for all commands on Windows.
402
    """
6437.65.1 by Scanferlato
Fix two typos in docs and comment
403
    # First, split the command line
7143.15.2 by Jelmer Vernooij
Run autopep8.
404
    s = cmdline.Splitter(
405
        command_line, single_quotes_allowed=single_quotes_allowed)
406
407
    # Bug #587868 Now make sure that the length of s agrees with sys.argv
408
    # we do this by simply counting the number of arguments in each. The counts should
409
    # agree no matter what encoding sys.argv is in (AFAIK)
410
    # len(arguments) < len(sys.argv) should be an impossibility since python gets
5274.4.8 by Jason Spashett
Reverse merge -r 5280..5279
411
    # args from the very same PEB as does GetCommandLineW
412
    arguments = list(s)
7143.15.2 by Jelmer Vernooij
Run autopep8.
413
5274.4.8 by Jason Spashett
Reverse merge -r 5280..5279
414
    # Now shorten the command line we get from GetCommandLineW to match sys.argv
5274.4.12 by Martin
Change interface of _command_line_to_argv so old tests can still be used with new stripping logic
415
    if len(arguments) < len(argv):
5274.4.11 by Martin
Minor cleanup, use plain AssertionError and revert needless variable rename
416
        raise AssertionError("Split command line can't be shorter than argv")
5274.4.12 by Martin
Change interface of _command_line_to_argv so old tests can still be used with new stripping logic
417
    arguments = arguments[len(arguments) - len(argv):]
7143.15.2 by Jelmer Vernooij
Run autopep8.
418
5274.4.2 by Jason Spashett
Tidied up fix for 587868. Put assert in for impossible case in command line lengths.
419
    # Carry on to process globs (metachars) in the command line
5274.4.1 by Jason Spashett
Initial Fix for 587868
420
    # expand globs if necessary
4786.1.1 by John Arbash Meinel
Work on doing globbing, etc for all commands on Windows.
421
    # TODO: Use 'globbing' instead of 'glob.glob', this gives us stuff like
422
    #       '**/' style globs
5274.4.11 by Martin
Minor cleanup, use plain AssertionError and revert needless variable rename
423
    args = []
5274.4.8 by Jason Spashett
Reverse merge -r 5280..5279
424
    for is_quoted, arg in arguments:
4913.5.25 by Gordon Tyler
Simplified win32utils.command_line_to_argv and made it private since it's no longer used outside of the module.
425
        if is_quoted or not glob.has_magic(arg):
5274.4.11 by Martin
Minor cleanup, use plain AssertionError and revert needless variable rename
426
            args.append(arg)
4786.1.1 by John Arbash Meinel
Work on doing globbing, etc for all commands on Windows.
427
        else:
5274.4.11 by Martin
Minor cleanup, use plain AssertionError and revert needless variable rename
428
            args.extend(glob_one(arg))
429
    return args
5274.4.3 by Jason Spashett
Merge from lp:bzr. Remove code for fix 588277 (revs 5274.3.1 - 5274.3.2) as this bug also fixes that issue. Make changes as per code review.
430
4786.1.1 by John Arbash Meinel
Work on doing globbing, etc for all commands on Windows.
431
7341.1.1 by Martin
Remove win32 code using pywin32 library
432
if has_ctypes_win32:
4355.2.1 by Alexander Belchenko
Using unicode Windows API to obtain command-line arguments.
433
    def get_unicode_argv():
4913.5.15 by Gordon Tyler
Python < 2.6 doesn't support use_last_error.
434
        prototype = ctypes.WINFUNCTYPE(ctypes.c_wchar_p)
4913.5.1 by Gordon Tyler
Changed shlex_split_unicode in commands.py to use win32utils.command_line_to_argv on win32 and cleaned up win32utils.get_unicode_argv.
435
        GetCommandLineW = prototype(("GetCommandLineW",
436
                                     ctypes.windll.kernel32))
437
        command_line = GetCommandLineW()
438
        if command_line is None:
439
            raise ctypes.WinError()
5274.4.8 by Jason Spashett
Reverse merge -r 5280..5279
440
        # Skip the first argument, since we only care about parameters
5274.4.12 by Martin
Change interface of _command_line_to_argv so old tests can still be used with new stripping logic
441
        argv = _command_line_to_argv(command_line, sys.argv)[1:]
4355.2.1 by Alexander Belchenko
Using unicode Windows API to obtain command-line arguments.
442
        return argv
6658.6.1 by Martin
Remove winver, win98 support code, and deprecated code
443
6339.2.3 by Martin Packman
Remove ability to specify a maximum buffer size as it serves no particular purpose
444
    def get_environ_unicode(key, default=None):
6339.2.1 by Martin Packman
Add wrapper for GetEnvironmentVariableW on windows for unicode environ access
445
        """Get `key` from environment as unicode or `default` if unset
446
6362.2.5 by Martin Packman
Tweaks including those suggested by vila in review
447
        The environment is natively unicode on modern windows versions but
448
        Python 2 only accesses it through the legacy bytestring api.
449
450
        Environmental variable names are case insenstive on Windows.
451
6339.2.3 by Martin Packman
Remove ability to specify a maximum buffer size as it serves no particular purpose
452
        A large enough buffer will be allocated to retrieve the value, though
453
        it may take two calls to the underlying library function.
6339.2.1 by Martin Packman
Add wrapper for GetEnvironmentVariableW on windows for unicode environ access
454
        """
6339.2.2 by Martin Packman
Variable name changes and docstring fix suggested by vila in review
455
        cfunc = getattr(get_environ_unicode, "_c_function", None)
456
        if cfunc is None:
6339.2.1 by Martin Packman
Add wrapper for GetEnvironmentVariableW on windows for unicode environ access
457
            from ctypes.wintypes import DWORD, LPCWSTR, LPWSTR
6339.2.2 by Martin Packman
Variable name changes and docstring fix suggested by vila in review
458
            cfunc = ctypes.WINFUNCTYPE(DWORD, LPCWSTR, LPWSTR, DWORD)(
6339.2.1 by Martin Packman
Add wrapper for GetEnvironmentVariableW on windows for unicode environ access
459
                ("GetEnvironmentVariableW", ctypes.windll.kernel32))
6339.2.2 by Martin Packman
Variable name changes and docstring fix suggested by vila in review
460
            get_environ_unicode._c_function = cfunc
7143.15.2 by Jelmer Vernooij
Run autopep8.
461
        buffer_size = 256  # heuristic, 256 characters often enough
6339.2.1 by Martin Packman
Add wrapper for GetEnvironmentVariableW on windows for unicode environ access
462
        while True:
7341.1.1 by Martin
Remove win32 code using pywin32 library
463
            buf = ctypes.create_unicode_buffer(buffer_size)
464
            length = cfunc(key, buf, buffer_size)
6339.2.1 by Martin Packman
Add wrapper for GetEnvironmentVariableW on windows for unicode environ access
465
            if not length:
466
                code = ctypes.GetLastError()
7143.15.2 by Jelmer Vernooij
Run autopep8.
467
                if code == 203:  # ERROR_ENVVAR_NOT_FOUND
6339.2.1 by Martin Packman
Add wrapper for GetEnvironmentVariableW on windows for unicode environ access
468
                    return default
469
                raise ctypes.WinError(code)
470
            if buffer_size > length:
7341.1.1 by Martin
Remove win32 code using pywin32 library
471
                return buf[:length]
6339.2.1 by Martin Packman
Add wrapper for GetEnvironmentVariableW on windows for unicode environ access
472
            buffer_size = length
5425.5.5 by Martin
Quick implementation of dead process detection on win32
473
474
7341.1.1 by Martin
Remove win32 code using pywin32 library
475
if has_ctypes_win32:
5425.5.5 by Martin
Quick implementation of dead process detection on win32
476
    from ctypes.wintypes import BOOL, DWORD, HANDLE
477
    _kernel32 = ctypes.windll.kernel32
478
    _CloseHandle = ctypes.WINFUNCTYPE(BOOL, HANDLE)(
479
        ("CloseHandle", _kernel32))
480
    _OpenProcess = ctypes.WINFUNCTYPE(HANDLE, DWORD, BOOL, DWORD)(
481
        ("OpenProcess", _kernel32))
7143.15.2 by Jelmer Vernooij
Run autopep8.
482
5425.5.5 by Martin
Quick implementation of dead process detection on win32
483
    def _ctypes_is_local_pid_dead(pid):
484
        """True if pid doesn't correspond to live process on this machine"""
7143.15.2 by Jelmer Vernooij
Run autopep8.
485
        handle = _OpenProcess(1, False, pid)  # PROCESS_TERMINATE
5425.5.5 by Martin
Quick implementation of dead process detection on win32
486
        if not handle:
487
            errorcode = ctypes.GetLastError()
7143.15.2 by Jelmer Vernooij
Run autopep8.
488
            if errorcode == 5:  # ERROR_ACCESS_DENIED
5425.5.5 by Martin
Quick implementation of dead process detection on win32
489
                # Probably something alive we're not allowed to kill
490
                return False
7143.15.2 by Jelmer Vernooij
Run autopep8.
491
            elif errorcode == 87:  # ERROR_INVALID_PARAMETER
5425.5.5 by Martin
Quick implementation of dead process detection on win32
492
                return True
493
            raise ctypes.WinError(errorcode)
494
        _CloseHandle(handle)
495
        return False
7341.1.1 by Martin
Remove win32 code using pywin32 library
496
5425.5.5 by Martin
Quick implementation of dead process detection on win32
497
    is_local_pid_dead = _ctypes_is_local_pid_dead