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