Debian Codegraph @

js/src/tests/lib/remote.py

raw
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5
6 # remote.py -- Python library for Android devices.
7
8 import os
9 import posixpath
10
11
12 def push_libs(options, device, dest_dir):
13 # This saves considerable time in pushing unnecessary libraries
14 # to the device but needs to be updated if the dependencies change.
15 required_libs = [
16 "libnss3.so",
17 "libmozglue.so",
18 "libnspr4.so",
19 "libplc4.so",
20 "libplds4.so",
21 ]
22
23 for file in os.listdir(options.local_lib):
24 if file in required_libs:
25 local_file = os.path.join(options.local_lib, file)
26 remote_file = posixpath.join(dest_dir, file)
27 assert os.path.isfile(local_file)
28 device.push(local_file, remote_file)
29 device.chmod(remote_file)
30
31
32 def push_progs(options, device, progs, dest_dir):
33 assert isinstance(progs, list)
34 for local_file in progs:
35 remote_file = posixpath.join(dest_dir, os.path.basename(local_file))
36 assert os.path.isfile(local_file)
37 device.push(local_file, remote_file)
38 device.chmod(remote_file)
39
40
41 def init_remote_dir(device, path):
42 device.rm(path, recursive=True, force=True)
43 device.mkdir(path, parents=True)
44
45
46 # We only have one device per test run.
47 DEVICE = None
48
49
50 def init_device(options):
51 # Initialize the device
52 global DEVICE
53
54 assert options.remote and options.js_shell
55
56 if DEVICE is not None:
57 return DEVICE
58
59 from mozdevice import ADBDeviceFactory, ADBError, ADBTimeoutError
60
61 try:
62 if not options.local_lib:
63 # if not specified, use the local directory containing
64 # the js binary to find the necessary libraries.
65 options.local_lib = posixpath.dirname(options.js_shell)
66
67 # Try to find 'adb' off the build environment to automatically use the
68 # .mozbuild version if possible. In test automation, we don't have
69 # mozbuild available so use the default 'adb' that automation provides.
70 try:
71 from mozbuild.base import MozbuildObject
72 from mozrunner.devices.android_device import get_adb_path
73
74 context = MozbuildObject.from_environment()
75 adb_path = get_adb_path(context)
76 except ImportError:
77 adb_path = "adb"
78
79 DEVICE = ADBDeviceFactory(
80 adb=adb_path,
81 device=options.device_serial,
82 test_root=options.remote_test_root,
83 )
84
85 bin_dir = posixpath.join(options.remote_test_root, "bin")
86 tests_dir = posixpath.join(options.remote_test_root, "tests")
87 temp_dir = posixpath.join(options.remote_test_root, "tmp")
88
89 # Create directory structure on device
90 init_remote_dir(DEVICE, options.remote_test_root)
91 init_remote_dir(DEVICE, tests_dir)
92 init_remote_dir(DEVICE, bin_dir)
93 init_remote_dir(DEVICE, temp_dir)
94
95 # Push js shell and libraries.
96 push_libs(options, DEVICE, bin_dir)
97 push_progs(options, DEVICE, [options.js_shell], bin_dir)
98
99 # update options.js_shell to point to the js binary on the device
100 options.js_shell = os.path.join(bin_dir, "js")
101
102 return DEVICE
103
104 except (ADBError, ADBTimeoutError):
105 print("TEST-UNEXPECTED-FAIL | remote.py : Device initialization failed")
106 raise