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):
if not ('#!' in lines[0]) and ('python' in lines[0]):
# The script doesn't follow distutil's rules. Ignore it.
return []
source_encoding_line = ''
original_content = ''.join(lines[1:])
if (len(lines) > 1) and is_source_encoding_line(lines[1]):
# The second line contains a source encoding line. Copy it verbatim.
source_encoding_line = lines[1].rstrip()
original_content = ''.join(lines[2:])
lines = lines[1:] # Strip off the first hashbang line.
line_with_first_import = len(lines)
for line_number, line in enumerate(lines):
if not 'import' in line:
continue
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)
contents = distutils_script_template % dict(
python = python,
source_encoding_line = source_encoding_line,
path = path,
initialization = initialization,
relative_paths_setup = rsetup,
original_content = original_content
before = before,
after = after
)
return _create_script(contents, dest)
......@@ -1158,9 +1166,8 @@ if __name__ == '__main__':
sys.exit(%(module_name)s.%(attrs)s(%(arguments)s))
'''
distutils_script_template = script_header + '''\
%(source_encoding_line)s
distutils_script_template = script_header + '''
%(before)s
%(relative_paths_setup)s
import sys
sys.path[0:0] = [
......@@ -1168,7 +1175,7 @@ sys.path[0:0] = [
]
%(initialization)s
%(original_content)s'''
%(after)s'''
def _pyscript(path, dest, rsetup, initialization=''):
......
......@@ -962,12 +962,14 @@ It also works for zipped eggs:
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
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
contain a source encoding hint; buildout retains it.
like for the console_scripts.
>>> cat(distbin, 'distutilsscript')
#!/usr/local/bin/python2.7
# -*- coding: utf-8 -*-
"""Module docstring."""
from __future__ import print_statement
<BLANKLINE>
<BLANKLINE>
import sys
sys.path[0:0] = [
......@@ -975,8 +977,15 @@ contain a source encoding hint; buildout retains it.
]
<BLANKLINE>
<BLANKLINE>
import os
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
there's no specific method to pass them to.
......
......@@ -2945,6 +2945,9 @@ def create_sample_eggs(test, executable=sys.executable):
tmp, 'distutilsscript',
'#!/usr/bin/python\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'
)
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