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","marque",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 return self.firstFat
00323
00324
00325
00326
00327
00328
00329 def ensureMounted(self):
00330 mount_paths=self.getProp("device-mount-paths")
00331 if len(mount_paths)==0:
00332 path=self.getProp("device-file-by-id")
00333 if isinstance(path,dbus.Array):
00334 path=path[0]
00335 subprocess.call("udisks --mount %s" %path,shell=True)
00336 return path
00337 else:
00338 return mount_paths[0]
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 class Available:
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367 def __init__(self, checkable=False, access="disk"):
00368 self.checkable=checkable
00369 self.access=access
00370 self.bus = dbus.SystemBus()
00371 proxy = self.bus.get_object("org.freedesktop.UDisks",
00372 "/org/freedesktop/UDisks")
00373 iface = dbus.Interface(proxy, "org.freedesktop.UDisks")
00374 self.disks={}
00375 self.enumDev=iface.EnumerateDevices()
00376
00377 for path in self.enumDev:
00378 ud=uDisk(path, self.bus, checkable)
00379 if ud.isUsbDisk():
00380 self.disks[ud]=[]
00381
00382 if bool(ud.getProp("device-is-partition-table")) == False:
00383
00384
00385 self.disks[ud].append(ud)
00386
00387 for path in self.enumDev:
00388 ud=uDisk(path, self.bus, checkable)
00389 for d in self.disks.keys():
00390 if ud.master() == d.path:
00391 self.disks[d].append(ud)
00392
00393 self.firstFats = self.getFirstFats()
00394
00395 if self.access=="firstFat":
00396 for p in self.firstFats:
00397 p.ensureMounted()
00398
00399
00400
00401
00402
00403 def __trunc__(self):
00404 return len(self.firstFats)
00405
00406
00407
00408
00409
00410
00411
00412
00413 def compare(self, other):
00414 result=self.summary()==other.summary()
00415 return result
00416
00417
00418
00419
00420
00421
00422
00423 def contains(self, ud):
00424 for k in self.disks.keys():
00425 if k.getProp("device-file-by-id")==ud.getProp("device-file-by-id"): return True
00426 return False
00427
00428
00429
00430
00431
00432
00433 def summary(self):
00434 r= "Available USB discs\n"
00435 r+= "===================\n"
00436 for d in sorted(self.disks.keys(), key=lambda disk: disk.getFatUuid()):
00437 r+="%s\n" %(d.title(),)
00438 if len(self.disks[d])>0:
00439 r+=" Partitions :\n"
00440 for part in sorted(self.disks[d], key=lambda disk: disk.getFatUuid()):
00441 r+=" %s\n" %(part.path,)
00442 return r
00443
00444
00445
00446
00447
00448
00449 def __str__(self):
00450 r= "Available USB discs\n"
00451 r+= "===================\n"
00452 for d in self.disks.keys():
00453 r+="%s\n" %d
00454 if len(self.disks[d])>0:
00455 r+=" Partitions :\n"
00456 for part in self.disks[d]:
00457 r+=" %s\n" %(part.path)
00458 r+=part.valuableProperties(12)+"\n"
00459 return r
00460
00461
00462
00463
00464
00465
00466
00467
00468 def __getitem__(self, n):
00469 if self.access=="disk":
00470 return self.disks.keys()[n]
00471 elif self.access=="firstFat":
00472 return self.firstFats[n]
00473
00474
00475
00476
00477
00478
00479
00480 def __len__(self):
00481 if self.access=="disk":
00482 return len(self.disks)
00483 elif self.access=="firstFat":
00484 return len(self.firstFats)
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496 def getFirstFats(self, setOwners=False):
00497 result=[]
00498 for d in self.disks.keys():
00499 for p in self.disks[d]:
00500 if p.isDosFat() or p==d :
00501
00502
00503
00504 result.append(p)
00505
00506 d.fatuuid=p.uuid
00507 d.firstFat=p
00508 p.fatuuid=p.uuid
00509 if setOwners:
00510 p.owner=d.owner
00511 break
00512 return result
00513
00514 if __name__=="__main__":
00515 machin=Available()
00516 print machin
00517
00518