forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord_netlog.py
executable file
·138 lines (116 loc) · 4.89 KB
/
record_netlog.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
#!/usr/bin/env vpython3
#
# Copyright 2019 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Takes a netlog for the WebViews in a given application.
Developer guide:
https://chromium.googlesource.com/chromium/src/+/HEAD/android_webview/docs/net-debugging.md
"""
from __future__ import print_function
import argparse
import logging
import os
import posixpath
import re
import sys
import time
sys.path.append(
os.path.join(
os.path.dirname(__file__), os.pardir, os.pardir, 'build', 'android'))
# pylint: disable=wrong-import-position,import-error
import devil_chromium
from devil.android import device_errors
from devil.android import flag_changer
from devil.android import device_utils
from devil.android.tools import script_common
from devil.utils import logging_common
WEBVIEW_COMMAND_LINE = 'webview-command-line'
def _WaitUntilCtrlC():
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print() # print a new line after the "^C" the user typed to the console
def CheckAppNotRunning(device, package_name, force):
is_running = bool(device.GetApplicationPids(package_name))
if is_running:
msg = ('Netlog requires setting commandline flags, which only works if the '
'application ({}) is not already running. Please kill the app and '
'restart the script.'.format(
package_name))
if force:
logging.warning(msg)
else:
# Extend the sentence to mention the user can skip the check.
msg = re.sub(r'\.$', ', or pass --force to ignore this check.', msg)
raise RuntimeError(msg)
def main():
parser = argparse.ArgumentParser(description="""
Configures WebView to start recording a netlog. This script chooses a suitable
netlog filename for the application, and will pull the netlog off the device
when the user terminates the script (with ctrl-C). For a more complete usage
guide, open your web browser to:
https://chromium.googlesource.com/chromium/src/+/HEAD/android_webview/docs/net-debugging.md
""")
parser.add_argument(
'--package',
required=True,
type=str,
help='Package name of the application you intend to use.')
parser.add_argument(
'--force',
default=False,
action='store_true',
help='Suppress user checks.')
script_common.AddEnvironmentArguments(parser)
script_common.AddDeviceArguments(parser)
logging_common.AddLoggingArguments(parser)
args = parser.parse_args()
logging_common.InitializeLogging(args)
devil_chromium.Initialize(adb_path=args.adb_path)
# Only use a single device, for the sake of simplicity (of implementation and
# user experience).
devices = device_utils.DeviceUtils.HealthyDevices(device_arg=args.devices)
device = devices[0]
if len(devices) > 1:
raise device_errors.MultipleDevicesError(devices)
if device.build_type == 'user':
device_setup_url = ('https://chromium.googlesource.com/chromium/src/+/HEAD/'
'android_webview/docs/device-setup.md')
raise RuntimeError('It appears your device is a "user" build. We only '
'support capturing netlog on userdebug/eng builds. See '
'{} to configure a development device or set up an '
'emulator.'.format(device_setup_url))
package_name = args.package
device_netlog_file_name = 'netlog.json'
device_netlog_path = posixpath.join(
device.GetApplicationDataDirectory(package_name), 'app_webview',
device_netlog_file_name)
CheckAppNotRunning(device, package_name, args.force)
# Append to the existing flags, to allow users to experiment with other
# features/flags enabled. The CustomCommandLineFlags will restore the original
# flag state after the user presses 'ctrl-C'.
changer = flag_changer.FlagChanger(device, WEBVIEW_COMMAND_LINE)
new_flags = changer.GetCurrentFlags()
new_flags.append('--log-net-log={}'.format(device_netlog_path))
logging.info('Running with flags %r', new_flags)
with flag_changer.CustomCommandLineFlags(device, WEBVIEW_COMMAND_LINE,
new_flags):
print('Netlog will start recording as soon as app starts up. Press ctrl-C '
'to stop recording.')
_WaitUntilCtrlC()
host_netlog_path = 'netlog.json'
print('Pulling netlog to "%s"' % host_netlog_path)
# The netlog file will be under the app's uid, which the default shell doesn't
# have permission to read (but root does). Prefer this to EnableRoot(), which
# restarts the adb daemon.
if device.PathExists(device_netlog_path, as_root=True):
device.PullFile(device_netlog_path, host_netlog_path, as_root=True)
device.RemovePath(device_netlog_path, as_root=True)
else:
raise RuntimeError(
'Unable to find a netlog file in the "{}" app data directory. '
'Did you restart and run the app?'.format(package_name))
if __name__ == '__main__':
main()