Implement lib-search pagination, dependencies uninstalling

This commit is contained in:
Ivan Kravets
2014-09-24 22:18:19 +03:00
parent c0c3c8efcd
commit 9c2dbf73f3

View File

@@ -1,40 +1,53 @@
# Copyright (C) Ivan Kravets <me@ikravets.com> # Copyright (C) Ivan Kravets <me@ikravets.com>
# See LICENSE for details. # See LICENSE for details.
from click import argument, echo, group, option, secho, style import click
from platformio.exception import LibAlreadyInstalledError from platformio.exception import LibAlreadyInstalledError
from platformio.libmanager import LibraryManager from platformio.libmanager import LibraryManager
from platformio.util import get_api_result, get_lib_dir from platformio.util import get_api_result, get_lib_dir
@group(short_help="Library Manager") @click.group(short_help="Library Manager")
def cli(): def cli():
pass pass
@cli.command("search", short_help="Search for library") @cli.command("search", short_help="Search for library")
@option("-a", "--author", multiple=True) @click.option("-a", "--author", multiple=True)
@option("-k", "--keyword", multiple=True) @click.option("-k", "--keyword", multiple=True)
@argument("query") @click.argument("query")
def lib_search(query, author, keyword): def lib_search(query, author, keyword):
for key, values in dict(author=author, keyword=keyword).iteritems(): for key, values in dict(author=author, keyword=keyword).iteritems():
for value in values: for value in values:
query += ' %s:"%s"' % (key, value) query += ' %s:"%s"' % (key, value)
result = get_api_result("/lib/search", dict(query=query)) result = get_api_result("/lib/search", dict(query=query))
secho("Found %d libraries:" % result['total'], click.secho("Found %d libraries:" % result['total'],
fg="green" if result['total'] else "yellow") fg="green" if result['total'] else "yellow")
for item in result['items']:
echo("{name:<30} {description}".format( while True:
name=style(item['name'], fg="cyan"), for item in result['items']:
description=item['description'] click.echo("{name:<30} {description}".format(
)) name=click.style(item['name'], fg="cyan"),
description=item['description']
))
if int(result['page'])*int(result['perpage']) >= int(result['total']):
break
if click.confirm("Show next libraries?"):
result = get_api_result(
"/lib/search",
dict(query=query, page=str(int(result['page']) + 1))
)
else:
break
@cli.command("install", short_help="Install library") @cli.command("install", short_help="Install library")
@argument("names", nargs=-1) @click.argument("names", nargs=-1)
@option("-v", "--version") @click.option("-v", "--version")
def lib_install_cli(names, version): def lib_install_cli(names, version):
lib_install(names, version) lib_install(names, version)
@@ -42,32 +55,50 @@ def lib_install_cli(names, version):
def lib_install(names, version=None): def lib_install(names, version=None):
lm = LibraryManager(get_lib_dir()) lm = LibraryManager(get_lib_dir())
for name in names: for name in names:
echo("Installing %s library:" % style(name, fg="cyan")) click.echo("Installing %s library:" % click.style(name, fg="cyan"))
try: try:
if lm.install(name, version): if lm.install(name, version):
secho("The library '%s' has been successfully installed!" % click.secho(
name, fg="green") "The library '%s' has been successfully installed!" %
name, fg="green")
info = lm.get_info(name) info = lm.get_info(name)
if "dependencies" in info: if "dependencies" in info:
secho("Installing dependencies:", fg="yellow") click.secho("Installing dependencies:", fg="yellow")
lib_install(info['dependencies']) lib_install(info['dependencies'])
except LibAlreadyInstalledError: except LibAlreadyInstalledError:
secho("Already installed", fg="yellow") click.secho("Already installed", fg="yellow")
@cli.command("uninstall", short_help="Uninstall libraries") @cli.command("uninstall", short_help="Uninstall libraries")
@argument("names", nargs=-1) @click.argument("names", nargs=-1)
def lib_uninstall_cli(names): def lib_uninstall_cli(names):
lib_uninstall(names) lib_uninstall(names)
def lib_uninstall_dependency(dependency):
lm = LibraryManager(get_lib_dir())
for name in lm.get_installed():
info = lm.get_info(name)
if dependency in info.get("dependencies", []):
return
lib_uninstall([dependency])
def lib_uninstall(names): def lib_uninstall(names):
lm = LibraryManager(get_lib_dir()) lm = LibraryManager(get_lib_dir())
for name in names: for name in names:
info = lm.get_info(name)
if lm.uninstall(name): if lm.uninstall(name):
secho("The library '%s' has been successfully " # find dependencies
"uninstalled!" % name, fg="green") if "dependencies" in info:
for d in info['dependencies']:
lib_uninstall_dependency(d)
click.secho("The library '%s' has been successfully "
"uninstalled!" % name, fg="green")
@cli.command("list", short_help="List installed libraries") @cli.command("list", short_help="List installed libraries")
@@ -75,19 +106,19 @@ def lib_list():
lm = LibraryManager(get_lib_dir()) lm = LibraryManager(get_lib_dir())
for name in lm.get_installed(): for name in lm.get_installed():
info = lm.get_info(name) info = lm.get_info(name)
echo("{name:<30} {description}".format( click.echo("{name:<30} {description}".format(
name=style(info['name'], fg="cyan"), name=click.style(info['name'], fg="cyan"),
description=info['description'] description=info['description']
)) ))
@cli.command("show", short_help="Show details about installed libraries") @cli.command("show", short_help="Show details about installed libraries")
@argument("name") @click.argument("name")
def lib_show(name): def lib_show(name):
lm = LibraryManager(get_lib_dir()) lm = LibraryManager(get_lib_dir())
info = lm.get_info(name) info = lm.get_info(name)
secho(info['name'], fg="cyan") click.secho(info['name'], fg="cyan")
echo("-" * len(info['name'])) click.echo("-" * len(info['name']))
if "author" in info: if "author" in info:
_data = [] _data = []
@@ -97,13 +128,13 @@ def lib_show(name):
if k == "email": if k == "email":
_value = "<%s>" % _value _value = "<%s>" % _value
_data.append(_value) _data.append(_value)
echo("Author: %s" % " ".join(_data)) click.echo("Author: %s" % " ".join(_data))
echo("Keywords: %s" % info['keywords']) click.echo("Keywords: %s" % info['keywords'])
echo("Version: %s" % info['version']) click.echo("Version: %s" % info['version'])
echo() click.echo()
echo(info['description']) click.echo(info['description'])
echo() click.echo()
@cli.command("update", short_help="Update installed libraries") @cli.command("update", short_help="Update installed libraries")
@@ -115,28 +146,28 @@ def lib_update():
for name in lib_names: for name in lib_names:
info = lm.get_info(name) info = lm.get_info(name)
echo("Updating %s library:" % style(name, fg="yellow")) click.echo("Updating %s library:" % click.style(name, fg="yellow"))
current_version = info['version'] current_version = info['version']
latest_version = versions[name] latest_version = versions[name]
echo("Versions: Current=%s, Latest=%s \t " % ( click.echo("Versions: Current=%s, Latest=%s \t " % (
current_version, latest_version), nl=False) current_version, latest_version), nl=False)
if current_version == latest_version: if current_version == latest_version:
echo("[%s]" % (style("Up-to-date", fg="green"))) click.echo("[%s]" % (click.style("Up-to-date", fg="green")))
continue continue
else: else:
echo("[%s]" % (style("Out-of-date", fg="red"))) click.echo("[%s]" % (click.style("Out-of-date", fg="red")))
lib_uninstall([name]) lib_uninstall([name])
lib_install([name]) lib_install([name])
@cli.command("register", short_help="Register new library") @cli.command("register", short_help="Register new library")
@argument("config_url") @click.argument("config_url")
def lib_register(config_url): def lib_register(config_url):
result = get_api_result("/lib/register", data=dict(config_url=config_url)) result = get_api_result("/lib/register", data=dict(config_url=config_url))
if "message" in result and result['message']: if "message" in result and result['message']:
secho(result['message'], fg="green" if "successed" in result and click.secho(result['message'], fg="green" if "successed" in result and
result['successed'] else "red") result['successed'] else "red")