From 11c72d72d6852e7ab660c1855e7e40808ff8aa2a Mon Sep 17 00:00:00 2001 From: Owen W. Taylor Date: Oct 05 2021 16:45:35 +0000 Subject: Hide modified repositories from the output of 'fedora-third-party list' When a Flatpak repository (such as the Fedora Flathub Selection) repository is modified to remove the filter (typically by reinstalling a new flatpakrepo file for the same repository), hide it from the 'fedora-third-party list' output, so that a tool like GNOME Software will no longer display it as a third-party repository. --- diff --git a/doc/fedora-third-party.1.md b/doc/fedora-third-party.1.md index aa0b2e1..292e6fa 100644 --- a/doc/fedora-third-party.1.md +++ b/doc/fedora-third-party.1.md @@ -66,7 +66,7 @@ Look for newly added third-party repository definitions, and if third-party repositories are enabled, enable DNF repositories and create Flatpak remotes. -### list [*--csv* | *--columns=col1,col2*] +### list [*-a* | *--all* | *--csv* | *--columns=col1,col2*] Prints the list of currently configured repositories. By default, the list will be pretty-printed in tabular form with headers. The *--csv* option can @@ -82,6 +82,10 @@ get stable output - the default set of columns may change. With the current columns, you can assume that the values will not be quoted. +Flatpak repositories with the `keep_unfiltered` option set are hidden by +default from the list if there is a matching remote without a filter. +*a* or *--all* can be passed to show all repositories. + ## CONFIGURATION Repositories are configured by files in `/usr/lib/fedora-third-party/conf.d`. @@ -102,12 +106,14 @@ Example of configuration for a Flatpak remote: [somerepo] # The Flatpak remote must match and also be 'somerepo' type=flatpak + # Path to a flatpakrepo file defining the remote. If it's a relative # path, it will be interpreted relative to the directory where the # config file is. flatpakrepo=somerepo.flatpakrepo # If present, 'fedora-third-party disable' will leave this repository -# untouched if it has been modified to not have a filter. +# untouched if it has been modified to not have a filter, and it will +# also be hidden from the output of 'fedora-third-party list' keep_unfiltered=yes ``` diff --git a/fedora_third_party/cli.py b/fedora_third_party/cli.py index 4501168..381ed55 100644 --- a/fedora_third_party/cli.py +++ b/fedora_third_party/cli.py @@ -81,20 +81,26 @@ def query(cfg: config.Config, quiet: bool): @cli.command() +@click.option("-a", "--all", is_flag=True, + help="Show repositories even if they are replaced by a system repository") @click.option("--csv", is_flag=True, help="Print output in csv form") @click.option("--columns", help="Comma separated list of columns [name,type]", default="name,type") @pass_config -def list(cfg: config.Config, csv: bool, columns: str): +def list(cfg: config.Config, all: bool, csv: bool, columns: str): """List currently configured repositories""" table = Table() table.add_column("name", "Name", "name") table.add_column("type", "Type", "type") - repositories = sorted(cfg.list_repositories(), key=lambda x: x.name) + repositories = cfg.list_repositories() + if not all: + repositories = [r for r in cfg.list_repositories() if not r.is_hidden()] + + repositories = sorted(repositories, key=lambda x: x.name) table.dump(repositories, csv=csv, columns=[x.strip() for x in columns.split(",")]) diff --git a/fedora_third_party/repository.py b/fedora_third_party/repository.py index badeee8..26192ad 100644 --- a/fedora_third_party/repository.py +++ b/fedora_third_party/repository.py @@ -29,6 +29,9 @@ class Repository(ABC): def disable(self): pass + def is_hidden(self): + return False + def is_seen(self): return self.cfg.is_repository_seen(self) @@ -81,6 +84,17 @@ class FlatpakRepository(Repository): if not os.path.isabs(self.flatpakrepo): self.flatpakrepo = str(self.cfg.libconfdir / self.flatpakrepo) + def get_existing_remote(self): + return next((r for r in get_flatpak_remotes() if r.name == self.name), None) + + def is_hidden(self): + if self.keep_unfiltered: + remote = self.get_existing_remote() + if remote and remote.filter is None: + return True + + return False + def run_flatpak(self, args): command = ["flatpak"] command += args @@ -89,8 +103,7 @@ class FlatpakRepository(Repository): subprocess.check_call(command) def enable(self): - remotes = get_flatpak_remotes() - if not any(self.name == remote.name for remote in remotes): + if not self.get_existing_remote(): self.run_flatpak(['remote-add', '--from', self.name, self.flatpakrepo]) self.cfg.set_repository_added(self, True) @@ -99,7 +112,7 @@ class FlatpakRepository(Repository): if not self.cfg.is_repository_added(self): return - remote = next((r for r in get_flatpak_remotes() if r.name == self.name), None) + remote = self.get_existing_remote() if remote is None: return diff --git a/tests/test_cli.py b/tests/test_cli.py index aa8b5ff..efe94f5 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -116,6 +116,32 @@ def test_cli_list(setup): """) +def test_cli_list_hide_unfiltered(setup): + setup.set_flatpak_remotes({ + "flathub": "-" + }) + + runner = CliRunner() + result = runner.invoke(cli, ['list'], obj=setup.cfg) + assert not result.exception + assert result.exit_code == 0 + assert result.output == dedent("""\ + Name Type + ------------------------------- ---- + rpmfusion-nonfree-nvidia-driver dnf + """) + + result = runner.invoke(cli, ['list', '-a'], obj=setup.cfg) + assert not result.exception + assert result.exit_code == 0 + assert result.output == dedent("""\ + Name Type + ------------------------------- ------- + flathub flatpak + rpmfusion-nonfree-nvidia-driver dnf + """) + + def test_cli_list_csv(setup): runner = CliRunner() result = runner.invoke(cli, ['list', '--csv'], obj=setup.cfg) diff --git a/tests/test_repository.py b/tests/test_repository.py index ba99b91..977be02 100644 --- a/tests/test_repository.py +++ b/tests/test_repository.py @@ -108,3 +108,15 @@ def test_flatpak_repository_keep_unfiltered(setup): repository.disable() assert setup.calls == [ ] + + +def test_flatpak_repository_hide_unfiltered(setup): + repository = make_flatpak_repository(setup) + + repository.enable() + + setup.set_flatpak_remotes({ + "myrepo": "-" + }) + + assert repository.is_hidden()