Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle pip package versions like pip does [rev2] #909

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@
kwargs['name'] += '_modules'
kwargs['entry_points'] = {}
else:
kwargs['install_requires'] += ['catkin_pkg >= 0.4.0', 'rospkg >= 1.4.0', 'rosdistro >= 0.7.5']
kwargs['install_requires'] += ['catkin_pkg >= 0.4.0', 'rospkg >= 1.4.0', 'rosdistro >= 0.7.5', 'packaging']

setup(**kwargs)
62 changes: 55 additions & 7 deletions src/rosdep2/platforms/pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import subprocess
import sys

from packaging.requirements import Requirement
from packaging.version import InvalidVersion, parse
from ..core import InstallFailed
from ..installers import PackageManagerInstaller
from ..shell_utils import read_stdout
Expand Down Expand Up @@ -77,9 +79,24 @@ def is_cmd_available(cmd):
return False


def parse_version(version_str):
"""
Given a textual representation of a Python package version, return its parsed representation.

:param version_str: The textual representation of the version.
:return: The parsed representation of None if the version cannot be parsed.
:rtype: packaging.version.Version or packaging.version.LegacyVersion or None
"""
try:
return parse(version_str)
except InvalidVersion:
return None


def pip_detect(pkgs, exec_fn=None):
"""
Given a list of package, return the list of installed packages.
Given a list of package specifications, return the list of installed
packages which meet the specifications.

:param exec_fn: function to execute Popen and read stdout (for testing)
"""
Expand All @@ -92,31 +109,53 @@ def pip_detect(pkgs, exec_fn=None):
exec_fn = read_stdout
fallback_to_pip_show = True
pkg_list = exec_fn(pip_cmd + ['freeze']).split('\n')
pkg_list = [p for p in pkg_list if len(p) > 0]

ret_list = []
version_list = []
req_list = []

for pkg in pkg_list:
pkg_row = pkg.split('==')
if pkg_row[0] in pkgs:
ret_list.append(pkg_row[0])

# Account for some unusual instances of === instead of ==
pkg_row[1] = pkg_row[1].strip('=')

version_list.append((pkg_row[0], parse_version(pkg_row[1])))

for pkg in pkgs:
req_list.append(Requirement(pkg))

for req in req_list:
for pkg in [ver for ver in version_list if ver[0] == req.name]:
if pkg[1] is None or pkg[1] in req.specifier:
ret_list.append(req.name)

# Try to detect with the return code of `pip show`.
# This can show the existance of things like `argparse` which
# otherwise do not show up.
# See:
# https://github.com/pypa/pip/issues/1570#issuecomment-71111030
if fallback_to_pip_show:
for pkg in [p for p in pkgs if p not in ret_list]:
for req in [r for r in req_list if r.name not in ret_list]:
# does not see retcode but stdout for old pip to check if installed
proc = subprocess.Popen(
pip_cmd + ['show', pkg],
pip_cmd + ['show', req.name],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
output, _ = proc.communicate()
output = output.strip()
if proc.returncode == 0 and output:
# `pip show` detected it, add it to the list.
ret_list.append(pkg)
# `pip show` detected it, check the version.
show_split = output.split('\n')

for line in [s for s in show_split if s.startswith('Version:')]:
version = parse_version(line.strip().split()[1])

if version is None or version in req.specifier:
# version matches, add it to the list
ret_list.append(req.name)

return ret_list

Expand All @@ -138,6 +177,15 @@ def get_version_strings(self):
]
return version_strings

def get_packages_to_install(self, resolved, reinstall=False):
if reinstall:
return resolved
if not resolved:
return []
else:
detected = self.detect_fn(resolved)
return [x for x in resolved if Requirement(x).name not in detected]

def get_install_command(self, resolved, interactive=True, reinstall=False, quiet=False):
pip_cmd = get_pip_command()
if not pip_cmd:
Expand Down
4 changes: 2 additions & 2 deletions stdeb.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ X-Python3-Version: >= 3.6
Setup-Env-Vars: SKIP_PYTHON_MODULES=1

[rosdep_modules]
Depends: ca-certificates, python-rospkg-modules (>= 1.4.0), python-yaml, python-catkin-pkg-modules (>= 0.4.0), python-rosdistro-modules (>= 0.7.5), python-setuptools, sudo
Depends3: ca-certificates, python3-rospkg-modules (>= 1.4.0), python3-yaml, python3-catkin-pkg-modules (>= 0.4.0), python3-rosdistro-modules (>= 0.7.5), python3-setuptools, sudo
Depends: ca-certificates, python-rospkg-modules (>= 1.4.0), python-yaml, python-catkin-pkg-modules (>= 0.4.0), python-rosdistro-modules (>= 0.7.5), python-setuptools, python-packaging, sudo
Depends3: ca-certificates, python3-rospkg-modules (>= 1.4.0), python3-yaml, python3-catkin-pkg-modules (>= 0.4.0), python3-rosdistro-modules (>= 0.7.5), python3-setuptools, python3-packaging, sudo
Conflicts: python-rosdep (<< 0.18.0), python-rosdep2
Conflicts3: python3-rosdep (<< 0.18.0), python3-rosdep2
Replaces: python-rosdep (<< 0.18.0)
Expand Down
15 changes: 15 additions & 0 deletions test/test_rosdep_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ def test_pip_detect():
val = pip_detect(['paramiko', 'fakito', 'pycrypto'], exec_fn=m)
assert val == ['paramiko', 'pycrypto'], val

val = pip_detect(['paramiko', 'Brlapi==0.5.4', 'pycrypto'], exec_fn=m)
assert val == ['paramiko', 'Brlapi', 'pycrypto'], val

val = pip_detect(['paramiko', 'Brlapi==0.5.5', 'pycrypto'], exec_fn=m)
assert val == ['paramiko', 'pycrypto'], val

val = pip_detect(['Brlapi>0.5.*'], exec_fn=m)
assert val == ['Brlapi'], val

val = pip_detect(['Brlapi>0.5.*,<0.6'], exec_fn=m)
assert val == ['Brlapi'], val

val = pip_detect(['Brlapi>0.5.*,<0.5.4'], exec_fn=m)
assert val == [], val


def test_PipInstaller_get_depends():
# make sure PipInstaller supports depends
Expand Down