Module: PoisePython::Resources::PythonPackage

Defined in:
lib/poise_python/resources/python_package.rb

Overview

A python_package resource to manage Python installations using pip.

Examples:

python_package 'django' do
  python '2'
  version '1.8.3'
end

Since:

  • 1.0.0

Provides:

  • python_package

Actions:

  • install

  • upgrade

  • uninstall

Defined Under Namespace

Classes: Provider, Resource

Constant Summary collapse

PIP_HACK_SCRIPT =

A Python snippet to check which versions of things pip would try to install. Probably not 100% bulletproof.

Since:

  • 1.0.0

<<-EOH
import json
import re
import sys

import pip
# Don't use pkg_resources because I don't want to require it before this anyway.
if re.match(r'0\\.|1\\.|6\\.0', pip.__version__):
  sys.stderr.write('The python_package resource requires pip >= 6.1.0, currently '+pip.__version__+'\\n')
  sys.exit(1)

try:
  from pip.commands import InstallCommand
  from pip.index import PackageFinder
  from pip.req import InstallRequirement
except ImportError:
  # Pip 10 moved all internals to their own package.
  from pip._internal.commands import InstallCommand
  from pip._internal.index import PackageFinder
  from pip._internal.req import InstallRequirement

packages = {}
cmd = InstallCommand()
options, args = cmd.parse_args(sys.argv[1:])
with cmd._build_session(options) as session:
  if options.no_index:
    index_urls = []
  else:
    index_urls = [options.index_url] + options.extra_index_urls
  finder_options = dict(
    find_links=options.find_links,
    index_urls=index_urls,
    allow_all_prereleases=options.pre,
    process_dependency_links=options.process_dependency_links,
    trusted_hosts=options.trusted_hosts,
    session=session,
  )
  if getattr(options, 'format_control', None):
    finder_options['format_control'] = options.format_control
  finder = PackageFinder(**finder_options)
  find_all = getattr(finder, 'find_all_candidates', getattr(finder, '_find_all_versions', None))
  for arg in args:
    req = InstallRequirement.from_line(arg)
    found = finder.find_requirement(req, True)
    all_candidates = find_all(req.name)
    candidate = [c for c in all_candidates if c.location == found]
    if candidate:
      packages[candidate[0].project.lower()] = str(candidate[0].version)
json.dump(packages, sys.stdout)
EOH