aboutsummaryrefslogtreecommitdiff
path: root/deskutils/calibre/files/patch-src_calibre_devices_scanner.py
blob: c3d73f8e91e4f48e9f4b4b412d15d785f4c3a7d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
--- src/calibre/devices/scanner.py.orig	2011-05-02 11:35:14.000000000 -0500
+++ src/calibre/devices/scanner.py	2011-05-02 11:38:07.000000000 -0500
@@ -5,10 +5,10 @@
 manner.
 '''
 
-import sys, os, re
+import sys, os, re, subprocess
 from threading import RLock
 
-from calibre.constants import iswindows, isosx, plugins, islinux
+from calibre.constants import iswindows, isosx, plugins, islinux, isfreebsd
 
 osx_scanner = win_scanner = linux_scanner = None
 
@@ -155,17 +155,66 @@
             ans.add(tuple(dev))
         return ans
 
+class FreeBSDScanner(object):
+
+    def __call__(self):
+        ans = set([])
+
+        try:
+            out = subprocess.Popen("/usr/sbin/usbconfig dump_device_desc | /usr/bin/awk 'function get_str(s) { split(s, a, /<|>/); if (a[2] != \"no string\") { return a[2]; } else { return \"\";} } BEGIN {state=0;} /^[[:space:]]+idVendor/ {state = 1; vendor = $3; next;} /idProduct/ {productid = $3; next;} /bcdDevice/ {bcd = $3; next;} /iManufacturer/ { manufacturer = get_str($0); next; }  /iProduct/ { product = get_str($0); next;} /iSerialNumber/ { sn = get_str($0); next;} /^$/ {if (state == 1) { state = 0; printf(\"%s%%%%%s%%%%%s%%%%%s%%%%%s%%%%%s\\n\",vendor, productid, bcd, manufacturer, product, sn);} }'", shell=True, stdout=subprocess.PIPE).communicate()[0]
+        except OSError, e:
+           print >>sys.stderr, "Execution failed:", e
+
+        if out.strip() == "":
+            return ans
+        for line in out.strip().split("\n"):
+            ven, prod, bcd, man, prod_string, serial = line.strip().split("%%", 6)
+            dev = []
+            try:
+                dev.append(int(ven, 16))
+            except:
+                continue
+            try:
+                dev.append(int(prod, 16))
+            except:
+                continue
+            try:
+                dev.append(int(bcd, 16))
+            except:
+                continue
+            try:
+                dev.append(man)
+            except:
+                dev.append('')
+            try:
+                dev.append(prod_string)
+            except:
+                dev.append('')
+            try:
+                dev.append(serial)
+            except:
+                dev.append('')
+
+            ans.add(tuple(dev))
+        return ans
+
+
 linux_scanner = None
 
 if islinux:
     linux_scanner = LinuxScanner()
 
+freebsd_scanner = None
+
+if isfreebsd:
+    freebsd_scanner = FreeBSDScanner()
+
 class DeviceScanner(object):
 
     def __init__(self, *args):
         if isosx and osx_scanner is None:
             raise RuntimeError('The Python extension usbobserver must be available on OS X.')
-        self.scanner = win_scanner if iswindows else osx_scanner if isosx else linux_scanner
+        self.scanner = win_scanner if iswindows else osx_scanner if isosx else freebsd_scanner if isfreebsd else linux_scanner
         self.devices = []
 
     def scan(self):