aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2020-08-31 19:49:50 -0700
committerZac Medico <zmedico@gentoo.org>2020-09-07 17:17:54 -0700
commit03ae0d95797f68cf86748ae3da184f3018e8c64c (patch)
treed30baef482b8dc6e19155ad5ca092e57410f5372 /lib/_emerge
parentenv-update: create systemd user-session environment definition (diff)
downloadportage-03ae0d95797f68cf86748ae3da184f3018e8c64c.tar.gz
portage-03ae0d95797f68cf86748ae3da184f3018e8c64c.tar.bz2
portage-03ae0d95797f68cf86748ae3da184f3018e8c64c.zip
emerge --search: auto-detect regular expressions (bug 737480)
Automatically detect regular expressions when the search string contains any of these regular expression characters or character sequences: ^ $ * [ ] { } | ? .+ This simplifies usage, so that users no longer have to remember to prefix regular expressions with the % character. The new behavior can be disabled by --regex-search-auto=n, in case the regular expressions interpretation causes some kind of problem. Note that fuzzy search and regular expression search are mutually exclusive, and fuzzy search remains the default for search strings that do not contain any regular expression characters. Bug: https://bugs.gentoo.org/737480 Signed-off-by: Zac Medico <zmedico@gentoo.org>
Diffstat (limited to 'lib/_emerge')
-rw-r--r--lib/_emerge/actions.py1
-rw-r--r--lib/_emerge/main.py6
-rw-r--r--lib/_emerge/search.py12
3 files changed, 18 insertions, 1 deletions
diff --git a/lib/_emerge/actions.py b/lib/_emerge/actions.py
index a4ecfe43d..f57269817 100644
--- a/lib/_emerge/actions.py
+++ b/lib/_emerge/actions.py
@@ -2036,6 +2036,7 @@ def action_search(root_config, myopts, myfiles, spinner):
search_index=myopts.get("--search-index", "y") != "n",
search_similarity=myopts.get("--search-similarity"),
fuzzy=myopts.get("--fuzzy-search") != "n",
+ regex_auto=myopts.get("--regex-search-auto") != "n",
)
for mysearch in myfiles:
try:
diff --git a/lib/_emerge/main.py b/lib/_emerge/main.py
index 975738762..5075f7f57 100644
--- a/lib/_emerge/main.py
+++ b/lib/_emerge/main.py
@@ -709,6 +709,12 @@ def parse_opts(tmpcmdline, silent=False):
"action" : "store"
},
+ "--regex-search-auto": {
+ "help" : "Enable or disable automatic regular expression detection for search actions",
+ "choices": y_or_n,
+ "default": "y",
+ },
+
"--root": {
"help" : "specify the target root filesystem for merging packages",
"action" : "store"
diff --git a/lib/_emerge/search.py b/lib/_emerge/search.py
index a59191c1a..61eed0827 100644
--- a/lib/_emerge/search.py
+++ b/lib/_emerge/search.py
@@ -28,7 +28,7 @@ class search:
#
def __init__(self, root_config, spinner, searchdesc,
verbose, usepkg, usepkgonly, search_index=True,
- search_similarity=None, fuzzy=True):
+ search_similarity=None, fuzzy=True, regex_auto=False):
"""Searches the available and installed packages for the supplied search key.
The list of available and installed packages is created at object instantiation.
This makes successive searches faster."""
@@ -42,6 +42,7 @@ class search:
self.spinner = None
self.root_config = root_config
self.setconfig = root_config.setconfig
+ self.regex_auto = regex_auto
self.fuzzy = fuzzy
self.search_similarity = (80 if search_similarity is None
else search_similarity)
@@ -259,6 +260,15 @@ class search:
if '/' in self.searchkey:
match_category = 1
fuzzy = False
+
+ if self.regex_auto and not regexsearch and re.search(r'[\^\$\*\[\]\{\}\|\?]|\.\+', self.searchkey) is not None:
+ try:
+ re.compile(self.searchkey, re.I)
+ except Exception:
+ pass
+ else:
+ regexsearch = True
+
if regexsearch:
self.searchre=re.compile(self.searchkey,re.I)
else: