Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

More help info for archive_surgery.py #2327

Merged
merged 21 commits into from Jan 31, 2019
Merged
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 22 additions & 10 deletions scripts/archive_surgery.py
Expand Up @@ -25,25 +25,37 @@
logger = logging.getLogger()
logger.setLevel(logging.ERROR)


def main():
parser = argparse.ArgumentParser(description="Perform surgery on a model.tar.gz archive")
parser = argparse.ArgumentParser(description="Perform surgery on a model.tar.gz archive",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument("--input-file", required=True)
parser.add_argument("--output-file", required=True)
parser.add_argument("--editor")
parser.add_argument("--input-file", required=True,
help="path to input file")
parser.add_argument("--editor", default=os.environ.get("EDITOR"),
help="editor to launch, whose default value is `$EDITOR` the environment variable")
output = parser.add_mutually_exclusive_group()
output.add_argument("--output-file", help="path to output file")
output.add_argument("--inplace", action="store_true",
help="overwrite the input file with the modified configuration")
parser.add_argument("-f", "--force", action="store_true",
help="overwrite the output file if it exists")

args = parser.parse_args()

editor = args.editor or os.environ.get("EDITOR")
if editor is None:
if args.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")
if not args.inplace and os.path.exists(args.output_file) and not args.force:
raise ValueError("output file already exists, use --force to override")

archive_file = cached_path(args.input_file)
if not os.path.exists(archive_file):
raise ValueError("input file doesn't exist")
if args.inplace:
output_file = archive_file
else:
output_file = args.output_file

# Extract archive to temp dir
tempdir = tempfile.mkdtemp()
Expand All @@ -52,9 +64,9 @@ def main():
atexit.register(lambda: shutil.rmtree(tempdir))

config_path = os.path.join(tempdir, CONFIG_NAME)
subprocess.run([editor, config_path])
subprocess.run([args.editor, config_path])

with tarfile.open(args.output_file, "w:gz") as tar:
with tarfile.open(output_file, "w:gz") as tar:
tar.add(tempdir, arcname=os.path.sep)


Expand Down