def can_handle_windows(self, device_id, debug=False): from calibre.devices.scanner import win_pnp_drives drives = win_pnp_drives() for pnp_id in drives.values(): if self.windows_match_device(pnp_id, 'WINDOWS_MAIN_MEM'): return True if debug: print '\tNo match found in:', pnp_id return False
def device_info(ioreg_to_tmp=False, buf=None): from calibre.devices.scanner import DeviceScanner, win_pnp_drives from calibre.constants import iswindows import re res = {} device_details = {} device_set = set() drive_details = {} drive_set = set() res['device_set'] = device_set res['device_details'] = device_details res['drive_details'] = drive_details res['drive_set'] = drive_set try: s = DeviceScanner() s.scan() devices = (s.devices) if not iswindows: devices = [list(x) for x in devices] for dev in devices: for i in range(3): dev[i] = hex(dev[i]) d = dev[0] + dev[1] + dev[2] device_set.add(d) device_details[d] = dev[0:3] else: for dev in devices: vid = re.search('vid_([0-9a-f]*)&', dev) if vid: vid = vid.group(1) pid = re.search('pid_([0-9a-f]*)&', dev) if pid: pid = pid.group(1) rev = re.search('rev_([0-9a-f]*)$', dev) if rev: rev = rev.group(1) d = vid+pid+rev device_set.add(d) device_details[d] = (vid, pid, rev) drives = win_pnp_drives(debug=False) for drive,details in drives.iteritems(): order = 'ORD_' + str(drive.order) ven = re.search('VEN_([^&]*)&', details) if ven: ven = ven.group(1) prod = re.search('PROD_([^&]*)&', details) if prod: prod = prod.group(1) d = (order, ven, prod) drive_details[drive] = d drive_set.add(drive) finally: pass return res
def open_windows(self): from calibre.devices.scanner import win_pnp_drives, drivecmp time.sleep(5) drives = {} seen = set() prod_pat = re.compile(r'PROD_(.+?)&') dup_prod_id = False def check_for_dups(pnp_id): try: match = prod_pat.search(pnp_id) if match is not None: prodid = match.group(1) if prodid in seen: return True else: seen.add(prodid) except: pass return False for drive, pnp_id in win_pnp_drives().items(): if self.windows_match_device(pnp_id, 'WINDOWS_CARD_A_MEM') and \ not drives.get('carda', False): drives['carda'] = drive dup_prod_id |= check_for_dups(pnp_id) elif self.windows_match_device(pnp_id, 'WINDOWS_CARD_B_MEM') and \ not drives.get('cardb', False): drives['cardb'] = drive dup_prod_id |= check_for_dups(pnp_id) elif self.windows_match_device(pnp_id, 'WINDOWS_MAIN_MEM') and \ not drives.get('main', False): drives['main'] = drive dup_prod_id |= check_for_dups(pnp_id) if 'main' in drives.keys() and 'carda' in drives.keys() and \ 'cardb' in drives.keys(): break # This is typically needed when the device has the same # WINDOWS_MAIN_MEM and WINDOWS_CARD_A_MEM in which case # if the device is connected without a card, the above # will incorrectly identify the main mem as carda # See for example the driver for the Nook if drives.get('carda', None) is not None and \ drives.get('main', None) is None: drives['main'] = drives.pop('carda') if drives.get('main', None) is None: raise DeviceError( _('Unable to detect the %s disk drive. Try rebooting.') % self.__class__.__name__) # Sort drives by their PNP drive numbers if the CARD and MAIN # MEM strings are identical if dup_prod_id or \ self.WINDOWS_MAIN_MEM in (self.WINDOWS_CARD_A_MEM, self.WINDOWS_CARD_B_MEM) or \ self.WINDOWS_CARD_A_MEM == self.WINDOWS_CARD_B_MEM: letters = sorted(drives.values(), cmp=drivecmp) drives = {} for which, letter in zip(['main', 'carda', 'cardb'], letters): drives[which] = letter drives = self.windows_sort_drives(drives) self._main_prefix = drives.get('main') self._card_a_prefix = drives.get('carda', None) self._card_b_prefix = drives.get('cardb', None)
def debug(ioreg_to_tmp=False, buf=None): import textwrap from calibre.customize.ui import device_plugins from calibre.devices.scanner import DeviceScanner, win_pnp_drives from calibre.constants import iswindows, isosx, __version__ from calibre import prints oldo, olde = sys.stdout, sys.stderr if buf is None: buf = StringIO() sys.stdout = sys.stderr = buf try: out = partial(prints, file=buf) out('Version:', __version__) s = DeviceScanner() s.scan() devices = (s.devices) if not iswindows: devices = [list(x) for x in devices] for d in devices: for i in range(3): d[i] = hex(d[i]) out('USB devices on system:') out(pprint.pformat(devices)) if iswindows: drives = win_pnp_drives(debug=True) out('Drives detected:') for drive in sorted(drives.keys(), key=operator.attrgetter('order')): prints(u'\t(%d)'%drive.order, drive, '~', drives[drive]) ioreg = None if isosx: from calibre.devices.usbms.device import Device mount = repr(Device.osx_run_mount()) drives = pprint.pformat(Device.osx_get_usb_drives()) ioreg = 'Output from mount:\n'+mount+'\n\n' ioreg += 'Output from osx_get_usb_drives:\n'+drives+'\n\n' ioreg += Device.run_ioreg() connected_devices = [] devplugins = list(sorted(device_plugins(), cmp=lambda x,y:cmp(x.__class__.__name__, y.__class__.__name__))) out('Available plugins:', textwrap.fill(' '.join([x.__class__.__name__ for x in devplugins]))) out(' ') out('Looking for devices...') for dev in devplugins: connected, det = s.is_device_connected(dev, debug=True) if connected: out('\t\tDetected possible device', dev.__class__.__name__) connected_devices.append((dev, det)) out(' ') errors = {} success = False out('Devices possibly connected:', end=' ') for dev, det in connected_devices: out(dev.name, end=', ') if not connected_devices: out('None', end='') out(' ') for dev, det in connected_devices: out('Trying to open', dev.name, '...', end=' ') try: dev.reset(detected_device=det) dev.open(det, None) out('OK') except: import traceback errors[dev] = traceback.format_exc() out('failed') continue success = True if hasattr(dev, '_main_prefix'): out('Main memory:', repr(dev._main_prefix)) out('Total space:', dev.total_space()) break if not success and errors: out('Opening of the following devices failed') for dev,msg in errors.items(): out(dev) out(msg) out(' ') if ioreg is not None: ioreg = 'IOREG Output\n'+ioreg out(' ') if ioreg_to_tmp: open('/tmp/ioreg.txt', 'wb').write(ioreg) out('Dont forget to send the contents of /tmp/ioreg.txt') out('You can open it with the command: open /tmp/ioreg.txt') else: out(ioreg) if hasattr(buf, 'getvalue'): return buf.getvalue().decode('utf-8') finally: sys.stdout = oldo sys.stderr = olde
def debug(ioreg_to_tmp=False, buf=None, plugins=None, disabled_plugins=None): ''' If plugins is None, then this method calls startup and shutdown on the device plugins. So if you are using it in a context where startup could already have been called (for example in the main GUI), pass in the list of device plugins as the plugins parameter. ''' import textwrap from calibre.customize.ui import device_plugins, disabled_device_plugins from calibre.debug import print_basic_debug_info from calibre.devices.scanner import DeviceScanner, win_pnp_drives from calibre.constants import iswindows, isosx from calibre import prints oldo, olde = sys.stdout, sys.stderr if buf is None: buf = StringIO() sys.stdout = sys.stderr = buf out = partial(prints, file=buf) devplugins = device_plugins() if plugins is None else plugins devplugins = list(sorted(devplugins, cmp=lambda x,y:cmp(x.__class__.__name__, y.__class__.__name__))) if plugins is None: for d in devplugins: try: d.startup() except: out('Startup failed for device plugin: %s'%d) if disabled_plugins is None: disabled_plugins = list(disabled_device_plugins()) try: print_basic_debug_info(out=buf) s = DeviceScanner() s.scan() devices = (s.devices) if not iswindows: devices = [list(x) for x in devices] for d in devices: for i in range(3): d[i] = hex(d[i]) out('USB devices on system:') out(pprint.pformat(devices)) if iswindows: drives = win_pnp_drives(debug=True) out('Drives detected:') for drive in sorted(drives.keys(), key=operator.attrgetter('order')): prints(u'\t(%d)'%drive.order, drive, '~', drives[drive]) ioreg = None if isosx: from calibre.devices.usbms.device import Device mount = repr(Device.osx_run_mount()) drives = pprint.pformat(Device.osx_get_usb_drives()) ioreg = 'Output from mount:\n'+mount+'\n\n' ioreg += 'Output from osx_get_usb_drives:\n'+drives+'\n\n' ioreg += Device.run_ioreg() connected_devices = [] if disabled_plugins: out('\nDisabled plugins:', textwrap.fill(' '.join([x.__class__.__name__ for x in disabled_plugins]))) out(' ') else: out('\nNo disabled plugins') found_dev = False for dev in devplugins: if not dev.MANAGES_DEVICE_PRESENCE: continue out('Looking for devices of type:', dev.__class__.__name__) if dev.debug_managed_device_detection(s.devices, buf): found_dev = True break out(' ') if not found_dev: out('Looking for devices...') for dev in devplugins: if dev.MANAGES_DEVICE_PRESENCE: continue connected, det = s.is_device_connected(dev, debug=True) if connected: out('\t\tDetected possible device', dev.__class__.__name__) connected_devices.append((dev, det)) out(' ') errors = {} success = False out('Devices possibly connected:', end=' ') for dev, det in connected_devices: out(dev.name, end=', ') if not connected_devices: out('None', end='') out(' ') for dev, det in connected_devices: out('Trying to open', dev.name, '...', end=' ') try: dev.reset(detected_device=det) dev.open(det, None) out('OK') except: import traceback errors[dev] = traceback.format_exc() out('failed') continue success = True if hasattr(dev, '_main_prefix'): out('Main memory:', repr(dev._main_prefix)) out('Total space:', dev.total_space()) break if not success and errors: out('Opening of the following devices failed') for dev,msg in errors.items(): out(dev) out(msg) out(' ') if ioreg is not None: ioreg = 'IOREG Output\n'+ioreg out(' ') if ioreg_to_tmp: open('/tmp/ioreg.txt', 'wb').write(ioreg) out('Dont forget to send the contents of /tmp/ioreg.txt') out('You can open it with the command: open /tmp/ioreg.txt') else: out(ioreg) if hasattr(buf, 'getvalue'): return buf.getvalue().decode('utf-8', 'replace') finally: sys.stdout = oldo sys.stderr = olde if plugins is None: for d in devplugins: try: d.shutdown() except: pass
def debug(ioreg_to_tmp=False, buf=None): import textwrap from calibre.customize.ui import device_plugins from calibre.devices.scanner import DeviceScanner, win_pnp_drives from calibre.constants import iswindows, isosx, __version__ from calibre import prints oldo, olde = sys.stdout, sys.stderr if buf is None: buf = StringIO() sys.stdout = sys.stderr = buf try: out = partial(prints, file=buf) out('Version:', __version__) s = DeviceScanner() s.scan() devices = (s.devices) if not iswindows: devices = [list(x) for x in devices] for d in devices: for i in range(3): d[i] = hex(d[i]) out('USB devices on system:') out(pprint.pformat(devices)) if iswindows: drives = win_pnp_drives(debug=True) out('Drives detected:') for drive in sorted(drives.keys(), key=operator.attrgetter('order')): prints(u'\t(%d)' % drive.order, drive, '~', drives[drive]) ioreg = None if isosx: from calibre.devices.usbms.device import Device mount = repr(Device.osx_run_mount()) drives = pprint.pformat(Device.osx_get_usb_drives()) ioreg = 'Output from mount:\n' + mount + '\n\n' ioreg += 'Output from osx_get_usb_drives:\n' + drives + '\n\n' ioreg += Device.run_ioreg() connected_devices = [] devplugins = list( sorted(device_plugins(), cmp=lambda x, y: cmp(x.__class__.__name__, y.__class__. __name__))) out( 'Available plugins:', textwrap.fill(' '.join([x.__class__.__name__ for x in devplugins]))) out(' ') out('Looking for devices...') for dev in devplugins: connected, det = s.is_device_connected(dev, debug=True) if connected: out('\t\tDetected possible device', dev.__class__.__name__) connected_devices.append((dev, det)) out(' ') errors = {} success = False out('Devices possibly connected:', end=' ') for dev, det in connected_devices: out(dev.name, end=', ') if not connected_devices: out('None', end='') out(' ') for dev, det in connected_devices: out('Trying to open', dev.name, '...', end=' ') try: dev.reset(detected_device=det) dev.open(det, None) out('OK') except: import traceback errors[dev] = traceback.format_exc() out('failed') continue success = True if hasattr(dev, '_main_prefix'): out('Main memory:', repr(dev._main_prefix)) out('Total space:', dev.total_space()) break if not success and errors: out('Opening of the following devices failed') for dev, msg in errors.items(): out(dev) out(msg) out(' ') if ioreg is not None: ioreg = 'IOREG Output\n' + ioreg out(' ') if ioreg_to_tmp: open('/tmp/ioreg.txt', 'wb').write(ioreg) out('Dont forget to send the contents of /tmp/ioreg.txt') out('You can open it with the command: open /tmp/ioreg.txt') else: out(ioreg) if hasattr(buf, 'getvalue'): return buf.getvalue().decode('utf-8') finally: sys.stdout = oldo sys.stderr = olde