예제 #1
0
    def process_dependencies(self):  # pylint: disable=too-many-branches
        for spec in self.dependencies:
            found = False
            for storage_dir in self.env.GetLibSourceDirs():
                if found:
                    break
                lm = LibraryPackageManager(storage_dir)
                pkg = lm.get_package(spec)
                if not pkg:
                    continue
                for lb in self.env.GetLibBuilders():
                    if pkg.path != lb.path:
                        continue
                    if lb not in self.depbuilders:
                        self.depend_recursive(lb)
                    found = True
                    break
            if found:
                continue

            # look for built-in libraries by a name
            # which don't have package manifest
            for lb in self.env.GetLibBuilders():
                if lb.name != spec:
                    continue
                if lb not in self.depbuilders:
                    self.depend_recursive(lb)
                found = True
                break
예제 #2
0
def test_install_lib_depndencies(isolated_pio_core, tmpdir_factory):
    tmp_dir = tmpdir_factory.mktemp("tmp")

    src_dir = tmp_dir.join("lib-with-deps").mkdir()
    root_dir = src_dir.mkdir("root")
    root_dir.mkdir("src").join("main.cpp").write("#include <stdio.h>")
    root_dir.join("library.json").write("""
{
  "name": "lib-with-deps",
  "version": "2.0.0",
  "dependencies": [
    {
      "owner": "bblanchon",
      "name": "ArduinoJson",
      "version": "^6.16.1"
    },
    {
      "name": "external-repo",
      "version": "https://github.com/milesburton/Arduino-Temperature-Control-Library.git#4a0ccc1"
    }
  ]
}
""")

    lm = LibraryPackageManager(str(tmpdir_factory.mktemp("lib-storage")))
    lm.install("file://%s" % str(src_dir), silent=True)
    installed = lm.get_installed()
    assert len(installed) == 4
    assert set(["external-repo", "ArduinoJson", "lib-with-deps",
                "OneWire"]) == set(p.metadata.name for p in installed)
예제 #3
0
def lib_install(  # pylint: disable=too-many-arguments,unused-argument
    ctx, libraries, save, silent, interactive, force
):
    storage_dirs = ctx.meta[CTX_META_STORAGE_DIRS_KEY]
    storage_libdeps = ctx.meta.get(CTX_META_STORAGE_LIBDEPS_KEY, [])

    installed_pkgs = {}
    for storage_dir in storage_dirs:
        if not silent and (libraries or storage_dir in storage_libdeps):
            print_storage_header(storage_dirs, storage_dir)
        lm = LibraryPackageManager(storage_dir)

        if libraries:
            installed_pkgs = {
                library: lm.install(library, silent=silent, force=force)
                for library in libraries
            }

        elif storage_dir in storage_libdeps:
            builtin_lib_storages = None
            for library in storage_libdeps[storage_dir]:
                try:
                    lm.install(library, silent=silent, force=force)
                except UnknownPackageError as e:
                    if builtin_lib_storages is None:
                        builtin_lib_storages = get_builtin_libs()
                    if not silent or not is_builtin_lib(builtin_lib_storages, library):
                        click.secho("Warning! %s" % e, fg="yellow")

    if save and installed_pkgs:
        _save_deps(ctx, installed_pkgs)
예제 #4
0
def test_find_pkg_root(isolated_pio_core, tmpdir_factory):
    # has manifest
    pkg_dir = tmpdir_factory.mktemp("package-has-manifest")
    root_dir = pkg_dir.join("nested").mkdir().join("folder").mkdir()
    root_dir.join("platform.json").write("")
    pm = PlatformPackageManager()
    found_dir = pm.find_pkg_root(str(pkg_dir), spec=None)
    assert os.path.realpath(str(root_dir)) == os.path.realpath(found_dir)

    # does not have manifest
    pkg_dir = tmpdir_factory.mktemp("package-does-not-have-manifest")
    pkg_dir.join("nested").mkdir().join("folder").mkdir().join(
        "readme.txt").write("")
    pm = PlatformPackageManager()
    with pytest.raises(MissingPackageManifestError):
        pm.find_pkg_root(str(pkg_dir), spec=None)

    # library package without manifest, should find source root
    pkg_dir = tmpdir_factory.mktemp("library-package-without-manifest")
    root_dir = pkg_dir.join("nested").mkdir().join("folder").mkdir()
    root_dir.join("src").mkdir().join("main.cpp").write("")
    root_dir.join("include").mkdir().join("main.h").write("")
    assert os.path.realpath(str(root_dir)) == os.path.realpath(
        LibraryPackageManager.find_library_root(str(pkg_dir)))

    # library manager should create "library.json"
    lm = LibraryPackageManager()
    spec = PackageSpec("[email protected]")
    pkg_root = lm.find_pkg_root(str(pkg_dir), spec)
    manifest_path = os.path.join(pkg_root, "library.json")
    assert os.path.realpath(str(root_dir)) == os.path.realpath(pkg_root)
    assert os.path.isfile(manifest_path)
    manifest = lm.load_manifest(pkg_root)
    assert manifest["name"] == "custom-name"
    assert "0.0.0" in str(manifest["version"])
예제 #5
0
def lib_uninstall(ctx, libraries, save, silent):
    storage_dirs = ctx.meta[CTX_META_STORAGE_DIRS_KEY]
    uninstalled_pkgs = {}
    for storage_dir in storage_dirs:
        print_storage_header(storage_dirs, storage_dir)
        lm = LibraryPackageManager(storage_dir)
        uninstalled_pkgs = {
            library: lm.uninstall(library, silent=silent) for library in libraries
        }

    if save and uninstalled_pkgs:
        _save_deps(ctx, uninstalled_pkgs, action="remove")
예제 #6
0
def test_download(isolated_pio_core):
    url = "https://github.com/platformio/platformio-core/archive/v4.3.4.zip"
    checksum = "69d59642cb91e64344f2cdc1d3b98c5cd57679b5f6db7accc7707bd4c5d9664a"
    lm = LibraryPackageManager()
    archive_path = lm.download(url, checksum, silent=True)
    assert fs.calculate_file_hashsum("sha256", archive_path) == checksum
    lm.cleanup_expired_downloads()
    assert os.path.isfile(archive_path)
    # test outdated downloads
    lm.set_download_utime(archive_path, time.time() - lm.DOWNLOAD_CACHE_EXPIRE - 1)
    lm.cleanup_expired_downloads()
    assert not os.path.isfile(archive_path)
    # check that key is deleted from DB
    with open(lm.get_download_usagedb_path()) as fp:
        assert os.path.basename(archive_path) not in fp.read()
예제 #7
0
def lib_update(  # pylint: disable=too-many-arguments
    ctx, libraries, only_check, dry_run, silent, json_output
):
    storage_dirs = ctx.meta[CTX_META_STORAGE_DIRS_KEY]
    only_check = dry_run or only_check
    json_result = {}
    for storage_dir in storage_dirs:
        if not json_output:
            print_storage_header(storage_dirs, storage_dir)
        lib_deps = ctx.meta.get(CTX_META_STORAGE_LIBDEPS_KEY, {}).get(storage_dir, [])
        lm = LibraryPackageManager(storage_dir)
        _libraries = libraries or lib_deps or lm.get_installed()

        if only_check and json_output:
            result = []
            for library in _libraries:
                spec = None
                pkg = None
                if isinstance(library, PackageItem):
                    pkg = library
                else:
                    spec = PackageSpec(library)
                    pkg = lm.get_package(spec)
                if not pkg:
                    continue
                outdated = lm.outdated(pkg, spec)
                if not outdated.is_outdated(allow_incompatible=True):
                    continue
                manifest = lm.legacy_load_manifest(pkg)
                manifest["versionWanted"] = (
                    str(outdated.wanted) if outdated.wanted else None
                )
                manifest["versionLatest"] = (
                    str(outdated.latest) if outdated.latest else None
                )
                result.append(manifest)
            json_result[storage_dir] = result
        else:
            for library in _libraries:
                to_spec = (
                    None if isinstance(library, PackageItem) else PackageSpec(library)
                )
                try:
                    lm.update(
                        library, to_spec=to_spec, only_check=only_check, silent=silent
                    )
                except UnknownPackageError as e:
                    if library not in lib_deps:
                        raise e

    if json_output:
        return click.echo(
            dump_json_to_unicode(
                json_result[storage_dirs[0]] if len(storage_dirs) == 1 else json_result
            )
        )

    return True
예제 #8
0
def test_install_from_url(isolated_pio_core, tmpdir_factory):
    tmp_dir = tmpdir_factory.mktemp("tmp")
    storage_dir = tmpdir_factory.mktemp("storage")
    lm = LibraryPackageManager(str(storage_dir))

    # install from local directory
    src_dir = tmp_dir.join("local-lib-dir").mkdir()
    src_dir.join("main.cpp").write("")
    spec = PackageSpec("file://%s" % src_dir)
    pkg = lm.install(spec, silent=True)
    assert os.path.isfile(os.path.join(pkg.path, "main.cpp"))
    manifest = lm.load_manifest(pkg)
    assert manifest["name"] == "local-lib-dir"
    assert manifest["version"].startswith("0.0.0+")
    assert spec == pkg.metadata.spec

    # install from local archive
    src_dir = tmp_dir.join("archive-src").mkdir()
    root_dir = src_dir.mkdir("root")
    root_dir.mkdir("src").join("main.cpp").write("#include <stdio.h>")
    root_dir.join("library.json").write(
        '{"name": "manifest-lib-name", "version": "2.0.0"}'
    )
    tarball_path = PackagePacker(str(src_dir)).pack(str(tmp_dir))
    spec = PackageSpec("file://%s" % tarball_path)
    pkg = lm.install(spec, silent=True)
    assert os.path.isfile(os.path.join(pkg.path, "src", "main.cpp"))
    assert pkg == lm.get_package(spec)
    assert spec == pkg.metadata.spec

    # install from registry
    src_dir = tmp_dir.join("registry-1").mkdir()
    src_dir.join("library.properties").write(
        """
name = wifilib
version = 5.2.7
"""
    )
    spec = PackageSpec("company/wifilib @ ^5")
    pkg = lm.install_from_url("file://%s" % src_dir, spec)
    assert str(pkg.metadata.version) == "5.2.7"

    # check package folder names
    lm.memcache_reset()
    assert ["local-lib-dir", "manifest-lib-name", "wifilib"] == [
        os.path.basename(pkg.path) for pkg in lm.get_installed()
    ]
예제 #9
0
    def install_dependencies(self):
        def _is_builtin(spec):
            for lb in self.env.GetLibBuilders():
                if lb.name == spec:
                    return True
            return False

        not_found_specs = []
        for spec in self.dependencies:
            # check if built-in library
            if _is_builtin(spec):
                continue

            found = False
            for storage_dir in self.env.GetLibSourceDirs():
                lm = LibraryPackageManager(storage_dir)
                if lm.get_package(spec):
                    found = True
                    break
            if not found:
                not_found_specs.append(spec)

        did_install = False
        lm = LibraryPackageManager(
            self.env.subst(os.path.join("$PROJECT_LIBDEPS_DIR", "$PIOENV")))
        for spec in not_found_specs:
            try:
                lm.install(spec)
                did_install = True
            except (UnknownPackageError, InternetIsOffline) as e:
                click.secho("Warning! %s" % e, fg="yellow")

        # reset cache
        if did_install:
            DefaultEnvironment().Replace(__PIO_LIB_BUILDERS=None)
예제 #10
0
def system_info(json_output):
    project_config = ProjectConfig()
    data = {}
    data["core_version"] = {"title": "PlatformIO Core", "value": __version__}
    data["python_version"] = {
        "title": "Python",
        "value": "{0}.{1}.{2}-{3}.{4}".format(*list(sys.version_info)),
    }
    data["system"] = {"title": "System Type", "value": util.get_systype()}
    data["platform"] = {
        "title": "Platform",
        "value": platform.platform(terse=True)
    }
    data["filesystem_encoding"] = {
        "title": "File System Encoding",
        "value": compat.get_filesystem_encoding(),
    }
    data["locale_encoding"] = {
        "title": "Locale Encoding",
        "value": compat.get_locale_encoding(),
    }
    data["core_dir"] = {
        "title": "PlatformIO Core Directory",
        "value": project_config.get_optional_dir("core"),
    }
    data["platformio_exe"] = {
        "title":
        "PlatformIO Core Executable",
        "value":
        proc.where_is_program(
            "platformio.exe" if proc.WINDOWS else "platformio"),
    }
    data["python_exe"] = {
        "title": "Python Executable",
        "value": proc.get_pythonexe_path(),
    }
    data["global_lib_nums"] = {
        "title": "Global Libraries",
        "value": len(LibraryPackageManager().get_installed()),
    }
    data["dev_platform_nums"] = {
        "title": "Development Platforms",
        "value": len(PlatformPackageManager().get_installed()),
    }
    data["package_tool_nums"] = {
        "title":
        "Tools & Toolchains",
        "value":
        len(
            ToolPackageManager(
                project_config.get_optional_dir("packages")).get_installed()),
    }

    click.echo(
        json.dumps(data) if json_output else tabulate([(
            item["title"], item["value"]) for item in data.values()]))
예제 #11
0
def get_builtin_libs(storage_names=None):
    # pylint: disable=import-outside-toplevel
    from platformio.package.manager.library import LibraryPackageManager

    items = []
    storage_names = storage_names or []
    pm = PlatformPackageManager()
    for pkg in pm.get_installed():
        p = PlatformFactory.new(pkg)
        for storage in p.get_lib_storages():
            if storage_names and storage["name"] not in storage_names:
                continue
            lm = LibraryPackageManager(storage["path"])
            items.append({
                "name": storage["name"],
                "path": storage["path"],
                "items": lm.legacy_get_installed(),
            })
    return items
예제 #12
0
def test_update_without_metadata(isolated_pio_core, tmpdir_factory):
    storage_dir = tmpdir_factory.mktemp("storage")
    storage_dir.join("legacy-package").mkdir().join("library.json").write(
        '{"name": "AsyncMqttClient-esphome", "version": "0.8"}')
    storage_dir.join("legacy-dep").mkdir().join("library.json").write(
        '{"name": "AsyncTCP-esphome", "version": "1.1.1"}')
    lm = LibraryPackageManager(str(storage_dir))
    pkg = lm.get_package("AsyncMqttClient-esphome")
    outdated = lm.outdated(pkg)
    assert len(lm.get_installed()) == 2
    assert str(pkg.metadata.version) == "0.8.0"
    assert outdated.latest > semantic_version.Version("0.8.0")

    # update
    lm = LibraryPackageManager(str(storage_dir))
    new_pkg = lm.update(pkg, silent=True)
    assert len(lm.get_installed()) == 3
    assert new_pkg.metadata.spec.owner == "ottowinter"
예제 #13
0
def test_install_force(isolated_pio_core, tmpdir_factory):
    lm = LibraryPackageManager(str(tmpdir_factory.mktemp("lib-storage")))
    # install #64 ArduinoJson
    pkg = lm.install("64 @ ^5", silent=True)
    assert pkg.metadata.version.major == 5
    # try install the latest without specification
    pkg = lm.install("64", silent=True)
    assert pkg.metadata.version.major == 5
    assert len(lm.get_installed()) == 1
    # re-install the latest
    pkg = lm.install(64, silent=True, force=True)
    assert len(lm.get_installed()) == 1
    assert pkg.metadata.version.major > 5
예제 #14
0
def lib_list(ctx, json_output):
    storage_dirs = ctx.meta[CTX_META_STORAGE_DIRS_KEY]
    json_result = {}
    for storage_dir in storage_dirs:
        if not json_output:
            print_storage_header(storage_dirs, storage_dir)
        lm = LibraryPackageManager(storage_dir)
        items = lm.legacy_get_installed()
        if json_output:
            json_result[storage_dir] = items
        elif items:
            for item in sorted(items, key=lambda i: i["name"]):
                print_lib_item(item)
        else:
            click.echo("No items found")

    if json_output:
        return click.echo(
            dump_json_to_unicode(
                json_result[storage_dirs[0]] if len(storage_dirs) == 1 else json_result
            )
        )

    return True
예제 #15
0
def lib_install(  # pylint: disable=too-many-arguments,unused-argument
    ctx, libraries, save, silent, interactive, force
):
    storage_dirs = ctx.meta[CTX_META_STORAGE_DIRS_KEY]
    storage_libdeps = ctx.meta.get(CTX_META_STORAGE_LIBDEPS_KEY, [])

    installed_pkgs = {}
    for storage_dir in storage_dirs:
        if not silent and (libraries or storage_dir in storage_libdeps):
            print_storage_header(storage_dirs, storage_dir)
        lm = LibraryPackageManager(storage_dir)

        if libraries:
            installed_pkgs = {
                library: lm.install(library, silent=silent, force=force)
                for library in libraries
            }

        elif storage_dir in storage_libdeps:
            for library in storage_libdeps[storage_dir]:
                lm.install(library, silent=silent, force=force)

    if save and installed_pkgs:
        _save_deps(ctx, installed_pkgs)
예제 #16
0
def cli(ctx, core_packages, only_check, dry_run):
    # cleanup lib search results, cached board and platform lists
    cleanup_content_cache("http")

    only_check = dry_run or only_check

    update_core_packages(only_check)

    if core_packages:
        return

    click.echo()
    click.echo("Platform Manager")
    click.echo("================")
    ctx.invoke(cmd_platform_update, only_check=only_check)

    click.echo()
    click.echo("Library Manager")
    click.echo("===============")
    ctx.meta[CTX_META_STORAGE_DIRS_KEY] = [LibraryPackageManager().package_dir]
    ctx.invoke(cmd_lib_update, only_check=only_check)
예제 #17
0
def lib_show(library, json_output):
    lm = LibraryPackageManager()
    lib_id = lm.reveal_registry_package_id(library, silent=json_output)
    regclient = lm.get_registry_client_instance()
    lib = regclient.fetch_json_data("get", "/v2/lib/info/%d" % lib_id, cache_valid="1h")
    if json_output:
        return click.echo(dump_json_to_unicode(lib))

    title = "{ownername}/{name}".format(**lib)
    click.secho(title, fg="cyan")
    click.echo("=" * len(title))
    click.echo(lib["description"])
    click.echo()

    click.secho("ID: %d" % lib["id"])
    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.get(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
예제 #18
0
def test_update_with_metadata(isolated_pio_core, tmpdir_factory):
    storage_dir = tmpdir_factory.mktemp("storage")
    lm = LibraryPackageManager(str(storage_dir))

    # test non SemVer in registry
    pkg = lm.install("RadioHead @ <1.90", silent=True)
    outdated = lm.outdated(pkg)
    assert str(outdated.current) == "1.89.0"
    assert outdated.latest > semantic_version.Version("1.100.0")

    pkg = lm.install("ArduinoJson @ 5.10.1", silent=True)
    # tesy latest
    outdated = lm.outdated(pkg)
    assert str(outdated.current) == "5.10.1"
    assert outdated.wanted is None
    assert outdated.latest > outdated.current
    assert outdated.latest > semantic_version.Version("5.99.99")

    # test wanted
    outdated = lm.outdated(pkg, PackageSpec("ArduinoJson@~5"))
    assert str(outdated.current) == "5.10.1"
    assert str(outdated.wanted) == "5.13.4"
    assert outdated.latest > semantic_version.Version("6.16.0")

    # update to the wanted 5.x
    new_pkg = lm.update("ArduinoJson@^5",
                        PackageSpec("ArduinoJson@^5"),
                        silent=True)
    assert str(new_pkg.metadata.version) == "5.13.4"
    # check that old version is removed
    assert len(lm.get_installed()) == 2

    # update to the latest
    lm = LibraryPackageManager(str(storage_dir))
    pkg = lm.update("ArduinoJson", silent=True)
    assert pkg.metadata.version == outdated.latest
예제 #19
0
def test_registry(isolated_pio_core):
    lm = LibraryPackageManager()

    # reveal ID
    assert lm.reveal_registry_package_id(PackageSpec(id=13)) == 13
    assert lm.reveal_registry_package_id(PackageSpec(name="OneWire"),
                                         silent=True) == 1
    with pytest.raises(UnknownPackageError):
        lm.reveal_registry_package_id(
            PackageSpec(name="/non-existing-package/"))

    # fetch package data
    assert lm.fetch_registry_package(PackageSpec(id=1))["name"] == "OneWire"
    assert lm.fetch_registry_package(
        PackageSpec(name="ArduinoJson"))["id"] == 64
    assert (lm.fetch_registry_package(
        PackageSpec(id=13, owner="adafruit",
                    name="Renamed library"))["name"] == "Adafruit GFX Library")
    with pytest.raises(UnknownPackageError):
        lm.fetch_registry_package(
            PackageSpec(owner="unknown<>owner", name="/non-existing-package/"))
    with pytest.raises(UnknownPackageError):
        lm.fetch_registry_package(PackageSpec(name="/non-existing-package/"))
예제 #20
0
def test_uninstall(isolated_pio_core, tmpdir_factory):
    tmp_dir = tmpdir_factory.mktemp("tmp")
    storage_dir = tmpdir_factory.mktemp("storage")
    lm = LibraryPackageManager(str(storage_dir))

    # foo @ 1.0.0
    pkg_dir = tmp_dir.join("foo").mkdir()
    pkg_dir.join("library.json").write('{"name": "foo", "version": "1.0.0"}')
    foo_1_0_0_pkg = lm.install_from_url("file://%s" % pkg_dir, "foo")
    # foo @ 1.3.0
    pkg_dir = tmp_dir.join("foo-1.3.0").mkdir()
    pkg_dir.join("library.json").write('{"name": "foo", "version": "1.3.0"}')
    lm.install_from_url("file://%s" % pkg_dir, "foo")
    # bar
    pkg_dir = tmp_dir.join("bar").mkdir()
    pkg_dir.join("library.json").write('{"name": "bar", "version": "1.0.0"}')
    bar_pkg = lm.install("file://%s" % pkg_dir, silent=True)

    assert len(lm.get_installed()) == 3
    assert os.path.isdir(os.path.join(str(storage_dir), "foo"))
    assert os.path.isdir(os.path.join(str(storage_dir), "[email protected]"))

    # check detaching
    assert lm.uninstall("FOO", silent=True)
    assert len(lm.get_installed()) == 2
    assert os.path.isdir(os.path.join(str(storage_dir), "foo"))
    assert not os.path.isdir(os.path.join(str(storage_dir), "[email protected]"))

    # uninstall the rest
    assert lm.uninstall(foo_1_0_0_pkg.path, silent=True)
    assert lm.uninstall(bar_pkg, silent=True)

    assert not lm.get_installed()

    # test uninstall dependencies
    assert lm.install("AsyncMqttClient-esphome @ 0.8.4", silent=True)
    assert len(lm.get_installed()) == 3
    assert lm.uninstall("AsyncMqttClient-esphome",
                        silent=True,
                        skip_dependencies=True)
    assert len(lm.get_installed()) == 2

    lm = LibraryPackageManager(str(storage_dir))
    assert lm.install("AsyncMqttClient-esphome @ 0.8.4", silent=True)
    assert lm.uninstall("AsyncMqttClient-esphome", silent=True)
    assert not lm.get_installed()
예제 #21
0
def test_install_from_registry(isolated_pio_core, tmpdir_factory):
    # Libraries
    lm = LibraryPackageManager(str(tmpdir_factory.mktemp("lib-storage")))
    # library with dependencies
    lm.install("AsyncMqttClient-esphome @ 0.8.4", silent=True)
    assert len(lm.get_installed()) == 3
    pkg = lm.get_package("AsyncTCP-esphome")
    assert pkg.metadata.spec.owner == "ottowinter"
    assert not lm.get_package("non-existing-package")
    # mbed library
    assert lm.install("wolfSSL", silent=True)
    assert len(lm.get_installed()) == 4
    # case sensitive author name
    assert lm.install("DallasTemperature", silent=True)
    assert lm.get_package("OneWire").metadata.version.major >= 2
    assert len(lm.get_installed()) == 6

    # test conflicted names
    lm = LibraryPackageManager(str(
        tmpdir_factory.mktemp("conflicted-storage")))
    lm.install("[email protected]", silent=True)
    lm.install("[email protected]", silent=True)
    assert len(lm.get_installed()) == 2

    # Tools
    tm = ToolPackageManager(str(tmpdir_factory.mktemp("tool-storage")))
    pkg = tm.install("platformio/tool-stlink @ ~1.10400.0", silent=True)
    manifest = tm.load_manifest(pkg)
    assert tm.is_system_compatible(manifest.get("system"))
    assert util.get_systype() in manifest.get("system", [])

    # Test unknown
    with pytest.raises(UnknownPackageError):
        tm.install("unknown-package-tool @ 9.1.1", silent=True)
    with pytest.raises(UnknownPackageError):
        tm.install("owner/unknown-package-tool", silent=True)
예제 #22
0
def lib_search(query, json_output, page, noninteractive, **filters):
    regclient = LibraryPackageManager().get_registry_client_instance()
    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 = regclient.fetch_json_data(
        "get",
        "/v2/lib/search",
        params=dict(query=" ".join(query), page=page),
        cache_valid="1d",
    )

    if json_output:
        click.echo(dump_json_to_unicode(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 = regclient.fetch_json_data(
            "get",
            "/v2/lib/search",
            params=dict(query=" ".join(query), page=int(result["page"]) + 1),
            cache_valid="1d",
        )
예제 #23
0
def check_internal_updates(ctx, what):  # pylint: disable=too-many-branches
    last_check = app.get_state_item("last_check", {})
    interval = int(app.get_setting("check_%s_interval" % what)) * 3600 * 24
    if (time() - interval) < last_check.get(what + "_update", 0):
        return

    last_check[what + "_update"] = int(time())
    app.set_state_item("last_check", last_check)

    http.ensure_internet_on(raise_exception=True)

    outdated_items = []
    pm = PlatformPackageManager() if what == "platforms" else LibraryPackageManager()
    for pkg in pm.get_installed():
        if pkg.metadata.name in outdated_items:
            continue
        conds = [
            pm.outdated(pkg).is_outdated(),
            what == "platforms" and PlatformFactory.new(pkg).are_outdated_packages(),
        ]
        if any(conds):
            outdated_items.append(pkg.metadata.name)

    if not outdated_items:
        return

    terminal_width, _ = click.get_terminal_size()

    click.echo("")
    click.echo("*" * terminal_width)
    click.secho(
        "There are the new updates for %s (%s)" % (what, ", ".join(outdated_items)),
        fg="yellow",
    )

    if not app.get_setting("auto_update_" + what):
        click.secho("Please update them via ", fg="yellow", nl=False)
        click.secho(
            "`platformio %s update`"
            % ("lib --global" if what == "libraries" else "platform"),
            fg="cyan",
            nl=False,
        )
        click.secho(" command.\n", fg="yellow")
        click.secho(
            "If you want to manually check for the new versions "
            "without updating, please use ",
            fg="yellow",
            nl=False,
        )
        click.secho(
            "`platformio %s update --dry-run`"
            % ("lib --global" if what == "libraries" else "platform"),
            fg="cyan",
            nl=False,
        )
        click.secho(" command.", fg="yellow")
    else:
        click.secho("Please wait while updating %s ..." % what, fg="yellow")
        if what == "platforms":
            ctx.invoke(cmd_platform_update, platforms=outdated_items)
        elif what == "libraries":
            ctx.meta[CTX_META_STORAGE_DIRS_KEY] = [pm.package_dir]
            ctx.invoke(cmd_lib_update, libraries=outdated_items)
        click.echo()

        telemetry.send_event(category="Auto", action="Update", label=what.title())

    click.echo("*" * terminal_width)
    click.echo("")
예제 #24
0
def lib_stats(json_output):
    regclient = LibraryPackageManager().get_registry_client_instance()
    result = regclient.fetch_json_data("get", "/v2/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