Loading core/product_config.rbc +35 −19 Original line number Diff line number Diff line Loading @@ -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. Loading Loading @@ -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 Loading @@ -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): for p in patterns: if __mkpattern_matches(p, w): res.append(w) break return res def _notdir(paths): Loading @@ -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 Loading Loading @@ -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 = "" Loading @@ -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 Loading @@ -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) Loading tests/run.rbc +5 −1 Original line number Diff line number Diff line Loading @@ -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")) Loading tools/rbcrun/host.go +0 −20 Original line number Diff line number Diff line Loading @@ -20,7 +20,6 @@ import ( "os" "os/exec" "path/filepath" "regexp" "strings" "go.starlark.net/starlark" Loading Loading @@ -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, Loading Loading @@ -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 Loading tools/rbcrun/host_test.go +0 −4 Original line number Diff line number Diff line Loading @@ -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) Loading tools/rbcrun/testdata/regex.stardeleted 100644 → 0 +0 −13 Original line number Diff line number Diff line # 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
core/product_config.rbc +35 −19 Original line number Diff line number Diff line Loading @@ -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. Loading Loading @@ -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 Loading @@ -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): for p in patterns: if __mkpattern_matches(p, w): res.append(w) break return res def _notdir(paths): Loading @@ -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 Loading Loading @@ -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 = "" Loading @@ -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 Loading @@ -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) Loading
tests/run.rbc +5 −1 Original line number Diff line number Diff line Loading @@ -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")) Loading
tools/rbcrun/host.go +0 −20 Original line number Diff line number Diff line Loading @@ -20,7 +20,6 @@ import ( "os" "os/exec" "path/filepath" "regexp" "strings" "go.starlark.net/starlark" Loading Loading @@ -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, Loading Loading @@ -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 Loading
tools/rbcrun/host_test.go +0 −4 Original line number Diff line number Diff line Loading @@ -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) Loading
tools/rbcrun/testdata/regex.stardeleted 100644 → 0 +0 −13 Original line number Diff line number Diff line # 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()