Пример #1
0
def test_run_bash_cmd_bashexception():
    """
    Test if run_bash_cmd raises a BashException when trying to execute a
    command in a non-standard way (with missing argument in this case).
    """
    with pytest.raises(BashException):
        run_bash_cmd(['lsusb', '-d'])
Пример #2
0
def test_run_bash_cmd_oserror():
    """
    Test if run_bash_cmd raises an OSError when trying to execute a
    non-existent file.
    """
    with pytest.raises(OSError):
        run_bash_cmd(['no_command'])
Пример #3
0
def mount(mtp_url):
    """
    mount

    :argument mtp_url: device MTP URL
    :type mtp_url: str

    """
    run_bash_cmd(["gvfs-mount", mtp_url])
Пример #4
0
def ls(path):
    """
    ls

    :argument path: target path for listing content
    :type path: str

    """
    run_bash_cmd(["gvfs-ls", path])
Пример #5
0
def mount(mtp_url):
    """
    mount

    :argument mtp_url: device MTP URL
    :type mtp_url: str

    """
    run_bash_cmd(["gvfs-mount", mtp_url])
Пример #6
0
def cp(src, dst):
    """
    cp

    :argument src: source file/directory to be copied
    :type src: str
    :argument dst: destination file/directory
    :type dst: str

    """
    run_bash_cmd(["gvfs-copy", src, dst])
Пример #7
0
def rm(src):
    """
    rm -f

    NOTE: '-f' -> Ignore nonexistent and non-deletable files.

    :argument src: file to be removed
    :type src: str

    """
    run_bash_cmd(["gvfs-rm", "-f", src])
Пример #8
0
def mkdir(path):
    """
    mkdir -p

    NOTE: '-p' -> Create parent directories when necessary.

    :argument path: new directory path
    :type path: str

    """
    run_bash_cmd(["gvfs-mkdir", "-p", path])
Пример #9
0
def rm(src):
    """
    rm -f

    NOTE: '-f' -> Ignore nonexistent and non-deletable files.

    :argument src: file to be removed
    :type src: str

    """
    run_bash_cmd(["gvfs-rm", "-f", src])
Пример #10
0
def cp(src, dst):
    """
    cp

    :argument src: source file/directory to be copied
    :type src: str
    :argument dst: destination file/directory
    :type dst: str

    """
    run_bash_cmd(["gvfs-copy", src, dst])
Пример #11
0
    def test_run_bash_cmd_oserror(self):
        """
        Test 'run_bash_cmd' raises an OSError when trying to execute a
        non-existent file.
        """
        self.mock_popen.side_effect = OSError
        with self.assertRaises(OSError) as exc:
            run_bash_cmd(["no_command"])

        err_msg = 'Error while trying to execute command "no_command": None'
        self.assertEqual(str(exc.exception), err_msg)
Пример #12
0
def mkdir(path):
    """
    mkdir -p

    NOTE: '-p' -> Create parent directories when necessary.

    :argument path: new directory path
    :type path: str

    """
    run_bash_cmd(["gvfs-mkdir", "-p", path])
Пример #13
0
    def test_run_bash_cmd_bashexception(self):
        """
        Test 'run_bash_cmd' raises a BashException when trying to execute a
        command in a non-standard way (with missing argument in this case).
        """
        lsub_msg = 'lsusb: option requires an argument -- "d"'
        self.mock_popen.return_value = self._mock_communicate(("", lsub_msg))

        with self.assertRaises(BashException) as exc:
            run_bash_cmd(["lsusb", "-d"])

        err_msg = 'Command "lsusb -d" failed: {}'.format(lsub_msg)
        self.assertEqual(str(exc.exception), err_msg)
Пример #14
0
    def test_run_bash_cmd_output(self):
        """
        Test 'run_bash_cmd' returns an expected output for a valid command.
        """
        self.mock_popen.return_value = self._mock_communicate(("a", ""))

        out = run_bash_cmd(["echo", "a"])
        self.assertEqual(out, "a")
Пример #15
0
def get_connection_details(vendor, model):
    """
    Get device connection details (USB bus & device IDs).

    :argument vendor: device vendor name
    :type vendor: str
    :argument model: device model number
    :type model: str

    :returns tuple

    """
    vendor_devices = []

    vendor_pattern = re.compile(vendor, re.IGNORECASE)
    model_pattern = re.compile(model, re.IGNORECASE)

    usb_devices = run_bash_cmd(['lsusb'])

    # TODO: this assumes there is only one `vendor:model` device connected
    for device_info in usb_devices.split('\n'):
        if vendor_pattern.search(device_info) is None:
            continue
        else:
            # collect only the human readable device info (i.e. ignore IDs)
            vendor_devices.append(device_info[33:])

        if model_pattern.search(device_info) is None:
            continue

        # yep, USB bus ID and device ID are between these indexes
        usb_bus_id = device_info[4:7]
        device_id = device_info[15:18]

        return usb_bus_id, device_id

    else:
        # exception message base
        exc_msg = 'Device "{v} {m}" not found.\n'.format(v=vendor, m=model)

        # exception message extension base
        ext_base = '"{v}" devices were found'.format(v=vendor)

        if vendor_devices:
            exc_msg += ('Following {b}:\n{d}'
                        .format(b=ext_base, d='\n'.join(vendor_devices)))
        else:
            exc_msg += 'No {b}.'.format(b=ext_base)

        raise DeviceException(exc_msg)
Пример #16
0
def readlink(path):
    """
    A wrapper for the Linux `readlink` commmand.

    NOTE1: '-f' -> canonicalize by following path.
    NOTE2: `readlink` undestands '.', '..' and '/' and their combinations
    (e.g. './', '/..', '../').
    NOTE3: `readlink` strips trailing slashes by default.

    :argument path: path to resolve
    :type path: str

    :returns str
    """
    if not path:
        return path

    if path[0] == "~":
        path = os.path.expanduser(path)

    return run_bash_cmd(["readlink", "-f", path]) or path
Пример #17
0
def lsusb():
    """
    A wrapper for the Linux `lsusb` commmand.
    """
    return run_bash_cmd(["lsusb"])
Пример #18
0
def test_run_bash_cmd_expected_output():
    """
    Test if run_bash_cmd returns an expected output for a valid command.
    """
    assert run_bash_cmd(['echo', 'pysyncdroid']) == 'pysyncdroid'