-
Notifications
You must be signed in to change notification settings - Fork 6
Add fast xva_bridge.py script #350
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ dev = [ | |
| "ruff", | ||
| "types-requests", | ||
| "typing-extensions", | ||
| "libarchive-c==5.3", | ||
| ] | ||
|
|
||
| [tool.pyright] | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -10,4 +10,5 @@ pyyaml>=6.0 | |
| ruff | ||
| types-requests | ||
| typing-extensions | ||
| libarchive-c==5.3 | ||
| -r base.txt | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
stormi marked this conversation as resolved.
Show resolved
Hide resolved
|
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,141 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| # Tested on libarchive-c==5.3. Due to our use of library internals, may not work on other versions of libarchive-c. | ||
|
|
||
| import argparse | ||
| import io | ||
| import logging | ||
| import os | ||
| from xml.dom import minidom | ||
|
|
||
| import libarchive | ||
stormi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import libarchive.ffi | ||
|
|
||
| class XvaHeaderMember: | ||
| def __init__(self, member: minidom.Element): | ||
| self.member = member | ||
|
|
||
| def get_name(self): | ||
| for child in self.member.childNodes: | ||
| if child.nodeType == minidom.Node.ELEMENT_NODE and child.tagName == "name" and child.firstChild: | ||
| return child.firstChild.nodeValue | ||
| return None | ||
|
|
||
| def get_value(self): | ||
| for child in self.member.childNodes: | ||
| if child.nodeType == minidom.Node.ELEMENT_NODE and child.tagName == "value" and child.firstChild: | ||
| return child.firstChild.nodeValue | ||
| return None | ||
|
|
||
| def set_value(self, value: str): | ||
| for child in self.member.childNodes: | ||
| if child.nodeType == minidom.Node.ELEMENT_NODE and child.tagName == "value" and child.firstChild: | ||
| child.firstChild.nodeValue = value # type: ignore | ||
| return None | ||
|
|
||
|
|
||
| class XvaHeader: | ||
| def __init__(self, header_bytes: bytes): | ||
| self.xml = minidom.parseString(header_bytes.decode()) | ||
|
|
||
| def members(self): | ||
| for member in self.xml.getElementsByTagName("member"): | ||
| if member.nodeType == minidom.Node.ELEMENT_NODE: | ||
| yield XvaHeaderMember(member) | ||
|
|
||
| def get_bridge(self): | ||
| for member in self.members(): | ||
| if member.get_name() == "bridge": | ||
| return member.get_value() | ||
| raise ValueError("Could not find bridge value in XVA header") | ||
|
|
||
| def set_bridge(self, bridge: str): | ||
| for member in self.members(): | ||
| if member.get_name() == "bridge": | ||
| member.set_value(bridge) | ||
| return | ||
| raise ValueError("Could not find bridge value in XVA header") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("xva", help="input file path") | ||
| parser.add_argument( | ||
| "--set-bridge", help="new bridge value of format `xenbr0|xapi[:9]|...`; omit this option to show current bridge" | ||
| ) | ||
| parser.add_argument( | ||
| "--compression", | ||
| choices=["zstd", "gzip"], | ||
| default="zstd", | ||
| help="compression mode of new XVA when setting bridge value (default: zstd)", | ||
| ) | ||
| parser.add_argument("-o", "--output", help="output file path (must not be the same as input)") | ||
| parser.add_argument("--backup-path", help="backup file path") | ||
| parser.add_argument( | ||
| "--in-place", action="store_true", help="rename output file to input file; rename input file to backup file" | ||
| ) | ||
| parser.add_argument("-v", "--verbose", action="store_true", help="verbose logging") | ||
| args = parser.parse_args() | ||
|
|
||
| if args.verbose: | ||
| logging.getLogger().setLevel(logging.DEBUG) | ||
| else: | ||
| logging.getLogger().setLevel(logging.INFO) | ||
|
|
||
| with libarchive.file_reader(args.xva, "tar") as input_file: | ||
| logging.debug(f"Compression: {', '.join(filter.decode() for filter in input_file.filter_names)}") | ||
|
|
||
| entry_iter = iter(input_file) | ||
|
|
||
| header_entry = next(entry_iter) | ||
| if header_entry.pathname != "ova.xml": | ||
| raise ValueError("Unexpected header entry name") | ||
| with io.BytesIO() as header_writer: | ||
| for block in header_entry.get_blocks(): | ||
| header_writer.write(block) | ||
| header_bytes = header_writer.getvalue() | ||
|
|
||
| logging.debug(f"Header is {len(header_bytes)} bytes") | ||
|
|
||
| header = XvaHeader(header_bytes) | ||
| bridge = header.get_bridge() | ||
| logging.info(f"Found bridge {bridge}") | ||
|
|
||
| if args.set_bridge: | ||
| output_path = args.output | ||
| if not output_path: | ||
| output_path = args.xva + ".new" | ||
| logging.info(f"Output path: {output_path}") | ||
|
|
||
| logging.info(f"Setting bridge to {args.set_bridge}") | ||
| header.set_bridge(args.set_bridge) | ||
|
|
||
| logging.debug(f"Using compression {args.compression}") | ||
| with libarchive.file_writer(output_path, "pax_restricted", args.compression) as output_file: | ||
| new_header_bytes = header.xml.toxml().encode() | ||
| output_file.add_file_from_memory( | ||
| "ova.xml", len(new_header_bytes), new_header_bytes, permission=0o400, uid=0, gid=0 | ||
| ) | ||
|
|
||
| for entry in entry_iter: | ||
| logging.debug(f"Copying {entry.pathname}: {entry.size} bytes") | ||
| new_entry = libarchive.ArchiveEntry(entry.header_codec, perm=0o400, uid=0, gid=0) | ||
| for attr in ["filetype", "pathname", "size"]: | ||
| setattr(new_entry, attr, getattr(entry, attr)) | ||
|
|
||
| # ArchiveEntry doesn't expose block copying, so write the entry manually via the FFI interface | ||
| libarchive.ffi.write_header(output_file._pointer, new_entry._entry_p) | ||
| for block in entry.get_blocks(): | ||
| libarchive.ffi.write_data(output_file._pointer, block, len(block)) | ||
| libarchive.ffi.write_finish_entry(output_file._pointer) | ||
|
|
||
| if args.in_place: | ||
| backup_path = args.backup_path | ||
| if not backup_path: | ||
| backup_path = args.xva + ".bak" | ||
| logging.info(f"Backup path: {backup_path}") | ||
|
|
||
| logging.info(f"Renaming {args.xva} -> {backup_path}") | ||
| os.rename(args.xva, backup_path) | ||
| logging.info(f"Renaming {output_path} -> {args.xva}") | ||
| os.rename(output_path, args.xva) | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
Please explain in commit message why it should be pinned, are later ones breaking (or will break) API?
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.
done
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.
ok, now i understand thank you this is making sense, but on this other this is introducing some kind of techdebt, an option to mitigate this would be to try to make upstream expose its internal parts we are using, feel free to link a ticket to it, we can address it when it has to be updated.
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.
An upstream PR has already been created some time ago: https://redirect.github.com/Changaco/python-libarchive-c/pull/142
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.
ok i would have linked in commit message, this way if there are changes at upstream , we are notify on this (merged PR)