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

[feature] pip search by a module's name #5556

Closed
kkdd opened this issue Jul 1, 2018 · 3 comments
Closed

[feature] pip search by a module's name #5556

kkdd opened this issue Jul 1, 2018 · 3 comments
Labels
C: search 'pip search' type: feature request Request for a new feature

Comments

@kkdd
Copy link

kkdd commented Jul 1, 2018

Hello,
I would appreciate that pip search <module> has the following options:
(1) not only by name and summary but just by name
(2) exact matiching by name (with case insensitiveness)

Any better idea?

Describe the solution you'd like

--- pip/_internal/commands/search.py.orig
+++ pip/_internal/commands/search.py
@@ -38,6 +38,14 @@
             metavar='URL',
             default=PyPI.pypi_url,
             help='Base URL of Python Package Index (default %default)')
+        self.cmd_opts.add_option(
+            '-n', '--name',
+            action="store_true", default=False, dest='matchName',
+            help='Search for packages by name only')
+        self.cmd_opts.add_option(
+            '-e', '--exact',
+            action="store_true", default=False, dest='exactName',
+            help='Search for packages by exactly-matched name')
 
         self.parser.insert_option_group(0, self.cmd_opts)
 
@@ -52,7 +60,7 @@
         if sys.stdout.isatty():
             terminal_width = get_terminal_size()[0]
 
-        print_results(hits, terminal_width=terminal_width)
+        print_results(hits, options, terminal_width=terminal_width)
         if pypi_hits:
             return SUCCESS
         return NO_MATCHES_FOUND
@@ -62,7 +70,13 @@
         with self._build_session(options) as session:
             transport = PipXmlrpcTransport(index_url, session)
             pypi = xmlrpc_client.ServerProxy(index_url, transport)
-            hits = pypi.search({'name': query, 'summary': query}, 'or')
+            if options.matchName or options.exactName:
+                hits = pypi.search({'name': query})
+                if options.exactName:
+                    hits = filter(
+                      lambda hit: in_case_insensitive(hit['name'], query), hits)
+            else:
+                hits = pypi.search({'name': query, 'summary': query}, 'or')
             return hits
 
 
@@ -94,7 +108,7 @@
     return list(packages.values())
 
 
-def print_results(hits, name_column_width=None, terminal_width=None):
+def print_results(hits, options, name_column_width=None, terminal_width=None):
     if not hits:
         return
     if name_column_width is None:
@@ -117,19 +131,32 @@
 
         line = '%-*s - %s' % (name_column_width,
                               '%s (%s)' % (name, latest), summary)
+        if options.exactName:
+            line += '\n%-*s   %s' % (name_column_width, '',
+                                  '%s/%s' % (PyPI.pypi_url, name))
         try:
             logger.info(line)
-            if name in installed_packages:
+            if in_case_insensitive(name, installed_packages):
                 dist = pkg_resources.get_distribution(name)
                 with indent_log():
                     if dist.version == latest:
-                        logger.info('INSTALLED: %s (latest)', dist.version)
+                        if options.exactName:
+                            logger.info('INSTALLED')
+                        else:
+                            logger.info('INSTALLED: %s (latest)', dist.version)
                     else:
                         logger.info('INSTALLED: %s', dist.version)
-                        logger.info('LATEST:    %s', latest)
+                        if not options.exactName:
+                            logger.info('LATEST:    %s', latest)
+            elif options.exactName:
+                with indent_log():
+                    logger.info('NOT INSTALLED')
         except UnicodeEncodeError:
             pass
 
 
 def highest_version(versions):
     return max(versions, key=parse_version)
+
+def in_case_insensitive(x, list):
+    return x.lower() in [y.lower() for y in list]
@pradyunsg pradyunsg added C: search 'pip search' type: feature request Request for a new feature labels Jul 2, 2018
@pradyunsg
Copy link
Member

Hey @kkdd! Thanks for filing this issue. :)

I think this information can be useful to expose in search. I do have feedback on the code (avoid use of camelCase, don't pass options and some logic seems incorrect on first glance) but I'll refrain from going into more detail until there's a PR.

I guess it would help to see #4883 -- we're planning to remove 'pip search'.

@pradyunsg
Copy link
Member

Oops, I meant to link to #5216.

@pradyunsg
Copy link
Member

Closing this out, since it's unlikely we'd work on pip search given #5216.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
C: search 'pip search' type: feature request Request for a new feature
Projects
None yet
Development

No branches or pull requests

2 participants