This repository was archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
script for doing archive surgery #2223
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#! /usr/bin/env python | ||
""" | ||
Helper script for modifying config.json files that are locked inside | ||
model.tar.gz archives. This is useful if you need to rename things or | ||
add or remove values, usually because of changes to the library. | ||
|
||
This script will untar the archive to a temp directory, launch an editor | ||
to modify the config.json, and then re-tar everything to a new archive. | ||
If your $EDITOR environment variable is not set, you'll have to explicitly | ||
specify which editor to use. | ||
""" | ||
# pylint: disable=invalid-name,redefined-outer-name | ||
import argparse | ||
import atexit | ||
import logging | ||
import os | ||
import shutil | ||
import subprocess | ||
import tempfile | ||
import tarfile | ||
|
||
from allennlp.common.file_utils import cached_path | ||
from allennlp.models.archival import CONFIG_NAME | ||
|
||
logger = logging.getLogger() | ||
logger.setLevel(logging.ERROR) | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description="Perform surgery on a model.tar.gz archive") | ||
|
||
parser.add_argument("--input-file", required=True) | ||
parser.add_argument("--output-file", required=True) | ||
parser.add_argument("--editor") | ||
|
||
args = parser.parse_args() | ||
|
||
editor = args.editor or os.environ.get("EDITOR") | ||
if editor is None: | ||
raise RuntimeError("please specify an editor or set the $EDITOR environment variable") | ||
|
||
if os.path.exists(args.output_file): | ||
raise ValueError("output file already exists") | ||
|
||
archive_file = cached_path(args.input_file) | ||
if not os.path.exists(archive_file): | ||
raise ValueError("input file doesn't exist") | ||
|
||
# Extract archive to temp dir | ||
tempdir = tempfile.mkdtemp() | ||
with tarfile.open(archive_file, 'r:gz') as archive: | ||
archive.extractall(tempdir) | ||
atexit.register(lambda: shutil.rmtree(tempdir)) | ||
|
||
config_path = os.path.join(tempdir, CONFIG_NAME) | ||
subprocess.run([editor, config_path]) | ||
|
||
with tarfile.open(args.output_file, "w:gz") as tar: | ||
tar.add(tempdir, arcname=os.path.sep) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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.
I wonder if you get this for free with
argparse.FileType('w')