示例#1
0
 def find_candidates(self) -> List[CandidateDevice]:
     """Return a list of CandidateDevices."""
     context = pyudev.Context()
     candidates = []
     for disk in context.list_devices(subsystem="block", ID_BUS="usb"):
         serial_number = disk.properties.get("ID_SERIAL_SHORT")
         try:
             candidates.append(
                 CandidateDevice(
                     mount_points=_find_fs_mounts_for_device(disk.properties.get("DEVNAME")),
                     product_id=disk.properties.get("ID_MODEL_ID"),
                     vendor_id=disk.properties.get("ID_VENDOR_ID"),
                     serial_number=serial_number,
                     serial_port=_find_serial_port_for_device(serial_number),
                 )
             )
         except FilesystemMountpointError:
             logger.warning(
                 f"A USB block device was detected at path {disk.properties.get('DEVNAME')}. However, the"
                 " file system has failed to mount. Please disconnect and reconnect your device and try again."
                 "If this problem persists, try running fsck.vfat on your block device, as the file system may be "
                 "corrupted."
             )
             continue
     return candidates
示例#2
0
 def test_builds_list_of_candidates(self, mock_find_serial_port,
                                    mock_find_fs_mounts, mock_udev_context):
     expected_serial = "2090290209"
     expected_vid = "0x45"
     expected_pid = "0x48"
     expected_fs_mount = ["/media/user/DAPLINK"]
     mock_find_serial_port.return_value = None
     mock_find_fs_mounts.return_value = expected_fs_mount
     devs = [
         mock_device_factory(
             ID_SERIAL_SHORT=expected_serial,
             ID_VENDOR_ID=expected_vid,
             ID_MODEL_ID=expected_pid,
             DEVNAME="/dev/sdabcde",
         )
     ]
     mock_udev_context().list_devices.return_value = devs
     detector = device_detector.LinuxDeviceDetector()
     candidates = detector.find_candidates()
     self.assertEqual(
         candidates,
         [
             CandidateDevice(
                 serial_number=expected_serial,
                 vendor_id=expected_vid,
                 product_id=expected_pid,
                 mount_points=expected_fs_mount,
             )
         ],
     )
示例#3
0
def _build_candidate(
        device_data: system_profiler.USBDevice) -> CandidateDevice:
    assembled_data = _assemble_candidate_data(device_data)
    try:
        return CandidateDevice(**assembled_data)
    except ValueError as e:
        logging.debug(f"Unable to build candidate. {e}")
        raise InvalidCandidateDeviceDataError
    def test_produces_a_valid_candidate(self):
        candidate_data = build_candidate_data()
        candidate = CandidateDevice(**candidate_data)

        self.assertEqual(candidate.product_id, candidate_data["product_id"])
        self.assertEqual(candidate.vendor_id, candidate_data["vendor_id"])
        self.assertEqual(candidate.mount_points,
                         candidate_data["mount_points"])
        self.assertEqual(candidate.serial_number,
                         candidate_data["serial_number"])
        self.assertEqual(candidate.serial_port, candidate_data["serial_port"])
示例#5
0
 def map_to_candidate(usb_data: AggregatedUsbData) -> CandidateDevice:
     """Maps a USB device to a candidate."""
     serial_port = next(iter(usb_data.get("serial_port")), None)
     uid = usb_data.uid
     return CandidateDevice(
         product_id=uid.product_id,
         vendor_id=uid.vendor_id,
         mount_points=tuple(Path(disk.component_id) for disk in usb_data.get("disks")),
         serial_number=uid.uid.presumed_serial_number,
         serial_port=serial_port.port_name if serial_port else None,
     )
示例#6
0
    def test_builds_candidate_using_assembled_data(self,
                                                   _assemble_candidate_data):
        device_data = {
            "vendor_id": "0xff",
            "product_id": "0x24",
            "serial_number": "123456",
            "mount_points": ["/Volumes/A"],
            "serial_port": "port-1",
        }
        _assemble_candidate_data.return_value = device_data

        self.assertEqual(
            _build_candidate(device_data),
            CandidateDevice(**device_data),
        )
 def test_raises_when_serial_number_is_empty(self):
     candidate_data = build_candidate_data(serial_number="")
     with self.assertRaisesRegex(ValueError, "serial_number"):
         CandidateDevice(**candidate_data)
 def test_raises_when_mount_points_are_empty(self):
     with self.assertRaisesRegex(ValueError, "mount_points"):
         CandidateDevice(product_id="1234",
                         vendor_id="1234",
                         mount_points=[],
                         serial_number="1234")
 def test_prefixes_vendor_id_hex_value(self):
     candidate_data = build_candidate_data(vendor_id="cbad")
     candidate = CandidateDevice(**candidate_data)
     self.assertEqual(candidate.vendor_id, "0xcbad")
 def test_raises_when_vendor_id_is_not_hex(self):
     candidate_data = build_candidate_data(vendor_id="TEST")
     with self.assertRaisesRegex(ValueError, "vendor_id"):
         CandidateDevice(**candidate_data)
 def test_prefixes_product_id_hex_value(self):
     candidate_data = build_candidate_data(product_id="ff01")
     candidate = CandidateDevice(**candidate_data)
     self.assertEqual(candidate.product_id, "0xff01")
 def test_raises_when_product_id_is_empty(self):
     candidate_data = build_candidate_data(product_id="")
     with self.assertRaisesRegex(ValueError, "product_id"):
         CandidateDevice(**candidate_data)