Glob during loading phase

40 views
Skip to first unread message

tim.b...@gmail.com

unread,
Mar 7, 2017, 3:33:43 AM3/7/17
to bazel-discuss
Say I'm in the implementation= part of a skylark rule, and I've got a bunch of files via ctx.files.<attr>. Is there an easy way to glob through these files? I'm trying to avoid having to write my own glob function that iterates over each File's file.path. I feel like there's just a utility function I'm missing somewhere...

p.s. The skylark references link is 404ing: https://bazel.build/versions/master/docs/skylark-overview.html

Damien Martin-guillerez

unread,
Mar 7, 2017, 3:54:28 AM3/7/17
to tim.b...@gmail.com, bazel-discuss
What are you trying to do exactly? Because ctx.files.<attr> should already returns you an iterable with the list of files in it, what would you want more?

About the PS: https://bazel.build/versions/master/docs/skylark/concepts.html <-- this is the correct skylark overview, where did you found the other link?

On Tue, Mar 7, 2017 at 9:33 AM <tim.b...@gmail.com> wrote:
Say I'm in the implementation= part of a skylark rule, and I've got a bunch of files via ctx.files.<attr>. Is there an easy way to glob through these files? I'm trying to avoid having to write my own glob function that iterates over each File's file.path. I feel like there's just a utility function I'm missing somewhere...

p.s. The skylark references link is 404ing: https://bazel.build/versions/master/docs/skylark-overview.html

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/6447ce37-932e-488c-92bf-c3d8253eaef6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dmitry Lomov

unread,
Mar 7, 2017, 4:02:59 AM3/7/17
to Damien Martin-guillerez, tim.b...@gmail.com, bazel-discuss
I am guessing you want a regexp over file names in rule implementation? We do not have regexps but that would be  a valid feature request.

Filed https://github.com/bazelbuild/bazel/issues/2643 for broken 'Extensions | Reference' link on the NavBar (I am guessing that is how you git that url)

On Tue, Mar 7, 2017 at 9:54 AM, 'Damien Martin-guillerez' via bazel-discuss <bazel-...@googlegroups.com> wrote:
What are you trying to do exactly? Because ctx.files.<attr> should already returns you an iterable with the list of files in it, what would you want more?

About the PS: https://bazel.build/versions/master/docs/skylark/concepts.html <-- this is the correct skylark overview, where did you found the other link?
On Tue, Mar 7, 2017 at 9:33 AM <tim.b...@gmail.com> wrote:
Say I'm in the implementation= part of a skylark rule, and I've got a bunch of files via ctx.files.<attr>. Is there an easy way to glob through these files? I'm trying to avoid having to write my own glob function that iterates over each File's file.path. I feel like there's just a utility function I'm missing somewhere...

p.s. The skylark references link is 404ing: https://bazel.build/versions/master/docs/skylark-overview.html

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAN3hOS_90MiPnxGpZGt2ZHUXmxKZoMej3O6h9NyJq7SEfXhxkQ%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Google Germany GmbH
Erika-Mann-Straße 33, 80636 München, Germany

Dmitry Lomov

unread,
Mar 7, 2017, 4:06:09 AM3/7/17
to Damien Martin-guillerez, tim.b...@gmail.com, bazel-discuss
(also rule implementation is "analysis phase". Loading phase is when the usual globs apply - as in `java_library(.., src = glob('*.java'))`)

On Tue, Mar 7, 2017 at 10:02 AM, Dmitry Lomov <dsl...@google.com> wrote:
I am guessing you want a regexp over file names in rule implementation? We do not have regexps but that would be  a valid feature request.

Filed https://github.com/bazelbuild/bazel/issues/2643 for broken 'Extensions | Reference' link on the NavBar (I am guessing that is how you git that url)

On Tue, Mar 7, 2017 at 9:54 AM, 'Damien Martin-guillerez' via bazel-discuss <bazel-discuss@googlegroups.com> wrote:
What are you trying to do exactly? Because ctx.files.<attr> should already returns you an iterable with the list of files in it, what would you want more?

About the PS: https://bazel.build/versions/master/docs/skylark/concepts.html <-- this is the correct skylark overview, where did you found the other link?

On Tue, Mar 7, 2017 at 9:33 AM <tim.b...@gmail.com> wrote:
Say I'm in the implementation= part of a skylark rule, and I've got a bunch of files via ctx.files.<attr>. Is there an easy way to glob through these files? I'm trying to avoid having to write my own glob function that iterates over each File's file.path. I feel like there's just a utility function I'm missing somewhere...

p.s. The skylark references link is 404ing: https://bazel.build/versions/master/docs/skylark-overview.html

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/6447ce37-932e-488c-92bf-c3d8253eaef6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAN3hOS_90MiPnxGpZGt2ZHUXmxKZoMej3O6h9NyJq7SEfXhxkQ%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Google Germany GmbH
Erika-Mann-Straße 33, 80636 München, Germany

Damien Martin-guillerez

unread,
Mar 7, 2017, 4:08:32 AM3/7/17
to Dmitry Lomov, tim.b...@gmail.com, bazel-discuss
If that's the case, you can use list filtering, e.g.: files = [f for ctx.files.myattr if f.basename[-4:] == ".jar"]

On Tue, Mar 7, 2017 at 10:03 AM 'Dmitry Lomov' via bazel-discuss <bazel-...@googlegroups.com> wrote:
I am guessing you want a regexp over file names in rule implementation? We do not have regexps but that would be  a valid feature request.

Filed https://github.com/bazelbuild/bazel/issues/2643 for broken 'Extensions | Reference' link on the NavBar (I am guessing that is how you git that url)

On Tue, Mar 7, 2017 at 9:54 AM, 'Damien Martin-guillerez' via bazel-discuss <bazel-...@googlegroups.com> wrote:
What are you trying to do exactly? Because ctx.files.<attr> should already returns you an iterable with the list of files in it, what would you want more?

About the PS: https://bazel.build/versions/master/docs/skylark/concepts.html <-- this is the correct skylark overview, where did you found the other link?
On Tue, Mar 7, 2017 at 9:33 AM <tim.b...@gmail.com> wrote:
Say I'm in the implementation= part of a skylark rule, and I've got a bunch of files via ctx.files.<attr>. Is there an easy way to glob through these files? I'm trying to avoid having to write my own glob function that iterates over each File's file.path. I feel like there's just a utility function I'm missing somewhere...

p.s. The skylark references link is 404ing: https://bazel.build/versions/master/docs/skylark-overview.html

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Google Germany GmbH
Erika-Mann-Straße 33, 80636 München, Germany

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAObu7DFeM1KkxbsF9MjBz7%2BpjzUHnR9uOG-GoqOtYOz5U1WOjQ%40mail.gmail.com.

tim.b...@gmail.com

unread,
Mar 7, 2017, 2:32:06 PM3/7/17
to bazel-discuss, dsl...@google.com, tim.b...@gmail.com
Yup, I'm doing manual filtering now. It works, but gets a bit convoluted for wildcard matching.

I'd like to be able to do what Dmitry suggested and simply filter the filenames passed into the rule implementation somehow; doesn't particularly matter to me if it's implemented via glob-style "foo/bar/**/*.baz" or regex. I'm imagining something along the lines of:

1) filtered_files = ctx.files.<attr>.find("foo/bar/[^\/]*.h")
2) filtered_files = [f for ctx.files.<attr> if f.basename.matches(".*\.c$")]

Happy to file a request on github if either sounds reasonable.

George Gensure

unread,
Mar 7, 2017, 3:25:11 PM3/7/17
to tim.b...@gmail.com, bazel-discuss, dsl...@google.com
I have a patch that I will be putting up shortly which causes the {plur,singul}arity of an attribute to be expanded through outputs via templates:

def impl(ctx):
  myoutputs = ctx.outputs.myoutputs
  print len(myoutputs)
  ... something with a generating action for each myoutputs

myrule = rule(
  impl,
  attrs={"srcs": attr.label_list(allow_files=FileType([".in"])},
  outputs={"myoutputs": "%{srcs}.out"}
)

myrule(
  name="glob_myrule",
  srcs=glob([*.in])
)

This is currently not possible under bazel, it fails when the list is interpreted into the string iirc, but the diff is fairly simple.  Think that will resolve your issue?

-George

To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/b1d2091a-bf72-40db-86e8-341fd159e58e%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages