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