[Python-checkins] r46985 - sandbox/trunk/setuptools/setuptools/command/easy_install.py

phillip.eby python-checkins at python.org
Thu Jun 15 22:16:16 CEST 2006


Author: phillip.eby
Date: Thu Jun 15 22:16:16 2006
New Revision: 46985

Modified:
   sandbox/trunk/setuptools/setuptools/command/easy_install.py
Log:
Implement detection of non-Python scripts, as described in
http://mail.python.org/pipermail/distutils-sig/2006-June/006359.html


Modified: sandbox/trunk/setuptools/setuptools/command/easy_install.py
==============================================================================
--- sandbox/trunk/setuptools/setuptools/command/easy_install.py	(original)
+++ sandbox/trunk/setuptools/setuptools/command/easy_install.py	Thu Jun 15 22:16:16 2006
@@ -575,8 +575,9 @@
     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 @@
                 "__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 @@
 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 @@
                 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())


More information about the Python-checkins mailing list