Commit 9aee1376 authored by Gary Poster's avatar Gary Poster

this exposes the ability to request that site be imported in the ``script``...

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.
parent 696b86d0
......@@ -914,6 +914,7 @@ def scripts(reqs, working_set, executable, dest,
interpreter=None,
initialization='',
relative_paths=False,
import_site=False,
):
path = [dist.location for dist in working_set]
......@@ -956,16 +957,29 @@ def scripts(reqs, working_set, executable, dest,
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 @@ base = os.path.dirname(os.path.abspath(os.path.realpath(__file__)))
"""
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 @@ def _script(module_name, attrs, path, dest, executable, arguments,
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 @@ import sys
sys.path[0:0] = [
%(path)s,
]
%(import_site)s
%(initialization)s
import %(module_name)s
......@@ -1087,16 +1107,21 @@ if __name__ == '__main__':
'''
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 @@ import sys
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:')
......
......@@ -635,6 +635,42 @@ Passing entry-point information directly is handy when using eggs (or
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 @@ module:
>>> 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 @@ to pass a common base directory of the scripts and eggs:
join(base, 'bar'),
]
<BLANKLINE>
<BLANKLINE>
import eggrecipedemo
<BLANKLINE>
if __name__ == '__main__':
......
......@@ -89,6 +89,7 @@ Our buildout script has been updated to use the new eggs:
'/sample-buildout/eggs/setuptools-99.99-py2.4.egg',
]
<BLANKLINE>
<BLANKLINE>
import zc.buildout.buildout
<BLANKLINE>
if __name__ == '__main__':
......
......@@ -383,6 +383,7 @@ Let's look at the script that was generated:
'/sample-buildout/spam',
]
<BLANKLINE>
<BLANKLINE>
import eggrecipedemo
<BLANKLINE>
if __name__ == '__main__':
......@@ -588,6 +589,7 @@ declare entry points using the entry-points option:
'/sample-buildout/spam',
]
<BLANKLINE>
<BLANKLINE>
import foo.bar
<BLANKLINE>
if __name__ == '__main__':
......
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