~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/win32utils.py

Use get_environ_unicode throughout win32utils and always return unicode paths

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import struct
25
25
import sys
26
26
 
27
 
from bzrlib import cmdline
 
27
from bzrlib import (
 
28
    cmdline,
 
29
    symbol_versioning,
 
30
    )
28
31
from bzrlib.i18n import gettext
29
32
 
30
33
# Windows version
248
251
    one that moves with the user as they logon to different machines, and
249
252
    a 'local' one that stays local to the machine.  This returns the 'roaming'
250
253
    directory, and thus is suitable for storing user-preferences, etc.
251
 
 
252
 
    Returned value can be unicode or plain string.
253
 
    To convert plain string to unicode use
254
 
    s.decode(osutils.get_user_encoding())
255
 
    (XXX - but see bug 262874, which asserts the correct encoding is 'mbcs')
256
254
    """
257
255
    appdata = _get_sh_special_folder_path(CSIDL_APPDATA)
258
256
    if appdata:
259
257
        return appdata
260
 
    # from env variable
261
 
    appdata = os.environ.get('APPDATA')
262
 
    if appdata:
263
 
        return appdata
264
 
    # if we fall to this point we on win98
265
 
    # at least try C:/WINDOWS/Application Data
266
 
    windir = os.environ.get('windir')
267
 
    if windir:
268
 
        appdata = os.path.join(windir, 'Application Data')
269
 
        if os.path.isdir(appdata):
270
 
            return appdata
271
 
    # did not find anything
272
 
    return None
 
258
    # Use APPDATA if defined, will return None if not
 
259
    return get_environ_unicode('APPDATA')
273
260
 
274
261
 
275
262
def get_local_appdata_location():
281
268
    a 'local' one that stays local to the machine.  This returns the 'local'
282
269
    directory, and thus is suitable for caches, temp files and other things
283
270
    which don't need to move with the user.
284
 
 
285
 
    Returned value can be unicode or plain string.
286
 
    To convert plain string to unicode use
287
 
    s.decode(osutils.get_user_encoding())
288
 
    (XXX - but see bug 262874, which asserts the correct encoding is 'mbcs')
289
271
    """
290
272
    local = _get_sh_special_folder_path(CSIDL_LOCAL_APPDATA)
291
273
    if local:
292
274
        return local
293
275
    # Vista supplies LOCALAPPDATA, but XP and earlier do not.
294
 
    local = os.environ.get('LOCALAPPDATA')
 
276
    local = get_environ_unicode('LOCALAPPDATA')
295
277
    if local:
296
278
        return local
297
279
    return get_appdata_location()
302
284
    Assume on win32 it's the <My Documents> folder.
303
285
    If location cannot be obtained return system drive root,
304
286
    i.e. C:\
305
 
 
306
 
    Returned value can be unicode or plain string.
307
 
    To convert plain string to unicode use
308
 
    s.decode(osutils.get_user_encoding())
309
287
    """
310
288
    home = _get_sh_special_folder_path(CSIDL_PERSONAL)
311
289
    if home:
312
290
        return home
313
 
    # try for HOME env variable
314
 
    home = os.path.expanduser('~')
315
 
    if home != '~':
 
291
    home = get_environ_unicode('HOME')
 
292
    if home is not None:
316
293
        return home
 
294
    homepath = get_environ_unicode('HOMEPATH')
 
295
    if homepath is not None:
 
296
        return os.path.join(get_environ_unicode('HOMEDIR', ''), home)
317
297
    # at least return windows root directory
318
 
    windir = os.environ.get('windir')
 
298
    windir = get_environ_unicode('WINDIR')
319
299
    if windir:
320
300
        return os.path.splitdrive(windir)[0] + '/'
321
301
    # otherwise C:\ is good enough for 98% users
322
 
    return 'C:/'
 
302
    return unicode('C:/')
323
303
 
324
304
 
325
305
def get_user_name():
342
322
            if GetUserName(buf, ctypes.byref(n)):
343
323
                return buf.value
344
324
    # otherwise try env variables
345
 
    return os.environ.get('USERNAME', None)
 
325
    return get_environ_unicode('USERNAME')
346
326
 
347
327
 
348
328
# 1 == ComputerNameDnsHostname, which returns "The DNS host name of the local
388
368
            if (GetComputerName is not None
389
369
                and GetComputerName(buf, ctypes.byref(n))):
390
370
                return buf.value
391
 
    # otherwise try env variables, which will be 'mbcs' encoded
392
 
    # on Windows (Python doesn't expose the native win32 unicode environment)
393
 
    # According to this:
394
 
    # http://msdn.microsoft.com/en-us/library/aa246807.aspx
395
 
    # environment variables should always be encoded in 'mbcs'.
396
 
    try:
397
 
        return os.environ['COMPUTERNAME'].decode("mbcs")
398
 
    except KeyError:
399
 
        return None
 
371
    return get_environ_unicode('COMPUTERNAME')
400
372
 
401
373
 
402
374
def _ensure_unicode(s):
406
378
    return s
407
379
 
408
380
 
409
 
def get_appdata_location_unicode():
410
 
    return _ensure_unicode(get_appdata_location())
 
381
get_appdata_location_unicode = symbol_versioning.deprecated_method(
 
382
    symbol_versioning.deprecated_in((2, 5, 0)))(get_appdata_location)
411
383
 
412
 
def get_home_location_unicode():
413
 
    return _ensure_unicode(get_home_location())
 
384
get_home_location_unicode = symbol_versioning.deprecated_method(
 
385
    symbol_versioning.deprecated_in((2, 5, 0)))(get_home_location)
414
386
 
415
387
def get_user_name_unicode():
416
388
    return _ensure_unicode(get_user_name())
433
405
        return path
434
406
 
435
407
 
436
 
 
437
408
def glob_one(possible_glob):
438
409
    """Same as glob.glob().
439
410