-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrit
executable file
·98 lines (79 loc) · 2.46 KB
/
crit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python
import argparse
import sys
import json
import pycriu
def inf(opts):
if opts['in']:
return open(opts['in'], 'r')
else:
return sys.stdin
def outf(opts):
if opts['out']:
return open(opts['out'], 'w+')
else:
return sys.stdout
def decode(opts):
indent = None
try:
img = pycriu.images.load(inf(opts), opts['pretty'])
except pycriu.images.MagicException as exc:
print >>sys.stderr, "Unknown magic %#x.\n"\
"Maybe you are feeding me an image with "\
"raw data(i.e. pages.img)?" % exc.magic
sys.exit(1)
if opts['pretty']:
indent = 4
f = outf(opts)
json.dump(img, f, indent=indent)
if f == sys.stdout:
f.write("\n")
def encode(opts):
img = json.load(inf(opts))
pycriu.images.dump(img, outf(opts))
def info(opts):
infs = pycriu.images.info(inf(opts))
json.dump(infs, sys.stdout, indent = 4)
print
def main():
desc = 'CRiu Image Tool'
parser = argparse.ArgumentParser(description=desc,
formatter_class=argparse.RawTextHelpFormatter)
subparsers = parser.add_subparsers(help='Use crit CMD --help for command-specific help')
# Decode
decode_parser = subparsers.add_parser('decode',
help = 'convert criu image from binary type to json')
decode_parser.add_argument('--pretty',
help = 'Multiline with indents and some numerical fields in field-specific format',
action = 'store_true')
decode_parser.add_argument('-i',
'--in',
help = 'criu image in binary format to be decoded (stdin by default)')
decode_parser.add_argument('-o',
'--out',
help = 'where to put criu image in json format (stdout by default)')
decode_parser.set_defaults(func=decode)
# Encode
encode_parser = subparsers.add_parser('encode',
help = 'convert criu image from json type to binary')
encode_parser.add_argument('-i',
'--in',
help = 'criu image in json format to be encoded (stdin by default)')
encode_parser.add_argument('-o',
'--out',
help = 'where to put criu image in binary format (stdout by default)')
encode_parser.set_defaults(func=encode)
# Info
info_parser = subparsers.add_parser('info',
help = 'show info about image')
info_parser.add_argument("in")
info_parser.set_defaults(func=info)
# Show
show_parser = subparsers.add_parser('show',
help = "convert criu image from binary to human-readable json")
show_parser.add_argument("in")
show_parser.set_defaults(func=decode, pretty=True, out=None)
opts = vars(parser.parse_args())
opts["func"](opts)
if __name__ == '__main__':
main()