Archived
saving dependency work
This commit is contained in:
@@ -28,9 +28,10 @@ if MYPY_CHECK_RUNNING:
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
from typing_extensions import TypedDict
|
||||
|
||||
TransformedHit = TypedDict(
|
||||
'TransformedHit',
|
||||
{'name': str, 'summary': str, 'versions': List[str]},
|
||||
"TransformedHit",
|
||||
{"name": str, "summary": str, "versions": List[str]},
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -46,18 +47,20 @@ class SearchCommand(Command, SessionCommandMixin):
|
||||
def add_options(self):
|
||||
# type: () -> None
|
||||
self.cmd_opts.add_option(
|
||||
'-i', '--index',
|
||||
dest='index',
|
||||
metavar='URL',
|
||||
"-i",
|
||||
"--index",
|
||||
dest="index",
|
||||
metavar="URL",
|
||||
default=PyPI.pypi_url,
|
||||
help='Base URL of Python Package Index (default %default)')
|
||||
help="Base URL of Python Package Index (default %default)",
|
||||
)
|
||||
|
||||
self.parser.insert_option_group(0, self.cmd_opts)
|
||||
|
||||
def run(self, options, args):
|
||||
# type: (Values, List[str]) -> int
|
||||
if not args:
|
||||
raise CommandError('Missing required argument (search query).')
|
||||
raise CommandError("Missing required argument (search query).")
|
||||
query = args
|
||||
pypi_hits = self.search(query, options)
|
||||
hits = transform_hits(pypi_hits)
|
||||
@@ -80,7 +83,7 @@ class SearchCommand(Command, SessionCommandMixin):
|
||||
transport = PipXmlrpcTransport(index_url, session)
|
||||
pypi = xmlrpc_client.ServerProxy(index_url, transport)
|
||||
try:
|
||||
hits = pypi.search({'name': query, 'summary': query}, 'or')
|
||||
hits = pypi.search({"name": query, "summary": query}, "or")
|
||||
except xmlrpc_client.Fault as fault:
|
||||
message = "XMLRPC request failed [code: {code}]\n{string}".format(
|
||||
code=fault.faultCode,
|
||||
@@ -99,22 +102,22 @@ def transform_hits(hits):
|
||||
"""
|
||||
packages = OrderedDict() # type: OrderedDict[str, TransformedHit]
|
||||
for hit in hits:
|
||||
name = hit['name']
|
||||
summary = hit['summary']
|
||||
version = hit['version']
|
||||
name = hit["name"]
|
||||
summary = hit["summary"]
|
||||
version = hit["version"]
|
||||
|
||||
if name not in packages.keys():
|
||||
packages[name] = {
|
||||
'name': name,
|
||||
'summary': summary,
|
||||
'versions': [version],
|
||||
"name": name,
|
||||
"summary": summary,
|
||||
"versions": [version],
|
||||
}
|
||||
else:
|
||||
packages[name]['versions'].append(version)
|
||||
packages[name]["versions"].append(version)
|
||||
|
||||
# if this is the highest version, replace summary and score
|
||||
if version == highest_version(packages[name]['versions']):
|
||||
packages[name]['summary'] = summary
|
||||
if version == highest_version(packages[name]["versions"]):
|
||||
packages[name]["summary"] = summary
|
||||
|
||||
return list(packages.values())
|
||||
|
||||
@@ -124,27 +127,31 @@ def print_results(hits, name_column_width=None, terminal_width=None):
|
||||
if not hits:
|
||||
return
|
||||
if name_column_width is None:
|
||||
name_column_width = max([
|
||||
len(hit['name']) + len(highest_version(hit.get('versions', ['-'])))
|
||||
for hit in hits
|
||||
]) + 4
|
||||
name_column_width = (
|
||||
max(
|
||||
[
|
||||
len(hit["name"]) + len(highest_version(hit.get("versions", ["-"])))
|
||||
for hit in hits
|
||||
]
|
||||
)
|
||||
+ 4
|
||||
)
|
||||
|
||||
installed_packages = [p.project_name for p in pkg_resources.working_set]
|
||||
for hit in hits:
|
||||
name = hit['name']
|
||||
summary = hit['summary'] or ''
|
||||
latest = highest_version(hit.get('versions', ['-']))
|
||||
name = hit["name"]
|
||||
summary = hit["summary"] or ""
|
||||
latest = highest_version(hit.get("versions", ["-"]))
|
||||
if terminal_width is not None:
|
||||
target_width = terminal_width - name_column_width - 5
|
||||
if target_width > 10:
|
||||
# wrap and indent summary to fit terminal
|
||||
summary_lines = textwrap.wrap(summary, target_width)
|
||||
summary = ('\n' + ' ' * (name_column_width + 3)).join(
|
||||
summary_lines)
|
||||
summary = ("\n" + " " * (name_column_width + 3)).join(summary_lines)
|
||||
|
||||
line = '{name_latest:{name_column_width}} - {summary}'.format(
|
||||
name_latest='{name} ({latest})'.format(**locals()),
|
||||
**locals())
|
||||
line = "{name_latest:{name_column_width}} - {summary}".format(
|
||||
name_latest="{name} ({latest})".format(**locals()), **locals()
|
||||
)
|
||||
try:
|
||||
write_output(line)
|
||||
if name in installed_packages:
|
||||
@@ -152,14 +159,17 @@ def print_results(hits, name_column_width=None, terminal_width=None):
|
||||
assert dist is not None
|
||||
with indent_log():
|
||||
if dist.version == latest:
|
||||
write_output('INSTALLED: %s (latest)', dist.version)
|
||||
write_output("INSTALLED: %s (latest)", dist.version)
|
||||
else:
|
||||
write_output('INSTALLED: %s', dist.version)
|
||||
write_output("INSTALLED: %s", dist.version)
|
||||
if parse_version(latest).pre:
|
||||
write_output('LATEST: %s (pre-release; install'
|
||||
' with "pip install --pre")', latest)
|
||||
write_output(
|
||||
"LATEST: %s (pre-release; install"
|
||||
' with "pip install --pre")',
|
||||
latest,
|
||||
)
|
||||
else:
|
||||
write_output('LATEST: %s', latest)
|
||||
write_output("LATEST: %s", latest)
|
||||
except UnicodeEncodeError:
|
||||
pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user