forked from mountainstorm/MobileDevice
-
Notifications
You must be signed in to change notification settings - Fork 3
/
crashmover.py
153 lines (131 loc) · 4.83 KB
/
crashmover.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/python
# coding: utf-8
# Copyright (c) 2013 Mountainstorm
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from MobileDevice import *
from amdevice import *
from plistservice import *
import os
import time
class CrashMover(object):
u'''Moves crash logs from their various scattered locations into the afc
crash log directory'''
def __init__(self, amdevice):
self.s = amdevice.start_service(u'com.apple.crashreportmover')
if self.s is None:
raise RuntimeError(u'Unable to launch: com.apple.crashreportmover')
def disconnect(self):
os.close(self.s)
def move_crashlogs(self, extensions=None):
u'''Moves all crash logs into the afc crash log directory
Arguments:
extensions -- if present a list of crash file extensions to move
XXX not currently working
'''
# XXX should we wait just in case?
time.sleep(2)
buf = os.read(self.s, 1)
while True:
buf += os.read(self.s, 1)
if buf == 'ping':
break # done!
def register_argparse_crashmover(cmdargs):
import argparse
import sys
import afccrashlogdirectory
import posixpath
import stat
def cmd_crashmove(args, dev):
cm = CrashMover(dev)
cm.move_crashlogs()
cm.disconnect()
def get_logs(afc, path, dest):
dirlist = []
for name in afc.listdir(path):
info = afc.lstat(posixpath.join(path, name))
if info.st_ifmt == stat.S_IFDIR:
dirlist.append((
posixpath.join(path, name),
os.path.join(dest, name)
))
try:
os.mkdir(os.path.join(dest, name))
except OSError:
pass # it already exists
elif info.st_ifmt == stat.S_IFLNK:
pass # XXX handle symlinks e.g. LatestCrash*
else:
s = afc.open(posixpath.join(path, name), u'r')
d = open(os.path.join(dest, name), u'w+')
d.write(s.readall())
d.close()
s.close()
for names in dirlist:
get_logs(afc, names[0], names[1])
def del_logs(afc, path):
dirlist = []
for name in afc.listdir(path):
info = afc.lstat(posixpath.join(path, name))
if info.st_ifmt == stat.S_IFDIR:
dirlist.append(posixpath.join(path, name))
else:
afc.remove(posixpath.join(path, name))
for name in dirlist:
del_logs(afc, name)
afc.remove(name)
def cmd_crashget(args, dev):
# move the crashes
cm = CrashMover(dev)
cm.move_crashlogs()
cm.disconnect()
# retrieve the crashes
afc = afccrashlogdirectory.AFCCrashLogDirectory(dev)
get_logs(afc, u'/', args.dest.decode(u'utf-8'))
# optionally, delete the crashes
if args.delete_logs:
del_logs(afc, u'/')
afc.disconnect()
# cmd_crashmove command
crashparser = cmdargs.add_parser(
u'crash',
help=u'manipulates crash logs'
)
crashcmd = crashparser.add_subparsers()
crashmovecmd = crashcmd.add_parser(
u'move',
help=u'moves crash logs into the afc directory'
)
crashmovecmd.set_defaults(func=cmd_crashmove)
# get the crash logs
crashgetcmd = crashcmd.add_parser(
u'get',
help=u'retrieves crash logs from the device'
)
crashgetcmd.add_argument(
u'-d',
dest=u'delete_logs',
action=u'store_true',
help=u'if specified, delete the crash logs after retrieval'
)
crashgetcmd.add_argument(
u'dest',
help=u'destination directory; files are appended into it'
)
crashgetcmd.set_defaults(func=cmd_crashget)