blob: d14942ead3bf94887d7a7546e345a6e624121437 [file] [log] [blame]
Sergei Trofimov4e6afe92015-10-09 09:30:04 +01001Target
2======
3
4
5.. class:: Target(connection_settings=None, platform=None, working_directory=None, executables_directory=None, connect=True, modules=None, load_default_modules=True, shell_prompt=DEFAULT_SHELL_PROMPT)
6
7 :class:`Target` is the primary interface to the remote device. All interactions
8 with the device are performed via a :class:`Target` instance, either
9 directly, or via its modules or a wrapper interface (such as an
10 :class:`Instrument`).
11
12 :param connection_settings: A ``dict`` that specifies how to connect to the remote
Sergei Trofimov01253102016-12-07 18:03:44 +000013 device. Its contents depend on the specific :class:`Target` type (used see
14 :ref:`connection-types`\ ).
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010015
16 :param platform: A :class:`Target` defines interactions at Operating System level. A
17 :class:`Platform` describes the underlying hardware (such as CPUs
18 available). If a :class:`Platform` instance is not specified on
19 :class:`Target` creation, one will be created automatically and it will
20 dynamically probe the device to discover as much about the underlying
Sergei Trofimov1ba7fbd2016-12-09 15:06:35 +000021 hardware as it can. See also :ref:`platform`\ .
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010022
23 :param working_directory: This is primary location for on-target file system
24 interactions performed by ``devlib``. This location *must* be readable and
25 writable directly (i.e. without sudo) by the connection's user account.
26 It may or may not allow execution. This location will be created,
27 if necessary, during ``setup()``.
28
29 If not explicitly specified, this will be set to a default value
30 depending on the type of :class:`Target`
31
32 :param executables_directory: This is the location to which ``devlib`` will
33 install executable binaries (either during ``setup()`` or via an
34 explicit ``install()`` call). This location *must* support execution
35 (obviously). It should also be possible to write to this location,
36 possibly with elevated privileges (i.e. on a rooted Linux target, it
37 should be possible to write here with sudo, but not necessarily directly
38 by the connection's account). This location will be created,
39 if necessary, during ``setup()``.
40
41 This location does *not* to be same as the system's executables
42 location. In fact, to prevent devlib from overwriting system's defaults,
43 it better if this is a separate location, if possible.
44
45 If not explicitly specified, this will be set to a default value
46 depending on the type of :class:`Target`
47
48 :param connect: Specifies whether a connections should be established to the
49 target. If this is set to ``False``, then ``connect()`` must be
50 explicitly called later on before the :class:`Target` instance can be
51 used.
52
53 :param modules: a list of additional modules to be installed. Some modules will
54 try to install by default (if supported by the underlying target).
55 Current default modules are ``hotplug``, ``cpufreq``, ``cpuidle``,
Sergei Trofimov1ba7fbd2016-12-09 15:06:35 +000056 ``cgroups``, and ``hwmon`` (See :ref:`modules`\ ).
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010057
58 See modules documentation for more detail.
59
60 :param load_default_modules: If set to ``False``, default modules listed
61 above will *not* attempt to load. This may be used to either speed up
62 target instantiation (probing for initializing modules takes a bit of time)
63 or if there is an issue with one of the modules on a particular device
64 (the rest of the modules will then have to be explicitly specified in
65 the ``modules``).
66
67 :param shell_prompt: This is a regular expression that matches the shell
68 prompted on the target. This may be used by some modules that establish
69 auxiliary connections to a target over UART.
70
71.. attribute:: Target.core_names
72
73 This is a list containing names of CPU cores on the target, in the order in
74 which they are index by the kernel. This is obtained via the underlying
75 :class:`Platform`.
76
77.. attribute:: Target.core_clusters
78
79 Some devices feature heterogeneous core configurations (such as ARM
80 big.LITTLE). This is a list that maps CPUs onto underlying clusters.
81 (Usually, but not always, clusters correspond to groups of CPUs with the same
82 name). This is obtained via the underlying :class:`Platform`.
83
84.. attribute:: Target.big_core
85
86 This is the name of the cores that the "big"s in an ARM big.LITTLE
87 configuration. This is obtained via the underlying :class:`Platform`.
88
89.. attribute:: Target.little_core
90
91 This is the name of the cores that the "little"s in an ARM big.LITTLE
92 configuration. This is obtained via the underlying :class:`Platform`.
93
94.. attribute:: Target.is_connected
95
96 A boolean value that indicates whether an active connection exists to the
97 target device.
98
99.. attribute:: Target.connected_as_root
100
101 A boolean value that indicate whether the account that was used to connect to
102 the target device is "root" (uid=0).
103
104.. attribute:: Target.is_rooted
105
106 A boolean value that indicates whether the connected user has super user
107 privileges on the devices (either is root, or is a sudoer).
108
109.. attribute:: Target.kernel_version
110
111 The version of the kernel on the target device. This returns a
112 :class:`KernelVersion` instance that has separate ``version`` and ``release``
113 fields.
114
115.. attribute:: Target.os_version
116
117 This is a dict that contains a mapping of OS version elements to their
118 values. This mapping is OS-specific.
119
120.. attribute:: Target.cpuinfo
121
122 This is a :class:`Cpuinfo` instance which contains parsed contents of
123 ``/proc/cpuinfo``.
124
125.. attribute:: Target.number_of_cpus
126
127 The total number of CPU cores on the target device.
128
129.. attribute:: Target.config
130
131 A :class:`KernelConfig` instance that contains parsed kernel config from the
132 target device. This may be ``None`` if kernel config could not be extracted.
133
134.. attribute:: Target.user
135
136 The name of the user logged in on the target device.
137
138.. attribute:: Target.conn
139
140 The underlying connection object. This will be ``None`` if an active
141 connection does not exist (e.g. if ``connect=False`` as passed on
142 initialization and ``connect()`` has not been called).
143
144 .. note:: a :class:`Target` will automatically create a connection per
145 thread. This will always be set to the connection for the current
146 thread.
147
148.. method:: Target.connect([timeout])
149
150 Establish a connection to the target. It is usually not necessary to call
151 this explicitly, as a connection gets automatically established on
152 instantiation.
153
154.. method:: Target.disconnect()
155
156 Disconnect from target, closing all active connections to it.
157
158.. method:: Target.get_connection([timeout])
159
160 Get an additional connection to the target. A connection can be used to
161 execute one blocking command at time. This will return a connection that can
162 be used to interact with a target in parallel while a blocking operation is
163 being executed.
164
165 This should *not* be used to establish an initial connection; use
166 ``connect()`` instead.
167
168 .. note:: :class:`Target` will automatically create a connection per
169 thread, so you don't normally need to use this explicitly in
170 threaded code. This is generally useful if you want to perform a
171 blocking operation (e.g. using ``background()``) while at the same
172 time doing something else in the same host-side thread.
173
174.. method:: Target.setup([executables])
175
176 This will perform an initial one-time set up of a device for devlib
177 interaction. This involves deployment of tools relied on the :class:`Target`,
178 creation of working locations on the device, etc.
179
180 Usually, it is enough to call this method once per new device, as its effects
181 will persist across reboots. However, it is safe to call this method multiple
182 times. It may therefore be a good practice to always call it once at the
183 beginning of a script to ensure that subsequent interactions will succeed.
184
185 Optionally, this may also be used to deploy additional tools to the device
186 by specifying a list of binaries to install in the ``executables`` parameter.
187
188.. method:: Target.reboot([hard [, connect, [timeout]]])
189
190 Reboot the target device.
191
192 :param hard: A boolean value. If ``True`` a hard reset will be used instead
193 of the usual soft reset. Hard reset must be supported (usually via a
194 module) for this to work. Defaults to ``False``.
195 :param connect: A boolean value. If ``True``, a connection will be
196 automatically established to the target after reboot. Defaults to
197 ``True``.
198 :param timeout: If set, this will be used by various (platform-specific)
199 operations during reboot process to detect if the reboot has failed and
200 the device has hung.
201
202.. method:: Target.push(source, dest [, timeout])
203
204 Transfer a file from the host machine to the target device.
205
206 :param source: path of to the file on the host
207 :param dest: path of to the file on the target
208 :param timeout: timeout (in seconds) for the transfer; if the transfer does
209 not complete within this period, an exception will be raised.
210
211.. method:: Target.pull(source, dest [, timeout])
212
213 Transfer a file from the target device to the host machine.
214
215 :param source: path of to the file on the target
216 :param dest: path of to the file on the host
217 :param timeout: timeout (in seconds) for the transfer; if the transfer does
218 not complete within this period, an exception will be raised.
219
220.. method:: Target.execute(command [, timeout [, check_exit_code [, as_root]]])
221
222 Execute the specified command on the target device and return its output.
223
224 :param command: The command to be executed.
225 :param timeout: Timeout (in seconds) for the execution of the command. If
226 specified, an exception will be raised if execution does not complete
227 with the specified period.
228 :param check_exit_code: If ``True`` (the default) the exit code (on target)
229 from execution of the command will be checked, and an exception will be
230 raised if it is not ``0``.
231 :param as_root: The command will be executed as root. This will fail on
232 unrooted targets.
233
234.. method:: Target.background(command [, stdout [, stderr [, as_root]]])
235
236 Execute the command on the target, invoking it via subprocess on the host.
237 This will return :class:`subprocess.Popen` instance for the command.
238
239 :param command: The command to be executed.
240 :param stdout: By default, standard output will be piped from the subprocess;
241 this may be used to redirect it to an alternative file handle.
242 :param stderr: By default, standard error will be piped from the subprocess;
243 this may be used to redirect it to an alternative file handle.
244 :param as_root: The command will be executed as root. This will fail on
245 unrooted targets.
246
247 .. note:: This **will block the connection** until the command completes.
248
249.. method:: Target.invoke(binary [, args [, in_directory [, on_cpus [, as_root [, timeout]]]]])
250
251 Execute the specified binary on target (must already be installed) under the
252 specified conditions and return the output.
253
254 :param binary: binary to execute. Must be present and executable on the device.
255 :param args: arguments to be passed to the binary. The can be either a list or
256 a string.
257 :param in_directory: execute the binary in the specified directory. This must
258 be an absolute path.
259 :param on_cpus: taskset the binary to these CPUs. This may be a single ``int`` (in which
260 case, it will be interpreted as the mask), a list of ``ints``, in which
261 case this will be interpreted as the list of cpus, or string, which
262 will be interpreted as a comma-separated list of cpu ranges, e.g.
263 ``"0,4-7"``.
264 :param as_root: Specify whether the command should be run as root
265 :param timeout: If this is specified and invocation does not terminate within this number
266 of seconds, an exception will be raised.
267
268.. method:: Target.kick_off(command [, as_root])
269
270 Kick off the specified command on the target and return immediately. Unlike
271 ``background()`` this will not block the connection; on the other hand, there
272 is not way to know when the command finishes (apart from calling ``ps()``)
273 or to get its output (unless its redirected into a file that can be pulled
274 later as part of the command).
275
276 :param command: The command to be executed.
277 :param as_root: The command will be executed as root. This will fail on
278 unrooted targets.
279
280.. method:: Target.read_value(path [,kind])
281
282 Read the value from the specified path. This is primarily intended for
283 sysfs/procfs/debugfs etc.
284
285 :param path: file to read
286 :param kind: Optionally, read value will be converted into the specified
287 kind (which should be a callable that takes exactly one parameter).
288
289.. method:: Target.read_int(self, path)
290
291 Equivalent to ``Target.read_value(path, kind=devlab.utils.types.integer)``
292
293.. method:: Target.read_bool(self, path)
294
295 Equivalent to ``Target.read_value(path, kind=devlab.utils.types.boolean)``
296
297.. method:: Target.write_value(path, value [, verify])
298
299 Write the value to the specified path on the target. This is primarily
300 intended for sysfs/procfs/debugfs etc.
301
302 :param path: file to write into
303 :param value: value to be written
304 :param verify: If ``True`` (the default) the value will be read back after
305 it is written to make sure it has been written successfully. This due to
306 some sysfs entries silently failing to set the written value without
307 returning an error code.
308
309.. method:: Target.reset()
310
311 Soft reset the target. Typically, this means executing ``reboot`` on the
312 target.
313
314.. method:: Target.check_responsive()
315
316 Returns ``True`` if the target appears to be responsive and ``False``
317 otherwise.
318
319.. method:: Target.kill(pid[, signal[, as_root]])
320
321 Kill a process on the target.
322
323 :param pid: PID of the process to be killed.
324 :param signal: Signal to be used to kill the process. Defaults to
325 ``signal.SIGTERM``.
326 :param as_root: If set to ``True``, kill will be issued as root. This will
327 fail on unrooted targets.
328
329.. method:: Target.killall(name[, signal[, as_root]])
330
331 Kill all processes with the specified name on the target. Other parameters
332 are the same as for ``kill()``.
333
334.. method:: Target.get_pids_of(name)
335
336 Return a list of PIDs of all running instances of the specified process.
337
338.. method:: Target.ps()
339
340 Return a list of :class:`PsEntry` instances for all running processes on the
341 system.
342
343.. method:: Target.file_exists(self, filepath)
344
345 Returns ``True`` if the specified path exists on the target and ``False``
346 otherwise.
347
348.. method:: Target.list_file_systems()
349
350 Lists file systems mounted on the target. Returns a list of
351 :class:`FstabEntry`\ s.
352
353.. method:: Target.list_directory(path[, as_root])
354
355 List (optionally, as root) the contents of the specified directory. Returns a
356 list of strings.
357
358
359.. method:: Target.get_workpath(self, path)
360
361 Convert the specified path to an absolute path relative to
362 ``working_directory`` on the target. This is a shortcut for
363 ``t.path.join(t.working_directory, path)``
364
365.. method:: Target.tempfile([prefix [, suffix]])
366
367 Get a path to a temporary file (optionally, with the specified prefix and/or
368 suffix) on the target.
369
370.. method:: Target.remove(path[, as_root])
371
372 Delete the specified path on the target. Will work on files and directories.
373
374.. method:: Target.core_cpus(core)
375
376 Return a list of numeric cpu IDs corresponding to the specified core name.
377
378.. method:: Target.list_online_cpus([core])
379
380 Return a list of numeric cpu IDs for all online CPUs (optionally, only for
381 CPUs corresponding to the specified core).
382
383.. method:: Target.list_offline_cpus([core])
384
385 Return a list of numeric cpu IDs for all offline CPUs (optionally, only for
386 CPUs corresponding to the specified core).
387
388.. method:: Target.getenv(variable)
389
390 Return the value of the specified environment variable on the device
391
392.. method:: Target.capture_screen(filepath)
393
394 Take a screenshot on the device and save it to the specified file on the
395 host. This may not be supported by the target.
396
397.. method:: Target.install(filepath[, timeout[, with_name]])
398
399 Install an executable on the device.
400
401 :param filepath: path to the executable on the host
402 :param timeout: Optional timeout (in seconds) for the installation
403 :param with_name: This may be used to rename the executable on the target
404
405.. method:: Target.uninstall(name)
406
407 Uninstall the specified executable from the target
408
409.. method:: Target.get_installed(name)
410
411 Return the full installation path on the target for the specified executable,
412 or ``None`` if the executable is not installed.
413
414.. method:: Target.which(name)
415
416 Alias for ``get_installed()``
417
418.. method:: Target.is_installed(name)
419
420 Returns ``True`` if an executable with the specified name is installed on the
421 target and ``False`` other wise.
422
Sergei Trofimovc4784e02016-07-14 11:15:08 +0100423.. method:: Target.extract(path, dest=None)
424
425 Extracts the specified archive/file and returns the path to the extrated
426 contents. The extraction method is determined based on the file extension.
427 ``zip``, ``tar``, ``gzip``, and ``bzip2`` are supported.
428
429 :param dest: Specified an on-target destination directory (which must exist)
430 for the extrated contents.
431
432 Returns the path to the extracted contents. In case of files (gzip and
433 bzip2), the path to the decompressed file is returned; for archives, the
434 path to the directory with the archive's contents is returned.