Skip to content

Commit

Permalink
Feature/remove sys reqs wildcard (#5176)
Browse files Browse the repository at this point in the history
* 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 <jgsogo@gmail.com>

* Remove function from the cache

* Move the output close the to the call in the api

* Restore the lost decorator
  • Loading branch information
Minimonium authored and danimtb committed May 28, 2019
1 parent 4e78595 commit edab446
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
22 changes: 9 additions & 13 deletions conans/client/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand All @@ -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)')
Expand Down
20 changes: 15 additions & 5 deletions conans/client/conan_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
35 changes: 34 additions & 1 deletion conans/test/integration/system_reqs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit edab446

Please sign in to comment.