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