From edab446b6c1c126c639c7161413200fa4b003247 Mon Sep 17 00:00:00 2001 From: Alexandr Timofeev Date: Tue, 28 May 2019 10:22:45 +0300 Subject: [PATCH] Feature/remove sys reqs wildcard (#5176) * Add a case of removing system requirements by a wildcard * Add tests to check removing system requirements by a wildcard * Create remove_system_reqs Conan API functions * Call package layout directly Co-Authored-By: Javier G. Sogo * Remove function from the cache * Move the output close the to the call in the api * Restore the lost decorator --- conans/client/command.py | 22 ++++++------- conans/client/conan_api.py | 20 +++++++++--- conans/test/integration/system_reqs_test.py | 35 ++++++++++++++++++++- 3 files changed, 58 insertions(+), 19 deletions(-) diff --git a/conans/client/command.py b/conans/client/command.py index 59e4307fb18..34d2e5c4b85 100644 --- a/conans/client/command.py +++ b/conans/client/command.py @@ -16,7 +16,7 @@ from conans.client.printer import Printer from conans.errors import ConanException, ConanInvalidConfiguration, NoRemoteAvailable, \ ConanMigrationError -from conans.model.ref import ConanFileReference, PackageReference +from conans.model.ref import ConanFileReference, PackageReference, check_valid_ref from conans.unicode import get_cwd from conans.util.config_parser import get_bool_from_text from conans.util.files import exception_message_safe @@ -938,10 +938,6 @@ def remove(self, *args): self._warn_python2() - # NOTE: returns the expanded pattern (if a pattern was given), and checks - # that the query parameter wasn't abused - reference = self._check_query_parameter(args.pattern_or_reference, args.query) - if args.packages is not None and args.query: raise ConanException("'-q' and '-p' parameters can't be used at the same time") @@ -958,16 +954,16 @@ def remove(self, *args): self._user_io.out.info("Cache locks removed") return elif args.system_reqs: - if not reference: - raise ConanException("Please specify a valid package reference to be cleaned") if args.packages: raise ConanException("'-t' and '-p' parameters can't be used at the same time") - try: - self._conan.remove_system_reqs(reference) - self._user_io.out.info("Cache system_reqs from %s has been removed" % reference) - return - except Exception as error: - raise ConanException("Unable to remove system_reqs: %s" % error) + if not args.pattern_or_reference: + raise ConanException("Please specify a valid pattern or reference to be cleaned") + + if check_valid_ref(args.pattern_or_reference, allow_pattern=False): + ref = ConanFileReference.loads(args.pattern_or_reference) + return self._conan.remove_system_reqs(ref) + + return self._conan.remove_system_reqs_by_pattern(args.pattern_or_reference) else: if not args.pattern_or_reference: raise ConanException('Please specify a pattern to be removed ("*" for all)') diff --git a/conans/client/conan_api.py b/conans/client/conan_api.py index a1b915dfea1..148f233dcb0 100644 --- a/conans/client/conan_api.py +++ b/conans/client/conan_api.py @@ -53,6 +53,7 @@ from conans.model.version import Version from conans.model.workspace import Workspace from conans.paths import BUILD_INFO, CONANINFO, get_conan_user_home +from conans.search.search import search_recipes from conans.tools import set_global_instances from conans.unicode import get_cwd from conans.util.files import exception_message_safe, mkdir, save_files @@ -964,13 +965,22 @@ def remote_clean(self): return self._cache.registry.clear() @api_method - def remove_locks(self): - self._cache.remove_locks() + def remove_system_reqs(self, ref): + try: + self._cache.package_layout(ref).remove_system_reqs() + self._user_io.out.info( + "Cache system_reqs from %s has been removed" % repr(ref)) + except Exception as error: + raise ConanException("Unable to remove system_reqs: %s" % error) @api_method - def remove_system_reqs(self, reference): - ref = ConanFileReference.loads(reference) - self._cache.package_layout(ref).remove_system_reqs() + def remove_system_reqs_by_pattern(self, pattern): + for ref in search_recipes(self._cache, pattern=pattern): + self.remove_system_reqs(ref) + + @api_method + def remove_locks(self): + self._cache.remove_locks() @api_method def profile_list(self): diff --git a/conans/test/integration/system_reqs_test.py b/conans/test/integration/system_reqs_test.py index 46bbb0b54e4..d4acd52bb86 100644 --- a/conans/test/integration/system_reqs_test.py +++ b/conans/test/integration/system_reqs_test.py @@ -177,11 +177,44 @@ def remove_system_reqs_test(self): self.assertIn("*+Running system requirements+*", client.user_io.out) self.assertTrue(os.path.exists(system_reqs_path)) + # Wildcard system_reqs removal + ref_other = ConanFileReference.loads("Test/0.1@user/channel_other") + system_reqs_path_other = os.path.dirname(client.cache.package_layout(ref_other).system_reqs()) + + client.run("create . user/channel_other") + client.run("remove --system-reqs '*'") + self.assertIn("Cache system_reqs from Test/0.1@user/channel has been removed", + client.user_io.out) + self.assertIn("Cache system_reqs from Test/0.1@user/channel_other has been removed", + client.user_io.out) + self.assertFalse(os.path.exists(system_reqs_path)) + self.assertFalse(os.path.exists(system_reqs_path_other)) + + # Check that wildcard isn't triggered randomly + client.run("create . user/channel_other") + client.run("remove --system-reqs Test/0.1@user/channel") + self.assertIn("Cache system_reqs from Test/0.1@user/channel has been removed", + client.user_io.out) + self.assertNotIn("Cache system_reqs from Test/0.1@user/channel_other has been removed", + client.user_io.out) + self.assertFalse(os.path.exists(system_reqs_path)) + self.assertTrue(os.path.exists(system_reqs_path_other)) + + # Check partial wildcard + client.run("create . user/channel") + client.run("remove --system-reqs Test/0.1@user/channel_*") + self.assertNotIn("Cache system_reqs from Test/0.1@user/channel has been removed", + client.user_io.out) + self.assertIn("Cache system_reqs from Test/0.1@user/channel_other has been removed", + client.user_io.out) + self.assertTrue(os.path.exists(system_reqs_path)) + self.assertFalse(os.path.exists(system_reqs_path_other)) + def invalid_remove_reqs_test(self): client = TestClient() with six.assertRaisesRegex(self, Exception, - "ERROR: Please specify a valid package reference to be cleaned"): + "ERROR: Please specify a valid pattern or reference to be cleaned"): client.run("remove --system-reqs") # wrong file reference should be treated as error