Gentoo Archives: gentoo-commits

From: Zac Medico <zmedico@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/portage:master commit in: man/, lib/_emerge/
Date: Tue, 08 Sep 2020 00:18:46
Message-Id: 1599524274.03ae0d95797f68cf86748ae3da184f3018e8c64c.zmedico@gentoo
1 commit: 03ae0d95797f68cf86748ae3da184f3018e8c64c
2 Author: Zac Medico <zmedico <AT> gentoo <DOT> org>
3 AuthorDate: Tue Sep 1 02:49:50 2020 +0000
4 Commit: Zac Medico <zmedico <AT> gentoo <DOT> org>
5 CommitDate: Tue Sep 8 00:17:54 2020 +0000
6 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=03ae0d95
7
8 emerge --search: auto-detect regular expressions (bug 737480)
9
10 Automatically detect regular expressions when the search string
11 contains any of these regular expression characters or character
12 sequences:
13
14 ^ $ * [ ] { } | ? .+
15
16 This simplifies usage, so that users no longer have to remember
17 to prefix regular expressions with the % character. The new
18 behavior can be disabled by --regex-search-auto=n, in case the
19 regular expressions interpretation causes some kind of problem.
20
21 Note that fuzzy search and regular expression search are
22 mutually exclusive, and fuzzy search remains the default for
23 search strings that do not contain any regular expression
24 characters.
25
26 Bug: https://bugs.gentoo.org/737480
27 Signed-off-by: Zac Medico <zmedico <AT> gentoo.org>
28
29 lib/_emerge/actions.py | 1 +
30 lib/_emerge/main.py | 6 ++++++
31 lib/_emerge/search.py | 12 +++++++++++-
32 man/emerge.1 | 12 +++++++++++-
33 4 files changed, 29 insertions(+), 2 deletions(-)
34
35 diff --git a/lib/_emerge/actions.py b/lib/_emerge/actions.py
36 index a4ecfe43d..f57269817 100644
37 --- a/lib/_emerge/actions.py
38 +++ b/lib/_emerge/actions.py
39 @@ -2036,6 +2036,7 @@ def action_search(root_config, myopts, myfiles, spinner):
40 search_index=myopts.get("--search-index", "y") != "n",
41 search_similarity=myopts.get("--search-similarity"),
42 fuzzy=myopts.get("--fuzzy-search") != "n",
43 + regex_auto=myopts.get("--regex-search-auto") != "n",
44 )
45 for mysearch in myfiles:
46 try:
47
48 diff --git a/lib/_emerge/main.py b/lib/_emerge/main.py
49 index 975738762..5075f7f57 100644
50 --- a/lib/_emerge/main.py
51 +++ b/lib/_emerge/main.py
52 @@ -709,6 +709,12 @@ def parse_opts(tmpcmdline, silent=False):
53 "action" : "store"
54 },
55
56 + "--regex-search-auto": {
57 + "help" : "Enable or disable automatic regular expression detection for search actions",
58 + "choices": y_or_n,
59 + "default": "y",
60 + },
61 +
62 "--root": {
63 "help" : "specify the target root filesystem for merging packages",
64 "action" : "store"
65
66 diff --git a/lib/_emerge/search.py b/lib/_emerge/search.py
67 index a59191c1a..61eed0827 100644
68 --- a/lib/_emerge/search.py
69 +++ b/lib/_emerge/search.py
70 @@ -28,7 +28,7 @@ class search:
71 #
72 def __init__(self, root_config, spinner, searchdesc,
73 verbose, usepkg, usepkgonly, search_index=True,
74 - search_similarity=None, fuzzy=True):
75 + search_similarity=None, fuzzy=True, regex_auto=False):
76 """Searches the available and installed packages for the supplied search key.
77 The list of available and installed packages is created at object instantiation.
78 This makes successive searches faster."""
79 @@ -42,6 +42,7 @@ class search:
80 self.spinner = None
81 self.root_config = root_config
82 self.setconfig = root_config.setconfig
83 + self.regex_auto = regex_auto
84 self.fuzzy = fuzzy
85 self.search_similarity = (80 if search_similarity is None
86 else search_similarity)
87 @@ -259,6 +260,15 @@ class search:
88 if '/' in self.searchkey:
89 match_category = 1
90 fuzzy = False
91 +
92 + if self.regex_auto and not regexsearch and re.search(r'[\^\$\*\[\]\{\}\|\?]|\.\+', self.searchkey) is not None:
93 + try:
94 + re.compile(self.searchkey, re.I)
95 + except Exception:
96 + pass
97 + else:
98 + regexsearch = True
99 +
100 if regexsearch:
101 self.searchre=re.compile(self.searchkey,re.I)
102 else:
103
104 diff --git a/man/emerge.1 b/man/emerge.1
105 index fe7d05a21..c1bcd0220 100644
106 --- a/man/emerge.1
107 +++ b/man/emerge.1
108 @@ -229,7 +229,9 @@ explicitly discarded by running `emaint \-\-fix cleanresume` (see
109 .BR \-\-search ", " \-s
110 Searches for matches of the supplied string in the ebuild repository.
111 By default emerge uses a case-insensitive simple search, but you can
112 -enable a regular expression search by prefixing the search string with %.
113 +enable a regular expression search by prefixing the search string with %
114 +(the % prefix can often be omitted if the
115 +\fB\-\-regex\-search\-auto\fR option is enabled).
116 For example, \fBemerge \-\-search "%^kde"\fR searches for any package whose
117 name starts with "kde"; \fBemerge \-\-search "%gcc$"\fR searches for any
118 package that ends with "gcc"; \fBemerge \-\-search "office"\fR searches for
119 @@ -764,6 +766,14 @@ matching packages due to \fB\-\-rebuild\fR.
120 A space separated list of package names or slot atoms. Emerge will not rebuild
121 packages that depend on matching packages due to \fB\-\-rebuild\fR.
122 .TP
123 +.BR "\-\-regex\-search\-auto < y | n >"
124 +Enable or disable automatic regular expression detection for search actions.
125 +If this option is enabled (the default), then regular expression search
126 +is automatically enabled when the search string is a valid regular expression
127 +which contains any of these commonly used regular expression characters or
128 +character sequences:
129 +^ $ * [ ] { } | ? .+
130 +.TP
131 .BR \-\-oneshot ", " \-1
132 Emerge as normal, but do not add the packages to the world file
133 for later updating.