8000 script for doing archive surgery by joelgrus · Pull Request #2223 · allenai/allennlp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

script for doing archive surgery #2223

Merged
merged 3 commits into from
Jan 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions scripts/archive_surgery.py
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")
Copy link
Member

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')


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()
0