Add your favorite display function to pip search

Hello. To search for a python library module (within pypi), use the pip command as shown below to get the search results, but I also wanted to add the following result display.

$ pip search cvxopt
cvxopt                    - Convex optimization package
  INSTALLED: 1.1.7 (latest)
  https://pypi.python.org/pypi/cvxopt   <==I want to add this display

$ pip search pandas
pandas                    - Powerful data structures for data analysis, time series,and statistics
  LATEST:    0.15.2 (not installed)     <==I want to add this display
  https://pypi.python.org/pypi/pandas   <==I want to add this display

So, I modified the search of pip (v.6.0.x) a little and realized it.

$ ./mypatch.rb search.py.patch

mypatch.rb


#!/usr/bin/ruby
# coding: utf-8
cmd = "patch -b -p0 < "
ARGV.each {|file|
    `#{cmd + file + " 2>&1"}`  # for sh/bash
}

search.py.patch


--- /usr/local/lib/python2.7/site-packages/pip/commands/search.py
+++ search.py.orig
@@ -47,7 +47,7 @@
         if sys.stdout.isatty():
             terminal_width = get_terminal_size()[0]
 
-        print_results(hits, terminal_width=terminal_width)
+        print_results(hits, query, terminal_width=terminal_width)
         if pypi_hits:
             return SUCCESS
         return NO_MATCHES_FOUND
@@ -101,7 +101,7 @@
     return package_list
 
 
-def print_results(hits, name_column_width=25, terminal_width=None):
+def print_results(hits, query, name_column_width=25, terminal_width=None):
     installed_packages = [p.project_name for p in pkg_resources.working_set]
     for hit in hits:
         name = hit['name']
@@ -125,6 +125,11 @@
                     else:
                         logger.info('INSTALLED: %s', dist.version)
                         logger.info('LATEST:    %s', latest)
+            elif in_case_insensitive(name, query):
+                latest = highest_version(hit['versions'])
+                logger.info('  LATEST:    %s (not installed)' % latest)
+            if in_case_insensitive(name, query):
+                logger.info('  %s/%s' % (PyPI.pypi_url, name))
         except UnicodeEncodeError:
             pass
 
@@ -133,3 +138,7 @@
     return next(iter(
         sorted(versions, key=pkg_resources.parse_version, reverse=True)
     ))
+
+def in_case_insensitive(x, list):
+    return x.lower() in [y.lower() for y in list]
+

Recommended Posts

Add your favorite display function to pip search
Add a GPIO board to your computer. (1)
Add your own content view to mitmproxy
Want to add type hints to your Python decorator?
How to add a Python module search path
Steps to install your own library with pip
Add CSV export function to management screen with django-import-export
Wagtail Recommendation (5) Let's add your own block to StreamField
Python> function> Docstrings> Description to display with help () / .__ doc__