1
1

More than 5 years have passed since last update.

pip search に自分好みの表示機能を加える

Last updated at Posted at 2015-01-08

こんにちは。
python のライブラリモジュールを(pypi 内で)検索するには下記のように pip コマンドを利用して検索結果を得ますが、加えて下記のような結果表示も加えたくなりました。

$ pip search cvxopt
cvxopt                    - Convex optimization package
  INSTALLED: 1.1.7 (latest)
  https://pypi.python.org/pypi/cvxopt   <== この表示を加えたい

$ pip search pandas
pandas                    - Powerful data structures for data analysis, time series,and statistics
  LATEST:    0.15.2 (not installed)     <== この表示を加えたい
  https://pypi.python.org/pypi/pandas   <== この表示を加えたい

そこで、pip (v.6.0.x) の search に少し手を加えて実現してみました。

$ ./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]
+
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1