2008-08-16 23:26:59 +08:00
====================================
2010-07-27 03:15:15 +08:00
Miscellaneous features of the py lib
2008-08-16 23:26:59 +08:00
====================================
2010-07-27 03:15:15 +08:00
Mapping the standard python library into py
2008-08-16 23:26:59 +08:00
===========================================
2010-07-27 03:15:15 +08:00
The ``py.std`` object allows lazy access to
2009-04-02 15:52:32 +08:00
standard library modules. For example, to get to the print-exception
2010-07-27 03:15:15 +08:00
functionality of the standard library you can write::
2008-08-16 23:26:59 +08:00
py.std.traceback.print_exc()
2010-07-27 03:15:15 +08:00
without having to do anything else than the usual ``import py``
2009-04-02 15:52:32 +08:00
at the beginning. You can access any other top-level standard
2010-07-27 03:15:15 +08:00
library module this way. This means that you will only trigger
2009-04-02 15:52:32 +08:00
imports of modules that are actually needed. Note that no attempt
2010-07-27 03:15:15 +08:00
is made to import submodules.
2008-08-16 23:26:59 +08:00
Support for interaction with system utilities/binaries
======================================================
Currently, the py lib offers two ways to interact with
2009-11-02 20:00:48 +08:00
system executables. ``py.process.cmdexec()`` invokes
2008-08-16 23:26:59 +08:00
the shell in order to execute a string. The other
2010-07-27 03:15:15 +08:00
one, ``py.path.local``'s 'sysexec()' method lets you
directly execute a binary.
2008-08-16 23:26:59 +08:00
Both approaches will raise an exception in case of a return-
2010-07-27 03:15:15 +08:00
code other than 0 and otherwise return the stdout-output
2008-08-16 23:26:59 +08:00
of the child process.
2010-07-27 03:15:15 +08:00
The shell based approach
2008-08-16 23:26:59 +08:00
------------------------
2010-07-27 03:15:15 +08:00
You can execute a command via your system shell
by doing something like::
2008-08-16 23:26:59 +08:00
out = py.process.cmdexec('ls -v')
2010-07-27 03:15:15 +08:00
However, the ``cmdexec`` approach has a few shortcomings:
2008-08-16 23:26:59 +08:00
- it relies on the underlying system shell
- it neccessitates shell-escaping for expressing arguments
2010-07-27 03:15:15 +08:00
- it does not easily allow to "fix" the binary you want to run.
- it only allows to execute executables from the local
filesystem
2008-08-16 23:26:59 +08:00
2010-07-27 03:15:15 +08:00
.. _sysexec:
2008-08-16 23:26:59 +08:00
local paths have ``sysexec``
2010-07-27 03:15:15 +08:00
----------------------------
2008-08-16 23:26:59 +08:00
2010-07-27 03:15:15 +08:00
In order to synchronously execute an executable file you
2009-04-03 02:59:31 +08:00
can use ``sysexec``::
2008-08-16 23:26:59 +08:00
2010-07-27 03:15:15 +08:00
binsvn.sysexec('ls', 'http://codespeak.net/svn')
2008-08-16 23:26:59 +08:00
where ``binsvn`` is a path that points to the ``svn`` commandline
2009-04-03 02:59:31 +08:00
binary. Note that this function does not offer any shell-escaping
2010-07-27 03:15:15 +08:00
so you have to pass in already separated arguments.
2008-08-16 23:26:59 +08:00
finding an executable local path
--------------------------------
2010-07-27 03:15:15 +08:00
Finding an executable is quite different on multiple platforms.
2008-08-16 23:26:59 +08:00
Currently, the ``PATH`` environment variable based search on
2010-07-27 03:15:15 +08:00
unix platforms is supported::
2008-08-16 23:26:59 +08:00
py.path.local.sysfind('svn')
2010-07-27 03:15:15 +08:00
which returns the first path whose ``basename`` matches ``svn``.
2008-08-16 23:26:59 +08:00
In principle, `sysfind` deploys platform specific algorithms
to perform the search. On Windows, for example, it may look
2010-07-27 03:15:15 +08:00
at the registry (XXX).
2008-08-16 23:26:59 +08:00
2010-07-27 03:15:15 +08:00
To make the story complete, we allow to pass in a second ``checker``
argument that is called for each found executable. For example, if
2008-08-16 23:26:59 +08:00
you have multiple binaries available you may want to select the
2010-07-27 03:15:15 +08:00
right version::
2008-08-16 23:26:59 +08:00
def mysvn(p):
""" check that the given svn binary has version 1.1. """
line = p.execute('--version'').readlines()[0]
2010-07-27 03:15:15 +08:00
if line.find('version 1.1'):
return p
binsvn = py.path.local.sysfind('svn', checker=mysvn)
2008-08-16 23:26:59 +08:00
Cross-Python Version compatibility helpers
=============================================
2009-11-02 20:00:48 +08:00
The ``py.builtin`` namespace provides a number of helpers that help to write python code compatible across Python interpreters, mainly Python2 and Python3. Type ``help(py.builtin)`` on a Python prompt for a the selection of builtins.