|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# --- BEGIN_HEADER --- |
| 5 | +# |
| 6 | +# edpickle - a simple pickled object editor. |
| 7 | +# Copyright (C) 2003-2025 The MiG Project by the Science HPC Center at UCPH |
| 8 | +# |
| 9 | +# This file is part of MiG. |
| 10 | +# |
| 11 | +# MiG is free software: you can redistribute it and/or modify |
| 12 | +# it under the terms of the GNU General Public License as published by |
| 13 | +# the Free Software Foundation; either version 2 of the License, or |
| 14 | +# (at your option) any later version. |
| 15 | +# |
| 16 | +# MiG is distributed in the hope that it will be useful, |
| 17 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +# GNU General Public License for more details. |
| 20 | +# |
| 21 | +# You should have received a copy of the GNU General Public License |
| 22 | +# along with this program; if not, write to the Free Software |
| 23 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| 24 | +# USA. |
| 25 | +# |
| 26 | +# --- END_HEADER --- |
| 27 | +# |
| 28 | + |
| 29 | +"""Edit pickled objects on disk.""" |
| 30 | + |
| 31 | +from __future__ import absolute_import, print_function |
| 32 | + |
| 33 | +# NOTE: keep backwards compatible input reader on python2 |
| 34 | +try: |
| 35 | + import raw_input as read_input |
| 36 | +except ImportError: |
| 37 | + from builtins import input as read_input |
| 38 | + |
| 39 | +import os |
| 40 | +import sys |
| 41 | + |
| 42 | +# Try to import mig to assure we have a suitable python module load path |
| 43 | +try: |
| 44 | + import mig |
| 45 | +except ImportError: |
| 46 | + mig = None # type: ignore[assignment] |
| 47 | + |
| 48 | +if mig is None: |
| 49 | + # NOTE: include cmd parent path in search path for mig.X imports to work |
| 50 | + MIG_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0]))) |
| 51 | + print("Using mig installation in %s" % MIG_ROOT) |
| 52 | + sys.path.append(MIG_ROOT) |
| 53 | + |
| 54 | +from mig.shared.serial import dump, load |
| 55 | + |
| 56 | +SERIALIZER = "pickle" |
| 57 | + |
| 58 | + |
| 59 | +def main(argv): |
| 60 | + """Run interactive session.""" |
| 61 | + dirty = False |
| 62 | + path = None |
| 63 | + obj = None |
| 64 | + if argv[1:]: |
| 65 | + path = argv[1] |
| 66 | + print("opening pickle in %s" % path) |
| 67 | + obj = load(path, serializer=SERIALIZER) |
| 68 | + print("pickled object loaded as 'obj'") |
| 69 | + else: |
| 70 | + print("ready to open pickle - type 'help' for instructions") |
| 71 | + |
| 72 | + while True: |
| 73 | + command = input("Enter command: ") |
| 74 | + command = command.lower().strip() |
| 75 | + if command in ["o", "open"]: |
| 76 | + path = read_input("Path to open: ") |
| 77 | + obj = load(path, serializer=SERIALIZER) |
| 78 | + elif command in ["h", "help"]: |
| 79 | + print("Valid commands include:") |
| 80 | + print("(d)isplay to display the opened pickled object") |
| 81 | + print("(e)dit to edit the opened pickled object") |
| 82 | + print("(o)pen to open a new pickle file") |
| 83 | + print("(c)lose to close the opened pickled object") |
| 84 | + print("(q)uit to quit pickle editor") |
| 85 | + elif command in ["d", "display"]: |
| 86 | + print(obj) |
| 87 | + elif command in ["e", "edit"]: |
| 88 | + edit = read_input("Edit command: ") |
| 89 | + # eval(edit) |
| 90 | + eval(compile(edit, "command-line", "single")) |
| 91 | + dirty = True |
| 92 | + elif command in ["c", "close", "q", "quit"]: |
| 93 | + if dirty: |
| 94 | + flush = read_input("Modified object not saved - save now?: ") |
| 95 | + if flush.lower() in ("y", "yes"): |
| 96 | + while not path: |
| 97 | + path = read_input("Object save path: ").strip() |
| 98 | + dump(obj, path, serializer=SERIALIZER) |
| 99 | + dirty = False |
| 100 | + obj = None |
| 101 | + if command in ("q", "quit"): |
| 102 | + print("Closing") |
| 103 | + break |
| 104 | + else: |
| 105 | + print("unknown command '%s'" % command) |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + if len(sys.argv) not in (1, 2): |
| 110 | + print("Usage: %s [PATH]" % sys.argv[0]) |
| 111 | + print("Edit pickled object file - load from PATH if provided") |
| 112 | + sys.exit(1) |
| 113 | + main(sys.argv) |
0 commit comments