Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config clean command sketch #17514

Draft
wants to merge 4 commits into
base: develop2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions conan/api/conan_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from conan.api.subapi.local import LocalAPI
from conan.api.subapi.lockfile import LockfileAPI
from conan.api.subapi.workspace import WorkspaceAPI
from conan import conan_version
from conan.api.subapi.config import ConfigAPI
from conan.api.subapi.download import DownloadAPI
from conan.api.subapi.export import ExportAPI
Expand All @@ -19,7 +18,6 @@
from conan.api.subapi.remove import RemoveAPI
from conan.api.subapi.search import SearchAPI
from conan.api.subapi.upload import UploadAPI
from conans.client.migrations import ClientMigrator
from conan.errors import ConanException
from conan.internal.paths import get_conan_user_home
from conans.model.version_range import validate_conan_version
Expand All @@ -37,10 +35,11 @@ def __init__(self, cache_folder=None):
self.cache_folder = self.workspace.home_folder() or cache_folder or get_conan_user_home()
self.home_folder = self.cache_folder # Lets call it home, deprecate "cache"

# Migration system
migrator = ClientMigrator(self.cache_folder, conan_version)
migrator.migrate()
self.reinit()

def reinit(self):
self.config = ConfigAPI(self)
self.config.migrate()
self.command = CommandAPI(self)
self.remotes = RemotesAPI(self)
# Search recipes by wildcard and packages filtering by configuracion
Expand All @@ -52,7 +51,6 @@ def __init__(self, cache_folder=None):
self.graph = GraphAPI(self)
self.export = ExportAPI(self)
self.remove = RemoveAPI(self)
self.config = ConfigAPI(self)
self.new = NewAPI(self)
self.upload = UploadAPI(self)
self.download = DownloadAPI(self)
Expand Down
24 changes: 23 additions & 1 deletion conan/api/subapi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from conans.model.pkg_type import PackageType
from conans.model.recipe_ref import RecipeReference
from conans.model.settings import Settings
from conans.util.files import load, save
from conans.util.files import load, save, rmdir, remove


class ConfigAPI:
Expand Down Expand Up @@ -188,3 +188,25 @@ def appending_recursive_dict_update(d, u):
appending_recursive_dict_update(settings, settings_user)

return Settings(settings)

def migrate(self):
# Migration system
# TODO: Still needs refactoring - using it here for now for tests
from conans.client.migrations import ClientMigrator
migrator = ClientMigrator(self.home(), conan_version)
migrator.migrate()

def clean(self):
# TODO: Check what we're deleting
contents = os.listdir(self.home())
for content in contents:
# keep packages
if content not in ("p", "version.txt"):
content_path = os.path.join(self.home(), content)
ConanOutput().debug(f"Removing {content_path}")
if os.path.isdir(content_path):
rmdir(content_path)
else:
remove(content_path)
# CHECK: This also generates a remotes.json that is not there after a conan profile show?
self.migrate()
14 changes: 14 additions & 0 deletions conan/cli/commands/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from conan.api.input import UserInput
from conan.api.model import Remote
from conan.api.output import cli_out_write
from conan.cli.command import conan_command, conan_subcommand, OnceArgument
Expand Down Expand Up @@ -132,3 +133,16 @@ def config_show(conan_api, parser, subparser, *args):
args = parser.parse_args(*args)

return conan_api.config.show(args.pattern)


@conan_subcommand()
def config_clean(conan_api, parser, subparser, *args):
"""
Clean the configuration files in the Conan home folder. (Keeping installed packages)
"""
subparser.add_argument("-c", "--confirm", action='store_true',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably doesn't need confirmation. The confirmation only would make sense if applied as the one in other commands:

Do you want to clean "remotes.txt", yes/no?
Do you want to clean "settings.yml" yes/no?
...

So probably just drop the arg, not necessary for this command.

help="Do not request confirmation")
args = parser.parse_args(*args)
ui = UserInput(conan_api.config.get("core:non_interactive"))
if args.confirm or ui.request_boolean("Clear all configuration files?"):
conan_api.config.clean()
19 changes: 19 additions & 0 deletions test/integration/command/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,22 @@ def test_config_show():
tc.run("config show zlib/*:foo")
assert "zlib/*:user.mycategory:foo" in tc.out
assert "zlib/*:user.myothercategory:foo" in tc.out


def test_config_clean():
tc = TestClient(light=True)
tc.run("profile detect --name=foo")
tc.run("remote add bar http://fakeurl")
tc.save_home({"global.conf": "core.upload:retry=7\n",
"extensions/compatibility/mycomp.py": "",
"extensions/commands/cmd_foo.py": "",
})
tc.run("config clean -c")
tc.run("profile list")
assert "foo" not in tc.out
tc.run("remote list")
assert "bar" not in tc.out
tc.run("config show core.upload:retry")
assert "7" not in tc.out
assert not os.path.exists(os.path.join(tc.cache_folder, "extensions"))

Loading