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