Show More
@@ -1,123 +1,125 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 |
|
8 | # This software may be used and distributed according to the terms | |
9 | # of the GNU General Public License, incorporated herein by reference. |
|
9 | # of the GNU General Public License, incorporated herein by reference. | |
10 | # |
|
10 | # | |
11 | """allow to use MBCS path with problematic encoding. |
|
11 | """allow to use MBCS path with problematic encoding. | |
12 |
|
12 | |||
13 | Some MBCS encodings are not good for some path operations |
|
13 | Some MBCS encodings are not good for some path operations (i.e. | |
14 |
|
|
14 | splitting path, case conversion, etc.) with its encoded bytes. We call | |
15 |
|
|
15 | such a encoding (i.e. shift_jis and big5) as "problematic encoding". | |
16 |
|
|
16 | This extension can be used to fix the issue with those encodings by | |
17 |
|
|
17 | wrapping some functions to convert to unicode string before path | |
18 |
|
|
18 | operation. | |
19 |
|
19 | |||
20 | This extension is usefull for: |
|
20 | This extension is usefull for: | |
21 | * Japanese Windows users using shift_jis encoding. |
|
21 | * Japanese Windows users using shift_jis encoding. | |
22 | * Chinese Windows users using big5 encoding. |
|
22 | * Chinese Windows users using big5 encoding. | |
23 | * All users who use a repository with one of problematic encodings |
|
23 | * All users who use a repository with one of problematic encodings on | |
24 |
|
|
24 | case-insensitive file system. | |
25 |
|
25 | |||
26 | This extension is not needed for: |
|
26 | This extension is not needed for: | |
27 | * Any user who use only ascii chars in path. |
|
27 | * Any user who use only ascii chars in path. | |
28 | * Any user who do not use any of problematic encodings. |
|
28 | * Any user who do not use any of problematic encodings. | |
29 |
|
29 | |||
30 | Note that there are some limitations on using this extension: |
|
30 | Note that there are some limitations on using this extension: | |
31 | * You should use single encoding in one repository. |
|
31 | * You should use single encoding in one repository. | |
32 |
* You should set same encoding for the repository by locale or |
|
32 | * You should set same encoding for the repository by locale or | |
|
33 | HGENCODING. | |||
33 |
|
34 | |||
34 | To use this extension, enable the extension in .hg/hgrc or ~/.hgrc: |
|
35 | To use this extension, enable the extension in .hg/hgrc or ~/.hgrc: | |
35 |
|
36 | |||
36 | [extensions] |
|
37 | [extensions] | |
37 | hgext.win32mbcs = |
|
38 | hgext.win32mbcs = | |
38 |
|
39 | |||
39 |
Path encoding conversion are done between unicode and |
|
40 | Path encoding conversion are done between unicode and | |
40 |
which is decided by mercurial from current locale |
|
41 | encoding.encoding which is decided by mercurial from current locale | |
|
42 | setting or HGENCODING. | |||
41 |
|
43 | |||
42 | """ |
|
44 | """ | |
43 |
|
45 | |||
44 | import os |
|
46 | import os | |
45 | from mercurial.i18n import _ |
|
47 | from mercurial.i18n import _ | |
46 | from mercurial import util, encoding |
|
48 | from mercurial import util, encoding | |
47 |
|
49 | |||
48 | def decode(arg): |
|
50 | def decode(arg): | |
49 | if isinstance(arg, str): |
|
51 | if isinstance(arg, str): | |
50 | uarg = arg.decode(encoding.encoding) |
|
52 | uarg = arg.decode(encoding.encoding) | |
51 | if arg == uarg.encode(encoding.encoding): |
|
53 | if arg == uarg.encode(encoding.encoding): | |
52 | return uarg |
|
54 | return uarg | |
53 | raise UnicodeError("Not local encoding") |
|
55 | raise UnicodeError("Not local encoding") | |
54 | elif isinstance(arg, tuple): |
|
56 | elif isinstance(arg, tuple): | |
55 | return tuple(map(decode, arg)) |
|
57 | return tuple(map(decode, arg)) | |
56 | elif isinstance(arg, list): |
|
58 | elif isinstance(arg, list): | |
57 | return map(decode, arg) |
|
59 | return map(decode, arg) | |
58 | return arg |
|
60 | return arg | |
59 |
|
61 | |||
60 | def encode(arg): |
|
62 | def encode(arg): | |
61 | if isinstance(arg, unicode): |
|
63 | if isinstance(arg, unicode): | |
62 | return arg.encode(encoding.encoding) |
|
64 | return arg.encode(encoding.encoding) | |
63 | elif isinstance(arg, tuple): |
|
65 | elif isinstance(arg, tuple): | |
64 | return tuple(map(encode, arg)) |
|
66 | return tuple(map(encode, arg)) | |
65 | elif isinstance(arg, list): |
|
67 | elif isinstance(arg, list): | |
66 | return map(encode, arg) |
|
68 | return map(encode, arg) | |
67 | return arg |
|
69 | return arg | |
68 |
|
70 | |||
69 | def wrapper(func, args): |
|
71 | def wrapper(func, args): | |
70 | # check argument is unicode, then call original |
|
72 | # check argument is unicode, then call original | |
71 | for arg in args: |
|
73 | for arg in args: | |
72 | if isinstance(arg, unicode): |
|
74 | if isinstance(arg, unicode): | |
73 | return func(*args) |
|
75 | return func(*args) | |
74 |
|
76 | |||
75 | try: |
|
77 | try: | |
76 | # convert arguments to unicode, call func, then convert back |
|
78 | # convert arguments to unicode, call func, then convert back | |
77 | return encode(func(*decode(args))) |
|
79 | return encode(func(*decode(args))) | |
78 | except UnicodeError: |
|
80 | except UnicodeError: | |
79 | # If not encoded with encoding.encoding, report it then |
|
81 | # If not encoded with encoding.encoding, report it then | |
80 | # continue with calling original function. |
|
82 | # continue with calling original function. | |
81 | raise util.Abort(_("[win32mbcs] filename conversion fail with" |
|
83 | raise util.Abort(_("[win32mbcs] filename conversion fail with" | |
82 | " %s encoding\n") % (encoding.encoding)) |
|
84 | " %s encoding\n") % (encoding.encoding)) | |
83 |
|
85 | |||
84 | def wrapname(name): |
|
86 | def wrapname(name): | |
85 | idx = name.rfind('.') |
|
87 | idx = name.rfind('.') | |
86 | module = name[:idx] |
|
88 | module = name[:idx] | |
87 | name = name[idx+1:] |
|
89 | name = name[idx+1:] | |
88 | module = eval(module) |
|
90 | module = eval(module) | |
89 | func = getattr(module, name) |
|
91 | func = getattr(module, name) | |
90 | def f(*args): |
|
92 | def f(*args): | |
91 | return wrapper(func, args) |
|
93 | return wrapper(func, args) | |
92 | try: |
|
94 | try: | |
93 | f.__name__ = func.__name__ # fail with python23 |
|
95 | f.__name__ = func.__name__ # fail with python23 | |
94 | except Exception: |
|
96 | except Exception: | |
95 | pass |
|
97 | pass | |
96 | setattr(module, name, f) |
|
98 | setattr(module, name, f) | |
97 |
|
99 | |||
98 | # List of functions to be wrapped. |
|
100 | # List of functions to be wrapped. | |
99 | # NOTE: os.path.dirname() and os.path.basename() are safe because |
|
101 | # NOTE: os.path.dirname() and os.path.basename() are safe because | |
100 | # they use result of os.path.split() |
|
102 | # they use result of os.path.split() | |
101 | funcs = '''os.path.join os.path.split os.path.splitext |
|
103 | funcs = '''os.path.join os.path.split os.path.splitext | |
102 | os.path.splitunc os.path.normpath os.path.normcase os.makedirs |
|
104 | os.path.splitunc os.path.normpath os.path.normcase os.makedirs | |
103 | util.endswithsep util.splitpath util.checkcase util.fspath''' |
|
105 | util.endswithsep util.splitpath util.checkcase util.fspath''' | |
104 |
|
106 | |||
105 | # codec and alias names of sjis and big5 to be faked. |
|
107 | # codec and alias names of sjis and big5 to be faked. | |
106 | problematic_encodings = '''big5 big5-tw csbig5 big5hkscs big5-hkscs |
|
108 | problematic_encodings = '''big5 big5-tw csbig5 big5hkscs big5-hkscs | |
107 | hkscs cp932 932 ms932 mskanji ms-kanji shift_jis csshiftjis shiftjis |
|
109 | hkscs cp932 932 ms932 mskanji ms-kanji shift_jis csshiftjis shiftjis | |
108 | sjis s_jis shift_jis_2004 shiftjis2004 sjis_2004 sjis2004 |
|
110 | sjis s_jis shift_jis_2004 shiftjis2004 sjis_2004 sjis2004 | |
109 | shift_jisx0213 shiftjisx0213 sjisx0213 s_jisx0213''' |
|
111 | shift_jisx0213 shiftjisx0213 sjisx0213 s_jisx0213''' | |
110 |
|
112 | |||
111 | def reposetup(ui, repo): |
|
113 | def reposetup(ui, repo): | |
112 | # TODO: decide use of config section for this extension |
|
114 | # TODO: decide use of config section for this extension | |
113 | if not os.path.supports_unicode_filenames: |
|
115 | if not os.path.supports_unicode_filenames: | |
114 | ui.warn(_("[win32mbcs] cannot activate on this platform.\n")) |
|
116 | ui.warn(_("[win32mbcs] cannot activate on this platform.\n")) | |
115 | return |
|
117 | return | |
116 |
|
118 | |||
117 | # fake is only for relevant environment. |
|
119 | # fake is only for relevant environment. | |
118 | if encoding.encoding.lower() in problematic_encodings.split(): |
|
120 | if encoding.encoding.lower() in problematic_encodings.split(): | |
119 | for f in funcs.split(): |
|
121 | for f in funcs.split(): | |
120 | wrapname(f) |
|
122 | wrapname(f) | |
121 | ui.debug(_("[win32mbcs] activated with encoding: %s\n") |
|
123 | ui.debug(_("[win32mbcs] activated with encoding: %s\n") | |
122 | % encoding.encoding) |
|
124 | % encoding.encoding) | |
123 |
|
125 |
General Comments 0
You need to be logged in to leave comments.
Login now