Show More
@@ -1,147 +1,147 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.2 |
|
5 | # Version: 0.2 | |
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, incorporated herein by reference. |
|
9 | # GNU General Public License version 2, incorporated herein by reference. | |
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 | - You should set same encoding for the repository by locale or |
|
36 | - You should set same encoding for the repository by locale or | |
37 | HGENCODING. |
|
37 | HGENCODING. | |
38 |
|
38 | |||
39 | Path encoding conversion are done between Unicode and |
|
39 | Path encoding conversion are done between Unicode and | |
40 | encoding.encoding which is decided by Mercurial from current locale |
|
40 | encoding.encoding which is decided by Mercurial from current locale | |
41 | setting or HGENCODING. |
|
41 | setting or HGENCODING. | |
42 | ''' |
|
42 | ''' | |
43 |
|
43 | |||
44 | import os, sys |
|
44 | import os, sys | |
45 | from mercurial.i18n import _ |
|
45 | from mercurial.i18n import _ | |
46 | from mercurial import util, encoding |
|
46 | from mercurial import util, encoding | |
47 |
|
47 | |||
48 | def decode(arg): |
|
48 | def decode(arg): | |
49 | if isinstance(arg, str): |
|
49 | if isinstance(arg, str): | |
50 | uarg = arg.decode(encoding.encoding) |
|
50 | uarg = arg.decode(encoding.encoding) | |
51 | if arg == uarg.encode(encoding.encoding): |
|
51 | if arg == uarg.encode(encoding.encoding): | |
52 | return uarg |
|
52 | return uarg | |
53 | raise UnicodeError("Not local encoding") |
|
53 | raise UnicodeError("Not local encoding") | |
54 | elif isinstance(arg, tuple): |
|
54 | elif isinstance(arg, tuple): | |
55 | return tuple(map(decode, arg)) |
|
55 | return tuple(map(decode, arg)) | |
56 | elif isinstance(arg, list): |
|
56 | elif isinstance(arg, list): | |
57 | return map(decode, arg) |
|
57 | return map(decode, arg) | |
58 | elif isinstance(arg, dict): |
|
58 | elif isinstance(arg, dict): | |
59 | for k, v in arg.items(): |
|
59 | for k, v in arg.items(): | |
60 | arg[k] = decode(v) |
|
60 | arg[k] = decode(v) | |
61 | return arg |
|
61 | return arg | |
62 |
|
62 | |||
63 | def encode(arg): |
|
63 | def encode(arg): | |
64 | if isinstance(arg, unicode): |
|
64 | if isinstance(arg, unicode): | |
65 | return arg.encode(encoding.encoding) |
|
65 | return arg.encode(encoding.encoding) | |
66 | elif isinstance(arg, tuple): |
|
66 | elif isinstance(arg, tuple): | |
67 | return tuple(map(encode, arg)) |
|
67 | return tuple(map(encode, arg)) | |
68 | elif isinstance(arg, list): |
|
68 | elif isinstance(arg, list): | |
69 | return map(encode, arg) |
|
69 | return map(encode, arg) | |
70 | elif isinstance(arg, dict): |
|
70 | elif isinstance(arg, dict): | |
71 | for k, v in arg.items(): |
|
71 | for k, v in arg.items(): | |
72 | arg[k] = encode(v) |
|
72 | arg[k] = encode(v) | |
73 | return arg |
|
73 | return arg | |
74 |
|
74 | |||
75 | def appendsep(s): |
|
75 | def appendsep(s): | |
76 | # ensure the path ends with os.sep, appending it if necessary. |
|
76 | # ensure the path ends with os.sep, appending it if necessary. | |
77 | try: |
|
77 | try: | |
78 | us = decode(s) |
|
78 | us = decode(s) | |
79 | except UnicodeError: |
|
79 | except UnicodeError: | |
80 | us = s |
|
80 | us = s | |
81 | if us and us[-1] not in ':/\\': |
|
81 | if us and us[-1] not in ':/\\': | |
82 | s += os.sep |
|
82 | s += os.sep | |
83 | return s |
|
83 | return s | |
84 |
|
84 | |||
85 | def wrapper(func, args, kwds): |
|
85 | def wrapper(func, args, kwds): | |
86 | # check argument is unicode, then call original |
|
86 | # check argument is unicode, then call original | |
87 | for arg in args: |
|
87 | for arg in args: | |
88 | if isinstance(arg, unicode): |
|
88 | if isinstance(arg, unicode): | |
89 | return func(*args, **kwds) |
|
89 | return func(*args, **kwds) | |
90 |
|
90 | |||
91 | try: |
|
91 | try: | |
92 | # convert arguments to unicode, call func, then convert back |
|
92 | # convert arguments to unicode, call func, then convert back | |
93 | return encode(func(*decode(args), **decode(kwds))) |
|
93 | return encode(func(*decode(args), **decode(kwds))) | |
94 | except UnicodeError: |
|
94 | except UnicodeError: | |
95 | raise util.Abort(_("[win32mbcs] filename conversion failed with" |
|
95 | raise util.Abort(_("[win32mbcs] filename conversion failed with" | |
96 | " %s encoding\n") % (encoding.encoding)) |
|
96 | " %s encoding\n") % (encoding.encoding)) | |
97 |
|
97 | |||
98 | def wrapperforlistdir(func, args, kwds): |
|
98 | def wrapperforlistdir(func, args, kwds): | |
99 | # Ensure 'path' argument ends with os.sep to avoids |
|
99 | # Ensure 'path' argument ends with os.sep to avoids | |
100 | # misinterpreting last 0x5c of MBCS 2nd byte as path separator. |
|
100 | # misinterpreting last 0x5c of MBCS 2nd byte as path separator. | |
101 | if args: |
|
101 | if args: | |
102 | args = list(args) |
|
102 | args = list(args) | |
103 | args[0] = appendsep(args[0]) |
|
103 | args[0] = appendsep(args[0]) | |
104 | if 'path' in kwds: |
|
104 | if 'path' in kwds: | |
105 | kwds['path'] = appendsep(kwds['path']) |
|
105 | kwds['path'] = appendsep(kwds['path']) | |
106 | return func(*args, **kwds) |
|
106 | return func(*args, **kwds) | |
107 |
|
107 | |||
108 | def wrapname(name, wrapper): |
|
108 | def wrapname(name, wrapper): | |
109 | module, name = name.rsplit('.', 1) |
|
109 | module, name = name.rsplit('.', 1) | |
110 | module = sys.modules[module] |
|
110 | module = sys.modules[module] | |
111 | func = getattr(module, name) |
|
111 | func = getattr(module, name) | |
112 | def f(*args, **kwds): |
|
112 | def f(*args, **kwds): | |
113 | return wrapper(func, args, kwds) |
|
113 | return wrapper(func, args, kwds) | |
114 | try: |
|
114 | try: | |
115 | f.__name__ = func.__name__ # fail with python23 |
|
115 | f.__name__ = func.__name__ # fail with python23 | |
116 | except Exception: |
|
116 | except Exception: | |
117 | pass |
|
117 | pass | |
118 | setattr(module, name, f) |
|
118 | setattr(module, name, f) | |
119 |
|
119 | |||
120 | # List of functions to be wrapped. |
|
120 | # List of functions to be wrapped. | |
121 | # NOTE: os.path.dirname() and os.path.basename() are safe because |
|
121 | # NOTE: os.path.dirname() and os.path.basename() are safe because | |
122 | # they use result of os.path.split() |
|
122 | # they use result of os.path.split() | |
123 | funcs = '''os.path.join os.path.split os.path.splitext |
|
123 | funcs = '''os.path.join os.path.split os.path.splitext | |
124 | os.path.splitunc os.path.normpath os.path.normcase os.makedirs |
|
124 | os.path.splitunc os.path.normpath os.path.normcase os.makedirs | |
125 | mercurial.util.endswithsep mercurial.util.splitpath mercurial.util.checkcase |
|
125 | mercurial.util.endswithsep mercurial.util.splitpath mercurial.util.checkcase | |
126 | mercurial.util.fspath mercurial.util.pconvert''' |
|
126 | mercurial.util.fspath mercurial.util.pconvert mercurial.util.normpath''' | |
127 |
|
127 | |||
128 | # codec and alias names of sjis and big5 to be faked. |
|
128 | # codec and alias names of sjis and big5 to be faked. | |
129 | problematic_encodings = '''big5 big5-tw csbig5 big5hkscs big5-hkscs |
|
129 | problematic_encodings = '''big5 big5-tw csbig5 big5hkscs big5-hkscs | |
130 | hkscs cp932 932 ms932 mskanji ms-kanji shift_jis csshiftjis shiftjis |
|
130 | hkscs cp932 932 ms932 mskanji ms-kanji shift_jis csshiftjis shiftjis | |
131 | sjis s_jis shift_jis_2004 shiftjis2004 sjis_2004 sjis2004 |
|
131 | sjis s_jis shift_jis_2004 shiftjis2004 sjis_2004 sjis2004 | |
132 | shift_jisx0213 shiftjisx0213 sjisx0213 s_jisx0213 950 cp950 ms950 ''' |
|
132 | shift_jisx0213 shiftjisx0213 sjisx0213 s_jisx0213 950 cp950 ms950 ''' | |
133 |
|
133 | |||
134 | def reposetup(ui, repo): |
|
134 | def reposetup(ui, repo): | |
135 | # TODO: decide use of config section for this extension |
|
135 | # TODO: decide use of config section for this extension | |
136 | if not os.path.supports_unicode_filenames: |
|
136 | if not os.path.supports_unicode_filenames: | |
137 | ui.warn(_("[win32mbcs] cannot activate on this platform.\n")) |
|
137 | ui.warn(_("[win32mbcs] cannot activate on this platform.\n")) | |
138 | return |
|
138 | return | |
139 |
|
139 | |||
140 | # fake is only for relevant environment. |
|
140 | # fake is only for relevant environment. | |
141 | if encoding.encoding.lower() in problematic_encodings.split(): |
|
141 | if encoding.encoding.lower() in problematic_encodings.split(): | |
142 | for f in funcs.split(): |
|
142 | for f in funcs.split(): | |
143 | wrapname(f, wrapper) |
|
143 | wrapname(f, wrapper) | |
144 | wrapname("mercurial.osutil.listdir", wrapperforlistdir) |
|
144 | wrapname("mercurial.osutil.listdir", wrapperforlistdir) | |
145 | ui.debug(_("[win32mbcs] activated with encoding: %s\n") |
|
145 | ui.debug(_("[win32mbcs] activated with encoding: %s\n") | |
146 | % encoding.encoding) |
|
146 | % encoding.encoding) | |
147 |
|
147 |
General Comments 0
You need to be logged in to leave comments.
Login now