Exemplo n.º 1
0
def lib_search(query, json_output, page, noninteractive, **filters):
    if not query:
        query = []
    if not isinstance(query, list):
        query = list(query)

    for key, values in filters.iteritems():
        for value in values:
            query.append('%s:"%s"' % (key, value))

    result = get_api_result("/lib/search",
                            dict(query=" ".join(query), page=page))

    if json_output:
        click.echo(json.dumps(result))
        return

    if result['total'] == 0:
        click.secho(
            "Nothing has been found by your request\n"
            "Try a less-specific search or use truncation (or wildcard) "
            "operator",
            fg="yellow",
            nl=False)
        click.secho(" *", fg="green")
        click.secho("For example: DS*, PCA*, DHT* and etc.\n", fg="yellow")
        click.echo("For more examples and advanced search syntax, "
                   "please use documentation:")
        click.secho(
            "http://docs.platformio.org"
            "/en/stable/userguide/lib/cmd_search.html\n",
            fg="cyan")
        return

    click.secho("Found %d libraries:\n" % result['total'],
                fg="green" if result['total'] else "yellow")

    if result['total']:
        echo_liblist_header()

    while True:
        for item in result['items']:
            echo_liblist_item(item)

        if (int(result['page']) * int(result['perpage']) >= int(
                result['total'])):
            break

        if noninteractive:
            click.echo()
            click.secho("Loading next %d libraries... Press Ctrl+C to stop!" %
                        result['perpage'],
                        fg="yellow")
            click.echo()
            sleep(5)
        elif not click.confirm("Show next libraries?"):
            break
        result = get_api_result(
            "/lib/search",
            dict(query=" ".join(query), page=int(result['page']) + 1))
Exemplo n.º 2
0
def lib_search(query, **filters):
    for key, values in filters.iteritems():
        for value in values:
            query += ' %s:"%s"' % (key, value)

    result = get_api_result("/lib/search", dict(query=query))
    click.secho("Found %d libraries:\n" % result['total'],
                fg="green" if result['total'] else "yellow")

    if result['total']:
        echo_liblist_header()

    while True:
        for item in result['items']:
            echo_liblist_item(item)

        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
Exemplo n.º 3
0
def lib_search(query, **filters):
    for key, values in filters.iteritems():
        for value in values:
            query += ' %s:"%s"' % (key, value)

    result = get_api_result("/lib/search", dict(query=query))
    click.secho("Found %d libraries:\n" % result['total'],
                fg="green" if result['total'] else "yellow")

    if result['total']:
        echo_liblist_header()

    while True:
        for item in result['items']:
            echo_liblist_item(item)

        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
Exemplo n.º 4
0
def lib_search(query, json_output, page, **filters):
    if not query:
        query = []
    if not isinstance(query, list):
        query = list(query)

    for key, values in filters.iteritems():
        for value in values:
            query.append('%s:"%s"' % (key, value))

    result = get_api_result("/lib/search",
                            dict(query=" ".join(query), page=page))

    if json_output:
        click.echo(json.dumps(result))
        return

    if result['total'] == 0:
        click.secho(
            "Nothing has been found by your request\n"
            "Try a less-specific search or use truncation (or wildcard) "
            "operator", fg="yellow", nl=False)
        click.secho(" *", fg="green")
        click.secho("For example: DS*, PCA*, DHT* and etc.\n", fg="yellow")
        click.echo("For more examples and advanced search syntax, "
                   "please use documentation:")
        click.secho("http://docs.platformio.org"
                    "/en/latest/userguide/lib/cmd_search.html\n", fg="cyan")
        return

    click.secho("Found %d libraries:\n" % result['total'],
                fg="green" if result['total'] else "yellow")

    if result['total']:
        echo_liblist_header()

    while True:
        for item in result['items']:
            echo_liblist_item(item)

        if (int(result['page']) * int(result['perpage']) >=
                int(result['total'])):
            break

        if (app.get_setting("enable_prompts") and
                click.confirm("Show next libraries?")):
            result = get_api_result(
                "/lib/search",
                dict(query=" ".join(query), page=int(result['page']) + 1)
            )
        else:
            break
Exemplo n.º 5
0
def lib_search(query, **filters):
    if not query:
        query = []
    if not isinstance(query, list):
        query = list(query)

    for key, values in filters.iteritems():
        for value in values:
            query.append('%s:"%s"' % (key, value))

    result = get_api_result("/lib/search", dict(query=" ".join(query)))
    if result['total'] == 0:
        click.secho(
            "Nothing has been found by your request\n"
            "Try a less-specific search or use truncation (or wildcard) "
            "operator",
            fg="yellow",
            nl=False)
        click.secho(" *", fg="green")
        click.secho("For example: DS*, PCA*, DHT* and etc.\n", fg="yellow")
        click.echo("For more examples and advanced search syntax, "
                   "please use documentation:")
        click.secho(
            "http://docs.platformio.org"
            "/en/latest/userguide/lib/cmd_search.html\n",
            fg="cyan")
        return

    click.secho("Found %d libraries:\n" % result['total'],
                fg="green" if result['total'] else "yellow")

    if result['total']:
        echo_liblist_header()

    while True:
        for item in result['items']:
            echo_liblist_item(item)

        if (int(result['page']) * int(result['perpage']) >= int(
                result['total'])):
            break

        if (app.get_setting("enable_prompts")
                and click.confirm("Show next libraries?")):
            result = get_api_result(
                "/lib/search",
                dict(query=" ".join(query), page=str(int(result['page']) + 1)))
        else:
            break
Exemplo n.º 6
0
def lib_stats(json_output):
    result = util.get_api_result("/lib/stats", cache_valid="1h")

    if json_output:
        return click.echo(dump_json_to_unicode(result))

    for key in ("updated", "added"):
        tabular_data = [(
            click.style(item["name"], fg="cyan"),
            time.strftime("%c", util.parse_date(item["date"])),
            "https://platformio.org/lib/show/%s/%s" %
            (item["id"], quote(item["name"])),
        ) for item in result.get(key, [])]
        table = tabulate(
            tabular_data,
            headers=[
                click.style("RECENTLY " + key.upper(), bold=True), "Date",
                "URL"
            ],
        )
        click.echo(table)
        click.echo()

    for key in ("lastkeywords", "topkeywords"):
        tabular_data = [(
            click.style(name, fg="cyan"),
            "https://platformio.org/lib/search?query=" +
            quote("keyword:%s" % name),
        ) for name in result.get(key, [])]
        table = tabulate(
            tabular_data,
            headers=[
                click.style(
                    ("RECENT" if key == "lastkeywords" else "POPULAR") +
                    " KEYWORDS",
                    bold=True,
                ),
                "URL",
            ],
        )
        click.echo(table)
        click.echo()

    for key, title in (("dlday", "Today"), ("dlweek", "Week"), ("dlmonth",
                                                                "Month")):
        tabular_data = [(
            click.style(item["name"], fg="cyan"),
            "https://platformio.org/lib/show/%s/%s" %
            (item["id"], quote(item["name"])),
        ) for item in result.get(key, [])]
        table = tabulate(
            tabular_data,
            headers=[
                click.style("FEATURED: " + title.upper(), bold=True), "URL"
            ],
        )
        click.echo(table)
        click.echo()

    return True
Exemplo n.º 7
0
def lib_install_dependency(ctx, data):
    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])

    result = get_api_result("/lib/search", dict(query=" ".join(query)))
    assert result['total'] > 0

    if result['total'] == 1 or not app.get_setting("enable_prompts"):
        ctx.invoke(lib_install, libid=[result['items'][0]['id']])
    else:
        click.secho(
            "Conflict: More than one dependent libraries have been found "
            "by request %s:" % json.dumps(data), fg="red")

        echo_liblist_header()
        for item in result['items']:
            echo_liblist_item(item)

        deplib_id = click.prompt(
            "Please choose one dependent library ID",
            type=click.Choice([str(i['id']) for i in result['items']]))
        ctx.invoke(lib_install, libid=[int(deplib_id)])
Exemplo n.º 8
0
def lib_register(config_url):
    if not config_url.startswith("http://") and not config_url.startswith("https://"):
        raise exception.InvalidLibConfURL(config_url)

    result = get_api_result("/lib/register", data=dict(config_url=config_url))
    if "message" in result and result["message"]:
        click.secho(result["message"], fg="green" if "successed" in result and result["successed"] else "red")
Exemplo n.º 9
0
    def install(self, id_, version=None):
        if self.is_installed(id_):
            raise LibAlreadyInstalledError()

        dlinfo = get_api_result("/lib/download/" + str(id_),
                                dict(version=version) if version else None)
        dlpath = None
        tmplib_dir = join(self.lib_dir, str(id_))
        try:
            dlpath = self.download(dlinfo['url'], gettempdir())
            if not isdir(tmplib_dir):
                makedirs(tmplib_dir)
            self.unpack(dlpath, tmplib_dir)
        finally:
            if dlpath:
                remove(dlpath)

        info = self.get_info(id_)
        rename(tmplib_dir, join(self.lib_dir, "%s_ID%d" % (
            re.sub(r"[^\da-zA-Z]+", "_", info['name']), id_)))

        telemetry.on_event(
            category="LibraryManager", action="Install",
            label="#%d %s" % (id_, info['name'])
        )

        return True
Exemplo n.º 10
0
def lib_update():
    lm = LibraryManager(get_lib_dir())

    lib_ids = [str(item['id']) for item in lm.get_installed().values()]
    if not lib_ids:
        return

    versions = get_api_result("/lib/version/" + str(",".join(lib_ids)))
    for id_ in lib_ids:
        info = lm.get_info(int(id_))

        click.echo("Updating  [ %s ] %s library:" % (
            click.style(id_, fg="yellow"),
            click.style(info['name'], fg="cyan")))

        current_version = info['version']
        latest_version = versions[id_]

        if latest_version is None:
            click.secho("Unknown library", fg="red")
            continue

        click.echo("Versions: Current=%s, Latest=%s \t " % (
            current_version, latest_version), nl=False)

        if current_version == latest_version:
            click.echo("[%s]" % (click.style("Up-to-date", fg="green")))
            continue
        else:
            click.echo("[%s]" % (click.style("Out-of-date", fg="red")))

        lib_uninstall([int(id_)])
        lib_install([int(id_)])
Exemplo n.º 11
0
 def get_latest_repo_version(self, name, requirements, silent=False):
     item = self.max_satisfying_repo_version(
         util.get_api_result(
             "/lib/info/%d" % self.get_pkg_id_by_name(
                 name, requirements, silent=silent),
             cache_valid="1d")['versions'], requirements)
     return item['name'] if item else None
Exemplo n.º 12
0
 def get_latest_repo_version(self, name, requirements, silent=False):
     item = self.max_satisfying_repo_version(
         util.get_api_result(
             "/lib/info/%d" %
             self.get_pkg_id_by_name(name, requirements, silent=silent),
             cache_valid="1h")['versions'], requirements)
     return item['name'] if item else None
Exemplo n.º 13
0
def lib_install_dependency(ctx, data):
    assert isinstance(data, dict)
    query = []
    for key in data:
        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])

    result = get_api_result("/lib/search", dict(query=" ".join(query)))
    assert result['total'] > 0

    if result['total'] == 1 or not app.get_setting("enable_prompts"):
        ctx.invoke(lib_install, libid=[result['items'][0]['id']])
    else:
        click.secho(
            "Conflict: More than one dependent libraries have been found "
            "by request %s:" % json.dumps(data),
            fg="red")

        echo_liblist_header()
        for item in result['items']:
            echo_liblist_item(item)

        deplib_id = click.prompt("Please choose one dependent library ID",
                                 type=click.Choice(
                                     [str(i['id']) for i in result['items']]))
        ctx.invoke(lib_install, libid=[int(deplib_id)])
Exemplo n.º 14
0
def lib_update():
    lm = LibraryManager(get_lib_dir())

    lib_ids = [str(item['id']) for item in lm.get_installed().values()]
    if not lib_ids:
        return

    versions = get_api_result("/lib/version/" + str(",".join(lib_ids)))
    for id_ in lib_ids:
        info = lm.get_info(int(id_))

        click.echo("Updating  [ %s ] %s library:" % (click.style(
            id_, fg="yellow"), click.style(info['name'], fg="cyan")))

        current_version = info['version']
        latest_version = versions[id_]

        if latest_version is None:
            click.secho("Unknown library", fg="red")
            continue

        click.echo("Versions: Current=%s, Latest=%s \t " %
                   (current_version, latest_version),
                   nl=False)

        if current_version == latest_version:
            click.echo("[%s]" % (click.style("Up-to-date", fg="green")))
            continue
        else:
            click.echo("[%s]" % (click.style("Out-of-date", fg="red")))

        lib_uninstall([int(id_)])
        lib_install([int(id_)])
Exemplo n.º 15
0
 def get_latest_repo_version(self, name, requirements):
     item = self.max_satisfying_repo_version(
         util.get_api_result(
             "/lib/versions/%d" % self._get_pkg_id_by_name(name,
                                                           requirements),
             cache_valid="1h"),
         requirements)
     return item['version'] if item else None
Exemplo n.º 16
0
def lib_register(config_url):
    if (not config_url.startswith("http://") and not
            config_url.startswith("https://")):
        raise exception.InvalidLibConfURL(config_url)

    result = get_api_result("/lib/register", data=dict(config_url=config_url))
    if "message" in result and result['message']:
        click.secho(result['message'], fg="green" if "successed" in result and
                    result['successed'] else "red")
Exemplo n.º 17
0
 def get_latest_repo_version(self, name, requirements, silent=False):
     item = self.max_satisfying_repo_version(
         util.get_api_result("/lib/info/%d" % self.search_lib_id(
             {
                 "name": name,
                 "requirements": requirements
             }, silent=silent),
                             cache_valid="1h")['versions'], requirements)
     return item['name'] if item else None
Exemplo n.º 18
0
def pytest_generate_tests(metafunc):
    if "package_data" not in metafunc.fixturenames:
        return
    pkgs_manifest = get_api_result("/packages/manifest")
    assert isinstance(pkgs_manifest, dict)
    packages = []
    for _, variants in pkgs_manifest.iteritems():
        for item in variants:
            packages.append(item)
    metafunc.parametrize("package_data", packages)
Exemplo n.º 19
0
 def get_latest_repo_version(self, name, requirements, silent=False):
     item = self.max_satisfying_repo_version(
         util.get_api_result(
             "/lib/info/%d" % self.search_lib_id(
                 {
                     "name": name,
                     "requirements": requirements
                 },
                 silent=silent),
             cache_valid="1h")['versions'], requirements)
     return item['name'] if item else None
Exemplo n.º 20
0
def lib_search(query, **filters):
    if not query:
        query = ""

    for key, values in filters.iteritems():
        for value in values:
            query += ' %s:"%s"' % (key, value)

    result = get_api_result("/lib/search", dict(query=query))
    if result["total"] == 0:
        click.secho(
            "Nothing has been found by your request\n"
            "Try a less-specific search or use truncation (or wildcard) "
            "operator",
            fg="yellow",
            nl=False,
        )
        click.secho(" *", fg="green")
        click.secho("For example: DS*, PCA*, DHT* and etc.\n", fg="yellow")
        click.echo("For more examples and advanced search syntax, " "please use documentation:")
        click.secho("http://docs.platformio.org" "/en/latest/userguide/lib/cmd_search.html\n", fg="cyan")
        return

    click.secho("Found %d libraries:\n" % result["total"], fg="green" if result["total"] else "yellow")

    if result["total"]:
        echo_liblist_header()

    while True:
        for item in result["items"]:
            echo_liblist_item(item)

        if int(result["page"]) * int(result["perpage"]) >= int(result["total"]):
            break

        if app.get_setting("enable_prompts") and click.confirm("Show next libraries?"):
            result = get_api_result("/lib/search", dict(query=query, page=str(int(result["page"]) + 1)))
        else:
            break
Exemplo n.º 21
0
    def _install_from_piorepo(self, name, requirements):
        assert name.startswith("id="), name
        version = self.get_latest_repo_version(name, requirements)
        if not version:
            raise exception.UndefinedPackageVersion(requirements or "latest",
                                                    util.get_systype())
        dl_data = util.get_api_result("/lib/download/" + str(name[3:]),
                                      dict(version=version),
                                      cache_valid="30d")
        assert dl_data

        return self._install_from_url(
            name, dl_data['url'].replace("http://", "https://")
            if app.get_setting("enable_ssl") else dl_data['url'], requirements)
Exemplo n.º 22
0
def lib_register(config_url):
    if not config_url.startswith("http://") and not config_url.startswith("https://"):
        raise exception.InvalidLibConfURL(config_url)

    # Validate manifest
    ManifestSchema().load_manifest(
        ManifestParserFactory.new_from_url(config_url).as_dict()
    )

    result = util.get_api_result("/lib/register", data=dict(config_url=config_url))
    if "message" in result and result["message"]:
        click.secho(
            result["message"],
            fg="green" if "successed" in result and result["successed"] else "red",
        )
Exemplo n.º 23
0
    def _install_from_piorepo(self, name, requirements):
        assert name.startswith("id="), name
        version = self.get_latest_repo_version(name, requirements)
        if not version:
            raise exception.UndefinedPackageVersion(requirements or "latest",
                                                    util.get_systype())
        dl_data = util.get_api_result(
            "/lib/download/" + str(name[3:]),
            dict(version=version),
            cache_valid="30d")
        assert dl_data

        return self._install_from_url(
            name, dl_data['url'].replace("http://", "https://")
            if app.get_setting("enable_ssl") else dl_data['url'], requirements)
Exemplo n.º 24
0
def lib_install_dependency(ctx, data):
    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])

    result = get_api_result("/lib/search", dict(query=" ".join(query)))
    assert result['total'] == 1
    ctx.invoke(lib_install, libid=[result['items'][0]['id']])
Exemplo n.º 25
0
def lib_install_dependency(data):
    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])

    result = get_api_result("/lib/search", dict(query=" ".join(query)))
    assert result['total'] == 1
    lib_install([result['items'][0]['id']])
Exemplo n.º 26
0
 def _install_from_piorepo(self, name, requirements):
     assert name.startswith("id=")
     version = self.get_latest_repo_version(name, requirements)
     if not version:
         raise exception.UndefinedPackageVersion(requirements or "latest",
                                                 util.get_systype())
     dl_data = util.get_api_result("/lib/download/" + str(name[3:]),
                                   dict(version=version))
     assert dl_data
     pkg_dir = None
     try:
         pkg_dir = self._install_from_url(
             name, dl_data['url'].replace("http://", "https://"),
             requirements)
     except exception.APIRequestError:
         pkg_dir = self._install_from_url(name, dl_data['url'],
                                          requirements)
     return pkg_dir
Exemplo n.º 27
0
def platform_frameworks(query, json_output):
    frameworks = []
    for framework in util.get_api_result("/frameworks", cache_valid="30d"):
        if query == "all":
            query = ""
        search_data = json.dumps(framework)
        if query and query.lower() not in search_data.lower():
            continue
        framework['homepage'] = ("http://platformio.org/frameworks/" +
                                 framework['name'])
        framework['platforms'] = [
            platform['name'] for platform in _get_registry_platforms()
            if framework['name'] in platform['frameworks']
        ]
        frameworks.append(framework)

    if json_output:
        click.echo(json.dumps(frameworks))
    else:
        _print_platforms(frameworks)
Exemplo n.º 28
0
def platform_frameworks(query, json_output):
    frameworks = []
    for framework in util.get_api_result("/frameworks", cache_valid="7d"):
        if query == "all":
            query = ""
        search_data = dump_json_to_unicode(framework)
        if query and query.lower() not in search_data.lower():
            continue
        framework['homepage'] = ("https://platformio.org/frameworks/" +
                                 framework['name'])
        framework['platforms'] = [
            platform['name'] for platform in _get_registry_platforms()
            if framework['name'] in platform['frameworks']
        ]
        frameworks.append(framework)

    frameworks = sorted(frameworks, key=lambda manifest: manifest['name'])
    if json_output:
        click.echo(dump_json_to_unicode(frameworks))
    else:
        _print_platforms(frameworks)
Exemplo n.º 29
0
def platform_search(query, json_output):
    platforms = []
    for platform in util.get_api_result("/platforms", cache_valid="365d"):
        if query == "all":
            query = ""

        search_data = json.dumps(platform)
        if query and query.lower() not in search_data.lower():
            continue

        platforms.append({
            "name": platform['name'],
            "title": platform['title'],
            "description": platform['description'],
            "packages": platform['packages']
        })

    if json_output:
        click.echo(json.dumps(platforms))
    else:
        _print_platforms(platforms)
Exemplo n.º 30
0
def platform_search(query, json_output):
    platforms = []
    for platform in util.get_api_result("/platforms", cache_valid="365d"):
        if query == "all":
            query = ""

        search_data = json.dumps(platform)
        if query and query.lower() not in search_data.lower():
            continue

        platforms.append({
            "name": platform['name'],
            "title": platform['title'],
            "description": platform['description'],
            "packages": platform['packages']
        })

    if json_output:
        click.echo(json.dumps(platforms))
    else:
        _print_platforms(platforms)
Exemplo n.º 31
0
def platform_frameworks(query, json_output):
    frameworks = []
    for framework in util.get_api_result("/frameworks", cache_valid="7d"):
        if query == "all":
            query = ""
        search_data = json.dumps(framework)
        if query and query.lower() not in search_data.lower():
            continue
        framework['homepage'] = (
            "https://platformio.org/frameworks/" + framework['name'])
        framework['platforms'] = [
            platform['name'] for platform in _get_registry_platforms()
            if framework['name'] in platform['frameworks']
        ]
        frameworks.append(framework)

    frameworks = sorted(frameworks, key=lambda manifest: manifest['name'])
    if json_output:
        click.echo(json.dumps(frameworks))
    else:
        _print_platforms(frameworks)
Exemplo n.º 32
0
    def install(self, id_, version=None):
        if self.is_installed(id_):
            raise LibAlreadyInstalledError()

        dlinfo = get_api_result("/lib/download/" + str(id_),
                                dict(version=version) if version else None)
        dlpath = None
        tmplib_dir = join(self.lib_dir, str(id_))
        try:
            dlpath = self.download(dlinfo['url'], gettempdir())
            if not isdir(tmplib_dir):
                makedirs(tmplib_dir)
            self.unpack(dlpath, tmplib_dir)
        finally:
            if dlpath:
                remove(dlpath)

        info = self.get_info(id_)
        rename(tmplib_dir, join(self.lib_dir, "%s_ID%d" % (
            re.sub(r"[^\da-z]+", "_", info['name'], flags=re.I), id_)))
        return True
Exemplo n.º 33
0
    def install(self, id_, version=None):
        if self.is_installed(id_):
            raise LibAlreadyInstalledError()

        dlinfo = get_api_result("/lib/download/" + str(id_),
                                dict(version=version) if version else None)
        dlpath = None
        tmplib_dir = join(self.lib_dir, str(id_))
        try:
            dlpath = self.download(dlinfo['url'], gettempdir())
            if not isdir(tmplib_dir):
                makedirs(tmplib_dir)
            self.unpack(dlpath, tmplib_dir)
        finally:
            if dlpath:
                remove(dlpath)

        info = self.get_info(id_)
        rename(
            tmplib_dir,
            join(
                self.lib_dir, "%s_ID%d" %
                (re.sub(r"[^\da-z]+", "_", info['name'], flags=re.I), id_)))
        return True
Exemplo n.º 34
0
 def get_latest_versions(self):
     lib_ids = [str(item['id']) for item in self.get_installed().values()]
     if not lib_ids:
         return None
     return get_api_result("/lib/version/" + str(",".join(lib_ids)))
Exemplo n.º 35
0
except ImportError:
    from urllib.parse import ParseResult, urlparse, urlunparse

RST_COPYRIGHT = """..  Copyright (c) 2014-present PlatformIO <*****@*****.**>
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
       http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
"""

API_PACKAGES = util.get_api_result("/packages")
API_FRAMEWORKS = util.get_api_result("/frameworks")
BOARDS = PlatformManager().get_installed_boards()
PLATFORM_MANIFESTS = PlatformManager().get_installed()
DOCS_ROOT_DIR = realpath(join(dirname(realpath(__file__)), "..", "docs"))


def is_compat_platform_and_framework(platform, framework):
    p = PlatformFactory.newPlatform(platform)
    return framework in (p.frameworks or {}).keys()


def campaign_url(url, source="platformio.org", medium="docs"):
    data = urlparse(url)
    query = data.query
    if query:
Exemplo n.º 36
0
 def get_registered_boards():
     return util.get_api_result("/boards")
Exemplo n.º 37
0
def lib_show(library, json_output):
    lm = LibraryManager()
    name, requirements, _ = lm.parse_pkg_input(library)
    lib_id = lm.get_pkg_id_by_name(name,
                                   requirements,
                                   silent=json_output,
                                   interactive=not json_output)
    lib = get_api_result("/lib/info/%d" % lib_id, cache_valid="1d")
    if json_output:
        return click.echo(json.dumps(lib))

    click.secho(lib['name'], fg="cyan")
    click.echo("=" * len(lib['name']))
    click.secho("#ID: %d" % lib['id'], bold=True)
    click.echo(lib['description'])
    click.echo()

    click.echo("Version: %s, released %s" %
               (lib['version']['name'], arrow.get(
                   lib['version']['released']).humanize()))
    click.echo("Manifest: %s" % lib['confurl'])
    for key in ("homepage", "repository", "license"):
        if key not in lib or not lib[key]:
            continue
        if isinstance(lib[key], list):
            click.echo("%s: %s" % (key.title(), ", ".join(lib[key])))
        else:
            click.echo("%s: %s" % (key.title(), lib[key]))

    blocks = []

    _authors = []
    for author in lib.get("authors", []):
        _data = []
        for key in ("name", "email", "url", "maintainer"):
            if not author[key]:
                continue
            if key == "email":
                _data.append("<%s>" % author[key])
            elif key == "maintainer":
                _data.append("(maintainer)")
            else:
                _data.append(author[key])
        _authors.append(" ".join(_data))
    if _authors:
        blocks.append(("Authors", _authors))

    blocks.append(("Keywords", lib['keywords']))
    for key in ("frameworks", "platforms"):
        if key not in lib or not lib[key]:
            continue
        blocks.append(("Compatible %s" % key, [i['title'] for i in lib[key]]))
    blocks.append(("Headers", lib['headers']))
    blocks.append(("Examples", lib['examples']))
    blocks.append(("Versions", [
        "%s, released %s" % (v['name'], arrow.get(v['released']).humanize())
        for v in lib['versions']
    ]))
    blocks.append(("Unique Downloads", [
        "Today: %s" % lib['dlstats']['day'],
        "Week: %s" % lib['dlstats']['week'],
        "Month: %s" % lib['dlstats']['month']
    ]))

    for (title, rows) in blocks:
        click.echo()
        click.secho(title, bold=True)
        click.echo("-" * len(title))
        for row in rows:
            click.echo(row)
Exemplo n.º 38
0
def test_api_internet_offline(without_internet, isolated_pio_home):
    with pytest.raises(exception.InternetIsOffline):
        util.get_api_result("/stats")
Exemplo n.º 39
0
 def get_manifest(cls):
     try:
         return cls._cached_manifest
     except AttributeError:
         cls._cached_manifest = get_api_result("/packages/manifest")
     return cls._cached_manifest
Exemplo n.º 40
0
def lib_stats(json_output):
    result = get_api_result("/lib/stats", cache_valid="1h")

    if json_output:
        return click.echo(json.dumps(result))

    printitem_tpl = "{name:<33} {url}"
    printitemdate_tpl = "{name:<33} {date:23} {url}"

    def _print_title(title):
        click.secho(title.upper(), bold=True)
        click.echo("*" * len(title))

    def _print_header(with_date=False):
        click.echo((printitemdate_tpl if with_date else printitem_tpl).format(
            name=click.style("Name", fg="cyan"),
            date="Date",
            url=click.style("Url", fg="blue")))

        terminal_width, _ = click.get_terminal_size()
        click.echo("-" * terminal_width)

    def _print_lib_item(item):
        date = str(
            time.strftime("%c", util.parse_date(item['date'])) if "date" in
            item else "")
        url = click.style(
            "https://platformio.org/lib/show/%s/%s" % (item['id'],
                                                       quote(item['name'])),
            fg="blue")
        click.echo(
            (printitemdate_tpl if "date" in item else printitem_tpl).format(
                name=click.style(item['name'], fg="cyan"), date=date, url=url))

    def _print_tag_item(name):
        click.echo(
            printitem_tpl.format(
                name=click.style(name, fg="cyan"),
                url=click.style(
                    "https://platformio.org/lib/search?query=" + quote(
                        "keyword:%s" % name),
                    fg="blue")))

    for key in ("updated", "added"):
        _print_title("Recently " + key)
        _print_header(with_date=True)
        for item in result.get(key, []):
            _print_lib_item(item)
        click.echo()

    _print_title("Recent keywords")
    _print_header(with_date=False)
    for item in result.get("lastkeywords"):
        _print_tag_item(item)
    click.echo()

    _print_title("Popular keywords")
    _print_header(with_date=False)
    for item in result.get("topkeywords"):
        _print_tag_item(item)
    click.echo()

    for key, title in (("dlday", "Today"), ("dlweek", "Week"), ("dlmonth",
                                                                "Month")):
        _print_title("Featured: " + title)
        _print_header(with_date=False)
        for item in result.get(key, []):
            _print_lib_item(item)
        click.echo()

    return True
Exemplo n.º 41
0
def _get_registry_platforms():
    platforms = util.get_api_result("/platforms", cache_valid="7d")
    pm = PlatformManager()
    for platform in platforms or []:
        platform['versions'] = pm.get_all_repo_versions(platform['name'])
    return platforms
Exemplo n.º 42
0
    def search_lib_id(  # pylint: disable=too-many-branches
            self,
            filters,
            silent=False,
            interactive=False):
        assert isinstance(filters, dict)
        assert "name" in filters

        # try to find ID within installed packages
        lib_id = self._get_lib_id_from_installed(filters)
        if lib_id:
            return lib_id

        # looking in PIO Library Registry
        if not silent:
            click.echo("Looking for %s library in registry" %
                       click.style(filters["name"], fg="cyan"))
        query = []
        for key in filters:
            if key not in ("name", "authors", "frameworks", "platforms"):
                continue
            values = filters[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] if key.endswith("s") else key, value))

        lib_info = None
        result = util.get_api_result("/v2/lib/search",
                                     dict(query=" ".join(query)),
                                     cache_valid="1h")
        if result["total"] == 1:
            lib_info = result["items"][0]
        elif result["total"] > 1:
            if silent and not interactive:
                lib_info = result["items"][0]
            else:
                click.secho(
                    "Conflict: More than one library has been found "
                    "by request %s:" % json.dumps(filters),
                    fg="yellow",
                    err=True,
                )
                # pylint: disable=import-outside-toplevel
                from platformio.commands.lib import print_lib_item

                for item in result["items"]:
                    print_lib_item(item)

                if not interactive:
                    click.secho(
                        "Automatically chose the first available library "
                        "(use `--interactive` option to make a choice)",
                        fg="yellow",
                        err=True,
                    )
                    lib_info = result["items"][0]
                else:
                    deplib_id = click.prompt(
                        "Please choose library ID",
                        type=click.Choice(
                            [str(i["id"]) for i in result["items"]]),
                    )
                    for item in result["items"]:
                        if item["id"] == int(deplib_id):
                            lib_info = item
                            break

        if not lib_info:
            if list(filters) == ["name"]:
                raise exception.LibNotFound(filters["name"])
            raise exception.LibNotFound(str(filters))
        if not silent:
            click.echo("Found: %s" % click.style(
                "https://platformio.org/lib/show/{id}/{name}".format(
                    **lib_info),
                fg="blue",
            ))
        return int(lib_info["id"])
Exemplo n.º 43
0
def _get_registry_platforms():
    platforms = util.get_api_result("/platforms", cache_valid="7d")
    pm = PlatformManager()
    for platform in platforms or []:
        platform['versions'] = pm.get_all_repo_versions(platform['name'])
    return platforms
Exemplo n.º 44
0
 def get_manifest(cls):
     return util.get_api_result("/packages/manifest")
Exemplo n.º 45
0
    def search_lib_id(  # pylint: disable=too-many-branches
            self,
            filters,
            silent=False,
            interactive=False):
        assert isinstance(filters, dict)
        assert "name" in filters

        # try to find ID within installed packages
        lib_id = self._get_lib_id_from_installed(filters)
        if lib_id:
            return lib_id

        # looking in PIO Library Registry
        if not silent:
            click.echo("Looking for %s library in registry" % click.style(
                filters['name'], fg="cyan"))
        query = []
        for key in filters:
            if key not in ("name", "authors", "frameworks", "platforms"):
                continue
            values = filters[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] if key.endswith("s") else key, value))

        lib_info = None
        result = util.get_api_result(
            "/v2/lib/search", dict(query=" ".join(query)), cache_valid="1h")
        if result['total'] == 1:
            lib_info = result['items'][0]
        elif result['total'] > 1:
            if silent and not interactive:
                lib_info = result['items'][0]
            else:
                click.secho(
                    "Conflict: More than one library has been found "
                    "by request %s:" % json.dumps(filters),
                    fg="yellow",
                    err=True)
                for item in result['items']:
                    commands.lib.print_lib_item(item)

                if not interactive:
                    click.secho(
                        "Automatically chose the first available library "
                        "(use `--interactive` option to make a choice)",
                        fg="yellow",
                        err=True)
                    lib_info = result['items'][0]
                else:
                    deplib_id = click.prompt(
                        "Please choose library ID",
                        type=click.Choice(
                            [str(i['id']) for i in result['items']]))
                    for item in result['items']:
                        if item['id'] == int(deplib_id):
                            lib_info = item
                            break

        if not lib_info:
            if filters.keys() == ["name"]:
                raise exception.LibNotFound(filters['name'])
            else:
                raise exception.LibNotFound(str(filters))
        if not silent:
            click.echo("Found: %s" % click.style(
                "https://platformio.org/lib/show/{id}/{name}".format(
                    **lib_info),
                fg="blue"))
        return int(lib_info['id'])
Exemplo n.º 46
0
 def get_manifest(cls):
     try:
         return cls._cached_manifest
     except AttributeError:
         cls._cached_manifest = get_api_result("/packages")
     return cls._cached_manifest
Exemplo n.º 47
0
def lib_show(library, json_output):
    lm = LibraryManager()
    name, requirements, _ = lm.parse_pkg_uri(library)
    lib_id = lm.search_lib_id({
        "name": name,
        "requirements": requirements
    },
                              silent=json_output,
                              interactive=not json_output)
    lib = get_api_result("/lib/info/%d" % lib_id, cache_valid="1d")
    if json_output:
        return click.echo(json.dumps(lib))

    click.secho(lib['name'], fg="cyan")
    click.echo("=" * len(lib['name']))
    click.secho("#ID: %d" % lib['id'], bold=True)
    click.echo(lib['description'])
    click.echo()

    click.echo(
        "Version: %s, released %s" %
        (lib['version']['name'],
         time.strftime("%c", util.parse_date(lib['version']['released']))))
    click.echo("Manifest: %s" % lib['confurl'])
    for key in ("homepage", "repository", "license"):
        if key not in lib or not lib[key]:
            continue
        if isinstance(lib[key], list):
            click.echo("%s: %s" % (key.title(), ", ".join(lib[key])))
        else:
            click.echo("%s: %s" % (key.title(), lib[key]))

    blocks = []

    _authors = []
    for author in lib.get("authors", []):
        _data = []
        for key in ("name", "email", "url", "maintainer"):
            if not author[key]:
                continue
            if key == "email":
                _data.append("<%s>" % author[key])
            elif key == "maintainer":
                _data.append("(maintainer)")
            else:
                _data.append(author[key])
        _authors.append(" ".join(_data))
    if _authors:
        blocks.append(("Authors", _authors))

    blocks.append(("Keywords", lib['keywords']))
    for key in ("frameworks", "platforms"):
        if key not in lib or not lib[key]:
            continue
        blocks.append(("Compatible %s" % key, [i['title'] for i in lib[key]]))
    blocks.append(("Headers", lib['headers']))
    blocks.append(("Examples", lib['examples']))
    blocks.append(("Versions", [
        "%s, released %s" %
        (v['name'], time.strftime("%c", util.parse_date(v['released'])))
        for v in lib['versions']
    ]))
    blocks.append(("Unique Downloads", [
        "Today: %s" % lib['dlstats']['day'],
        "Week: %s" % lib['dlstats']['week'],
        "Month: %s" % lib['dlstats']['month']
    ]))

    for (title, rows) in blocks:
        click.echo()
        click.secho(title, bold=True)
        click.echo("-" * len(title))
        for row in rows:
            click.echo(row)

    return True
Exemplo n.º 48
0
def lib_search(query, json_output, page, noninteractive, **filters):
    if not query:
        query = []
    if not isinstance(query, list):
        query = list(query)

    for key, values in filters.items():
        for value in values:
            query.append('%s:"%s"' % (key, value))

    result = get_api_result(
        "/v2/lib/search",
        dict(query=" ".join(query), page=page),
        cache_valid="1d")

    if json_output:
        click.echo(json.dumps(result))
        return

    if result['total'] == 0:
        click.secho(
            "Nothing has been found by your request\n"
            "Try a less-specific search or use truncation (or wildcard) "
            "operator",
            fg="yellow",
            nl=False)
        click.secho(" *", fg="green")
        click.secho("For example: DS*, PCA*, DHT* and etc.\n", fg="yellow")
        click.echo("For more examples and advanced search syntax, "
                   "please use documentation:")
        click.secho(
            "https://docs.platformio.org/page/userguide/lib/cmd_search.html\n",
            fg="cyan")
        return

    click.secho(
        "Found %d libraries:\n" % result['total'],
        fg="green" if result['total'] else "yellow")

    while True:
        for item in result['items']:
            print_lib_item(item)

        if (int(result['page']) * int(result['perpage']) >= int(
                result['total'])):
            break

        if noninteractive:
            click.echo()
            click.secho(
                "Loading next %d libraries... Press Ctrl+C to stop!" %
                result['perpage'],
                fg="yellow")
            click.echo()
            time.sleep(5)
        elif not click.confirm("Show next libraries?"):
            break
        result = get_api_result(
            "/v2/lib/search", {
                "query": " ".join(query),
                "page": int(result['page']) + 1
            },
            cache_valid="1d")
Exemplo n.º 49
0
def test_api_cache(monkeypatch, isolated_pio_home):
    api_kwargs = {"url": "/stats", "cache_valid": "10s"}
    result = util.get_api_result(**api_kwargs)
    assert result and "boards" in result
    monkeypatch.setattr(util, '_internet_on', lambda: False)
    assert util.get_api_result(**api_kwargs) == result
Exemplo n.º 50
0
def lib_register(config_url):
    result = get_api_result("/lib/register", data=dict(config_url=config_url))
    if "message" in result and result['message']:
        click.secho(result['message'], fg="green" if "successed" in result and
                    result['successed'] else "red")
Exemplo n.º 51
0
 def get_registered_boards():
     return util.get_api_result("/boards", cache_valid="365d")
Exemplo n.º 52
0
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from math import ceil
from os.path import dirname, isfile, join, realpath
from sys import exit as sys_exit
from sys import path

path.append("..")

from platformio import util
from platformio.managers.platform import PlatformFactory, PlatformManager

API_PACKAGES = util.get_api_result("/packages")
API_FRAMEWORKS = util.get_api_result("/frameworks")
BOARDS = PlatformManager().get_installed_boards()
PLATFORM_MANIFESTS = PlatformManager().get_installed()


def is_compat_platform_and_framework(platform, framework):
    p = PlatformFactory.newPlatform(platform)
    for pkg in p.packages.keys():
        if pkg.startswith("framework-%s" % framework):
            return True
    return False


def generate_boards(boards):
Exemplo n.º 53
0
def lib_stats(json_output):
    result = get_api_result("/lib/stats", cache_valid="1h")

    if json_output:
        return click.echo(json.dumps(result))

    printitem_tpl = "{name:<33} {url}"
    printitemdate_tpl = "{name:<33} {date:23} {url}"

    def _print_title(title):
        click.secho(title.upper(), bold=True)
        click.echo("*" * len(title))

    def _print_header(with_date=False):
        click.echo((printitemdate_tpl if with_date else printitem_tpl).format(
            name=click.style("Name", fg="cyan"),
            date="Date",
            url=click.style("Url", fg="blue")))

        terminal_width, _ = click.get_terminal_size()
        click.echo("-" * terminal_width)

    def _print_lib_item(item):
        click.echo(
            (printitemdate_tpl if "date" in item else printitem_tpl).format(
                name=click.style(item['name'], fg="cyan"),
                date=str(
                    arrow.get(item['date']).humanize() if "date" in
                    item else ""),
                url=click.style("http://platformio.org/lib/show/%s/%s" %
                                (item['id'], quote(item['name'])),
                                fg="blue")))

    def _print_tag_item(name):
        click.echo(
            printitem_tpl.format(
                name=click.style(name, fg="cyan"),
                url=click.style("http://platformio.org/lib/search?query=" +
                                quote("keyword:%s" % name),
                                fg="blue")))

    for key in ("updated", "added"):
        _print_title("Recently " + key)
        _print_header(with_date=True)
        for item in result.get(key, []):
            _print_lib_item(item)
        click.echo()

    _print_title("Recent keywords")
    _print_header(with_date=False)
    for item in result.get("lastkeywords"):
        _print_tag_item(item)
    click.echo()

    _print_title("Popular keywords")
    _print_header(with_date=False)
    for item in result.get("topkeywords"):
        _print_tag_item(item)
    click.echo()

    for key, title in (("dlday", "Today"), ("dlweek", "Week"), ("dlmonth",
                                                                "Month")):
        _print_title("Featured: " + title)
        _print_header(with_date=False)
        for item in result.get(key, []):
            _print_lib_item(item)
        click.echo()
Exemplo n.º 54
0
    def search_for_library(  # pylint: disable=too-many-branches
            filters,
            silent=False,
            interactive=False):
        assert isinstance(filters, dict)
        assert "name" in filters
        if not silent:
            click.echo("Looking for %s library in registry" %
                       click.style(filters['name'], fg="cyan"))
        query = []
        for key in filters:
            if key not in ("name", "authors", "frameworks", "platforms"):
                continue
            values = filters[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] if key.endswith("s") else key, value))

        lib_info = None
        result = util.get_api_result("/v2/lib/search",
                                     dict(query=" ".join(query)),
                                     cache_valid="1h")
        if result['total'] == 1:
            lib_info = result['items'][0]
        elif result['total'] > 1:
            if silent and not interactive:
                lib_info = result['items'][0]
            else:
                click.secho("Conflict: More than one library has been found "
                            "by request %s:" % json.dumps(filters),
                            fg="yellow",
                            err=True)
                for item in result['items']:
                    commands.lib.print_lib_item(item)

                if not interactive:
                    click.secho(
                        "Automatically chose the first available library "
                        "(use `--interactive` option to make a choice)",
                        fg="yellow",
                        err=True)
                    lib_info = result['items'][0]
                else:
                    deplib_id = click.prompt("Please choose library ID",
                                             type=click.Choice([
                                                 str(i['id'])
                                                 for i in result['items']
                                             ]))
                    for item in result['items']:
                        if item['id'] == int(deplib_id):
                            lib_info = item
                            break

        if not lib_info:
            if filters.keys() == ["name"]:
                raise exception.LibNotFound(filters['name'])
            else:
                raise exception.LibNotFound(str(filters))
        if not silent:
            click.echo("Found: %s" % click.style(
                "http://platformio.org/lib/show/{id}/{name}".format(
                    **lib_info),
                fg="blue"))
        return lib_info
Exemplo n.º 55
0
 def get_manifest(cls):
     return util.get_api_result("/packages/manifest")
Exemplo n.º 56
0
 def get_registered_boards():
     return util.get_api_result("/boards", cache_valid="7d")