[Python-checkins] python/nondist/sandbox/setuptools/setuptools/command develop.py, 1.5, 1.6 easy_install.py, 1.28, 1.29

pje@users.sourceforge.net pje at users.sourceforge.net
Sat Sep 17 03:13:05 CEST 2005


Update of /cvsroot/python/python/nondist/sandbox/setuptools/setuptools/command
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8383/setuptools/command

Modified Files:
	develop.py easy_install.py 
Log Message:
Added support to solve the infamous "we want .py on Windows, no 
extension elsewhere" problem, while also bypassing the need for PATHEXT
on Windows, and in fact the need to even write script files at all, for
any platform.  Instead, you define "entry points" in your setup script,
in this case the names of the scripts you want (without extensions) and
the functions that should be imported and run to implement the scripts.
Setuptools will then generate platform-appropriate script files at 
install time, including an .exe wrapper when installing on Windows.


Index: develop.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setuptools/setuptools/command/develop.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- develop.py	18 Jul 2005 01:39:45 -0000	1.5
+++ develop.py	17 Sep 2005 01:13:02 -0000	1.6
@@ -101,6 +101,11 @@
             return easy_install.install_egg_scripts(self,dist)
 
         # create wrapper scripts in the script dir, pointing to dist.scripts
+
+        # new-style...
+        self.install_console_scripts(dist)  
+
+        # ...and old-style
         for script_name in self.distribution.scripts or []:
             script_path = os.path.abspath(convert_path(script_name))
             script_name = os.path.basename(script_path)
@@ -116,8 +121,3 @@
 
 
 
-
-
-
-
-

Index: easy_install.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setuptools/setuptools/command/easy_install.py,v
retrieving revision 1.28
retrieving revision 1.29
diff -u -d -r1.28 -r1.29
--- easy_install.py	3 Sep 2005 04:50:05 -0000	1.28
+++ easy_install.py	17 Sep 2005 01:13:02 -0000	1.29
@@ -244,6 +244,19 @@
 
 
 
+    def install_egg_scripts(self, dist):
+        """Write all the scripts for `dist`, unless scripts are excluded"""
+
+        self.install_console_scripts(dist)
+        if self.exclude_scripts or not dist.metadata_isdir('scripts'):
+            return
+
+        for script_name in dist.metadata_listdir('scripts'):
+            self.install_script(
+                dist, script_name,
+                dist.get_metadata('scripts/'+script_name).replace('\r','\n')
+            )
+
     def add_output(self, path):
         if os.path.isdir(path):
             for base, dirs, files in os.walk(path):
@@ -272,19 +285,6 @@
 
 
 
-
-
-
-
-
-
-
-
-
-
-
-
-
     def easy_install(self, spec, deps=False):
         tmpdir = tempfile.mkdtemp(prefix="easy_install-")
         download = None
@@ -408,16 +408,6 @@
             )
 
 
-    def install_egg_scripts(self, dist):
-        if self.exclude_scripts or not dist.metadata_isdir('scripts'):
-            return
-
-        for script_name in dist.metadata_listdir('scripts'):
-            self.install_script(
-                dist, script_name,
-                dist.get_metadata('scripts/'+script_name).replace('\r','\n')
-            )
-
     def should_unzip(self, dist):
         if self.zip_ok is not None:
             return not self.zip_ok
@@ -449,23 +439,63 @@
         ensure_directory(dst); shutil.move(setup_base, dst)
         return dst
 
+
+
+
+
+
+
+
+
+
+
+    def install_console_scripts(self, dist):
+        """Write new-style console scripts, unless excluded"""
+        
+        if self.exclude_scripts:
+            return
+
+        spec = str(dist.as_requirement())
+        group = 'console_scripts'
+
+        for name,ep in dist.get_entry_map(group).items():
+
+            script_text = get_script_header("") + (
+                "# EASY-INSTALL-ENTRY-SCRIPT: %(spec)r,%(group)r,%(name)r\n"
+                "import sys\n"
+                "from pkg_resources import load_entry_point\n"
+                "\n"
+                "sys.exit(\n"
+                "   load_entry_point(%(spec)r, %(group)r, %(name)r)()\n"
+                ")\n"
+            ) % locals()
+
+            if sys.platform=='win32':
+                # On Windows, add a .py extension and an .exe launcher
+                self.write_script(name+'.py', script_text)
+                self.write_script(
+                    name+'.exe', resource_string('setuptools','launcher.exe'),
+                    'b' # write in binary mode
+                )
+            else:
+                # On other platforms, we assume the right thing to do is to
+                # write the stub with no extension.
+                self.write_script(name, script_text)
+
+
+
+
+
+
+
+
+
     def install_script(self, dist, script_name, script_text, dev_path=None):
-        log.info("Installing %s script to %s", script_name,self.script_dir)
-        target = os.path.join(self.script_dir, script_name)
-        first, rest = script_text.split('\n',1)
-        from distutils.command.build_scripts import first_line_re
-        match = first_line_re.match(first)
-        options = ''
-        if match:
-            options = match.group(1) or ''
-            if options:
-                options = ' '+options
+        """Generate a legacy script wrapper and install it"""
         spec = str(dist.as_requirement())
-        executable = os.path.normpath(sys.executable)
 
         if dev_path:
-            script_text = (
-                "#!%(executable)s%(options)s\n"
+            script_text = get_script_header(script_text) + (                
                 "# EASY-INSTALL-DEV-SCRIPT: %(spec)r,%(script_name)r\n"
                 "from pkg_resources import require; require(%(spec)r)\n"
                 "del require\n"
@@ -473,23 +503,34 @@
                 "execfile(__file__)\n"
             ) % locals()
         else:
-            script_text = (
-                "#!%(executable)s%(options)s\n"
+            script_text = get_script_header(script_text) + (                
+                "#!python\n"
                 "# EASY-INSTALL-SCRIPT: %(spec)r,%(script_name)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"):
+        """Write an executable file to the scripts directory"""
+        log.info("Installing %s script to %s", script_name, self.script_dir)
+
+        target = os.path.join(self.script_dir, script_name)
         self.add_output(target)
+
         if not self.dry_run:
             ensure_directory(target)
-            f = open(target,"w")
-            f.write(script_text)
+            f = open(target,"w"+mode)
+            f.write(contents)
             f.close()
             try:
                 os.chmod(target,0755)
             except (AttributeError, os.error):
                 pass
 
+
     def install_eggs(self, spec, dist_filename, tmpdir):
         # .egg dirs or files are already built, so just return them
         if dist_filename.lower().endswith('.egg'):
@@ -1118,26 +1159,26 @@
         Environment.remove(self,dist)
 
 
-def main(argv, **kw):
-    from setuptools import setup
-    setup(script_args = ['-q','easy_install', '-v']+argv, **kw)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+def get_script_header(script_text):
+    """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)
+    match = first_line_re.match(first)
+    options = ''
+    if match:
+        script_text = rest
+        options = match.group(1) or ''
+        if options:
+            options = ' '+options
+    executable = os.path.normpath(sys.executable)
+    return "#!%(executable)s%(options)s\n" % locals()
 
 
+def main(argv=None, **kw):
+    from setuptools import setup
+    if argv is None:
+        argv = sys.argv[1:]
+    setup(script_args = ['-q','easy_install', '-v']+argv, **kw)
 
 
 



More information about the Python-checkins mailing list