427
def glob_one(possible_glob):
428
"""Same as glob.glob().
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
434
corrected_glob, corrected = _ensure_with_dir(possible_glob)
435
glob_files = glob.glob(corrected_glob)
438
# special case to let the normal code path handle
439
# files that do not exist, etc.
440
glob_files = [possible_glob]
442
glob_files = [_undo_ensure_with_dir(elem, corrected)
443
for elem in glob_files]
444
return [elem.replace(u'\\', u'/') for elem in glob_files]
420
447
def glob_expand(file_list):
421
448
"""Replacement for glob expansion by the shell.
431
458
if not file_list:
434
460
expanded_file_list = []
435
461
for possible_glob in file_list:
437
# work around bugs in glob.glob()
438
# - Python bug #1001604 ("glob doesn't return unicode with ...")
439
# - failing expansion for */* with non-iso-8859-* chars
440
possible_glob, corrected = _ensure_with_dir(possible_glob)
441
glob_files = glob.glob(possible_glob)
444
# special case to let the normal code path handle
445
# files that do not exists
446
expanded_file_list.append(
447
_undo_ensure_with_dir(possible_glob, corrected))
449
glob_files = [_undo_ensure_with_dir(elem, corrected) for elem in glob_files]
450
expanded_file_list += glob_files
452
return [elem.replace(u'\\', u'/') for elem in expanded_file_list]
462
expanded_file_list.extend(glob_one(possible_glob))
463
return expanded_file_list
455
466
def get_app_path(appname):
456
467
"""Look up in Windows registry for full path to application executable.
457
Typicaly, applications create subkey with their basename
468
Typically, applications create subkey with their basename
458
469
in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
460
471
:param appname: name of application (if no filename extension
463
474
or appname itself if nothing found.
479
if not os.path.splitext(basename)[1]:
480
basename = appname + '.exe'
467
483
hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
468
r'SOFTWARE\Microsoft\Windows'
469
r'\CurrentVersion\App Paths')
484
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\' +
470
486
except EnvironmentError:
474
if not os.path.splitext(basename)[1]:
475
basename = appname + '.exe'
478
fullpath = _winreg.QueryValue(hkey, basename)
491
path, type_id = _winreg.QueryValueEx(hkey, '')
479
492
except WindowsError:
482
495
_winreg.CloseKey(hkey)
497
if type_id == REG_SZ:
499
if type_id == REG_EXPAND_SZ and has_win32api:
500
fullpath = win32api.ExpandEnvironmentStrings(path)
501
if len(fullpath) > 1 and fullpath[0] == '"' and fullpath[-1] == '"':
502
fullpath = fullpath[1:-1] # remove quotes around value
487
507
def set_file_attr_hidden(path):
488
508
"""Set file attributes to hidden if possible"""
489
509
if has_win32file:
490
win32file.SetFileAttributes(path, win32file.FILE_ATTRIBUTE_HIDDEN)
510
if winver != 'Windows 98':
511
SetFileAttributes = win32file.SetFileAttributesW
513
SetFileAttributes = win32file.SetFileAttributes
515
SetFileAttributes(path, win32file.FILE_ATTRIBUTE_HIDDEN)
516
except pywintypes.error, e:
517
from bzrlib import trace
518
trace.mutter('Unable to set hidden attribute on %r: %s', path, e)
521
def command_line_to_argv(command_line, wildcard_expansion=True,
522
single_quotes_allowed=False):
523
"""Convert a Unicode command line into a list of argv arguments.
525
This optionally does wildcard expansion, etc. It is intended to make
526
wildcards act closer to how they work in posix shells, versus how they
527
work by default on Windows. Quoted arguments are left untouched.
529
:param command_line: The unicode string to split into an arg list.
530
:param wildcard_expansion: Whether wildcard expansion should be applied to
531
each argument. True by default.
532
:param single_quotes_allowed: Whether single quotes are accepted as quoting
533
characters like double quotes. False by
535
:return: A list of unicode strings.
537
s = cmdline.Splitter(command_line, single_quotes_allowed=single_quotes_allowed)
538
# Now that we've split the content, expand globs if necessary
539
# TODO: Use 'globbing' instead of 'glob.glob', this gives us stuff like
542
for is_quoted, arg in s:
543
if is_quoted or not glob.has_magic(arg) or not wildcard_expansion:
546
args.extend(glob_one(arg))
550
if has_ctypes and winver != 'Windows 98':
551
def get_unicode_argv():
552
prototype = ctypes.WINFUNCTYPE(ctypes.c_wchar_p)
553
GetCommandLineW = prototype(("GetCommandLineW",
554
ctypes.windll.kernel32))
555
command_line = GetCommandLineW()
556
if command_line is None:
557
raise ctypes.WinError()
558
# Skip the first argument, since we only care about parameters
559
argv = command_line_to_argv(command_line)[1:]
560
if getattr(sys, 'frozen', None) is None:
561
# Invoked via 'python.exe' which takes the form:
562
# python.exe [PYTHON_OPTIONS] C:\Path\bzr [BZR_OPTIONS]
563
# we need to get only BZR_OPTIONS part,
564
# We already removed 'python.exe' so we remove everything up to and
565
# including the first non-option ('-') argument.
566
for idx in xrange(len(argv)):
567
if argv[idx][:1] != '-':
572
get_unicode_argv = None