Documentation for the pip accelerator API

The Python module pip_accel defines the classes and functions that implement the functionality of the pip accelerator and the pip-accel command. Instead of using the pip-accel command you can also use the pip accelerator as a Python module. In this case you’ll probably want to start by taking a look at the following functions:

class pip_accel.CustomInstallCommand(*args, **kw)

Required to call pip as a Python module instead of a subprocess.

The pip.commands.install.InstallCommand.run() method returns a RequirementSet object which pip-accel is interested in, however pip.basecommand.Command.main() swallows the requirement set (based on my reading of the pip 1.3.x source code).

To work around the problem described above, we subclass InstallCommand to wrap the run() method. Yes this is a bit sneaky, but I don’t fancy reimplementing pip.basecommand.Command.main() inside pip-accel!

class pip_accel.Timer

Easy to use timer to keep track of long during operations.

elapsed_time

Get the number of seconds elapsed since the timer object was created.

pip_accel.add_extension(download_path, archive_path)

Make sure all cached source distributions have the right file extension, because not all distribution sites provide URLs with proper filenames in them while we really need the proper filenames to build the local source index.

Parameters:
  • download_path – The pathname of the source distribution archive in the download cache.
  • archive_path – The pathname of the distribution archive in the source index directory.
Returns:

The (possibly modified) pathname of the distribution archive in the source index directory.

Previously this used the file executable, now it checks the magic file headers itself. I could have used any of the numerous libmagic bindings on PyPI, but that would add a binary dependency to pip-accel and I don’t want that :-).

pip_accel.build_binary_dists(requirements)

Convert source distributions to binary distributions.

Parameters:requirements – A list of tuples in the format of the return value of the unpack_source_dists() function.
Returns:True if it succeeds in building a binary distribution, False otherwise (probably because of missing binary dependencies like system libraries).
pip_accel.cache_binary_distribution(input_path, output_path)

Transform a binary distribution archive created with python setup.py bdist_dumb --format=gztar into a form that can be cached for future use. This comes down to making the pathnames inside the archive relative to the prefix that the binary distribution was built for.

Parameters:
  • input_path – The pathname of the original binary distribution archive
  • output_path – The pathname of the binary distribution in the cache directory.
pip_accel.download_source_dists(arguments)

Download missing source distributions.

Parameters:arguments – A list with the arguments intended for pip.
pip_accel.ensure_parsed_requirement(install_requirement)

InstallRequirement objects in RequirementSet objects have a req member, which apparently can be either a string or a pkg_resources.Requirement object. This function makes sure we’re dealing with a pkg_resources.Requirement object.

This was “copied” from the pip source code, I’m not sure if this code is actually necessary but it doesn’t hurt and pip probably did it for a reason. Right? :-)

Parameters:install_requirement – An InstallRequirement object produced by pip.
Returns:A pkg_resources.Requirement object.
pip_accel.expanduser(pathname)

Variant of os.path.expanduser() that doesn’t use $HOME but instead uses the home directory of the effective user id. This is basically a workaround for sudo -s not resetting $HOME.

Parameters:pathname – A pathname that may start with ~/, indicating the path should be interpreted as being relative to the home directory of the current (effective) user.
pip_accel.find_binary_dists()

Find all previously cached binary distributions.

Returns:A dictionary with (package-name, package-version, python-version) tuples as keys and pathnames of binary archives as values.
pip_accel.fix_hashbang(python, contents)

Rewrite the hashbang in an executable script so that the Python program inside the virtual environment is used instead of a system wide Python.

Parameters:
  • python – The absolute pathname of the Python program inside the virtual environment.
  • contents – A string with the contents of the script whose hashbang should be fixed.
Returns:

The modified contents of the script as a string.

pip_accel.get_python_version()

Return a string identifying the currently running Python version.

Returns:A string like “py2.6” or “py2.7” containing a short mnemonic prefix followed by the major and minor version numbers.
pip_accel.initialize_directories()

Create the directories for the download cache, the source index and the binary index if any of them don’t exist yet and reset the binary index when its format changes.

pip_accel.install_binary_dist(filename, install_prefix='/home/docs')

Install a binary distribution created with python setup.py bdist into the given prefix (a directory like /usr, /usr/local or a virtual environment).

Parameters:
  • filename – The pathname of the tar archive.
  • install_prefix – The “prefix” under which the requirements should be installed. This will be a pathname like /usr, /usr/local or the pathname of a virtual environment.
pip_accel.install_requirements(requirements, install_prefix='/home/docs')

Manually install all requirements from binary distributions.

Parameters:
  • requirements – A list of tuples in the format of the return value of unpack_source_dists().
  • install_prefix – The “prefix” under which the requirements should be installed. This will be a pathname like /usr, /usr/local or the pathname of a virtual environment.
Returns:

True if it succeeds in installing all requirements from binary distribution archives, False otherwise.

pip_accel.main()

Main logic of the pip-accel command.

pip_accel.print_usage()

Report the usage of the pip-accel command to the console.

pip_accel.run_pip(arguments, use_remote_index)

Execute a modified pip install command. This function assumes that the arguments concern a pip install command (main() makes sure of this).

Parameters:
  • arguments – A list of strings containing the arguments that will be passed to pip.
  • use_remote_index – A boolean indicating whether pip is allowed to contact http://pypi.python.org.
Returns:

A RequirementSet object created by pip, unless an exception is raised by pip (in which case the exception will bubble up).

pip_accel.sorted_requirements(requirement_set)

Sort the requirements in a RequirementSet.

Parameters:requirement_set – A RequirementSet object produced by pip.
Returns:A list of sorted InstallRequirement objects.
pip_accel.unpack_source_dists(arguments)

Check whether there are local source distributions available for all requirements, unpack the source distribution archives and find the names and versions of the requirements. By using the pip install --no-install command we avoid reimplementing the following pip features:

  • Parsing of requirements.txt (including recursive parsing)
  • Resolution of possibly conflicting pinned requirements
  • Unpacking source distributions in multiple formats
  • Finding the name & version of a given source distribution
Parameters:arguments – A list of strings with the command line arguments to be passed to the pip command.
Returns:A list of tuples with three strings each: The name of a requirement (package), its version number and the directory where the unpacked source distribution is located. If pip fails, an exception will be raised by pip.
pip_accel.update_source_dists_index()

Link newly downloaded source distributions into the local index directory using symbolic links.

Project Versions

Previous topic

pip-accel: Accelerator for pip, the Python package manager

This Page