207
197
return os.environ.get('USERNAME', None)
210
# 1 == ComputerNameDnsHostname, which returns "The DNS host name of the local
211
# computer or the cluster associated with the local computer."
212
_WIN32_ComputerNameDnsHostname = 1
214
200
def get_host_name():
215
201
"""Return host machine name.
216
202
If name cannot be obtained return None.
218
:return: A unicode string representing the host name. On win98, this may be
219
a plain string as win32 api doesn't support unicode.
204
Returned value can be unicode or plain sring.
205
To convert plain string to unicode use
206
s.decode(bzrlib.user_encoding)
223
return win32api.GetComputerNameEx(_WIN32_ComputerNameDnsHostname)
224
except (NotImplementedError, win32api.error):
225
# NotImplemented will happen on win9x...
229
210
kernel32 = ctypes.windll.kernel32
211
GetComputerName = getattr(kernel32, 'GetComputerName'+suffix)
230
212
except AttributeError:
231
pass # Missing the module we need
233
215
buf = create_buffer(MAX_COMPUTERNAME_LENGTH+1)
234
216
n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH+1)
236
# Try GetComputerNameEx which gives a proper Unicode hostname
237
GetComputerNameEx = getattr(kernel32, 'GetComputerNameEx'+suffix,
239
if (GetComputerNameEx is not None
240
and GetComputerNameEx(_WIN32_ComputerNameDnsHostname,
241
buf, ctypes.byref(n))):
244
# Try GetComputerName in case GetComputerNameEx wasn't found
245
# It returns the NETBIOS name, which isn't as good, but still ok.
246
# The first GetComputerNameEx might have changed 'n', so reset it
247
n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH+1)
248
GetComputerName = getattr(kernel32, 'GetComputerName'+suffix,
250
if (GetComputerName is not None
251
and GetComputerName(buf, ctypes.byref(n))):
253
# otherwise try env variables, which will be 'mbcs' encoded
254
# on Windows (Python doesn't expose the native win32 unicode environment)
256
# http://msdn.microsoft.com/en-us/library/aa246807.aspx
257
# environment variables should always be encoded in 'mbcs'.
259
return os.environ['COMPUTERNAME'].decode("mbcs")
217
if GetComputerName(buf, ctypes.byref(n)):
219
# otherwise try env variables
220
return os.environ.get('COMPUTERNAME', None)
264
223
def _ensure_unicode(s):
280
239
def get_host_name_unicode():
281
240
return _ensure_unicode(get_host_name())
284
def _ensure_with_dir(path):
285
if not os.path.split(path)[0] or path.startswith(u'*') or path.startswith(u'?'):
286
return u'./' + path, True
290
def _undo_ensure_with_dir(path, corrected):
298
def glob_expand(file_list):
299
"""Replacement for glob expansion by the shell.
301
Win32's cmd.exe does not do glob expansion (eg ``*.py``), so we do our own
304
:param file_list: A list of filenames which may include shell globs.
305
:return: An expanded list of filenames.
307
Introduced in bzrlib 0.18.
312
expanded_file_list = []
313
for possible_glob in file_list:
315
# work around bugs in glob.glob()
316
# - Python bug #1001604 ("glob doesn't return unicode with ...")
317
# - failing expansion for */* with non-iso-8859-* chars
318
possible_glob, corrected = _ensure_with_dir(possible_glob)
319
glob_files = glob.glob(possible_glob)
322
# special case to let the normal code path handle
323
# files that do not exists
324
expanded_file_list.append(
325
_undo_ensure_with_dir(possible_glob, corrected))
327
glob_files = [_undo_ensure_with_dir(elem, corrected) for elem in glob_files]
328
expanded_file_list += glob_files
330
return [elem.replace(u'\\', u'/') for elem in expanded_file_list]
333
def get_app_path(appname):
334
"""Look up in Windows registry for full path to application executable.
335
Typicaly, applications create subkey with their basename
336
in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
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.
345
hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
346
r'SOFTWARE\Microsoft\Windows'
347
r'\CurrentVersion\App Paths')
348
except EnvironmentError:
352
if not os.path.splitext(basename)[1]:
353
basename = appname + '.exe'
356
fullpath = _winreg.QueryValue(hkey, basename)
360
_winreg.CloseKey(hkey)
365
def set_file_attr_hidden(path):
366
"""Set file attributes to hidden if possible"""
368
win32file.SetFileAttributes(path, win32file.FILE_ATTRIBUTE_HIDDEN)