Show More
@@ -1,167 +1,184 | |||
|
1 | 1 | # win32mbcs.py -- MBCS filename support for Mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright (c) 2008 Shun-ichi Goto <shunichi.goto@gmail.com> |
|
4 | 4 | # |
|
5 | 5 | # Version: 0.3 |
|
6 | 6 | # Author: Shun-ichi Goto <shunichi.goto@gmail.com> |
|
7 | 7 | # |
|
8 | 8 | # This software may be used and distributed according to the terms of the |
|
9 | 9 | # GNU General Public License version 2 or any later version. |
|
10 | 10 | # |
|
11 | 11 | |
|
12 | 12 | '''allow the use of MBCS paths with problematic encodings |
|
13 | 13 | |
|
14 | 14 | Some MBCS encodings are not good for some path operations (i.e. |
|
15 | 15 | splitting path, case conversion, etc.) with its encoded bytes. We call |
|
16 | 16 | such a encoding (i.e. shift_jis and big5) as "problematic encoding". |
|
17 | 17 | This extension can be used to fix the issue with those encodings by |
|
18 | 18 | wrapping some functions to convert to Unicode string before path |
|
19 | 19 | operation. |
|
20 | 20 | |
|
21 | 21 | This extension is useful for: |
|
22 | 22 | |
|
23 | 23 | - Japanese Windows users using shift_jis encoding. |
|
24 | 24 | - Chinese Windows users using big5 encoding. |
|
25 | 25 | - All users who use a repository with one of problematic encodings on |
|
26 | 26 | case-insensitive file system. |
|
27 | 27 | |
|
28 | 28 | This extension is not needed for: |
|
29 | 29 | |
|
30 | 30 | - Any user who use only ASCII chars in path. |
|
31 | 31 | - Any user who do not use any of problematic encodings. |
|
32 | 32 | |
|
33 | 33 | Note that there are some limitations on using this extension: |
|
34 | 34 | |
|
35 | 35 | - You should use single encoding in one repository. |
|
36 | 36 | - If the repository path ends with 0x5c, .hg/hgrc cannot be read. |
|
37 | 37 | - win32mbcs is not compatible with fixutf8 extension. |
|
38 | 38 | |
|
39 | 39 | By default, win32mbcs uses encoding.encoding decided by Mercurial. |
|
40 | 40 | You can specify the encoding by config option:: |
|
41 | 41 | |
|
42 | 42 | [win32mbcs] |
|
43 | 43 | encoding = sjis |
|
44 | 44 | |
|
45 | 45 | It is useful for the users who want to commit with UTF-8 log message. |
|
46 | 46 | ''' |
|
47 | 47 | |
|
48 | 48 | import os, sys |
|
49 | 49 | from mercurial.i18n import _ |
|
50 | 50 | from mercurial import util, encoding |
|
51 | 51 | testedwith = 'internal' |
|
52 | 52 | |
|
53 | 53 | _encoding = None # see extsetup |
|
54 | 54 | |
|
55 | 55 | def decode(arg): |
|
56 | 56 | if isinstance(arg, str): |
|
57 | 57 | uarg = arg.decode(_encoding) |
|
58 | 58 | if arg == uarg.encode(_encoding): |
|
59 | 59 | return uarg |
|
60 | 60 | raise UnicodeError("Not local encoding") |
|
61 | 61 | elif isinstance(arg, tuple): |
|
62 | 62 | return tuple(map(decode, arg)) |
|
63 | 63 | elif isinstance(arg, list): |
|
64 | 64 | return map(decode, arg) |
|
65 | 65 | elif isinstance(arg, dict): |
|
66 | 66 | for k, v in arg.items(): |
|
67 | 67 | arg[k] = decode(v) |
|
68 | 68 | return arg |
|
69 | 69 | |
|
70 | 70 | def encode(arg): |
|
71 | 71 | if isinstance(arg, unicode): |
|
72 | 72 | return arg.encode(_encoding) |
|
73 | 73 | elif isinstance(arg, tuple): |
|
74 | 74 | return tuple(map(encode, arg)) |
|
75 | 75 | elif isinstance(arg, list): |
|
76 | 76 | return map(encode, arg) |
|
77 | 77 | elif isinstance(arg, dict): |
|
78 | 78 | for k, v in arg.items(): |
|
79 | 79 | arg[k] = encode(v) |
|
80 | 80 | return arg |
|
81 | 81 | |
|
82 | 82 | def appendsep(s): |
|
83 | 83 | # ensure the path ends with os.sep, appending it if necessary. |
|
84 | 84 | try: |
|
85 | 85 | us = decode(s) |
|
86 | 86 | except UnicodeError: |
|
87 | 87 | us = s |
|
88 | 88 | if us and us[-1] not in ':/\\': |
|
89 | 89 | s += os.sep |
|
90 | 90 | return s |
|
91 | 91 | |
|
92 | def wrapper(func, args, kwds): | |
|
93 | # check argument is unicode, then call original | |
|
92 | ||
|
93 | def basewrapper(func, argtype, enc, dec, args, kwds): | |
|
94 | # check check already converted, then call original | |
|
94 | 95 | for arg in args: |
|
95 |
if isinstance(arg, |
|
|
96 | if isinstance(arg, argtype): | |
|
96 | 97 | return func(*args, **kwds) |
|
97 | 98 | |
|
98 | 99 | try: |
|
99 |
# convert arguments |
|
|
100 | return encode(func(*decode(args), **decode(kwds))) | |
|
100 | # convert string arguments, call func, then convert back the | |
|
101 | # return value. | |
|
102 | return enc(func(*dec(args), **dec(kwds))) | |
|
101 | 103 | except UnicodeError: |
|
102 | 104 | raise util.Abort(_("[win32mbcs] filename conversion failed with" |
|
103 | 105 | " %s encoding\n") % (_encoding)) |
|
104 | 106 | |
|
107 | def wrapper(func, args, kwds): | |
|
108 | return basewrapper(func, unicode, encode, decode, args, kwds) | |
|
109 | ||
|
110 | ||
|
111 | def reversewrapper(func, args, kwds): | |
|
112 | return basewrapper(func, str, decode, encode, args, kwds) | |
|
113 | ||
|
105 | 114 | def wrapperforlistdir(func, args, kwds): |
|
106 | 115 | # Ensure 'path' argument ends with os.sep to avoids |
|
107 | 116 | # misinterpreting last 0x5c of MBCS 2nd byte as path separator. |
|
108 | 117 | if args: |
|
109 | 118 | args = list(args) |
|
110 | 119 | args[0] = appendsep(args[0]) |
|
111 | 120 | if 'path' in kwds: |
|
112 | 121 | kwds['path'] = appendsep(kwds['path']) |
|
113 | 122 | return func(*args, **kwds) |
|
114 | 123 | |
|
115 | 124 | def wrapname(name, wrapper): |
|
116 | 125 | module, name = name.rsplit('.', 1) |
|
117 | 126 | module = sys.modules[module] |
|
118 | 127 | func = getattr(module, name) |
|
119 | 128 | def f(*args, **kwds): |
|
120 | 129 | return wrapper(func, args, kwds) |
|
121 | 130 | try: |
|
122 | 131 | f.__name__ = func.__name__ # fails with Python 2.3 |
|
123 | 132 | except Exception: |
|
124 | 133 | pass |
|
125 | 134 | setattr(module, name, f) |
|
126 | 135 | |
|
127 | 136 | # List of functions to be wrapped. |
|
128 | 137 | # NOTE: os.path.dirname() and os.path.basename() are safe because |
|
129 | 138 | # they use result of os.path.split() |
|
130 | 139 | funcs = '''os.path.join os.path.split os.path.splitext |
|
131 | 140 | os.path.normpath os.makedirs |
|
132 | 141 | mercurial.util.endswithsep mercurial.util.splitpath mercurial.util.checkcase |
|
133 | 142 | mercurial.util.fspath mercurial.util.pconvert mercurial.util.normpath |
|
134 | 143 | mercurial.util.checkwinfilename mercurial.util.checkosfilename''' |
|
135 | 144 | |
|
145 | # These functions are required to be called with local encoded string | |
|
146 | # because they expects argument is local encoded string and cause | |
|
147 | # problem with unicode string. | |
|
148 | rfuncs = '''mercurial.encoding.upper mercurial.encoding.lower''' | |
|
149 | ||
|
136 | 150 | # List of Windows specific functions to be wrapped. |
|
137 | 151 | winfuncs = '''os.path.splitunc''' |
|
138 | 152 | |
|
139 | 153 | # codec and alias names of sjis and big5 to be faked. |
|
140 | 154 | problematic_encodings = '''big5 big5-tw csbig5 big5hkscs big5-hkscs |
|
141 | 155 | hkscs cp932 932 ms932 mskanji ms-kanji shift_jis csshiftjis shiftjis |
|
142 | 156 | sjis s_jis shift_jis_2004 shiftjis2004 sjis_2004 sjis2004 |
|
143 | 157 | shift_jisx0213 shiftjisx0213 sjisx0213 s_jisx0213 950 cp950 ms950 ''' |
|
144 | 158 | |
|
145 | 159 | def extsetup(ui): |
|
146 | 160 | # TODO: decide use of config section for this extension |
|
147 | 161 | if ((not os.path.supports_unicode_filenames) and |
|
148 | 162 | (sys.platform != 'cygwin')): |
|
149 | 163 | ui.warn(_("[win32mbcs] cannot activate on this platform.\n")) |
|
150 | 164 | return |
|
151 | 165 | # determine encoding for filename |
|
152 | 166 | global _encoding |
|
153 | 167 | _encoding = ui.config('win32mbcs', 'encoding', encoding.encoding) |
|
154 | 168 | # fake is only for relevant environment. |
|
155 | 169 | if _encoding.lower() in problematic_encodings.split(): |
|
156 | 170 | for f in funcs.split(): |
|
157 | 171 | wrapname(f, wrapper) |
|
158 | 172 | if os.name == 'nt': |
|
159 | 173 | for f in winfuncs.split(): |
|
160 | 174 | wrapname(f, wrapper) |
|
161 | 175 | wrapname("mercurial.osutil.listdir", wrapperforlistdir) |
|
176 | # wrap functions to be called with local byte string arguments | |
|
177 | for f in rfuncs.split(): | |
|
178 | wrapname(f, reversewrapper) | |
|
162 | 179 | # Check sys.args manually instead of using ui.debug() because |
|
163 | 180 | # command line options is not yet applied when |
|
164 | 181 | # extensions.loadall() is called. |
|
165 | 182 | if '--debug' in sys.argv: |
|
166 | 183 | ui.write("[win32mbcs] activated with encoding: %s\n" |
|
167 | 184 | % _encoding) |
General Comments 0
You need to be logged in to leave comments.
Login now