Changeset 37b07a5 in trunk
- Timestamp:
- 2010-07-25T08:32:16Z (15 years ago)
- Branches:
- master
- Children:
- 9d04b2a
- Parents:
- 54a9ba8
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified bin/tahoe-script.template ¶
r54a9ba8 r37b07a5 6 6 base = os.path.dirname(os.path.dirname(where)) 7 7 8 if sys.platform == "win32": 9 installed_tahoe = os.path.join(os.path.dirname(sys.executable), 'Scripts', 'tahoe.pyscript') 10 else: 11 installed_tahoe = "/usr/bin/tahoe" 12 8 13 whoami = '''\ 9 I am a "bin /tahoe" executable who is only for the convenience of running14 I am a "bin%stahoe" executable who is only for the convenience of running 10 15 Tahoe from its source distribution -- I work only when invoked as the "tahoe" 11 16 script that lives in the "bin/" subdirectory of a Tahoe source code 12 17 distribution, and only if you have already run "make". 13 ''' 18 ''' % (os.path.sep,) 14 19 15 20 # look for Tahoe.home . … … 20 25 I just tried to run and found that I am not living in such a directory, so I 21 26 am stopping now. To run Tahoe after it has been is installed, please execute 22 my brother, also named "tahoe", who gets installed into the appropriate place23 for executables when you run "make install" (perhaps as /usr/bin/tahoe).24 ''' 27 my brother, who gets installed into the appropriate place for executables 28 when you run "make install" (perhaps as "%s"). 29 ''' % (installed_tahoe,) 25 30 sys.exit(1) 26 31 … … 42 47 os.environ["PYTHONPATH"] = pp 43 48 44 # find the location of the tahoe executable. 45 bin_dir = "bin" 49 # find commandline args and the location of the tahoe executable. 46 50 if sys.platform == "win32": 47 bin_dir = "Scripts" 48 executable = os.path.join(base, "support", bin_dir, "tahoe") 51 import re 52 from ctypes import WINFUNCTYPE, POINTER, byref, c_wchar_p, c_int, windll 53 54 GetCommandLineW = WINFUNCTYPE(c_wchar_p)(("GetCommandLineW", windll.kernel32)) 55 CommandLineToArgvW = WINFUNCTYPE(POINTER(c_wchar_p), c_wchar_p, POINTER(c_int)) \ 56 (("CommandLineToArgvW", windll.shell32)) 57 58 argc = c_int(0) 59 argv_unicode = CommandLineToArgvW(GetCommandLineW(), byref(argc)) 60 61 # See src/allmydata/scripts/runner.py for the corresponding unmangler. 62 # Note that this doesn't escape \x7F. If it did, test_unicode_arguments_and_output 63 # in test_runner.py wouldn't work. 64 def mangle(s): 65 return str(re.sub(ur'[^\x20-\x7F]', lambda m: u'\x7F%x;' % (ord(m.group(0)),), s)) 66 67 argv = [mangle(argv_unicode[i]) for i in xrange(1, argc.value)] 68 local_tahoe = "Scripts\\tahoe.pyscript" 69 else: 70 argv = sys.argv 71 local_tahoe = "bin/tahoe" 72 73 script = os.path.join(base, "support", local_tahoe) 49 74 50 75 try: 51 res = subprocess.call([ executable] + sys.argv[1:], env=os.environ)76 res = subprocess.call([sys.executable, script] + argv[1:], env=os.environ) 52 77 except (OSError, IOError), le: 53 78 if le.args[0] == errno.ENOENT: 54 79 print whoami 55 80 print '''\ 56 I just tried to run and could not find my brother , named57 " ../support/bin/tahoe". To run Tahoe when it is installed, please execute my58 brother, also named "tahoe", who gets installed into the appropriate place59 for executables when you run "make install" (perhaps as /usr/bin/tahoe).60 ''' 81 I just tried to run and could not find my brother at 82 "%s". To run Tahoe when it is installed, please execute my 83 brother, who gets installed into the appropriate place for executables 84 when you run "make install" (perhaps as "%s"). 85 ''' % (script, installed_tahoe) 61 86 raise 62 87 except Exception, le: 63 88 print whoami 64 89 print '''\ 65 I just tried to invoke my brother , named "../support/bin/tahoe" and got an66 exception.67 ''' 90 I just tried to invoke my brother at "%s" 91 and got an exception. 92 ''' % (script,) 68 93 raise 69 94 else: -
TabularUnified setup.py ¶
r54a9ba8 r37b07a5 236 236 bin_tahoe_template = os.path.join("bin", "tahoe-script.template") 237 237 238 # Create the 'tahoe-script.py' file under the 'bin' directory. The 239 # 'tahoe-script.py' file is exactly the same as the 240 # 'tahoe-script.template' script except that the shebang line is 241 # rewritten to use our sys.executable for the interpreter. On 242 # Windows, create a tahoe.exe will execute it. On non-Windows, make a 243 # symlink to it from 'tahoe'. The tahoe.exe will be copied from the 244 # setuptools egg's cli.exe and this will work from a zip-safe and 245 # non-zip-safe setuptools egg. 238 if sys.platform == 'win32': 239 # 'tahoe' script is needed for cygwin 240 script_names = ["tahoe.pyscript", "tahoe"] 241 else: 242 script_names = ["tahoe"] 243 244 # Create the tahoe script file under the 'bin' directory. This 245 # file is exactly the same as the 'tahoe-script.template' script 246 # except that the shebang line is rewritten to use our sys.executable 247 # for the interpreter. 246 248 f = open(bin_tahoe_template, "rU") 247 249 script_lines = f.readlines() 248 250 f.close() 249 script_lines[0] = "#!%s\n" % sys.executable 250 tahoe_script = os.path.join("bin", "tahoe-script.py") 251 f = open(tahoe_script, "w") 252 for line in script_lines: 253 f.write(line) 254 f.close() 255 if sys.platform == "win32": 256 from pkg_resources import require 257 setuptools_egg = require("setuptools")[0].location 258 if os.path.isfile(setuptools_egg): 259 z = zipfile.ZipFile(setuptools_egg, 'r') 260 for filename in z.namelist(): 261 if 'cli.exe' in filename: 262 cli_exe = z.read(filename) 263 else: 264 cli_exe = os.path.join(setuptools_egg, 'setuptools', 'cli.exe') 265 tahoe_exe = os.path.join("bin", "tahoe.exe") 266 if os.path.isfile(setuptools_egg): 267 f = open(tahoe_exe, 'wb') 268 f.write(cli_exe) 269 f.close() 270 else: 271 shutil.copy(cli_exe, tahoe_exe) 272 else: 251 script_lines[0] = '#!%s\n' % (sys.executable,) 252 for script_name in script_names: 253 tahoe_script = os.path.join("bin", script_name) 273 254 try: 274 os.remove(os.path.join('bin', 'tahoe')) 275 except: 276 # okay, probably it was already gone 277 pass 278 os.symlink('tahoe-script.py', os.path.join('bin', 'tahoe')) 279 280 # chmod +x bin/tahoe-script.py 281 old_mode = stat.S_IMODE(os.stat(tahoe_script)[stat.ST_MODE]) 282 new_mode = old_mode | (stat.S_IXUSR | stat.S_IRUSR | 283 stat.S_IXGRP | stat.S_IRGRP | 284 stat.S_IXOTH | stat.S_IROTH ) 285 os.chmod(tahoe_script, new_mode) 255 os.remove(tahoe_script) 256 except Exception: 257 if os.path.exists(tahoe_script): 258 raise 259 f = open(tahoe_script, "wb") 260 for line in script_lines: 261 f.write(line) 262 f.close() 263 264 # chmod +x 265 old_mode = stat.S_IMODE(os.stat(tahoe_script)[stat.ST_MODE]) 266 new_mode = old_mode | (stat.S_IXUSR | stat.S_IRUSR | 267 stat.S_IXGRP | stat.S_IRGRP | 268 stat.S_IXOTH | stat.S_IROTH ) 269 os.chmod(tahoe_script, new_mode) 270 271 old_tahoe_exe = os.path.join("bin", "tahoe.exe") 272 try: 273 os.remove(old_tahoe_exe) 274 except Exception: 275 if os.path.exists(old_tahoe_exe): 276 raise 277 286 278 287 279 class MySdist(sdist.sdist): -
TabularUnified src/allmydata/scripts/runner.py ¶
r54a9ba8 r37b07a5 11 11 from allmydata.scripts.common import BaseOptions 12 12 from allmydata.scripts import debug, create_node, startstop_node, cli, keygen, stats_gatherer 13 from allmydata.util.encodingutil import quote_output, get_argv_encoding 13 14 14 15 def GROUP(s): … … 20 21 21 22 class Options(BaseOptions, usage.Options): 22 synopsis = " Usage: tahoe <command> [command options]"23 synopsis = "\nUsage: tahoe <command> [command options]" 23 24 subCommands = ( GROUP("Administration") 24 25 + create_node.subCommands … … 43 44 def runner(argv, 44 45 run_by_human=True, 45 stdin= sys.stdin, stdout=sys.stdout, stderr=sys.stderr,46 stdin=None, stdout=None, stderr=None, 46 47 install_node_control=True, additional_commands=None): 48 49 stdin = stdin or sys.stdin 50 stdout = stdout or sys.stdout 51 stderr = stderr or sys.stderr 47 52 48 53 config = Options() … … 64 69 while hasattr(c, 'subOptions'): 65 70 c = c.subOptions 66 print str(c) 67 print "%s: %s" % (sys.argv[0], e) 71 print >>stdout, str(c) 72 try: 73 msg = e.args[0].decode(get_argv_encoding()) 74 except Exception: 75 msg = repr(e) 76 print >>stdout, "%s: %s\n" % (sys.argv[0], quote_output(msg, quotemarks=False)) 68 77 return 1 69 78 … … 100 109 return rc 101 110 111 102 112 def run(install_node_control=True): 103 rc = runner(sys.argv[1:]) 113 if sys.platform == "win32": 114 from allmydata.windows.fixups import initialize 115 initialize() 116 117 rc = runner(sys.argv[1:], install_node_control=install_node_control) 104 118 sys.exit(rc) -
TabularUnified src/allmydata/test/__init__.py ¶
r54a9ba8 r37b07a5 25 25 # we disable incident reporting for all unit tests. 26 26 disable_foolscap_incidents() 27 28 import sys 29 if sys.platform == "win32": 30 from allmydata.windows.fixups import initialize 31 initialize() -
TabularUnified src/allmydata/test/test_encodingutil.py ¶
r54a9ba8 r37b07a5 22 22 print "Usage: %s lumi<e-grave>re" % sys.argv[0] 23 23 sys.exit(1) 24 24 25 if sys.platform == "win32": 26 try: 27 from allmydata.windows.fixups import initialize 28 except ImportError: 29 print "set PYTHONPATH to the src directory" 30 sys.exit(1) 31 initialize() 32 25 33 print 26 34 print "class MyWeirdOS(EncodingUtil, unittest.TestCase):" 27 35 print " uname = '%s'" % ' '.join(platform.uname()) 28 if sys.platform != "win32": 29 print " argv = %s" % repr(sys.argv[1]) 36 print " argv = %s" % repr(sys.argv[1]) 30 37 print " platform = '%s'" % sys.platform 31 38 print " filesystem_encoding = '%s'" % sys.getfilesystemencoding() 32 39 print " output_encoding = '%s'" % sys.stdout.encoding 33 print " argv_encoding = '%s'" % (sys.platform == "win32" and 'ascii' or sys.stdout.encoding) 34 40 print " argv_encoding = '%s'" % sys.stdout.encoding 35 41 try: 36 42 tmpdir = tempfile.mkdtemp() … … 57 63 58 64 from allmydata.test.common_util import ReallyEqualMixin 65 from allmydata.util import encodingutil 59 66 from allmydata.util.encodingutil import argv_to_unicode, unicode_to_url, \ 60 67 unicode_to_output, quote_output, unicode_platform, listdir_unicode, \ … … 65 72 66 73 class EncodingUtilErrors(ReallyEqualMixin, unittest.TestCase): 67 def tearDown(self):68 _reload()69 74 70 75 @patch('sys.stdout') … … 79 84 80 85 mock_stdout.encoding = 'koi8-r' 81 _reload() 82 self.failUnlessReallyEqual(get_output_encoding(), 'koi8-r') 86 expected = sys.platform == "win32" and 'utf-8' or 'koi8-r' 87 _reload() 88 self.failUnlessReallyEqual(get_output_encoding(), expected) 83 89 84 90 mock_stdout.encoding = 'nonexistent_encoding' 85 self.failUnlessRaises(AssertionError, _reload) 91 if sys.platform == "win32": 92 _reload() 93 self.failUnlessReallyEqual(get_output_encoding(), 'utf-8') 94 else: 95 self.failUnlessRaises(AssertionError, _reload) 86 96 87 97 @patch('locale.getpreferredencoding') … … 95 105 sys.stdout = DummyStdout() 96 106 try: 107 expected = sys.platform == "win32" and 'utf-8' or 'koi8-r' 97 108 _reload() 98 self.failUnlessReallyEqual(get_output_encoding(), 'koi8-r')109 self.failUnlessReallyEqual(get_output_encoding(), expected) 99 110 100 111 sys.stdout.encoding = None 101 112 _reload() 102 self.failUnlessReallyEqual(get_output_encoding(), 'koi8-r')113 self.failUnlessReallyEqual(get_output_encoding(), expected) 103 114 104 115 mock_locale_getpreferredencoding.return_value = None … … 108 119 sys.stdout = old_stdout 109 120 110 @patch('sys.stdout') 111 def test_argv_to_unicode(self, mock): 112 mock.encoding = 'utf-8' 113 _reload() 114 121 def test_argv_to_unicode(self): 122 encodingutil.output_encoding = 'utf-8' 115 123 self.failUnlessRaises(usage.UsageError, 116 124 argv_to_unicode, 117 125 lumiere_nfc.encode('latin1')) 118 126 119 @patch('sys.stdout') 120 def test_unicode_to_output(self, mock): 121 # Encoding koi8-r cannot represent e-grave 122 mock.encoding = 'koi8-r' 123 _reload() 127 def test_unicode_to_output(self): 128 encodingutil.output_encoding = 'koi8-r' 124 129 self.failUnlessRaises(UnicodeEncodeError, unicode_to_output, lumiere_nfc) 125 130 … … 172 177 u'/' + lumiere_nfc) 173 178 179 174 180 class EncodingUtil(ReallyEqualMixin): 175 181 def setUp(self): 176 # Mock sys.platform because unicode_platform() uses it177 182 self.original_platform = sys.platform 178 183 sys.platform = self.platform … … 198 203 @patch('sys.stdout') 199 204 def test_unicode_to_output(self, mock): 200 if ' output' not in dir(self):205 if 'argv' not in dir(self): 201 206 return 202 207 203 208 mock.encoding = self.output_encoding 204 209 _reload() 205 self.failUnlessReallyEqual(unicode_to_output(lumiere_nfc), self. output)210 self.failUnlessReallyEqual(unicode_to_output(lumiere_nfc), self.argv) 206 211 207 212 def test_unicode_platform(self): … … 288 293 289 294 class QuoteOutput(ReallyEqualMixin, unittest.TestCase): 295 def tearDown(self): 296 _reload() 297 290 298 def _check(self, inp, out, enc, optional_quotes): 291 299 out2 = out … … 294 302 self.failUnlessReallyEqual(quote_output(inp, encoding=enc), out) 295 303 self.failUnlessReallyEqual(quote_output(inp, encoding=enc, quotemarks=False), out2) 296 if out[0:2] != 'b"': 297 if isinstance(inp, str): 298 self.failUnlessReallyEqual(quote_output(unicode(inp), encoding=enc), out) 299 self.failUnlessReallyEqual(quote_output(unicode(inp), encoding=enc, quotemarks=False), out2) 300 else: 301 self.failUnlessReallyEqual(quote_output(inp.encode('utf-8'), encoding=enc), out) 302 self.failUnlessReallyEqual(quote_output(inp.encode('utf-8'), encoding=enc, quotemarks=False), out2) 304 if out[0:2] == 'b"': 305 pass 306 elif isinstance(inp, str): 307 self.failUnlessReallyEqual(quote_output(unicode(inp), encoding=enc), out) 308 self.failUnlessReallyEqual(quote_output(unicode(inp), encoding=enc, quotemarks=False), out2) 309 else: 310 self.failUnlessReallyEqual(quote_output(inp.encode('utf-8'), encoding=enc), out) 311 self.failUnlessReallyEqual(quote_output(inp.encode('utf-8'), encoding=enc, quotemarks=False), out2) 303 312 304 313 def _test_quote_output_all(self, enc): … … 369 378 check(u"\u2621\"", u"'\u2621\"'", True) 370 379 371 @patch('sys.stdout') 372 def test_quote_output_mock(self, mock_stdout): 373 mock_stdout.encoding = 'ascii' 374 _reload() 380 def test_quote_output_default(self): 381 encodingutil.output_encoding = 'ascii' 375 382 self.test_quote_output_ascii(None) 376 383 377 mock_stdout.encoding = 'latin1' 378 _reload() 384 encodingutil.output_encoding = 'latin1' 379 385 self.test_quote_output_latin1(None) 380 386 381 mock_stdout.encoding = 'utf-8' 382 _reload() 387 encodingutil.output_encoding = 'utf-8' 383 388 self.test_quote_output_utf8(None) 384 389 … … 386 391 class UbuntuKarmicUTF8(EncodingUtil, unittest.TestCase): 387 392 uname = 'Linux korn 2.6.31-14-generic #48-Ubuntu SMP Fri Oct 16 14:05:01 UTC 2009 x86_64' 388 output = 'lumi\xc3\xa8re'389 393 argv = 'lumi\xc3\xa8re' 390 394 platform = 'linux2' … … 396 400 class UbuntuKarmicLatin1(EncodingUtil, unittest.TestCase): 397 401 uname = 'Linux korn 2.6.31-14-generic #48-Ubuntu SMP Fri Oct 16 14:05:01 UTC 2009 x86_64' 398 output = 'lumi\xe8re'399 402 argv = 'lumi\xe8re' 400 403 platform = 'linux2' … … 404 407 dirlist = ['test_file', 'Blah blah.txt', '\xc4rtonwall.mp3'] 405 408 406 class Windows XP(EncodingUtil, unittest.TestCase):409 class Windows(EncodingUtil, unittest.TestCase): 407 410 uname = 'Windows XP 5.1.2600 x86 x86 Family 15 Model 75 Step ping 2, AuthenticAMD' 408 output = 'lumi\x8are'411 argv = 'lumi\xc3\xa8re' 409 412 platform = 'win32' 410 413 filesystem_encoding = 'mbcs' 411 output_encoding = 'cp850' 412 argv_encoding = 'ascii' 413 dirlist = [u'Blah blah.txt', u'test_file', u'\xc4rtonwall.mp3'] 414 415 class WindowsXP_UTF8(EncodingUtil, unittest.TestCase): 416 uname = 'Windows XP 5.1.2600 x86 x86 Family 15 Model 75 Step ping 2, AuthenticAMD' 417 output = 'lumi\xc3\xa8re' 418 platform = 'win32' 419 filesystem_encoding = 'mbcs' 420 output_encoding = 'cp65001' 421 argv_encoding = 'ascii' 422 dirlist = [u'Blah blah.txt', u'test_file', u'\xc4rtonwall.mp3'] 423 424 class WindowsVista(EncodingUtil, unittest.TestCase): 425 uname = 'Windows Vista 6.0.6000 x86 x86 Family 6 Model 15 Stepping 11, GenuineIntel' 426 output = 'lumi\x8are' 427 platform = 'win32' 428 filesystem_encoding = 'mbcs' 429 output_encoding = 'cp850' 430 argv_encoding = 'ascii' 414 output_encoding = 'utf-8' 415 argv_encoding = 'utf-8' 431 416 dirlist = [u'Blah blah.txt', u'test_file', u'\xc4rtonwall.mp3'] 432 417 … … 434 419 uname = 'Darwin g5.local 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:57:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_PPC Power Macintosh powerpc' 435 420 output = 'lumi\xc3\xa8re' 436 argv = 'lumi\xc3\xa8re'437 421 platform = 'darwin' 438 422 filesystem_encoding = 'utf-8' -
TabularUnified src/allmydata/test/test_runner.py ¶
r54a9ba8 r37b07a5 8 8 from cStringIO import StringIO 9 9 from allmydata.util import fileutil, pollmixin 10 from allmydata.util.encodingutil import unicode_to_argv, unicode_to_output 10 11 from allmydata.scripts import runner 11 12 … … 45 46 self.failUnless(out.startswith(required_ver_and_path), 46 47 str((out, err, rc_or_sig, required_ver_and_path))) 48 d.addCallback(_cb) 49 return d 50 51 def test_unicode_arguments_and_output(self): 52 self.skip_if_cannot_run_bintahoe() 53 54 tricky = u"\u2621" 55 try: 56 tricky_arg = unicode_to_argv(tricky, mangle=True) 57 tricky_out = unicode_to_output(tricky) 58 except UnicodeEncodeError: 59 raise unittest.SkipTest("A non-ASCII argument/output could not be encoded on this platform.") 60 61 d = utils.getProcessOutputAndValue(bintahoe, args=[tricky_arg], env=os.environ) 62 def _cb(res): 63 out, err, rc_or_sig = res 64 self.failUnlessEqual(rc_or_sig, 1, str((out, err, rc_or_sig))) 65 self.failUnlessIn("Unknown command: "+tricky_out, out) 47 66 d.addCallback(_cb) 48 67 return d -
TabularUnified src/allmydata/util/encodingutil.py ¶
r54a9ba8 r37b07a5 14 14 15 15 16 def _canonical_encoding(encoding):16 def canonical_encoding(encoding): 17 17 if encoding is None: 18 18 log.msg("Warning: falling back to UTF-8 encoding.", level=log.WEIRD) … … 24 24 encoding = 'ascii' 25 25 26 return encoding 27 28 def check_encoding(encoding): 26 29 # sometimes Python returns an encoding name that it doesn't support for conversion 27 30 # fail early if this happens … … 30 33 except (LookupError, AttributeError): 31 34 raise AssertionError("The character encoding '%s' is not supported for conversion." % (encoding,)) 32 33 return encoding34 35 35 36 filesystem_encoding = None … … 41 42 global filesystem_encoding, output_encoding, argv_encoding, is_unicode_platform 42 43 43 filesystem_encoding = _canonical_encoding(sys.getfilesystemencoding()) 44 45 outenc = None 46 if hasattr(sys.stdout, 'encoding'): 47 outenc = sys.stdout.encoding 48 if outenc is None: 49 try: 50 outenc = locale.getpreferredencoding() 51 except Exception: 52 pass # work around <http://bugs.python.org/issue1443504> 53 output_encoding = _canonical_encoding(outenc) 44 filesystem_encoding = canonical_encoding(sys.getfilesystemencoding()) 45 check_encoding(filesystem_encoding) 54 46 55 47 if sys.platform == 'win32': 56 # Unicode arguments are not supported on Windows yet; see #565 and #1074. 57 argv_encoding = 'ascii' 58 else: 59 argv_encoding = output_encoding 48 # On Windows we install UTF-8 stream wrappers for sys.stdout and 49 # sys.stderr, and reencode the arguments as UTF-8 (see scripts/runner.py). 50 output_encoding = 'utf-8' 51 else: 52 outenc = None 53 if hasattr(sys.stdout, 'encoding'): 54 outenc = sys.stdout.encoding 55 if outenc is None: 56 try: 57 outenc = locale.getpreferredencoding() 58 except Exception: 59 pass # work around <http://bugs.python.org/issue1443504> 60 output_encoding = canonical_encoding(outenc) 61 62 check_encoding(output_encoding) 63 argv_encoding = output_encoding 64 60 65 is_unicode_platform = sys.platform in ["win32", "darwin"] 61 66
Note: See TracChangeset
for help on using the changeset viewer.