-
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
Merged
xingao267
merged 21 commits into
bazelbuild:master
from
ArthurRab:recode_extract_image_id.sh_in_python
Jul 20, 2018
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
831991e
Added python version of extract_image_id.sh
c36f6b4
Changed compare_ids_test to use the python script
1efa388
Add invalid tar message to python script
f02bb10
Changed it to work with python2
45a7608
Updated the failing test rule to use the .py script
f2bab73
Removed the old sh script
1bcf506
Comments
6b0b88a
Formatting
847749d
Removed export of deleted file
8fcfae7
Comment correction
a463bd2
Changed the test to use a .py.tpl as well. Not sure if this is the be…
c093c40
WIP
ccc3cca
Added py_binary for extract_image_id
932530c
Changed compare_ids_test to python
d359757
Changed str concat to .format
532e941
Reduce code duplication
59d3858
Merge branch 'master' into recode_extract_image_id.sh_in_python
fbb6918
buildifier
028fec9
Formatting
4a8e6e1
pylint formatting fixes
69f7c62
Removed unnecessary variable
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Copyright 2015 The Bazel Authors. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """Compares the ids of the given valid image tarballs. | ||
|
|
||
| usage: compare_ids_test.py [-h] [--id ID] tars [tars ...] | ||
|
|
||
| positional arguments: | ||
| tars | ||
|
|
||
| optional arguments: | ||
| -h, --help show this help message and exit | ||
| --id ID | ||
|
|
||
| Used in compare_ids_test.bzl | ||
| More info can be found there | ||
|
|
||
| """ | ||
| import argparse | ||
|
|
||
| from extract_image_id import get_id | ||
|
|
||
|
|
||
| def compare_ids(tars, id_=None): | ||
| """Compares the ids of the given valid image tarballs. | ||
|
|
||
| Args: | ||
| tars: list of str paths to the image tarballs | ||
| id_: (optional) the id we want the images to have | ||
| if None, just makes sure they are all the same | ||
|
|
||
| """ | ||
|
|
||
| for image in tars: | ||
| current_id = get_id(image) | ||
| if id_ is None: | ||
| id_ = current_id | ||
| elif current_id != id_: | ||
| exit(1) | ||
|
|
||
| exit(0) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser() | ||
|
|
||
| parser.add_argument("tars", nargs="+", type=str, default=[]) | ||
|
|
||
| parser.add_argument("--id", type=str, default=None) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| compare_ids(args.tars, args.id) |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # Copyright 2015 The Bazel Authors. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """Extracts the id of a docker image from its tarball. | ||
|
|
||
| Takes one argument, the path to the tarball. | ||
| """ | ||
|
|
||
|
|
||
| from __future__ import print_function | ||
| from json import JSONDecoder | ||
| import sys | ||
| import tarfile | ||
|
|
||
|
|
||
| def get_id(tar_path): | ||
| """Extracts the id of a docker image from its tarball. | ||
|
|
||
| Args: | ||
| tar_path: str path to the tarball | ||
|
|
||
|
|
||
| Returns: | ||
| str id of the image | ||
|
|
||
| """ | ||
| tar = tarfile.open(tar_path, mode="r") | ||
|
|
||
| decoder = JSONDecoder() | ||
| try: | ||
| # Extracts it as a file object (not to the disk) | ||
| manifest = tar.extractfile("manifest.json").read().decode("utf-8") | ||
| except Exception as e: | ||
| print(( | ||
| "Unable to extract manifest.json, make sure {} " | ||
| "is a valid docker image.\n").format(tar_path), | ||
| e, | ||
| file=sys.stderr) | ||
| exit(1) | ||
|
|
||
| # Get the manifest dictionary from JSON | ||
| manifest = decoder.decode(manifest)[0] | ||
|
|
||
| # The name of the config file is of the form <image_id>.json | ||
| config_file = manifest["Config"] | ||
|
|
||
| # Get the id | ||
| id_ = config_file.split(".")[0] | ||
|
|
||
| return id_ | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print(get_id(sys.argv[1])) |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.