-
Notifications
You must be signed in to change notification settings - Fork 694
Recode extract_image_id.sh in python (as well as the compare_ids_test rule) #448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recode extract_image_id.sh in python (as well as the compare_ids_test rule) #448
Conversation
|
|
||
| for image in {tars} | ||
| do | ||
| current_id=$({id_script_path} $image) |
There was a problem hiding this comment.
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:
- Make this also a python script
- create a build target for the python script (one for each if you expect users to use extract_image_id.py independently of this test) (e.g., this is the target for extract_config.py: https://github.com/bazelbuild/rules_docker/blob/master/container/BUILD#L28)
- In the skylark rule declare a dependency on the python target (e.g., this is how layer_tools declares a dep on a script to extract a config: https://github.com/bazelbuild/rules_docker/blob/master/container/layer_tools.bzl#L196)
- Use the python target as executable from the skylark rule (e.g., this is how the extract config script is invoked: https://github.com/bazelbuild/rules_docker/blob/master/container/layer_tools.bzl#L23)
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)?
There was a problem hiding this comment.
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.
contrib/compare_ids_test.bzl
Outdated
| "{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]), |
There was a problem hiding this comment.
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"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- This should be a python script, not a template
- this build file should not have an exports_files target, it should have a py_binary target(s) (e.g., https://github.com/bazelbuild/rules_docker/blob/master/container/BUILD#L67)
contrib/compare_ids_test.bzl
Outdated
| allow_files = True, | ||
| single_file = True, | ||
| default = "compare_ids_test.sh.tpl", | ||
| default = "compare_ids_test.py.tpl", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also updated compare_ods_fail_test to work for this as well
| exports_files(["compare_ids_test.bzl"]) | ||
|
|
||
| exports_files(["extract_image_id.sh"]) | ||
| exports_files(["extract_image_id.py"]) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
contrib/compare_ids_test.bzl
Outdated
| # 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] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use ctx.executables https://docs.bazel.build/versions/master/skylark/lib/ctx.html#executable
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
Made the compare_ids_fail_test copy the contrib/BUILD file so as to not have to duplicate code
nlopezgi
left a comment
There was a problem hiding this 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
contrib/compare_ids_test.py
Outdated
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import sys | ||
| import os |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
contrib/extract_image_id.py
Outdated
|
|
||
| if __name__ == "__main__": | ||
| tar_path = sys.argv[1] | ||
| path = sys.argv[1] |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
Changed extract_image_id.sh to extract_image_id.py,
all uses of it will need to be converted over.