[PATCH] Support MBCS for fspath() and checkcase()

Matt Mackall mpm at selenic.com
Mon Jul 28 10:37:38 CDT 2008


On Mon, 2008-07-28 at 15:53 +0900, Shun-ichi Goto wrote:
> # HG changeset patch
> # User Shun-ichi GOTO <shunichi.goto at gmail.com>
> # Date 1217227736 -32400
> # Node ID f9a40d20d44e536aa8ce1818279cc07bdb15f53e
> # Parent  1a9577da9d02a386526c6a6791d6d57f56b28126
> Support MBCS for fspath() and checkcase().
> 
> diff -r 1a9577da9d02 -r f9a40d20d44e hgext/win32mbcs.py
> --- a/hgext/win32mbcs.py	Fri Jul 25 20:47:04 2008 +0200
> +++ b/hgext/win32mbcs.py	Mon Jul 28 15:48:56 2008 +0900
> @@ -130,6 +130,8 @@
>      os.makedirs = wrap(os.makedirs)
>      util.endswithsep = wrap(util.endswithsep)
>      util.splitpath = wrap(util.splitpath)
> +    util.checkcase = wrap(util.checkcase)
> +    util.fspath = wrap(util.fspath)

Looks fine.

>  def uninstall():
>      # restore original functions.
> @@ -142,6 +144,8 @@
>      os.makedirs = unwrap(os.makedirs)
>      util.endswithsep = unwrap(util.endswithsep)
>      util.splitpath = unwrap(util.splitpath)
> +    util.checkcase = unwrap(util.checkcase)
> +    util.fspath = unwrap(util.fspath)

Uninstall? When does that happen? Oh, it doesn't.

Hmm, this module could use a few cleanups, tell me what you think of
this. I've included the whole file since it's smaller and more readable
than the diff:

# win32mbcs.py -- MBCS filename support for Mercurial on Windows
#
# Copyright (c) 2008 Shun-ichi Goto <shunichi.goto at gmail.com>
#
# Version: 0.1
# Author:  Shun-ichi Goto <shunichi.goto at gmail.com>
#
# This software may be used and distributed according to the terms
# of the GNU General Public License, incorporated herein by reference.
#
"""Allow to use shift_jis/big5 filenames on Windows.

There is a well known issue "0x5c problem" on Windows.  It is a
trouble on handling path name as raw encoded byte sequence of
problematic encodings like shift_jis or big5.  The primary intent
of this extension is to allow using such a encoding on Mercurial
without strange file operation error.

By enabling this extension, hook mechanism is activated and some
functions are altered.  Usually, this encoding is your local encoding
on your system by default. So you can get benefit simply by enabling
this extension.

The encoding for filename is same one for terminal by default.  You
can change the encoding by setting HGENCODING environment variable.

This extension is usefull for:
 * Japanese Windows user using shift_jis encoding.
 * Chinese Windows user using big5 encoding.
 * Users who want to use a repository created with such a encoding.

Note: Unix people do not need to use this extension.

"""

import os
from mercurial.i18n import _
from mercurial import util

def decode(arg):
    if isinstance(arg, str):
        uarg = arg.decode(util._encoding)
        if arg == uarg.encode(util._encoding):
            return uarg
        raise UnicodeError("Not local encoding")
    elif isinstance(arg, tuple):
        return tuple(map(decode, arg))
    elif isinstance(arg, list):
        return map(decode, arg)
    return arg

def encode(arg):
    if isinstance(arg, unicode):
        return arg.encode(util._encoding)
    elif isinstance(arg, tuple):
        return tuple(map(encode, arg))
    elif isinstance(arg, list):
        return map(encode, arg)
    return arg

def wrapper(func, args):
    # check argument is unicode, then call original
    for arg in args:
        if isinstance(arg, unicode):
            return func(*args)

    try:
        # convert arguments to unicode, call func, then convert back
        return encode(func(*decode(args)))
    except UnicodeError, exc:
        # If not encoded with _local_fs_encoding, report it then
        # continue with calling original function.
        _ui.warn(_("WARNING: [win32mbcs] filename conversion fail for" +
                 " %s: '%s'\n") % (util._encoding, args))
        return func(*args)

def wrapname(name):
    module, name = name.rsplit('.', 1)
    module = eval(module)
    func = module.getattr(name)
    def f(*args):
        return wrapper(func, args)
    f.__name__ = func.__name__
    module.setattr(name, f)

# wrap some python functions and mercurial functions
# to handle raw bytes on Windows.
# NOTE: dirname and basename are safe because they use result
# of os.path.split()
funcs = '''os.path.join os.path.split os.path.splitext
 os.path.splitunc os.path.normpath os.path.normcase os.makedirs
 util.endswithsep util.splitpath'''

# codec and alias names of sjis and big5 to be faked.
problematic_encodings = '''big5 big5-tw csbig5 big5hkscs big5-hkscs
 hkscs cp932 932 ms932 mskanji ms-kanji shift_jis csshiftjis shiftjis
 sjis s_jis shift_jis_2004 shiftjis2004 sjis_2004 sjis2004
 shift_jisx0213 shiftjisx0213 sjisx0213 s_jisx0213'''

def reposetup(ui, repo):
    # TODO: decide use of config section for this extension
    if not os.path.supports_unicode_filenames:
        ui.warn(_("[win32mbcs] cannot activate on this platform.\n"))
        return

    # fake is only for relevant environment.
    if util._encoding.lower() in problematic_encodings.split():
        for f in funcs.split():
            wrapname(f)
        ui.debug(_("[win32mbcs] activated with encoding: %s\n") % util._encoding)



-- 
Mathematics is the supreme nostalgia of our time.



More information about the Mercurial-devel mailing list