2014-09-03 23:03:49 +03:00
|
|
|
# Copyright (C) Ivan Kravets <me@ikravets.com>
|
|
|
|
# See LICENSE for details.
|
|
|
|
|
2014-09-24 22:18:19 +03:00
|
|
|
import click
|
2014-09-04 18:58:12 +03:00
|
|
|
|
2014-10-19 00:14:11 +03:00
|
|
|
from platformio.exception import (LibAlreadyInstalledError,
|
|
|
|
LibInstallDependencyError)
|
2014-09-04 18:58:12 +03:00
|
|
|
from platformio.libmanager import LibraryManager
|
|
|
|
from platformio.util import get_api_result, get_lib_dir
|
2014-09-03 23:03:49 +03:00
|
|
|
|
2014-10-19 00:14:11 +03:00
|
|
|
LIBLIST_TPL = ("[{id:^14}] {name:<25} {compatibility:<30} "
|
|
|
|
"\"{authornames}\": {description}")
|
|
|
|
|
|
|
|
|
|
|
|
def echo_liblist_header():
|
|
|
|
click.echo(LIBLIST_TPL.format(
|
|
|
|
id=click.style("ID", fg="green"),
|
|
|
|
name=click.style("Name", fg="cyan"),
|
|
|
|
compatibility=click.style("Compatibility", fg="yellow"),
|
|
|
|
authornames="Authors",
|
|
|
|
description="Description"
|
|
|
|
))
|
|
|
|
click.echo("-" * 85)
|
|
|
|
|
|
|
|
|
|
|
|
def echo_liblist_item(item):
|
|
|
|
click.echo(LIBLIST_TPL.format(
|
|
|
|
id=click.style(str(item['id']), fg="green"),
|
|
|
|
name=click.style(item['name'], fg="cyan"),
|
|
|
|
compatibility=click.style(
|
|
|
|
", ".join(item['frameworks'] + item['platforms']),
|
|
|
|
fg="yellow"
|
|
|
|
),
|
|
|
|
authornames=", ".join(item['authornames']),
|
|
|
|
description=item['description']
|
|
|
|
))
|
|
|
|
|
|
|
|
|
2014-09-24 22:18:19 +03:00
|
|
|
@click.group(short_help="Library Manager")
|
2014-09-03 23:03:49 +03:00
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command("search", short_help="Search for library")
|
2014-09-24 22:18:19 +03:00
|
|
|
@click.option("-a", "--author", multiple=True)
|
|
|
|
@click.option("-k", "--keyword", multiple=True)
|
2014-10-19 00:14:11 +03:00
|
|
|
@click.option("-f", "--framework", multiple=True)
|
|
|
|
@click.option("-p", "--platform", multiple=True)
|
2014-11-24 21:59:25 +02:00
|
|
|
@click.argument("query", required=False)
|
2014-10-19 00:14:11 +03:00
|
|
|
def lib_search(query, **filters):
|
2014-11-24 21:59:25 +02:00
|
|
|
if not query:
|
|
|
|
query = ""
|
|
|
|
|
2014-10-19 00:14:11 +03:00
|
|
|
for key, values in filters.iteritems():
|
2014-09-06 12:12:13 +03:00
|
|
|
for value in values:
|
|
|
|
query += ' %s:"%s"' % (key, value)
|
|
|
|
|
|
|
|
result = get_api_result("/lib/search", dict(query=query))
|
2014-10-19 00:14:11 +03:00
|
|
|
click.secho("Found %d libraries:\n" % result['total'],
|
2014-09-24 22:18:19 +03:00
|
|
|
fg="green" if result['total'] else "yellow")
|
|
|
|
|
2014-10-19 00:14:11 +03:00
|
|
|
if result['total']:
|
|
|
|
echo_liblist_header()
|
|
|
|
|
2014-09-24 22:18:19 +03:00
|
|
|
while True:
|
|
|
|
for item in result['items']:
|
2014-10-19 00:14:11 +03:00
|
|
|
echo_liblist_item(item)
|
2014-09-24 22:18:19 +03:00
|
|
|
|
|
|
|
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
|
2014-09-04 18:58:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command("install", short_help="Install library")
|
2014-11-24 22:24:19 +02:00
|
|
|
@click.argument("libid", type=click.INT, nargs=-1, metavar="[LIBRARY_ID]")
|
2014-09-24 22:18:19 +03:00
|
|
|
@click.option("-v", "--version")
|
2014-11-24 22:24:19 +02:00
|
|
|
@click.pass_context
|
|
|
|
def lib_install(ctx, libid, version):
|
2014-09-04 18:58:12 +03:00
|
|
|
lm = LibraryManager(get_lib_dir())
|
2014-11-24 22:24:19 +02:00
|
|
|
for id_ in libid:
|
2014-10-19 00:14:11 +03:00
|
|
|
click.echo(
|
|
|
|
"Installing library [ %s ]:" % click.style(str(id_), fg="green"))
|
2014-09-04 18:58:12 +03:00
|
|
|
try:
|
2014-10-19 00:14:11 +03:00
|
|
|
if not lm.install(id_, version):
|
|
|
|
continue
|
|
|
|
|
|
|
|
info = lm.get_info(id_)
|
|
|
|
click.secho(
|
|
|
|
"The library #%s '%s' has been successfully installed!"
|
|
|
|
% (str(id_), info['name']), fg="green")
|
|
|
|
|
|
|
|
if "dependencies" in info:
|
|
|
|
click.secho("Installing dependencies:", fg="yellow")
|
|
|
|
_dependencies = info['dependencies']
|
|
|
|
if not isinstance(_dependencies, list):
|
|
|
|
_dependencies = [_dependencies]
|
|
|
|
for item in _dependencies:
|
|
|
|
try:
|
2014-11-24 22:24:19 +02:00
|
|
|
lib_install_dependency(ctx, item)
|
2014-10-19 00:14:11 +03:00
|
|
|
except AssertionError:
|
|
|
|
raise LibInstallDependencyError(str(item))
|
2014-09-06 12:53:17 +03:00
|
|
|
|
2014-09-04 18:58:12 +03:00
|
|
|
except LibAlreadyInstalledError:
|
2014-09-24 22:18:19 +03:00
|
|
|
click.secho("Already installed", fg="yellow")
|
2014-09-04 18:58:12 +03:00
|
|
|
|
|
|
|
|
2014-11-24 22:24:19 +02:00
|
|
|
def lib_install_dependency(ctx, data):
|
2014-10-19 00:14:11 +03:00
|
|
|
assert isinstance(data, dict)
|
|
|
|
query = []
|
|
|
|
for key in data.keys():
|
|
|
|
if key in ("authors", "frameworks", "platforms", "keywords"):
|
|
|
|
values = data[key]
|
|
|
|
if not isinstance(values, list):
|
|
|
|
values = [v.strip() for v in values.split(",") if v]
|
|
|
|
for value in values:
|
|
|
|
query.append('%s:"%s"' % (key[:-1], value))
|
|
|
|
elif isinstance(data[key], basestring):
|
|
|
|
query.append('+"%s"' % data[key])
|
2014-09-04 18:58:12 +03:00
|
|
|
|
2014-10-19 00:14:11 +03:00
|
|
|
result = get_api_result("/lib/search", dict(query=" ".join(query)))
|
|
|
|
assert result['total'] == 1
|
2014-11-24 22:24:19 +02:00
|
|
|
ctx.invoke(lib_install, libid=[result['items'][0]['id']])
|
2014-09-24 22:18:19 +03:00
|
|
|
|
|
|
|
|
2014-10-19 00:14:11 +03:00
|
|
|
@cli.command("uninstall", short_help="Uninstall libraries")
|
2014-11-24 22:24:19 +02:00
|
|
|
@click.argument("libid", type=click.INT, nargs=-1)
|
|
|
|
def lib_uninstall(libid):
|
2014-09-04 18:58:12 +03:00
|
|
|
lm = LibraryManager(get_lib_dir())
|
2014-11-24 22:24:19 +02:00
|
|
|
for id_ in libid:
|
2014-10-19 00:14:11 +03:00
|
|
|
info = lm.get_info(id_)
|
|
|
|
if lm.uninstall(id_):
|
|
|
|
click.secho("The library #%s '%s' has been successfully "
|
|
|
|
"uninstalled!" % (str(id_), info['name']), fg="green")
|
2014-09-04 18:58:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command("list", short_help="List installed libraries")
|
|
|
|
def lib_list():
|
|
|
|
lm = LibraryManager(get_lib_dir())
|
2014-10-19 00:14:11 +03:00
|
|
|
items = lm.get_installed().values()
|
|
|
|
if not items:
|
|
|
|
return
|
|
|
|
|
|
|
|
echo_liblist_header()
|
|
|
|
for item in items:
|
|
|
|
item['authornames'] = [i['name'] for i in item['authors']]
|
|
|
|
echo_liblist_item(item)
|
2014-09-04 18:58:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command("show", short_help="Show details about installed libraries")
|
2014-10-19 00:21:10 +03:00
|
|
|
@click.argument("libid", type=click.INT)
|
|
|
|
def lib_show(libid):
|
2014-09-04 18:58:12 +03:00
|
|
|
lm = LibraryManager(get_lib_dir())
|
2014-10-19 00:21:10 +03:00
|
|
|
info = lm.get_info(libid)
|
2014-09-24 22:18:19 +03:00
|
|
|
click.secho(info['name'], fg="cyan")
|
|
|
|
click.echo("-" * len(info['name']))
|
2014-09-04 18:58:12 +03:00
|
|
|
|
2014-10-19 00:14:11 +03:00
|
|
|
_authors = []
|
|
|
|
for author in info['authors']:
|
2014-09-04 18:58:12 +03:00
|
|
|
_data = []
|
2014-10-19 00:14:11 +03:00
|
|
|
for key in ("name", "email", "url", "maintainer"):
|
|
|
|
if not author[key]:
|
|
|
|
continue
|
|
|
|
if key == "email":
|
|
|
|
_data.append("<%s>" % author[key])
|
|
|
|
elif key == "maintainer":
|
2014-10-19 00:21:10 +03:00
|
|
|
_data.append("(maintainer)")
|
2014-10-19 00:14:11 +03:00
|
|
|
else:
|
|
|
|
_data.append(author[key])
|
|
|
|
_authors.append(" ".join(_data))
|
|
|
|
click.echo("Authors: %s" % ", ".join(_authors))
|
|
|
|
|
|
|
|
click.echo("Keywords: %s" % ", ".join(info['keywords']))
|
|
|
|
if "frameworks" in info:
|
|
|
|
click.echo("Frameworks: %s" % ", ".join(info['frameworks']))
|
|
|
|
if "platforms" in info:
|
|
|
|
click.echo("Platforms: %s" % ", ".join(info['platforms']))
|
2014-09-24 22:18:19 +03:00
|
|
|
click.echo("Version: %s" % info['version'])
|
|
|
|
click.echo()
|
|
|
|
click.echo(info['description'])
|
|
|
|
click.echo()
|
2014-09-04 18:58:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command("update", short_help="Update installed libraries")
|
2014-11-24 22:24:19 +02:00
|
|
|
@click.pass_context
|
|
|
|
def lib_update(ctx):
|
2014-09-04 18:58:12 +03:00
|
|
|
lm = LibraryManager(get_lib_dir())
|
2014-10-05 23:34:40 +03:00
|
|
|
|
2014-10-19 00:14:11 +03:00
|
|
|
lib_ids = [str(item['id']) for item in lm.get_installed().values()]
|
|
|
|
if not lib_ids:
|
2014-10-05 23:34:40 +03:00
|
|
|
return
|
2014-09-04 18:58:12 +03:00
|
|
|
|
2014-10-19 00:14:11 +03:00
|
|
|
versions = get_api_result("/lib/version/" + str(",".join(lib_ids)))
|
|
|
|
for id_ in lib_ids:
|
|
|
|
info = lm.get_info(int(id_))
|
2014-09-04 18:58:12 +03:00
|
|
|
|
2014-10-19 00:14:11 +03:00
|
|
|
click.echo("Updating [ %s ] %s library:" % (
|
|
|
|
click.style(id_, fg="yellow"),
|
2014-10-19 00:21:10 +03:00
|
|
|
click.style(info['name'], fg="cyan")))
|
2014-09-04 18:58:12 +03:00
|
|
|
|
|
|
|
current_version = info['version']
|
2014-10-19 00:14:11 +03:00
|
|
|
latest_version = versions[id_]
|
|
|
|
|
|
|
|
if latest_version is None:
|
|
|
|
click.secho("Unknown library", fg="red")
|
|
|
|
continue
|
2014-09-04 18:58:12 +03:00
|
|
|
|
2014-09-24 22:18:19 +03:00
|
|
|
click.echo("Versions: Current=%s, Latest=%s \t " % (
|
2014-09-04 18:58:12 +03:00
|
|
|
current_version, latest_version), nl=False)
|
|
|
|
|
|
|
|
if current_version == latest_version:
|
2014-09-24 22:18:19 +03:00
|
|
|
click.echo("[%s]" % (click.style("Up-to-date", fg="green")))
|
2014-09-04 18:58:12 +03:00
|
|
|
continue
|
|
|
|
else:
|
2014-09-24 22:18:19 +03:00
|
|
|
click.echo("[%s]" % (click.style("Out-of-date", fg="red")))
|
2014-09-04 18:58:12 +03:00
|
|
|
|
2014-11-24 22:24:19 +02:00
|
|
|
ctx.invoke(lib_uninstall, libid=[int(id_)])
|
|
|
|
ctx.invoke(lib_install, libid=[int(id_)])
|
2014-09-08 22:02:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
@cli.command("register", short_help="Register new library")
|
2014-09-24 22:18:19 +03:00
|
|
|
@click.argument("config_url")
|
2014-09-08 22:02:57 +03:00
|
|
|
def lib_register(config_url):
|
|
|
|
result = get_api_result("/lib/register", data=dict(config_url=config_url))
|
|
|
|
if "message" in result and result['message']:
|
2014-09-24 22:18:19 +03:00
|
|
|
click.secho(result['message'], fg="green" if "successed" in result and
|
|
|
|
result['successed'] else "red")
|