Skip to content

Commit 2af55c9

Browse files
committed
Allow device-scoped access to ADB facilities off of AdbDevice
1 parent 4725b1b commit 2af55c9

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

pwnlib/adb/adb.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,33 @@ def from_adb_output(line):
215215

216216
return AdbDevice(serial, type, **kwargs)
217217

218+
def __wrapped(self, function):
219+
"""Wrapps a callable in a scope which selects the current device."""
220+
@functools.wraps(function)
221+
def wrapper(*a, **kw):
222+
with context.local(device=self):
223+
return function(*a,**kw)
224+
return wrapper
225+
226+
def __getattr__(self, name):
227+
"""Provides scoped access to ``adb`` module propertise, in the context
228+
of this device.
229+
230+
>>> property = 'ro.build.fingerprint'
231+
>>> device = adb.current_device()
232+
>>> adb.getprop(property) == device.getprop(property)
233+
True
234+
"""
235+
with context.local(device=self):
236+
g = globals()
237+
238+
if name not in g:
239+
return super(AdbDevice, self).__getattr__(name)
240+
241+
value = g[name]
242+
243+
return self.__wrapped(value)
244+
218245
@LocalContext
219246
def wait_for_device(kick=False):
220247
"""Waits for a device to be connected.

0 commit comments

Comments
 (0)