Commit 122a8392 authored by Tres Seaver's avatar Tres Seaver

Merge branch 'reinout-future-import-scripts' of...

Merge branch 'reinout-future-import-scripts' of https://github.com/reinout/buildout into reinout-reinout-future-import-scripts
parents da6ba70e 0fb0c808
...@@ -1078,22 +1078,30 @@ def _distutils_script(path, dest, script_content, initialization, rsetup): ...@@ -1078,22 +1078,30 @@ def _distutils_script(path, dest, script_content, initialization, rsetup):
if not ('#!' in lines[0]) and ('python' in lines[0]): if not ('#!' in lines[0]) and ('python' in lines[0]):
# The script doesn't follow distutil's rules. Ignore it. # The script doesn't follow distutil's rules. Ignore it.
return [] return []
source_encoding_line = '' lines = lines[1:] # Strip off the first hashbang line.
original_content = ''.join(lines[1:]) line_with_first_import = len(lines)
if (len(lines) > 1) and is_source_encoding_line(lines[1]): for line_number, line in enumerate(lines):
# The second line contains a source encoding line. Copy it verbatim. if not 'import' in line:
source_encoding_line = lines[1].rstrip() continue
original_content = ''.join(lines[2:]) if not line.startswith('import') or line.startswith('from'):
continue
if '__future__' in line:
continue
line_with_first_import = line_number
break
before = ''.join(lines[:line_with_first_import])
after = ''.join(lines[line_with_first_import:])
python = _safe_arg(sys.executable) python = _safe_arg(sys.executable)
contents = distutils_script_template % dict( contents = distutils_script_template % dict(
python = python, python = python,
source_encoding_line = source_encoding_line,
path = path, path = path,
initialization = initialization, initialization = initialization,
relative_paths_setup = rsetup, relative_paths_setup = rsetup,
original_content = original_content before = before,
after = after
) )
return _create_script(contents, dest) return _create_script(contents, dest)
...@@ -1158,9 +1166,8 @@ if __name__ == '__main__': ...@@ -1158,9 +1166,8 @@ if __name__ == '__main__':
sys.exit(%(module_name)s.%(attrs)s(%(arguments)s)) sys.exit(%(module_name)s.%(attrs)s(%(arguments)s))
''' '''
distutils_script_template = script_header + '''\ distutils_script_template = script_header + '''
%(before)s
%(source_encoding_line)s
%(relative_paths_setup)s %(relative_paths_setup)s
import sys import sys
sys.path[0:0] = [ sys.path[0:0] = [
...@@ -1168,7 +1175,7 @@ sys.path[0:0] = [ ...@@ -1168,7 +1175,7 @@ sys.path[0:0] = [
] ]
%(initialization)s %(initialization)s
%(original_content)s''' %(after)s'''
def _pyscript(path, dest, rsetup, initialization=''): def _pyscript(path, dest, rsetup, initialization=''):
......
...@@ -962,12 +962,14 @@ It also works for zipped eggs: ...@@ -962,12 +962,14 @@ It also works for zipped eggs:
Distutils copies the script files verbatim, apart from a line at the top that Distutils copies the script files verbatim, apart from a line at the top that
looks like ``#!/usr/bin/python``, which gets replaced by the actual python looks like ``#!/usr/bin/python``, which gets replaced by the actual python
interpreter. Buildout does the same, but additionally also adds the sys.path interpreter. Buildout does the same, but additionally also adds the sys.path
like for the console_scripts. Note that the second line in the scripts can like for the console_scripts.
contain a source encoding hint; buildout retains it.
>>> cat(distbin, 'distutilsscript') >>> cat(distbin, 'distutilsscript')
#!/usr/local/bin/python2.7 #!/usr/local/bin/python2.7
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""Module docstring."""
from __future__ import print_statement
<BLANKLINE>
<BLANKLINE> <BLANKLINE>
import sys import sys
sys.path[0:0] = [ sys.path[0:0] = [
...@@ -975,8 +977,15 @@ contain a source encoding hint; buildout retains it. ...@@ -975,8 +977,15 @@ contain a source encoding hint; buildout retains it.
] ]
<BLANKLINE> <BLANKLINE>
<BLANKLINE> <BLANKLINE>
import os
import sys; sys.stdout.write("distutils!\n") import sys; sys.stdout.write("distutils!\n")
Note that there are several items that need to come first in such a script
*before* buildout's ``sys.path`` statements: a source encoding hint, a module
docstring and ``__future__`` imports. Buildout retains them in their proper
place by looking at the first non-future import and placing its ``sys.path``
statement before that.
Due to the nature of distutils scripts, buildout cannot pass arguments as Due to the nature of distutils scripts, buildout cannot pass arguments as
there's no specific method to pass them to. there's no specific method to pass them to.
......
...@@ -2945,6 +2945,9 @@ def create_sample_eggs(test, executable=sys.executable): ...@@ -2945,6 +2945,9 @@ def create_sample_eggs(test, executable=sys.executable):
tmp, 'distutilsscript', tmp, 'distutilsscript',
'#!/usr/bin/python\n' '#!/usr/bin/python\n'
'# -*- coding: utf-8 -*-\n' '# -*- coding: utf-8 -*-\n'
'"""Module docstring."""\n'
'from __future__ import print_statement\n'
'import os\n'
'import sys; sys.stdout.write("distutils!\\n")\n' 'import sys; sys.stdout.write("distutils!\\n")\n'
) )
write( write(
......
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