Merge "Remove regex functionality from rbcrun" am: bd78ed0726
am: 1b7d02336d
Original change: https://android-review.googlesource.com/c/platform/build/+/2048503 Change-Id: I0fd1862b3949efe75b73ca13786ca430bcfd9f1c Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
commit
39a0861614
5 changed files with 40 additions and 57 deletions
|
@ -385,7 +385,7 @@ def _addsuffix(suffix, string_or_list):
|
|||
|
||||
def __words(string_or_list):
|
||||
if type(string_or_list) == "list":
|
||||
return string_or_list
|
||||
string_or_list = " ".join(string_or_list)
|
||||
return _mkstrip(string_or_list).split()
|
||||
|
||||
# Handle manipulation functions.
|
||||
|
@ -504,10 +504,15 @@ def _filter_out(pattern, text):
|
|||
Return:
|
||||
list of words
|
||||
"""
|
||||
rex = __mk2regex(__words(pattern))
|
||||
patterns = [__mkparse_pattern(x) for x in __words(pattern)]
|
||||
res = []
|
||||
for w in __words(text):
|
||||
if not _regex_match(rex, w):
|
||||
match = False
|
||||
for p in patterns:
|
||||
if __mkpattern_matches(p, w):
|
||||
match = True
|
||||
break
|
||||
if not match:
|
||||
res.append(w)
|
||||
return res
|
||||
|
||||
|
@ -519,11 +524,13 @@ def _filter(pattern, text):
|
|||
which stands for any sequence of characters.
|
||||
text: string or list of words.
|
||||
"""
|
||||
rex = __mk2regex(__words(pattern))
|
||||
patterns = [__mkparse_pattern(x) for x in __words(pattern)]
|
||||
res = []
|
||||
for w in __words(text):
|
||||
if _regex_match(rex, w):
|
||||
res.append(w)
|
||||
for p in patterns:
|
||||
if __mkpattern_matches(p, w):
|
||||
res.append(w)
|
||||
break
|
||||
return res
|
||||
|
||||
def _notdir(paths):
|
||||
|
@ -533,15 +540,6 @@ def _notdir(paths):
|
|||
"""
|
||||
return " ".join([__base(w) for w in __words(paths)])
|
||||
|
||||
def __mk2regex(words):
|
||||
"""Returns regular expression equivalent to Make pattern."""
|
||||
|
||||
# TODO(asmundak): this will mishandle '\%'
|
||||
return "^(" + "|".join([w.replace("%", ".*", 1) for w in words if w]) + ")$"
|
||||
|
||||
def _regex_match(regex, w):
|
||||
return rblf_regex(regex, w)
|
||||
|
||||
def _require_artifacts_in_path(paths, allowed_paths):
|
||||
"""TODO."""
|
||||
pass
|
||||
|
@ -598,7 +596,11 @@ def _mkinfo(file, message = ""):
|
|||
|
||||
|
||||
def __mkparse_pattern(pattern):
|
||||
"""Parses Make's patsubst pattern."""
|
||||
"""Parses Make's patsubst pattern.
|
||||
|
||||
This is equivalent to pattern.split('%', 1), except it
|
||||
also takes into account escaping the % symbols.
|
||||
"""
|
||||
in_escape = False
|
||||
res = []
|
||||
acc = ""
|
||||
|
@ -618,6 +620,21 @@ def __mkparse_pattern(pattern):
|
|||
res.append(acc)
|
||||
return res
|
||||
|
||||
def __mkpattern_matches(pattern, word):
|
||||
"""Returns if a pattern matches a given word.
|
||||
|
||||
The pattern must be a list of strings of length at most 2.
|
||||
This checks if word is either equal to the pattern or
|
||||
starts/ends with the two parts of the pattern.
|
||||
"""
|
||||
if len(pattern) > 2:
|
||||
fail("Pattern can have at most 2 components")
|
||||
elif len(pattern) == 1:
|
||||
return pattern[0]==word
|
||||
else:
|
||||
return ((len(word) >= len(pattern[0])+len(pattern[1]))
|
||||
and word.startswith(pattern[0])
|
||||
and word.endswith(pattern[1]))
|
||||
|
||||
def __mkpatsubst_word(parsed_pattern,parsed_subst, word):
|
||||
(before, after) = parsed_pattern
|
||||
|
@ -639,12 +656,11 @@ def _mkpatsubst(pattern, replacement, s):
|
|||
$1 in regex terms).
|
||||
"""
|
||||
parsed_pattern = __mkparse_pattern(pattern)
|
||||
words = s if type(s) == "list" else _mkstrip(s).split(" ")
|
||||
if len(parsed_pattern) == 1:
|
||||
out_words = [ replacement if x == pattern else x for x in words]
|
||||
out_words = [ replacement if x == pattern else x for x in __words(s)]
|
||||
else:
|
||||
parsed_replacement = __mkparse_pattern(replacement)
|
||||
out_words = [__mkpatsubst_word(parsed_pattern, parsed_replacement, x) for x in words]
|
||||
out_words = [__mkpatsubst_word(parsed_pattern, parsed_replacement, x) for x in __words(s)]
|
||||
return out_words if type(s) == "list" else " ".join(out_words)
|
||||
|
||||
|
||||
|
|
|
@ -53,7 +53,11 @@ assert_eq(["foo/%"], rblf.mkpatsubst("%", "%/%", ["foo"]))
|
|||
assert_eq(["from/a:to/a", "from/b:to/b"], rblf.product_copy_files_by_pattern("from/%", "to/%", "a b"))
|
||||
|
||||
assert_eq([], rblf.filter(["a", "", "b"], "f"))
|
||||
assert_eq(["", "b"], rblf.filter_out(["a", "" ], ["a", "", "b"] ))
|
||||
assert_eq(["ab%c", "axyzb%c"], rblf.filter(["a%b%c"], ["ab%c", "axyzb%c", "axyzb%cd", "axyzbwc"]))
|
||||
assert_eq(["abc", "bcd"], rblf.filter(["a%", "b%"], ["abc", "def", "bcd", "xabc"]))
|
||||
assert_eq(["b", "ab"], rblf.filter_out(["a", "" ], ["a", "", "b", "ab"]))
|
||||
assert_eq(["c"], rblf.filter_out(["a", "b" ], ["a", "b", "c"]))
|
||||
assert_eq(["c"], rblf.filter_out(["a%", "b" ], ["abc", "b", "c"]))
|
||||
|
||||
assert_eq("foo.c no_folder", rblf.notdir(["src/foo.c", "no_folder"]))
|
||||
assert_eq("foo.c no_folder", rblf.notdir("src/foo.c no_folder"))
|
||||
|
|
|
@ -20,7 +20,6 @@ import (
|
|||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"go.starlark.net/starlark"
|
||||
|
@ -125,23 +124,6 @@ func fileExists(_ *starlark.Thread, b *starlark.Builtin, args starlark.Tuple,
|
|||
return starlark.True, nil
|
||||
}
|
||||
|
||||
// regexMatch(pattern, s) returns True if s matches pattern (a regex)
|
||||
func regexMatch(_ *starlark.Thread, b *starlark.Builtin, args starlark.Tuple,
|
||||
kwargs []starlark.Tuple) (starlark.Value, error) {
|
||||
var pattern, s string
|
||||
if err := starlark.UnpackPositionalArgs(b.Name(), args, kwargs, 2, &pattern, &s); err != nil {
|
||||
return starlark.None, err
|
||||
}
|
||||
match, err := regexp.MatchString(pattern, s)
|
||||
if err != nil {
|
||||
return starlark.None, err
|
||||
}
|
||||
if match {
|
||||
return starlark.True, nil
|
||||
}
|
||||
return starlark.False, nil
|
||||
}
|
||||
|
||||
// wildcard(pattern, top=None) expands shell's glob pattern. If 'top' is present,
|
||||
// the 'top/pattern' is globbed and then 'top/' prefix is removed.
|
||||
func wildcard(_ *starlark.Thread, b *starlark.Builtin, args starlark.Tuple,
|
||||
|
@ -291,8 +273,6 @@ func setup(env []string) {
|
|||
"rblf_file_exists": starlark.NewBuiltin("rblf_file_exists", fileExists),
|
||||
// To convert find-copy-subdir and product-copy-files-by pattern
|
||||
"rblf_find_files": starlark.NewBuiltin("rblf_find_files", find),
|
||||
// To convert makefile's $(filter ...)/$(filter-out)
|
||||
"rblf_regex": starlark.NewBuiltin("rblf_regex", regexMatch),
|
||||
// To convert makefile's $(shell cmd)
|
||||
"rblf_shell": starlark.NewBuiltin("rblf_shell", shell),
|
||||
// Output to stderr
|
||||
|
|
|
@ -147,10 +147,6 @@ func TestLoad(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestRegex(t *testing.T) {
|
||||
exerciseStarlarkTestFile(t, "testdata/regex.star")
|
||||
}
|
||||
|
||||
func TestShell(t *testing.T) {
|
||||
if err := os.Setenv("TEST_DATA_DIR", dataDir()); err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
13
tools/rbcrun/testdata/regex.star
vendored
13
tools/rbcrun/testdata/regex.star
vendored
|
@ -1,13 +0,0 @@
|
|||
# Tests rblf_regex
|
||||
load("assert.star", "assert")
|
||||
|
||||
|
||||
def test():
|
||||
pattern = "^(foo.*bar|abc.*d|1.*)$"
|
||||
for w in ("foobar", "fooxbar", "abcxd", "123"):
|
||||
assert.true(rblf_regex(pattern, w), "%s should match %s" % (w, pattern))
|
||||
for w in ("afoobar", "abcde"):
|
||||
assert.true(not rblf_regex(pattern, w), "%s should not match %s" % (w, pattern))
|
||||
|
||||
|
||||
test()
|
Loading…
Reference in a new issue