Skip to content

Conversation

@ArthurRab
Copy link
Contributor

Changed extract_image_id.sh to extract_image_id.py,
all uses of it will need to be converted over.


for image in {tars}
do
current_id=$({id_script_path} $image)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I apologize, I thought this pattern of using sh.tpl + python was common, but I looked in more detail and this is the wrong approach to invoke a python tool from a skylark rule.
I looked at examples, what you need to do is:

Also related, how similar is the functionality you have in your py script to the one in extract_config.py? could you reuse extract_config.py (asking as that script claims it extracts the v2.2 config file)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline. Used similar but modified method to make it work for a test.

"{id}": ctx.attr.id,
"{tars}": tars_string,
"{id_script_path}": ctx.file._id_extract_script.short_path,
"{tars}": repr([i.short_path for i in tar_files]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can be passed as args to a pythons script, no need to use a template.
You can use ctx.actions.run (e.g., https://github.com/bazelbuild/rules_docker/blob/master/contrib/passwd.bzl#L80) using as executable the py_binary target (note how the example uses the //container:build_tar target as the executable)

contrib/BUILD Outdated
exports_files(["structure-test.sh.tpl"])

exports_files(["compare_ids_test.sh.tpl"])
exports_files(["compare_ids_test.py.tpl"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allow_files = True,
single_file = True,
default = "compare_ids_test.sh.tpl",
default = "compare_ids_test.py.tpl",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ArthurRab ArthurRab changed the title Recode extract_image_id.sh in python Recode extract_image_id.sh in python (as well as the compare_ids_test rule) Jul 18, 2018
exports_files(["compare_ids_test.bzl"])

exports_files(["extract_image_id.sh"])
exports_files(["extract_image_id.py"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should not need to export this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use this file in the compare_ids_fail_test.bzl as it needs to copy it into the new workspace it produces to test the failing cases of the compare_ids_test rule.

# The compare_ids_test target (produced by the py_binary rule) produces 2 files,
# the executable and the source code. This retrieves the executable. A better method
# of retrieving it would be ideal, but I could not find one.
compare_ids_test_exectuable = ctx.files._compare_ids_test_script[1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


def _impl(ctx):
test_code = """ '
py_binary(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you need this duplicated from the BUILD file above here?
If its because your test is rewriting the file contrib/BUILD, then do not re write it, write a new BUILD file in some subdirectory (e.g., contrib/fail_test/BUILD)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to copy the BUILD instead of duplicate code

ArthurRab and others added 3 commits July 19, 2018 11:24
Made the compare_ids_fail_test copy the contrib/BUILD file so as to not have to duplicate code
Copy link
Contributor

@nlopezgi nlopezgi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks good overall, could you also ask Xin to go over the python bits and review

# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import os
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os and sys are not used. Can you run lint on all the py files

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. Did pyformat, forgot lint. Fixed.


if __name__ == "__main__":
tar_path = sys.argv[1]
path = sys.argv[1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to create variable path

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

@xingao267 xingao267 merged commit a25f00c into bazelbuild:master Jul 20, 2018
@ArthurRab ArthurRab deleted the recode_extract_image_id.sh_in_python branch July 20, 2018 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants