[Checkins] SVN: zc.buildout/branches/gary-4/ this exposes the ability to request that site be imported in the ``script`` function. A later commit will expose this in the buildout configuration choices.

Gary Poster gary.poster at canonical.com
Mon Dec 14 11:48:10 EST 2009


Log message for revision 106496:
  this exposes the ability to request that site be imported in the ``script`` function. A later commit will expose this in the buildout configuration choices.

Changed:
  U   zc.buildout/branches/gary-4/src/zc/buildout/easy_install.py
  U   zc.buildout/branches/gary-4/src/zc/buildout/easy_install.txt
  U   zc.buildout/branches/gary-4/src/zc/buildout/update.txt
  U   zc.buildout/branches/gary-4/zc.recipe.egg_/src/zc/recipe/egg/README.txt

-=-
Modified: zc.buildout/branches/gary-4/src/zc/buildout/easy_install.py
===================================================================
--- zc.buildout/branches/gary-4/src/zc/buildout/easy_install.py	2009-12-14 16:45:15 UTC (rev 106495)
+++ zc.buildout/branches/gary-4/src/zc/buildout/easy_install.py	2009-12-14 16:48:10 UTC (rev 106496)
@@ -914,6 +914,7 @@
             interpreter=None,
             initialization='',
             relative_paths=False,
+            import_site=False,
             ):
 
     path = [dist.location for dist in working_set]
@@ -956,16 +957,29 @@
 
         generated.extend(
             _script(module_name, attrs, spath, sname, executable, arguments,
-                    initialization, rpsetup)
+                    initialization, rpsetup, import_site)
             )
 
     if interpreter:
         sname = os.path.join(dest, interpreter)
         spath, rpsetup = _relative_path_and_setup(sname, path, relative_paths)
-        generated.extend(_pyscript(spath, sname, executable, rpsetup))
+        generated.extend(
+            _pyscript(spath, sname, executable, rpsetup, import_site))
 
     return generated
 
+import_site_snippet = '''\
+# We have to import pkg_resources before namespace
+# package .pth files are processed or else the distribution's namespace
+# packages will mask all of the egg-based packages in the same namespace
+# package.
+try:
+  import pkg_resources
+except ImportError:
+  pass
+import site
+'''
+
 def _relative_path_and_setup(sname, path, relative_paths):
     if relative_paths:
         relative_paths = os.path.normcase(relative_paths)
@@ -1028,11 +1042,15 @@
 """
 
 def _script(module_name, attrs, path, dest, executable, arguments,
-            initialization, rsetup):
+            initialization, rsetup, import_site):
     generated = []
     script = dest
     if is_win32:
         dest += '-script.py'
+    if import_site:
+        import_site = import_site_snippet
+    else:
+        import_site = ''
 
     contents = script_template % dict(
         python = _safe_arg(executable),
@@ -1042,6 +1060,7 @@
         arguments = arguments,
         initialization = initialization,
         relative_paths_setup = rsetup,
+        import_site=import_site,
         )
     changed = not (os.path.exists(dest) and open(dest).read() == contents)
 
@@ -1079,6 +1098,7 @@
 sys.path[0:0] = [
   %(path)s,
   ]
+%(import_site)s
 %(initialization)s
 import %(module_name)s
 
@@ -1087,16 +1107,21 @@
 '''
 
 
-def _pyscript(path, dest, executable, rsetup):
+def _pyscript(path, dest, executable, rsetup, import_site):
     generated = []
     script = dest
     if is_win32:
         dest += '-script.py'
+    if import_site:
+        import_site = import_site_snippet
+    else:
+        import_site = ''
 
     contents = py_script_template % dict(
         python = _safe_arg(executable),
         path = path,
         relative_paths_setup = rsetup,
+        import_site=import_site
         )
     changed = not (os.path.exists(dest) and open(dest).read() == contents)
 
@@ -1127,7 +1152,7 @@
 sys.path[0:0] = [
   %(path)s,
   ]
-
+%(import_site)s
 _interactive = True
 if len(sys.argv) > 1:
     _options, _args = __import__("getopt").getopt(sys.argv[1:], 'ic:m:')

Modified: zc.buildout/branches/gary-4/src/zc/buildout/easy_install.txt
===================================================================
--- zc.buildout/branches/gary-4/src/zc/buildout/easy_install.txt	2009-12-14 16:45:15 UTC (rev 106495)
+++ zc.buildout/branches/gary-4/src/zc/buildout/easy_install.txt	2009-12-14 16:48:10 UTC (rev 106496)
@@ -635,6 +635,42 @@
 distributions) that don't declare their entry points, such as
 distributions that aren't based on setuptools.
 
+As you can see by the shebang (first) line of the script, Python is
+invoked with -S.  This means that site.py is not imported, which in turn
+means that site-packages are not part of the path.  This is the safest
+approach, and let's you easily use a system Python to do buildout-based
+development.  Note that, because of the setuptools-provided .exe files
+that buildout uses to run scripts, it also works on Windows.
+
+However, if desired, you can also specify that site.py should be
+imported, using the ``import_site`` argument.  This is done carefully,
+as you can see in the comment below.
+
+    >>> scripts = zc.buildout.easy_install.scripts(
+    ...     ['demo'], ws, sys.executable, bin, import_site=True)
+    >>> cat(bin, 'demo') # doctest: +NORMALIZE_WHITESPACE
+    #!/usr/local/bin/python2.4 -S
+    <BLANKLINE>
+    import sys
+    sys.path[0:0] = [
+      '/sample-install/demo-0.3-py2.4.egg',
+      '/sample-install/demoneeded-1.1-py2.4.egg',
+      ]
+    # We have to import pkg_resources before namespace
+    # package .pth files are processed or else the distribution's namespace
+    # packages will mask all of the egg-based packages in the same namespace
+    # package.
+    try:
+      import pkg_resources
+    except ImportError:
+      pass
+    import site
+    <BLANKLINE>
+    import eggrecipedemo
+    <BLANKLINE>
+    if __name__ == '__main__':
+        eggrecipedemo.main()
+
 The interpreter keyword argument can be used to generate a script that can
 be used to invoke the Python interactive interpreter with the path set
 based on the working set.  This generated script can also be used to
@@ -716,6 +752,56 @@
     >>> print system(join(bin, 'py')+' -m pdb what'),
     Error: what does not exist
 
+The interpreter script generation also honors the ``import_site`` argument
+described above.
+
+    >>> scripts = zc.buildout.easy_install.scripts(
+    ...     ['demo'], ws, sys.executable, bin, interpreter='py',
+    ...     import_site=True)
+    >>> cat(bin, 'py') # doctest: +NORMALIZE_WHITESPACE
+    #!/usr/local/bin/python2.4 -S
+    <BLANKLINE>
+    import sys
+    <BLANKLINE>
+    sys.path[0:0] = [
+      '/sample-install/demo-0.3-pyN.N.egg',
+      '/sample-install/demoneeded-1.1-pyN.N.egg',
+      ]
+    # We have to import pkg_resources before namespace
+    # package .pth files are processed or else the distribution's namespace
+    # packages will mask all of the egg-based packages in the same namespace
+    # package.
+    try:
+      import pkg_resources
+    except ImportError:
+      pass
+    import site
+    <BLANKLINE>
+    _interactive = True
+    if len(sys.argv) > 1:
+        _options, _args = __import__("getopt").getopt(sys.argv[1:], 'ic:m:')
+        _interactive = False
+        for (_opt, _val) in _options:
+            if _opt == '-i':
+                _interactive = True
+            elif _opt == '-c':
+                exec _val
+            elif _opt == '-m':
+                sys.argv[1:] = _args
+                _args = []
+                __import__("runpy").run_module(
+                     _val, {}, "__main__", alter_sys=True)
+    <BLANKLINE>
+        if _args:
+            sys.argv[:] = _args
+            __file__ = _args[0]
+            del _options, _args
+            execfile(__file__)
+    <BLANKLINE>
+    if _interactive:
+        del _interactive
+        __import__("code").interact(banner="", local=globals())
+
 An additional argument can be passed to define which scripts to install
 and to provide script names. The argument is a dictionary mapping
 original script names to new script names.
@@ -853,6 +939,7 @@
       join(base, 'bar'),
       ]
     <BLANKLINE>
+    <BLANKLINE>
     import eggrecipedemo
     <BLANKLINE>
     if __name__ == '__main__':

Modified: zc.buildout/branches/gary-4/src/zc/buildout/update.txt
===================================================================
--- zc.buildout/branches/gary-4/src/zc/buildout/update.txt	2009-12-14 16:45:15 UTC (rev 106495)
+++ zc.buildout/branches/gary-4/src/zc/buildout/update.txt	2009-12-14 16:48:10 UTC (rev 106496)
@@ -89,6 +89,7 @@
       '/sample-buildout/eggs/setuptools-99.99-py2.4.egg',
       ]
     <BLANKLINE>
+    <BLANKLINE>
     import zc.buildout.buildout
     <BLANKLINE>
     if __name__ == '__main__':

Modified: zc.buildout/branches/gary-4/zc.recipe.egg_/src/zc/recipe/egg/README.txt
===================================================================
--- zc.buildout/branches/gary-4/zc.recipe.egg_/src/zc/recipe/egg/README.txt	2009-12-14 16:45:15 UTC (rev 106495)
+++ zc.buildout/branches/gary-4/zc.recipe.egg_/src/zc/recipe/egg/README.txt	2009-12-14 16:48:10 UTC (rev 106496)
@@ -383,6 +383,7 @@
       '/sample-buildout/spam',
       ]
     <BLANKLINE>
+    <BLANKLINE>
     import eggrecipedemo
     <BLANKLINE>
     if __name__ == '__main__':
@@ -588,6 +589,7 @@
       '/sample-buildout/spam',
       ]
     <BLANKLINE>
+    <BLANKLINE>
     import foo.bar
     <BLANKLINE>
     if __name__ == '__main__':



More information about the checkins mailing list