Commit 968ff840 authored by Philip Thiem's avatar Philip Thiem

old win wrapper script not expecting return values from write()

python 3 no longer has popen4
had some issue with ^M on end on line.

--HG--
branch : distribute
extra : rebase_source : 5b2c834e9a8dfd4027791cacef7c2bfe03652f31
parent 4bb7aab6
...@@ -17,7 +17,7 @@ Let's create a simple script, foo-script.py: ...@@ -17,7 +17,7 @@ Let's create a simple script, foo-script.py:
>>> from setuptools.command.easy_install import nt_quote_arg >>> from setuptools.command.easy_install import nt_quote_arg
>>> sample_directory = tempfile.mkdtemp() >>> sample_directory = tempfile.mkdtemp()
>>> f = open(os.path.join(sample_directory, 'foo-script.py'), 'w') >>> f = open(os.path.join(sample_directory, 'foo-script.py'), 'w')
>>> f.write( >>> bytes_written = f.write(
... """#!%(python_exe)s ... """#!%(python_exe)s
... import sys ... import sys
... input = repr(sys.stdin.read()) ... input = repr(sys.stdin.read())
...@@ -37,7 +37,7 @@ We'll also copy cli.exe to the sample-directory with the name foo.exe: ...@@ -37,7 +37,7 @@ We'll also copy cli.exe to the sample-directory with the name foo.exe:
>>> import pkg_resources >>> import pkg_resources
>>> f = open(os.path.join(sample_directory, 'foo.exe'), 'wb') >>> f = open(os.path.join(sample_directory, 'foo.exe'), 'wb')
>>> f.write( >>> bytes_written = f.write(
... pkg_resources.resource_string('setuptools', 'cli-32.exe') ... pkg_resources.resource_string('setuptools', 'cli-32.exe')
... ) ... )
>>> f.close() >>> f.close()
...@@ -49,12 +49,32 @@ GUI programs, the suffix '-script-pyw' is added.) This is why we ...@@ -49,12 +49,32 @@ GUI programs, the suffix '-script-pyw' is added.) This is why we
named out script the way we did. Now we can run out script by running named out script the way we did. Now we can run out script by running
the wrapper: the wrapper:
>>> import os >>> from subprocess import Popen, PIPE, STDOUT
>>> input, output = os.popen4('"'+nt_quote_arg(os.path.join(sample_directory, 'foo.exe')) >>> try:
... + r' arg1 "arg 2" "arg \"2\\\"" "arg 4\\" "arg5 a\\b"') ... unicode=unicode
>>> input.write('hello\nworld\n') ... except:
... unicode=str
>>> def popen4(cmd, *args):
... if hasattr(os, 'popen4'):
... input, output = os.popen4(cmd + " ".join(args))
... return input, output
... else:
... #emulate popen4 in python 3
... if cmd[0] == '"' and cmd[-1] != '"':
... cmd = cmd[1:]
... cmd += " ".join(args)
... p = Popen(cmd, shell=True, bufsize=0,
... stdin=PIPE, stdout=PIPE, stderr=STDOUT)
... return p.stdin, p.stdout
>>> input, output = popen4('"' + nt_quote_arg(os.path.join(sample_directory, 'foo.exe')),
... r' arg1', r'"arg 2"', r'"arg \"2\\\""', r'"arg 4\\"', r'"arg5 a\\b"')
>>> bytes_written = input.write('hello\nworld\n'.encode('utf-8'))
>>> input.close() >>> input.close()
>>> print(output.read()) >>> # This is needed for line ending differences between py2 and py3 on win32
>>> msg = unicode(output.read(), encoding='utf-8').split("\n")
>>> for line in msg:
... print(line.strip())
\foo-script.py \foo-script.py
['arg1', 'arg 2', 'arg "2\\"', 'arg 4\\', 'arg5 a\\\\b'] ['arg1', 'arg 2', 'arg "2\\"', 'arg 4\\', 'arg5 a\\\\b']
'hello\nworld\n' 'hello\nworld\n'
...@@ -83,7 +103,7 @@ options as usual. For example, to run in optimized mode and ...@@ -83,7 +103,7 @@ options as usual. For example, to run in optimized mode and
enter the interpreter after running the script, you could use -Oi: enter the interpreter after running the script, you could use -Oi:
>>> f = open(os.path.join(sample_directory, 'foo-script.py'), 'w') >>> f = open(os.path.join(sample_directory, 'foo-script.py'), 'w')
>>> f.write( >>> bytes_written = f.write(
... """#!%(python_exe)s -Oi ... """#!%(python_exe)s -Oi
... import sys ... import sys
... input = repr(sys.stdin.read()) ... input = repr(sys.stdin.read())
...@@ -96,9 +116,12 @@ enter the interpreter after running the script, you could use -Oi: ...@@ -96,9 +116,12 @@ enter the interpreter after running the script, you could use -Oi:
... """ % dict(python_exe=nt_quote_arg(sys.executable))) ... """ % dict(python_exe=nt_quote_arg(sys.executable)))
>>> f.close() >>> f.close()
>>> input, output = os.popen4(nt_quote_arg(os.path.join(sample_directory, 'foo.exe'))) >>> input, output = popen4(nt_quote_arg(os.path.join(sample_directory, 'foo.exe')))
>>> input.close() >>> input.close()
>>> print(output.read()) >>> # This is needed for line ending differences between py2 and py3 on win32
>>> msg = unicode(output.read(), encoding='utf-8').split("\n")
>>> for line in msg:
... print(line.strip())
\foo-script.py \foo-script.py
[] []
'' ''
...@@ -114,11 +137,11 @@ Now let's test the GUI version with the simple scipt, bar-script.py: ...@@ -114,11 +137,11 @@ Now let's test the GUI version with the simple scipt, bar-script.py:
>>> from setuptools.command.easy_install import nt_quote_arg >>> from setuptools.command.easy_install import nt_quote_arg
>>> sample_directory = tempfile.mkdtemp() >>> sample_directory = tempfile.mkdtemp()
>>> f = open(os.path.join(sample_directory, 'bar-script.pyw'), 'w') >>> f = open(os.path.join(sample_directory, 'bar-script.pyw'), 'w')
>>> f.write( >>> bytes_written = f.write(
... """#!%(python_exe)s ... """#!%(python_exe)s
... import sys ... import sys
... f = open(sys.argv[1], 'wb') ... f = open(sys.argv[1], 'wb')
... f.write(repr(sys.argv[2])) ... bytes_written = f.write(repr(sys.argv[2]).encode('utf-8'))
... f.close() ... f.close()
... """ % dict(python_exe=nt_quote_arg(sys.executable))) ... """ % dict(python_exe=nt_quote_arg(sys.executable)))
>>> f.close() >>> f.close()
...@@ -127,21 +150,23 @@ We'll also copy gui.exe to the sample-directory with the name bar.exe: ...@@ -127,21 +150,23 @@ We'll also copy gui.exe to the sample-directory with the name bar.exe:
>>> import pkg_resources >>> import pkg_resources
>>> f = open(os.path.join(sample_directory, 'bar.exe'), 'wb') >>> f = open(os.path.join(sample_directory, 'bar.exe'), 'wb')
>>> f.write( >>> bytes_written = f.write(
... pkg_resources.resource_string('setuptools', 'gui-32.exe') ... pkg_resources.resource_string('setuptools', 'gui-32.exe')
... ) ... )
>>> f.close() >>> f.close()
Finally, we'll run the script and check the result: Finally, we'll run the script and check the result:
>>> import os >>> input, output = popen4('"'+nt_quote_arg(os.path.join(sample_directory, 'bar.exe')),
>>> input, output = os.popen4('"'+nt_quote_arg(os.path.join(sample_directory, 'bar.exe')) ... r' "%s" "Test Argument"' % os.path.join(sample_directory, 'test_output.txt'))
... + r' "%s" "Test Argument"' % os.path.join(sample_directory, 'test_output.txt'))
>>> input.close() >>> input.close()
>>> print(output.read()) >>> # This is needed for line ending differences between py2 and py3 on win32
>>> msg = unicode(output.read(), encoding='utf-8').split("\n")
>>> for line in msg:
... print(line.strip())
<BLANKLINE> <BLANKLINE>
>>> f = open(os.path.join(sample_directory, 'test_output.txt'), 'rb') >>> f = open(os.path.join(sample_directory, 'test_output.txt'), 'rb')
>>> print(f.read()) >>> print(unicode(f.read(), encoding='utf-8'))
'Test Argument' 'Test Argument'
>>> f.close() >>> f.close()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment