Sort rblf_wildcard results and remove file existence functions

Kati's $(wildcard) results are guaranteed to be sorted,
but go's filepath.Glob() is not.

Remove the file existence functions because they can
just be replaced with wildcards.

Bug: 226974242
Test: go test
Change-Id: I02fb6292b932bc28cd856ec3c7cb9ed9e96ca630
This commit is contained in:
Cole Faust
2022-04-26 12:03:19 -07:00
parent 5f6235e580
commit c7b8b6ed88
4 changed files with 11 additions and 27 deletions

View File

@@ -20,6 +20,7 @@ import (
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"go.starlark.net/starlark"
@@ -111,19 +112,6 @@ func loader(thread *starlark.Thread, module string) (starlark.StringDict, error)
return e.globals, e.err
}
// fileExists returns True if file with given name exists.
func fileExists(_ *starlark.Thread, b *starlark.Builtin, args starlark.Tuple,
kwargs []starlark.Tuple) (starlark.Value, error) {
var path string
if err := starlark.UnpackPositionalArgs(b.Name(), args, kwargs, 1, &path); err != nil {
return starlark.None, err
}
if _, err := os.Stat(path); err != nil {
return starlark.False, nil
}
return starlark.True, 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,
@@ -150,6 +138,10 @@ func wildcard(_ *starlark.Thread, b *starlark.Builtin, args starlark.Tuple,
files[i] = strings.TrimPrefix(files[i], prefix)
}
}
// Kati uses glob(3) with no flags, which means it's sorted
// because GLOB_NOSORT is not passed. Go's glob is not
// guaranteed to sort the results.
sort.Strings(files)
return makeStringList(files), nil
}
@@ -269,8 +261,6 @@ func setup(env []string) {
"struct": starlark.NewBuiltin("struct", starlarkstruct.Make),
"rblf_cli": structFromEnv(env),
"rblf_env": structFromEnv(os.Environ()),
// To convert makefile's $(wildcard foo)
"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 $(shell cmd)

View File

@@ -4,9 +4,6 @@ load("assert.star", "assert")
def test():
myname = "file_ops.star"
assert.true(rblf_file_exists("."), "./ exists ")
assert.true(rblf_file_exists(myname), "the file %s does exist" % myname)
assert.true(not rblf_file_exists("no_such_file"), "the file no_such_file does not exist")
files = rblf_wildcard("*.star")
assert.true(myname in files, "expected %s in %s" % (myname, files))
files = rblf_wildcard("*.star", rblf_env.TEST_DATA_DIR)

View File

@@ -2,6 +2,6 @@
load("assert.star", "assert")
# Make sure that builtins are defined for the loaded module, too
assert.true(rblf_file_exists("module1.star"))
assert.true(not rblf_file_exists("no_such file"))
assert.true(rblf_wildcard("module1.star"))
assert.true(not rblf_wildcard("no_such file"))
test = "module1"