Ejemplo n.º 1
0
def _add_to_local_store_from_s3(pi: PluginInfo,
                                plugins_local_dir: Path,
                                force: bool = False):
    target = plugins_local_dir / pi.get_filename()
    if not force and target.exists():
        lib.log(f"Found: {target} (Skipping)")
    else:
        try:
            s3.download_file(pi.get_s3_bucket(), pi.get_s3_name(),
                             target.as_posix())
        except s3.S3Error as e:
            lib.log_error(str(e))
        else:
            lib.log(
                f"Downloaded {pi.get_s3_bucket()}/{pi.get_s3_name()} and saved to {target}"
            )
Ejemplo n.º 2
0
def _add_to_local_store_from_uri(plugins_local_dir: Path,
                                 uri: str,
                                 force: bool = False):
    full_name = uri.rsplit("/", 1)[-1]
    try:
        PluginInfo.make_from_filename(
            full_name)  # Will raise if name is invalid.
    except ValueError:
        lib.log_error("That doesn't look like a plugin. Aborting!", abort=True)
    store_filename = plugins_local_dir / full_name
    if not force and store_filename.exists():
        lib.log(f"Found: {store_filename} (Skipping)")
    else:
        filename, headers = urllib.request.urlretrieve(uri,
                                                       tempfile.mkstemp()[1])
        shutil.move(filename, store_filename)
        lib.log(f"Added to local store: {store_filename}")
Ejemplo n.º 3
0
 def test_make_from_s3(self, example_filename):
     filename, name, variant, version = example_filename
     pi = PluginInfo.make_from_s3("bucket", f"s3_path/{filename}")
     assert pi.name == name
     assert pi.variant == variant
     assert pi.semver == semver.VersionInfo.parse(version)
     assert pi.get_s3_bucket() == "bucket"
     assert pi.get_s3_path() == "s3_path"
     assert pi.get_s3_name() == f"s3_path/{filename}"
Ejemplo n.º 4
0
 def test_make_from_filename(self, pi_from_filename):
     """Quickly assert it works. Heavy lifting done in get_name_variant_version_from_filename."""
     pi, filename, name, variant, version = pi_from_filename
     assert pi.name == name
     assert pi.variant == variant
     assert pi.semver == semver.VersionInfo.parse(version)
     assert pi == PluginInfo.make_from_name_variant_version(name, variant, version)
     assert pi.get_s3_bucket() is None
     assert pi.get_s3_path() is None
     assert pi.get_s3_name() == filename
Ejemplo n.º 5
0
 def test_filter(self, plugininfo_list):
     pis = PluginInfos(plugininfo_list)
     expected = [
         PluginInfo.make_from_name_variant_version(*nvv)
         for nvv in [
             ("bar", "", "1.0.0"),
             ("bar", "trial", "1.0.0"),
             ("foo", "", "2.0.0"),
         ]
     ]
     assert sorted(PluginSpecs(["foo-2", "bar-ANY"]).filter(pis)) == expected
Ejemplo n.º 6
0
def _get_available_from_local_store(local_versions: PluginInfos,
                                    pi: PluginInfo) -> str:
    available_pi = local_versions.latest_version_matching_spec(
        # filter for name-and-variant
        PluginSpec(pi.name, pi.variant))
    if available_pi:
        available_version = available_pi.formatted_version()
        available = ("<same>" if available_version == pi.formatted_version()
                     else available_version)
    else:
        available = ""
    return available
Ejemplo n.º 7
0
 def test_filter_to_latest(self, plugininfo_list):
     pis = PluginInfos(plugininfo_list)
     expected = [
         PluginInfo.make_from_name_variant_version(*nvv)
         for nvv in [
             ("bar", "", "1.0.0"),
             ("bar", "trial", "1.0.0"),
             ("foo", "", "2.0.0"),
             ("foo", "trial", "1.0.0"),
             ("foo", "zzz", "1.0.0"),
         ]
     ]
     assert sorted(pis.filter_to_latest()) == expected
Ejemplo n.º 8
0
def install(obj, versions, show_logs, latest_existing, plugins):
    """Install/upgrade plugins by name, from files, or from a versions.toml file.

    Plugins provided as files are put in the local store before being installed.

    When described by name alone, the latest plugin of that name in the local store will be used.

    Plugins specified in the versions.toml file will be taken from the local store.

    """
    plugins_local_dir = obj["plugins_local_dir"]
    plugins_force = obj["plugins_force"]
    host = obj["host"]

    # Create a list of installation candidates.
    to_install_candidates = []
    for plugin in plugins:
        plugin_filename = Path(plugin).resolve()
        if plugin_filename.is_file():
            # If it looks like a file then just add it.
            _add_to_local_store_from_uri(plugins_local_dir,
                                         plugin_filename.as_uri(),
                                         force=True)
            to_install_candidates.append(
                PluginInfo.make_from_filename(plugin_filename))
        else:
            # Else assume it is a spec for a plugin already in the local store.
            to_install_candidates.append(PluginSpec.make_from_string(plugin))
    if versions:
        # Assume plugins already present in local store.
        to_install_candidates.extend(
            PluginSpecs.make_from_version_dict(
                lib.read_toml(Path(versions).expanduser())))
    if latest_existing:
        to_install_candidates.extend(
            PluginSpec(pi.name, pi.variant)
            for pi in PluginInfos.make_from_encapsia(host))

    # Work out and list installation plan.
    # to_install_candidates = sorted(PluginInfos(to_install_candidates))
    installed = PluginInfos.make_from_encapsia(host)
    local_store = PluginInfos.make_from_local_store(plugins_local_dir)
    plan = _create_install_plan(to_install_candidates,
                                installed,
                                local_store,
                                force_install=plugins_force)
    to_install = [i[0] for i in plan if i[4] != "skip"]
    headers = ["name*", "existing version**", "new version**", "action"]
    lib.log(tabulate([i[1:] for i in plan], headers=headers))
    _log_message_explaining_headers()

    # Seek confirmation unless force.
    if to_install and not plugins_force:
        click.confirm(
            "Do you wish to proceed with the above plan?",
            abort=True,
        )

    # Install them.
    lib.log("")
    if to_install:
        api = lib.get_api(**obj)
        for pi in to_install:
            success = _install_plugin(api,
                                      plugins_local_dir / pi.get_filename(),
                                      print_output=show_logs)
        if not success:
            lib.log_error("Some plugins failed to install.", abort=True)
    else:
        lib.log("Nothing to do!")
Ejemplo n.º 9
0
def presorted_plugininfo_list(name_variant_version_list):
    return [
        PluginInfo.make_from_name_variant_version(name, variant, version)
        for name, variant, version in sorted(name_variant_version_list)
    ]
Ejemplo n.º 10
0
def pi_from_filename(example_filename):
    filename, name, variant, version = example_filename
    return PluginInfo.make_from_filename(filename), filename, name, variant, version
Ejemplo n.º 11
0
 def test_filter(self, plugininfo_list, spec, expected):
     pis = PluginInfos(plugininfo_list)
     expected = [PluginInfo.make_from_name_variant_version(*nvv) for nvv in expected]
     assert sorted(PluginSpec.make_from_string(spec).filter(pis)) == expected
Ejemplo n.º 12
0
 def test_get_name_variant_version_from_filename_fails(self, filename):
     with pytest.raises(ValueError):
         PluginInfo.get_name_variant_version_from_filename(filename)
Ejemplo n.º 13
0
 def test_latest(self, plugininfo_list):
     pis = PluginInfos(plugininfo_list)
     assert pis.latest() == PluginInfo.make_from_name_variant_version(
         "foo", "zzz", "1.0.0"
     )
Ejemplo n.º 14
0
 def test_comparison(self, comparison, nvv1, nvv2):
     pi1 = PluginInfo.make_from_name_variant_version(*nvv1)
     pi2 = PluginInfo.make_from_name_variant_version(*nvv2)
     t1 = (nvv1[0], nvv1[1], semver.VersionInfo.parse(nvv1[2]))
     t2 = (nvv2[0], nvv2[1], semver.VersionInfo.parse(nvv2[2]))
     assert comparison(pi1, pi2) is comparison(t1, t2)
Ejemplo n.º 15
0
 def test_parse_version(self, version, expected):
     assert PluginInfo._parse_version(version) == expected
Ejemplo n.º 16
0
 def test_latest_version_matching_spec(self, plugininfo_list, spec, expected_nvv):
     pis = PluginInfos(plugininfo_list)
     expected = PluginInfo.make_from_name_variant_version(*expected_nvv)
     assert pis.latest_version_matching_spec(spec) == expected
Ejemplo n.º 17
0
 def test_get_name_variant_version_from_filename(self, filename, expected):
     assert PluginInfo.get_name_variant_version_from_filename(filename) == expected