Commit 41490c37 by Sébastien Eustace

Add search command

parent ed104cd0
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
- Added support for Python 2.7. - Added support for Python 2.7.
- Added a fallback mechanism (opt-in) for missing dependencies. - Added a fallback mechanism (opt-in) for missing dependencies.
- Added `search` command.
### Changes ### Changes
......
...@@ -20,6 +20,7 @@ from .commands import PublishCommand ...@@ -20,6 +20,7 @@ from .commands import PublishCommand
from .commands import RemoveCommand from .commands import RemoveCommand
from .commands import RunCommand from .commands import RunCommand
from .commands import ScriptCommand from .commands import ScriptCommand
from .commands import SearchCommand
from .commands import ShowCommand from .commands import ShowCommand
from .commands import UpdateCommand from .commands import UpdateCommand
from .commands import VersionCommand from .commands import VersionCommand
...@@ -100,6 +101,7 @@ class Application(BaseApplication): ...@@ -100,6 +101,7 @@ class Application(BaseApplication):
RemoveCommand(), RemoveCommand(),
RunCommand(), RunCommand(),
ScriptCommand(), ScriptCommand(),
SearchCommand(),
ShowCommand(), ShowCommand(),
UpdateCommand(), UpdateCommand(),
VersionCommand(), VersionCommand(),
......
...@@ -10,6 +10,7 @@ from .publish import PublishCommand ...@@ -10,6 +10,7 @@ from .publish import PublishCommand
from .remove import RemoveCommand from .remove import RemoveCommand
from .run import RunCommand from .run import RunCommand
from .script import ScriptCommand from .script import ScriptCommand
from .search import SearchCommand
from .show import ShowCommand from .show import ShowCommand
from .update import UpdateCommand from .update import UpdateCommand
from .version import VersionCommand from .version import VersionCommand
from .command import Command
class SearchCommand(Command):
"""
Searches for packages on remote repositories.
search
{ tokens* : The tokens to search for. }
{ --N|only-name : Search only in name. }
"""
def handle(self):
from poetry.repositories.base_repository import BaseRepository
flags = BaseRepository.SEARCH_FULLTEXT
if self.option('only-name'):
flags = BaseRepository.SEARCH_FULLTEXT
results = self.poetry.pool.search(self.argument('tokens'), flags)
for result in results:
self.line('')
name = '<info>{}</>'.format(
result.name
)
name += ' (<comment>{}</>)'.format(result.version)
self.line(name)
if result.description:
self.line(
' {}'.format(result.description)
)
...@@ -52,6 +52,10 @@ class LegacyRepository(PyPiRepository): ...@@ -52,6 +52,10 @@ class LegacyRepository(PyPiRepository):
} }
}) })
@property
def name(self):
return self._name
def find_packages(self, name, constraint=None, extras=None): def find_packages(self, name, constraint=None, extras=None):
packages = [] packages = []
......
...@@ -4,6 +4,7 @@ from typing import Union ...@@ -4,6 +4,7 @@ from typing import Union
import poetry.packages import poetry.packages
from .base_repository import BaseRepository from .base_repository import BaseRepository
from .legacy_repository import LegacyRepository
from .repository import Repository from .repository import Repository
...@@ -79,4 +80,11 @@ class Pool(BaseRepository): ...@@ -79,4 +80,11 @@ class Pool(BaseRepository):
return [] return []
def search(self, query, mode=BaseRepository.SEARCH_FULLTEXT): def search(self, query, mode=BaseRepository.SEARCH_FULLTEXT):
raise NotImplementedError() results = []
for repository in self._repositories:
if isinstance(repository, LegacyRepository):
continue
results += repository.search(query, mode=mode)
return results
from typing import List from typing import List
from typing import Union from typing import Union
try:
from xmlrpc.client import ServerProxy
except ImportError:
from xmlrpclib import ServerProxy
from cachecontrol import CacheControl from cachecontrol import CacheControl
from cachecontrol.caches.file_cache import FileCache from cachecontrol.caches.file_cache import FileCache
from cachy import CacheManager from cachy import CacheManager
...@@ -160,15 +165,13 @@ class PyPiRepository(Repository): ...@@ -160,15 +165,13 @@ class PyPiRepository(Repository):
if mode == self.SEARCH_FULLTEXT: if mode == self.SEARCH_FULLTEXT:
search['summary'] = query search['summary'] = query
client = ServerProxy(self._url) client = ServerProxy('https://pypi.python.org/pypi')
hits = client.search(search, 'or') hits = client.search(search, 'or')
for hit in hits: for hit in hits:
results.append({ result = Package(hit['name'], hit['version'], hit['version'])
'name': hit['name'], result.description = hit['summary']
'description': hit['summary'], results.append(result)
'version': hit['version']
})
return results return results
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment