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