Skip to content

Commit

Permalink
Merge f503f62 into a567f28
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLIrion committed Jun 13, 2020
2 parents a567f28 + f503f62 commit a624104
Show file tree
Hide file tree
Showing 19 changed files with 2,588 additions and 145 deletions.
12 changes: 9 additions & 3 deletions .travis.yml
@@ -1,17 +1,23 @@
language: python
python:
- "2.7"
- "3.5"
- "3.6"
- "3.7"
- "3.8"
addons:
apt:
packages:
- swig
- libusb-1.0-0-dev
install:
- pip install .
- pip install flake8 pylint coveralls cryptography libusb1>=1.0.16
- pip install flake8 pylint coveralls cryptography libusb1>=1.0.16 pycryptodome
- python --version 2>&1 | grep -q "Python 2" && pip install mock || true
script:
- flake8 adb_shell/ && pylint adb_shell/ && coverage run --source adb_shell setup.py test && coverage report -m
- if python --version 2>&1 | grep -q "Python 2" || python --version 2>&1 | grep -q "Python 3.5"; then flake8 adb_shell/ --exclude="adb_shell/adb_device_async.py,adb_shell/handle/base_handle_async.py,adb_shell/handle/tcp_handle_async.py" && pylint --ignore="adb_device_async.py,base_handle_async.py,tcp_handle_async.py" adb_shell/; fi
- if python --version 2>&1 | grep -q "Python 3.6" || python --version 2>&1 | grep -q "Python 3.7" || python --version 2>&1 | grep -q "Python 3.8"; then flake8 adb_shell/ && pylint adb_shell/; fi
- if python --version 2>&1 | grep -q "Python 2" || python --version 2>&1 | grep -q "Python 3.5"; then for synctest in $(cd tests && ls test*.py | grep -v async); do python -m unittest discover -s tests/ -t . -p "$synctest" || exit 1; done; fi
- if python --version 2>&1 | grep -q "Python 3.6" || python --version 2>&1 | grep -q "Python 3.7" || python --version 2>&1 | grep -q "Python 3.8"; then coverage run --source adb_shell -m unittest discover -s tests/ -t . && coverage report -m; fi
after_success:
- coveralls
- if python --version 2>&1 | grep -q "Python 3.6" || python --version 2>&1 | grep -q "Python 3.7" || python --version 2>&1 | grep -q "Python 3.8"; then coveralls; fi
16 changes: 7 additions & 9 deletions Makefile
Expand Up @@ -11,27 +11,25 @@ docs:
@cd docs && sphinx-apidoc -f -e -o source/ ../adb_shell/
@cd docs && make html && make html

.PHONY: doxygen
doxygen:
rm -rf docs/html
doxygen Doxyfile
SYNCTESTS := $(shell cd tests && ls test*.py | grep -v async)

.PHONY: test
test:
python setup.py test
python --version 2>&1 | grep -q "Python 2" && (for synctest in $(SYNCTESTS); do python -m unittest discover -s tests/ -t . -p "$$synctest"; done) || true
python --version 2>&1 | grep -q "Python 3" && python -m unittest discover -s tests/ -t . || true

.PHONY: coverage
coverage:
coverage run --source adb_shell setup.py test && coverage html && coverage report -m
coverage run --source adb_shell -m unittest discover -s tests/ -t . && coverage html && coverage report -m

.PHONY: tdd
tdd:
coverage run --source adb_shell setup.py test && coverage report -m
coverage run --source adb_shell -m unittest discover -s tests/ -t . && coverage report -m

.PHONY: lint
lint:
flake8 adb_shell/ && pylint adb_shell/
python --version 2>&1 | grep -q "Python 2" && (flake8 adb_shell/ --exclude="adb_shell/adb_device_async.py,adb_shell/handle/base_handle_async.py,adb_shell/handle/tcp_handle_async.py" && pylint --ignore="adb_device_async.py,base_handle_async.py,tcp_handle_async.py" adb_shell/) || (flake8 adb_shell/ && pylint adb_shell/)

.PHONY: alltests
alltests:
flake8 adb_shell/ && pylint adb_shell/ && coverage run --source adb_shell setup.py test && coverage report -m
flake8 adb_shell/ && pylint adb_shell/ && coverage run --source adb_shell -m unittest discover -s tests/ -t . && coverage report -m
128 changes: 1 addition & 127 deletions adb_shell/adb_device.py
Expand Up @@ -22,11 +22,6 @@
.. rubric:: Contents
* :class:`_AdbTransactionInfo`
* :class:`_FileSyncTransactionInfo`
* :meth:`_FileSyncTransactionInfo.can_add_to_send_buffer`
* :class:`AdbDevice`
* :meth:`AdbDevice._close`
Expand Down Expand Up @@ -65,8 +60,6 @@
"""


from collections import namedtuple
from contextlib import contextmanager
import io
import logging
import os
Expand All @@ -79,135 +72,16 @@
from .adb_message import AdbMessage, checksum, unpack
from .handle.base_handle import BaseHandle
from .handle.tcp_handle import TcpHandle
from .hidden_helpers import FILE_TYPES, DeviceFile, _AdbTransactionInfo, _FileSyncTransactionInfo, _open

try:
from .handle.usb_handle import UsbHandle
except ImportError:
UsbHandle = None


try:
FILE_TYPES = (file, io.IOBase)
except NameError: # pragma: no cover
FILE_TYPES = (io.IOBase,)

_LOGGER = logging.getLogger(__name__)

DeviceFile = namedtuple('DeviceFile', ['filename', 'mode', 'size', 'mtime'])


@contextmanager
def _open(name, mode='r'):
"""Handle opening and closing of files and IO streams.
Parameters
----------
name : str, io.IOBase
The name of the file *or* an IO stream
mode : str
The mode for opening the file
Yields
------
io.IOBase
The opened file *or* the IO stream
"""
try:
opened = open(name, mode) if isinstance(name, str) else None
if isinstance(name, str):
yield opened
else:
yield name
finally:
if isinstance(name, str):
opened.close()
else:
name.close()


class _AdbTransactionInfo(object): # pylint: disable=too-few-public-methods
"""A class for storing info and settings used during a single ADB "transaction."
Parameters
----------
local_id : int
The ID for the sender (i.e., the device running this code)
remote_id : int
The ID for the recipient
timeout_s : float, None
Timeout in seconds for sending and receiving packets, or ``None``; see :meth:`BaseHandle.bulk_read() <adb_shell.handle.base_handle.BaseHandle.bulk_read>`
and :meth:`BaseHandle.bulk_write() <adb_shell.handle.base_handle.BaseHandle.bulk_write>`
total_timeout_s : float
The total time in seconds to wait for a command in ``expected_cmds`` in :meth:`AdbDevice._read`
Attributes
----------
local_id : int
The ID for the sender (i.e., the device running this code)
remote_id : int
The ID for the recipient
timeout_s : float, None
Timeout in seconds for sending and receiving packets, or ``None``; see :meth:`BaseHandle.bulk_read() <adb_shell.handle.base_handle.BaseHandle.bulk_read>`
and :meth:`BaseHandle.bulk_write() <adb_shell.handle.base_handle.BaseHandle.bulk_write>`
total_timeout_s : float
The total time in seconds to wait for a command in ``expected_cmds`` in :meth:`AdbDevice._read`
"""
def __init__(self, local_id, remote_id, timeout_s=None, total_timeout_s=constants.DEFAULT_TOTAL_TIMEOUT_S):
self.local_id = local_id
self.remote_id = remote_id
self.timeout_s = timeout_s
self.total_timeout_s = total_timeout_s


class _FileSyncTransactionInfo(object): # pylint: disable=too-few-public-methods
"""A class for storing info used during a single FileSync "transaction."
Parameters
----------
recv_message_format : bytes
The FileSync message format
Attributes
----------
recv_buffer : bytearray
A buffer for storing received data
recv_message_format : bytes
The FileSync message format
recv_message_size : int
The FileSync message size
send_buffer : bytearray
A buffer for storing data to be sent
send_idx : int
The index in ``recv_buffer`` that will be the start of the next data packet sent
"""
def __init__(self, recv_message_format):
self.send_buffer = bytearray(constants.MAX_ADB_DATA)
self.send_idx = 0

self.recv_buffer = bytearray()
self.recv_message_format = recv_message_format
self.recv_message_size = struct.calcsize(recv_message_format)

def can_add_to_send_buffer(self, data_len):
"""Determine whether ``data_len`` bytes of data can be added to the send buffer without exceeding :const:`constants.MAX_ADB_DATA`.
Parameters
----------
data_len : int
The length of the data to be potentially added to the send buffer (not including the length of its header)
Returns
-------
bool
Whether ``data_len`` bytes of data can be added to the send buffer without exceeding :const:`constants.MAX_ADB_DATA`
"""
added_len = self.recv_message_size + data_len
return self.send_idx + added_len < constants.MAX_ADB_DATA


class AdbDevice(object):
"""A class with methods for connecting to a device and executing ADB commands.
Expand Down

0 comments on commit a624104

Please sign in to comment.