Commit 3df0e5d7 authored by PJ Eby's avatar PJ Eby

Implement detection of non-Python scripts, as described in

http://mail.python.org/pipermail/distutils-sig/2006-June/006359.html
(merge from trunk)

--HG--
branch : setuptools-0.6
extra : convert_revision : svn%3A6015fed2-1504-0410-9fe1-9d1591cc4771/sandbox/branches/setuptools-0.6%4046986
parent de6067f9
...@@ -1095,6 +1095,9 @@ set, if you haven't already got this set up on your machine. ...@@ -1095,6 +1095,9 @@ set, if you haven't already got this set up on your machine.
Release Notes/Change History Release Notes/Change History
============================ ============================
0.6b4
* Fix creating Python wrappers for non-Python scripts
0.6b3 0.6b3
* Fix local ``--find-links`` eggs not being copied except with * Fix local ``--find-links`` eggs not being copied except with
``--always-copy``. ``--always-copy``.
......
...@@ -575,8 +575,9 @@ Please make the appropriate changes for your system and try again. ...@@ -575,8 +575,9 @@ Please make the appropriate changes for your system and try again.
def install_script(self, dist, script_name, script_text, dev_path=None): def install_script(self, dist, script_name, script_text, dev_path=None):
"""Generate a legacy script wrapper and install it""" """Generate a legacy script wrapper and install it"""
spec = str(dist.as_requirement()) spec = str(dist.as_requirement())
is_script = is_python_script(script_text, script_name)
if dev_path: if is_script and dev_path:
script_text = get_script_header(script_text) + ( script_text = get_script_header(script_text) + (
"# EASY-INSTALL-DEV-SCRIPT: %(spec)r,%(script_name)r\n" "# EASY-INSTALL-DEV-SCRIPT: %(spec)r,%(script_name)r\n"
"__requires__ = %(spec)r\n" "__requires__ = %(spec)r\n"
...@@ -585,14 +586,13 @@ Please make the appropriate changes for your system and try again. ...@@ -585,14 +586,13 @@ Please make the appropriate changes for your system and try again.
"__file__ = %(dev_path)r\n" "__file__ = %(dev_path)r\n"
"execfile(__file__)\n" "execfile(__file__)\n"
) % locals() ) % locals()
else: elif is_script:
script_text = get_script_header(script_text) + ( script_text = get_script_header(script_text) + (
"# EASY-INSTALL-SCRIPT: %(spec)r,%(script_name)r\n" "# EASY-INSTALL-SCRIPT: %(spec)r,%(script_name)r\n"
"__requires__ = %(spec)r\n" "__requires__ = %(spec)r\n"
"import pkg_resources\n" "import pkg_resources\n"
"pkg_resources.run_script(%(spec)r, %(script_name)r)\n" "pkg_resources.run_script(%(spec)r, %(script_name)r)\n"
) % locals() ) % locals()
self.write_script(script_name, script_text) self.write_script(script_name, script_text)
def write_script(self, script_name, contents, mode="t", blockers=()): def write_script(self, script_name, contents, mode="t", blockers=()):
...@@ -1401,11 +1401,10 @@ class PthDistributions(Environment): ...@@ -1401,11 +1401,10 @@ class PthDistributions(Environment):
def get_script_header(script_text, executable=sys_executable): def get_script_header(script_text, executable=sys_executable):
"""Create a #! line, getting options (if any) from script_text""" """Create a #! line, getting options (if any) from script_text"""
from distutils.command.build_scripts import first_line_re from distutils.command.build_scripts import first_line_re
first, rest = (script_text+'\n').split('\n',1) first = (script_text+'\n').splitlines()[0]
match = first_line_re.match(first) match = first_line_re.match(first)
options = '' options = ''
if match: if match:
script_text = rest
options = match.group(1) or '' options = match.group(1) or ''
if options: if options:
options = ' '+options options = ' '+options
...@@ -1433,6 +1432,48 @@ def uncache_zipdir(path): ...@@ -1433,6 +1432,48 @@ def uncache_zipdir(path):
return return
def is_python(text, filename='<string>'):
"Is this string a valid Python script?"
try:
compile(text, filename, 'exec')
except SyntaxError:
return False
else:
return True
def is_python_script(script_text, filename):
"""Is this text, as a whole, a Python script? (as opposed to shell/bat/etc.
"""
if script_text.startswith('#!'):
# It begins with a '#!' line, so check if 'python' is in it somewhere
from distutils.command.build_scripts import first_line_re
lines = script_text.splitlines()
if first_line_re.match(lines[0]):
return True # It's got a python "#!" line, consider it Python
else:
return False # It's some other scripting language
if filename.endswith('.py') or filename.endswith('.pyw'):
return True # extension says it's Python
if is_python(script_text, filename):
return True # it's syntactically valid Python
return False # Not any Python I can recognize
def get_script_args(dist, executable=sys_executable): def get_script_args(dist, executable=sys_executable):
"""Yield write_script() argument tuples for a distribution's entrypoints""" """Yield write_script() argument tuples for a distribution's entrypoints"""
spec = str(dist.as_requirement()) spec = str(dist.as_requirement())
......
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