##// END OF EJS Templates
win32mbcs: drop code that was catering to Python 2.3 and earlier
Augie Fackler -
r30476:8a9681b9 default
parent child Browse files
Show More
@@ -1,196 +1,193 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.3
5 # Version: 0.3
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 or any later version.
9 # GNU General Public License version 2 or any later version.
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
22
23 - Japanese Windows users using shift_jis encoding.
23 - Japanese Windows users using shift_jis encoding.
24 - Chinese Windows users using big5 encoding.
24 - Chinese Windows users using big5 encoding.
25 - All users who use a repository with one of problematic encodings on
25 - All users who use a repository with one of problematic encodings on
26 case-insensitive file system.
26 case-insensitive file system.
27
27
28 This extension is not needed for:
28 This extension is not needed for:
29
29
30 - Any user who use only ASCII chars in path.
30 - Any user who use only ASCII chars in path.
31 - Any user who do not use any of problematic encodings.
31 - Any user who do not use any of problematic encodings.
32
32
33 Note that there are some limitations on using this extension:
33 Note that there are some limitations on using this extension:
34
34
35 - You should use single encoding in one repository.
35 - You should use single encoding in one repository.
36 - If the repository path ends with 0x5c, .hg/hgrc cannot be read.
36 - If the repository path ends with 0x5c, .hg/hgrc cannot be read.
37 - win32mbcs is not compatible with fixutf8 extension.
37 - win32mbcs is not compatible with fixutf8 extension.
38
38
39 By default, win32mbcs uses encoding.encoding decided by Mercurial.
39 By default, win32mbcs uses encoding.encoding decided by Mercurial.
40 You can specify the encoding by config option::
40 You can specify the encoding by config option::
41
41
42 [win32mbcs]
42 [win32mbcs]
43 encoding = sjis
43 encoding = sjis
44
44
45 It is useful for the users who want to commit with UTF-8 log message.
45 It is useful for the users who want to commit with UTF-8 log message.
46 '''
46 '''
47 from __future__ import absolute_import
47 from __future__ import absolute_import
48
48
49 import os
49 import os
50 import sys
50 import sys
51
51
52 from mercurial.i18n import _
52 from mercurial.i18n import _
53 from mercurial import (
53 from mercurial import (
54 encoding,
54 encoding,
55 error,
55 error,
56 )
56 )
57
57
58 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
58 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
59 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
59 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
60 # be specifying the version(s) of Mercurial they are tested with, or
60 # be specifying the version(s) of Mercurial they are tested with, or
61 # leave the attribute unspecified.
61 # leave the attribute unspecified.
62 testedwith = 'ships-with-hg-core'
62 testedwith = 'ships-with-hg-core'
63
63
64 _encoding = None # see extsetup
64 _encoding = None # see extsetup
65
65
66 def decode(arg):
66 def decode(arg):
67 if isinstance(arg, str):
67 if isinstance(arg, str):
68 uarg = arg.decode(_encoding)
68 uarg = arg.decode(_encoding)
69 if arg == uarg.encode(_encoding):
69 if arg == uarg.encode(_encoding):
70 return uarg
70 return uarg
71 raise UnicodeError("Not local encoding")
71 raise UnicodeError("Not local encoding")
72 elif isinstance(arg, tuple):
72 elif isinstance(arg, tuple):
73 return tuple(map(decode, arg))
73 return tuple(map(decode, arg))
74 elif isinstance(arg, list):
74 elif isinstance(arg, list):
75 return map(decode, arg)
75 return map(decode, arg)
76 elif isinstance(arg, dict):
76 elif isinstance(arg, dict):
77 for k, v in arg.items():
77 for k, v in arg.items():
78 arg[k] = decode(v)
78 arg[k] = decode(v)
79 return arg
79 return arg
80
80
81 def encode(arg):
81 def encode(arg):
82 if isinstance(arg, unicode):
82 if isinstance(arg, unicode):
83 return arg.encode(_encoding)
83 return arg.encode(_encoding)
84 elif isinstance(arg, tuple):
84 elif isinstance(arg, tuple):
85 return tuple(map(encode, arg))
85 return tuple(map(encode, arg))
86 elif isinstance(arg, list):
86 elif isinstance(arg, list):
87 return map(encode, arg)
87 return map(encode, arg)
88 elif isinstance(arg, dict):
88 elif isinstance(arg, dict):
89 for k, v in arg.items():
89 for k, v in arg.items():
90 arg[k] = encode(v)
90 arg[k] = encode(v)
91 return arg
91 return arg
92
92
93 def appendsep(s):
93 def appendsep(s):
94 # ensure the path ends with os.sep, appending it if necessary.
94 # ensure the path ends with os.sep, appending it if necessary.
95 try:
95 try:
96 us = decode(s)
96 us = decode(s)
97 except UnicodeError:
97 except UnicodeError:
98 us = s
98 us = s
99 if us and us[-1] not in ':/\\':
99 if us and us[-1] not in ':/\\':
100 s += os.sep
100 s += os.sep
101 return s
101 return s
102
102
103
103
104 def basewrapper(func, argtype, enc, dec, args, kwds):
104 def basewrapper(func, argtype, enc, dec, args, kwds):
105 # check check already converted, then call original
105 # check check already converted, then call original
106 for arg in args:
106 for arg in args:
107 if isinstance(arg, argtype):
107 if isinstance(arg, argtype):
108 return func(*args, **kwds)
108 return func(*args, **kwds)
109
109
110 try:
110 try:
111 # convert string arguments, call func, then convert back the
111 # convert string arguments, call func, then convert back the
112 # return value.
112 # return value.
113 return enc(func(*dec(args), **dec(kwds)))
113 return enc(func(*dec(args), **dec(kwds)))
114 except UnicodeError:
114 except UnicodeError:
115 raise error.Abort(_("[win32mbcs] filename conversion failed with"
115 raise error.Abort(_("[win32mbcs] filename conversion failed with"
116 " %s encoding\n") % (_encoding))
116 " %s encoding\n") % (_encoding))
117
117
118 def wrapper(func, args, kwds):
118 def wrapper(func, args, kwds):
119 return basewrapper(func, unicode, encode, decode, args, kwds)
119 return basewrapper(func, unicode, encode, decode, args, kwds)
120
120
121
121
122 def reversewrapper(func, args, kwds):
122 def reversewrapper(func, args, kwds):
123 return basewrapper(func, str, decode, encode, args, kwds)
123 return basewrapper(func, str, decode, encode, args, kwds)
124
124
125 def wrapperforlistdir(func, args, kwds):
125 def wrapperforlistdir(func, args, kwds):
126 # Ensure 'path' argument ends with os.sep to avoids
126 # Ensure 'path' argument ends with os.sep to avoids
127 # misinterpreting last 0x5c of MBCS 2nd byte as path separator.
127 # misinterpreting last 0x5c of MBCS 2nd byte as path separator.
128 if args:
128 if args:
129 args = list(args)
129 args = list(args)
130 args[0] = appendsep(args[0])
130 args[0] = appendsep(args[0])
131 if 'path' in kwds:
131 if 'path' in kwds:
132 kwds['path'] = appendsep(kwds['path'])
132 kwds['path'] = appendsep(kwds['path'])
133 return func(*args, **kwds)
133 return func(*args, **kwds)
134
134
135 def wrapname(name, wrapper):
135 def wrapname(name, wrapper):
136 module, name = name.rsplit('.', 1)
136 module, name = name.rsplit('.', 1)
137 module = sys.modules[module]
137 module = sys.modules[module]
138 func = getattr(module, name)
138 func = getattr(module, name)
139 def f(*args, **kwds):
139 def f(*args, **kwds):
140 return wrapper(func, args, kwds)
140 return wrapper(func, args, kwds)
141 try:
141 f.__name__ = func.__name__
142 f.__name__ = func.__name__ # fails with Python 2.3
143 except Exception:
144 pass
145 setattr(module, name, f)
142 setattr(module, name, f)
146
143
147 # List of functions to be wrapped.
144 # List of functions to be wrapped.
148 # NOTE: os.path.dirname() and os.path.basename() are safe because
145 # NOTE: os.path.dirname() and os.path.basename() are safe because
149 # they use result of os.path.split()
146 # they use result of os.path.split()
150 funcs = '''os.path.join os.path.split os.path.splitext
147 funcs = '''os.path.join os.path.split os.path.splitext
151 os.path.normpath os.makedirs mercurial.util.endswithsep
148 os.path.normpath os.makedirs mercurial.util.endswithsep
152 mercurial.util.splitpath mercurial.util.fscasesensitive
149 mercurial.util.splitpath mercurial.util.fscasesensitive
153 mercurial.util.fspath mercurial.util.pconvert mercurial.util.normpath
150 mercurial.util.fspath mercurial.util.pconvert mercurial.util.normpath
154 mercurial.util.checkwinfilename mercurial.util.checkosfilename
151 mercurial.util.checkwinfilename mercurial.util.checkosfilename
155 mercurial.util.split'''
152 mercurial.util.split'''
156
153
157 # These functions are required to be called with local encoded string
154 # These functions are required to be called with local encoded string
158 # because they expects argument is local encoded string and cause
155 # because they expects argument is local encoded string and cause
159 # problem with unicode string.
156 # problem with unicode string.
160 rfuncs = '''mercurial.encoding.upper mercurial.encoding.lower'''
157 rfuncs = '''mercurial.encoding.upper mercurial.encoding.lower'''
161
158
162 # List of Windows specific functions to be wrapped.
159 # List of Windows specific functions to be wrapped.
163 winfuncs = '''os.path.splitunc'''
160 winfuncs = '''os.path.splitunc'''
164
161
165 # codec and alias names of sjis and big5 to be faked.
162 # codec and alias names of sjis and big5 to be faked.
166 problematic_encodings = '''big5 big5-tw csbig5 big5hkscs big5-hkscs
163 problematic_encodings = '''big5 big5-tw csbig5 big5hkscs big5-hkscs
167 hkscs cp932 932 ms932 mskanji ms-kanji shift_jis csshiftjis shiftjis
164 hkscs cp932 932 ms932 mskanji ms-kanji shift_jis csshiftjis shiftjis
168 sjis s_jis shift_jis_2004 shiftjis2004 sjis_2004 sjis2004
165 sjis s_jis shift_jis_2004 shiftjis2004 sjis_2004 sjis2004
169 shift_jisx0213 shiftjisx0213 sjisx0213 s_jisx0213 950 cp950 ms950 '''
166 shift_jisx0213 shiftjisx0213 sjisx0213 s_jisx0213 950 cp950 ms950 '''
170
167
171 def extsetup(ui):
168 def extsetup(ui):
172 # TODO: decide use of config section for this extension
169 # TODO: decide use of config section for this extension
173 if ((not os.path.supports_unicode_filenames) and
170 if ((not os.path.supports_unicode_filenames) and
174 (sys.platform != 'cygwin')):
171 (sys.platform != 'cygwin')):
175 ui.warn(_("[win32mbcs] cannot activate on this platform.\n"))
172 ui.warn(_("[win32mbcs] cannot activate on this platform.\n"))
176 return
173 return
177 # determine encoding for filename
174 # determine encoding for filename
178 global _encoding
175 global _encoding
179 _encoding = ui.config('win32mbcs', 'encoding', encoding.encoding)
176 _encoding = ui.config('win32mbcs', 'encoding', encoding.encoding)
180 # fake is only for relevant environment.
177 # fake is only for relevant environment.
181 if _encoding.lower() in problematic_encodings.split():
178 if _encoding.lower() in problematic_encodings.split():
182 for f in funcs.split():
179 for f in funcs.split():
183 wrapname(f, wrapper)
180 wrapname(f, wrapper)
184 if os.name == 'nt':
181 if os.name == 'nt':
185 for f in winfuncs.split():
182 for f in winfuncs.split():
186 wrapname(f, wrapper)
183 wrapname(f, wrapper)
187 wrapname("mercurial.osutil.listdir", wrapperforlistdir)
184 wrapname("mercurial.osutil.listdir", wrapperforlistdir)
188 # wrap functions to be called with local byte string arguments
185 # wrap functions to be called with local byte string arguments
189 for f in rfuncs.split():
186 for f in rfuncs.split():
190 wrapname(f, reversewrapper)
187 wrapname(f, reversewrapper)
191 # Check sys.args manually instead of using ui.debug() because
188 # Check sys.args manually instead of using ui.debug() because
192 # command line options is not yet applied when
189 # command line options is not yet applied when
193 # extensions.loadall() is called.
190 # extensions.loadall() is called.
194 if '--debug' in sys.argv:
191 if '--debug' in sys.argv:
195 ui.write(("[win32mbcs] activated with encoding: %s\n")
192 ui.write(("[win32mbcs] activated with encoding: %s\n")
196 % _encoding)
193 % _encoding)
General Comments 0
You need to be logged in to leave comments. Login now