00001
00002
00003
00004 licence={}
00005 licence_en="""
00006 file usbDisk.py
00007 this file is part of the project scolasync
00008
00009 Copyright (C) 2010 Georges Khaznadar <georgesk@ofset.org>
00010
00011 This program is free software: you can redistribute it and/or modify
00012 it under the terms of the GNU General Public License as published by
00013 the Free Software Foundation, either version3 of the License, or
00014 (at your option) any later version.
00015
00016 This program is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 GNU General Public License for more details.
00020
00021 You should have received a copy of the GNU General Public License
00022 along with this program. If not, see <http://www.gnu.org/licenses/>.
00023 """
00024
00025 licence['en']=licence_en
00026
00027 import dbus, subprocess, os.path, re
00028 from PyQt4.QtGui import *
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 class uDisk:
00043
00044
00045
00046
00047
00048
00049
00050
00051 def __init__(self, path, bus, checkable=False):
00052 self.path=path
00053 self.device = bus.get_object("org.freedesktop.UDisks", self.path)
00054 self.device_prop = dbus.Interface(self.device, "org.freedesktop.DBus.Properties")
00055 self.selected=True
00056 self.checkable=checkable
00057 self.stickid=unicode(self.getProp("drive-serial"))
00058 self.uuid=self.getProp("id-uuid")
00059 self.fatuuid=None
00060 self.firstFat=None
00061
00062
00063
00064 _itemNames={
00065 "1device-mount-paths":QApplication.translate("uDisk","point de montage",None, QApplication.UnicodeUTF8),
00066 "2device-size":QApplication.translate("uDisk","taille",None, QApplication.UnicodeUTF8),
00067 "3drive-vendor":QApplication.translate("uDisk","vendeur",None, QApplication.UnicodeUTF8),
00068 "4drive-model":QApplication.translate("uDisk","modèle de disque",None, QApplication.UnicodeUTF8),
00069 "5drive-serial":QApplication.translate("uDisk","numéro de série",None, QApplication.UnicodeUTF8),
00070 }
00071
00072 _specialItems={"0Check":QApplication.translate("uDisk","cocher",None, QApplication.UnicodeUTF8)}
00073
00074 _ItemPattern=re.compile("[0-9]?(.*)")
00075
00076
00077
00078
00079
00080
00081
00082 def getFatUuid(self):
00083 return "%s" %self.fatuuid
00084
00085
00086
00087
00088
00089
00090
00091 def uniqueId(self):
00092 return self.getFatUuid()
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 def headers(checkable=False,locale="C"):
00105 if checkable:
00106 result=uDisk._specialItems.keys()+ uDisk._itemNames.keys()
00107 return sorted(result)
00108 else:
00109 return sorted(uDisk._itemNames.keys())
00110
00111 headers = staticmethod(headers)
00112
00113
00114
00115
00116
00117
00118
00119 def devicePropProxy(self, bus):
00120 return self.device_prop
00121
00122
00123
00124
00125
00126
00127
00128
00129 def isTrue(self,prop, value=None):
00130 if value==None:
00131 return bool(self.getProp(prop))
00132 else:
00133 return self.getProp(prop)==value
00134
00135
00136
00137
00138
00139
00140 def isUsbDisk(self):
00141 return self.isTrue("device-is-removable") and self.isTrue("drive-connection-interface","usb") and self.isTrue("device-size")
00142
00143
00144
00145
00146
00147
00148 def __str__(self):
00149 return self.title()+self.valuableProperties()
00150
00151
00152
00153
00154
00155
00156 def title(self):
00157 return self.path
00158
00159
00160
00161
00162
00163
00164
00165 def file(self):
00166 fileById=self.getProp("device-file-by-id")
00167 if isinstance(fileById, dbus.Array): fileById=fileById[0]
00168 return fileById
00169
00170
00171
00172
00173
00174
00175 def mountPoint(self):
00176 paths=self.getProp("device-mount-paths")
00177 if isinstance(paths, dbus.Array) and len(paths)>0:
00178 return paths[0]
00179 else:
00180 return None
00181
00182
00183
00184
00185
00186
00187
00188 def getProp(self, name):
00189 try:
00190 return self.device_prop.Get("org.freedesktop.UDisks", name)
00191 except:
00192 return None
00193
00194
00195
00196
00197
00198
00199 def isDosFat(self):
00200 try:
00201 code=eval(self.getProp("partition-type"))
00202 fatCodes = [0x04, 0x06, 0x0b, 0x0c, 0x0d, 0x0e]
00203 return code in fatCodes
00204 except:
00205 return False
00206
00207
00208
00209
00210
00211
00212 def valuableProperties(self,indent=4):
00213 prefix="\n"+" "*indent
00214 r=""
00215 props=["device-file-by-id",
00216 "device-mount-paths",
00217 "device-is-partition-table",
00218 "partition-table-count",
00219 "device-is-read-only",
00220 "device-is-drive",
00221 "device-is-optical-disc",
00222 "device-is-mounted",
00223 "drive-vendor",
00224 "drive-model",
00225 "drive-serial",
00226 "id-uuid",
00227 "partition-slave",
00228 "partition-type",
00229 "device-size"]
00230 for prop in props:
00231 p=self.getProp(prop)
00232 if isinstance(p,dbus.Array):
00233 if len(p)>0:
00234 r+=prefix+"%s = array:" %(prop)
00235 for s in p:
00236 r+=prefix+" "*indent+s
00237 elif isinstance(p,dbus.Boolean):
00238 r+=prefix+"%s = %s" %(prop, bool(p))
00239 elif isinstance(p,dbus.Int16) or isinstance(p,dbus.Int32) or isinstance(p,dbus.Int64) or isinstance(p,dbus.UInt16) or isinstance(p,dbus.UInt32) or isinstance(p,dbus.UInt64) or isinstance(p,int):
00240 if p < 10*1024:
00241 r+=prefix+"%s = %s" %(prop,p)
00242 elif p < 10*1024*1024:
00243 r+=prefix+"%s = %s k" %(prop,p/1024)
00244 elif p < 10*1024*1024*1024:
00245 r+=prefix+"%s = %s M" %(prop,p/1024/1024)
00246 else:
00247 r+=prefix+"%s = %s G" %(prop,p/1024/1024/1024)
00248 else:
00249 r+=prefix+"%s = %s" %(prop,p)
00250 return r
00251
00252
00253
00254
00255
00256
00257 def master(self):
00258 return self.getProp("partition-slave")
00259
00260
00261
00262
00263
00264
00265
00266
00267 def unNumberProp(self,n):
00268 m=uDisk._ItemPattern.match(self.headers()[n])
00269 try:
00270 prop=m.group(1)
00271 result=self.showableProp(prop)
00272 return result
00273 except:
00274 return ""
00275
00276
00277
00278
00279
00280
00281
00282
00283 def __getitem__(self,n):
00284 propListe=self.headers()
00285 if self.checkable:
00286 if n==0:
00287 return self.selected
00288 elif n <= len(propListe):
00289 return self.unNumberProp(n-1)
00290 else:
00291 if n < len(propListe):
00292 return self.unNumberProp(n)
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303 def showableProp(self, name):
00304 p=self.getProp(name)
00305 if isinstance(p,dbus.Array):
00306 if len(p)>0: return str(p[0])
00307 else: return ""
00308 elif isinstance(p,dbus.Boolean):
00309 return "%s" %bool(p)
00310 elif isinstance(p,dbus.Int16) or isinstance(p,dbus.Int32) or isinstance(p,dbus.Int64) or isinstance(p,dbus.UInt16) or isinstance(p,dbus.UInt32) or isinstance(p,dbus.UInt64) or isinstance(p,int):
00311 return int(p)
00312 else:
00313 return "%s" %p
00314
00315
00316
00317
00318
00319
00320 def getFirstFat(self):
00321 if self.isDosFat(): return self
00322
00323
00324
00325
00326 return self.firstFat
00327
00328
00329
00330
00331
00332
00333 def ensureMounted(self):
00334 mount_paths=self.getProp("device-mount-paths")
00335 if len(mount_paths)==0:
00336 path=self.getProp("device-file-by-id")
00337 if isinstance(path,dbus.Array):
00338 path=path[0]
00339 subprocess.call("udisks --mount %s" %path,shell=True)
00340 return path
00341 else:
00342 return mount_paths[0]
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 class Available:
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371 def __init__(self, checkable=False, access="disk"):
00372 self.checkable=checkable
00373 self.access=access
00374 self.bus = dbus.SystemBus()
00375 proxy = self.bus.get_object("org.freedesktop.UDisks",
00376 "/org/freedesktop/UDisks")
00377 iface = dbus.Interface(proxy, "org.freedesktop.UDisks")
00378 self.disks={}
00379 self.enumDev=iface.EnumerateDevices()
00380
00381 for path in self.enumDev:
00382 ud=uDisk(path, self.bus, checkable)
00383 if ud.isUsbDisk():
00384 self.disks[ud]=[]
00385
00386 if bool(ud.getProp("device-is-partition-table")) == False:
00387
00388
00389 self.disks[ud].append(ud)
00390
00391 for path in self.enumDev:
00392 ud=uDisk(path, self.bus, checkable)
00393 for d in self.disks.keys():
00394 if ud.master() == d.path:
00395 self.disks[d].append(ud)
00396
00397 self.firstFats = self.getFirstFats()
00398
00399 if self.access=="firstFat":
00400 for p in self.firstFats:
00401 p.ensureMounted()
00402
00403
00404
00405
00406
00407
00408
00409
00410 def compare(self, other):
00411 result=self.summary()==other.summary()
00412 return result
00413
00414
00415
00416
00417
00418
00419
00420 def contains(self, ud):
00421 for k in self.disks.keys():
00422 if k.getProp("device-file-by-id")==ud.getProp("device-file-by-id"): return True
00423 return False
00424
00425
00426
00427
00428
00429
00430 def summary(self):
00431 r= "Available USB discs\n"
00432 r+= "===================\n"
00433 for d in sorted(self.disks.keys(), key=lambda disk: disk.getFatUuid()):
00434 r+="%s\n" %(d.title(),)
00435 if len(self.disks[d])>0:
00436 r+=" Partitions :\n"
00437 for part in sorted(self.disks[d], key=lambda disk: disk.getFatUuid()):
00438 r+=" %s\n" %(part.path,)
00439 return r
00440
00441
00442
00443
00444
00445
00446 def __str__(self):
00447 r= "Available USB discs\n"
00448 r+= "===================\n"
00449 for d in self.disks.keys():
00450 r+="%s\n" %d
00451 if len(self.disks[d])>0:
00452 r+=" Partitions :\n"
00453 for part in self.disks[d]:
00454 r+=" %s\n" %(part.path)
00455 r+=part.valuableProperties(12)+"\n"
00456 return r
00457
00458
00459
00460
00461
00462
00463
00464
00465 def __getitem__(self, n):
00466 if self.access=="disk":
00467 return self.disks.keys()[n]
00468 elif self.access=="firstFat":
00469 return self.firstFats[n]
00470
00471
00472
00473
00474
00475
00476
00477 def __len__(self):
00478 if self.access=="disk":
00479 return len(self.disks)
00480 elif self.access=="firstFat":
00481 return len(self.firstFats)
00482
00483
00484
00485
00486
00487
00488
00489
00490 def getFirstFats(self):
00491 result=[]
00492 for d in self.disks.keys():
00493 for p in self.disks[d]:
00494 if p.isDosFat() or p==d :
00495
00496
00497
00498 result.append(p)
00499
00500 d.fatuuid=p.uuid
00501 d.firstFat=p
00502 p.fatuuid=p.uuid
00503 break
00504 return result
00505
00506 if __name__=="__main__":
00507 machin=Available()
00508 print machin
00509
00510