summaryrefslogtreecommitdiff
path: root/setuptools/command
diff options
context:
space:
mode:
authorPJ Eby <distutils-sig@python.org>2006-06-15 20:18:28 +0000
committerPJ Eby <distutils-sig@python.org>2006-06-15 20:18:28 +0000
commit3df0e5d77cbc397627f0af7f20fdcf34af199caa (patch)
treeb7039f543096058c2cb30774208f48a305c7adc7 /setuptools/command
parentde6067f9bc92f65f5f95dd242522958205f5f848 (diff)
downloadpython-setuptools-git-3df0e5d77cbc397627f0af7f20fdcf34af199caa.tar.gz
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
Diffstat (limited to 'setuptools/command')
-rwxr-xr-xsetuptools/command/easy_install.py51
1 files changed, 46 insertions, 5 deletions
diff --git a/setuptools/command/easy_install.py b/setuptools/command/easy_install.py
index eff60cf8..26f59e1a 100755
--- a/setuptools/command/easy_install.py
+++ b/setuptools/command/easy_install.py
@@ -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):
"""Generate a legacy script wrapper and install it"""
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) + (
"# EASY-INSTALL-DEV-SCRIPT: %(spec)r,%(script_name)r\n"
"__requires__ = %(spec)r\n"
@@ -585,14 +586,13 @@ Please make the appropriate changes for your system and try again.
"__file__ = %(dev_path)r\n"
"execfile(__file__)\n"
) % locals()
- else:
+ elif is_script:
script_text = get_script_header(script_text) + (
"# EASY-INSTALL-SCRIPT: %(spec)r,%(script_name)r\n"
"__requires__ = %(spec)r\n"
"import pkg_resources\n"
"pkg_resources.run_script(%(spec)r, %(script_name)r)\n"
) % locals()
-
self.write_script(script_name, script_text)
def write_script(self, script_name, contents, mode="t", blockers=()):
@@ -1401,11 +1401,10 @@ class PthDistributions(Environment):
def get_script_header(script_text, executable=sys_executable):
"""Create a #! line, getting options (if any) from script_text"""
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)
options = ''
if match:
- script_text = rest
options = match.group(1) or ''
if options:
options = ' '+options
@@ -1433,6 +1432,48 @@ def uncache_zipdir(path):
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):
"""Yield write_script() argument tuples for a distribution's entrypoints"""
spec = str(dist.as_requirement())