aboutsummaryrefslogtreecommitdiffstats
path: root/mageiaSync/mageiasync.py
blob: a871e1201ad6340b0b3405be5893d2108814b44c (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#!/usr/bin/python

from PyQt5.QtWidgets import (   QProgressDialog, QMainWindow,
        QDialog, QFileDialog, QApplication)
from PyQt5.QtGui import ( QStandardItemModel,QStandardItem, QDesktopServices,   )
from PyQt5.QtCore import ( QLibraryInfo,  QUrl, QItemSelectionModel )
from PyQt5 import QtCore  # , Qt, QThread, QObject, pyqtSignal)
import sys
import mageiaSyncUI
import mageiaSyncExt
import mageiaSyncDBprefs
import mageiaSyncDBprefs0
import mageiaSyncDBrename
import mageiaSyncAbout
import mageiaSyncCredits



class prefsDialog(QDialog,mageiaSyncDBprefs.Ui_prefsDialog ):

    def __init__(self, parent=None):
        QDialog.__init__(self,parent)
        self.setupUi(self)
        self.selectDest.clicked.connect(isosSync.selectDestination)

class prefsDialog0(QDialog,mageiaSyncDBprefs0.Ui_prefsDialog0 ):

    def __init__(self, parent=None):
        QDialog.__init__(self,parent)
        self.setupUi(self)

class renameDialog(QDialog,mageiaSyncDBrename.Ui_renameDialog ):
    #   Display a dialog box to choose to rename an old collection of ISOs to a new one

    def __init__(self, parent=None):
        QDialog.__init__(self,parent)
        self.setupUi(self)
        self.chooseDir.clicked.connect(isosSync.renameDir)

class aboutDialog(QDialog,mageiaSyncAbout.Ui_aboutDialog ):

    def __init__(self, parent=None):
        QDialog.__init__(self,parent)
        self.setupUi(self)
        self.creditsButton.clicked.connect(isosSync.credits)

class creditsDialog(QDialog,mageiaSyncCredits.Ui_creditsDialog ):

    def __init__(self, parent=None):
        QDialog.__init__(self,parent)
        self.setupUi(self)
        text=''
        try:
            with open('/usr/share/doc/python-mageiasync/README.md', 'r') as f:
                     block = f.readlines()
                     for line in block:
                         text+= line
        except:
           pass
        self.Readme.setText(text)


class LogWindow(QProgressDialog):
    #   Display a box at start during the remote directory list loading

    def __init__(self, parent=None):
        super(LogWindow, self).__init__(parent)
        self.setWindowModality(QtCore.Qt.WindowModal)
        self.setWindowTitle('Loading...')
        self.setLabelText(self.tr('Loading images list from repository.'))
        self.setMinimum(0)
        self.setMaximum(0)
        self.setAutoReset(False)
        self.setAutoClose(False)
        self.setMinimumDuration(1)

    def perform(self):
        self.progressDialog.setValue(self.progress)

class dbWarning(QProgressDialog):
    #   Display a box at start during the remote directory list loading

    def __init__(self, parent=None):
        super(dbWarning, self).__init__(parent)
        self.setWindowModality(QtCore.Qt.WindowModal)
        self.setWindowTitle(self.tr('Loading...'))
        self.setLabelText(self.tr('Loading images list from repository.'))
        self.setMinimum(0)
        self.setMaximum(0)
        self.setAutoReset(False)
        self.setAutoClose(False)
        self.setMinimumDuration(1)

    def perform(self):
        self.progressDialog.setValue(self.progress)

class IsosViewer(QMainWindow, mageiaSyncUI.Ui_mainWindow):
    #   Display the main window
    def __init__(self, parent=None):
        super(IsosViewer, self).__init__(parent)
        self.setupUi(self)
        self.connectActions()
        self.IprogressBar.setMinimum(0)
        self.IprogressBar.setMaximum(100)
        self.IprogressBar.setValue(0)
        self.IprogressBar.setEnabled(False)
        self.selectAllState=True
        self.stop.setEnabled(False)
        self.destination=''
        self.rsyncThread = mageiaSyncExt.syncThread(self)    # create a thread to launch rsync
        self.rsyncThread.progressSignal.connect(self.setProgress)
        self.rsyncThread.speedSignal.connect(self.setSpeed)
        self.rsyncThread.sizeSignal.connect(self.setSize)
        self.rsyncThread.remainSignal.connect(self.setRemain)
        self.rsyncThread.endSignal.connect(self.syncEnd)
        self.rsyncThread.lvM.connect(self.lvMessage)
        self.rsyncThread.checkSignal.connect(self.checks)
        self.checkThreads=[]    #   A list of thread for each iso

 #      Model for local list view in a table
        self.model = QStandardItemModel(0, 5, self)
        headers=[self.tr("Directory"),self.tr("Name"),self.tr("Size"),"SHA1","MD5"]
        i=0
        for label in headers:
            self.model.setHeaderData(i, QtCore.Qt.Horizontal,label )
            i+=1

#      Model for remote list view in a table
        self.modelRemote = QStandardItemModel(0, 4, self)
        headers=[self.tr("Directory"),self.tr("Name"),self.tr("Size"),self.tr("Date")]
        i=0
        for label in headers:
           self.modelRemote.setHeaderData(i, QtCore.Qt.Horizontal,label )
           i+=1

#       settings for the local list view
        self.localList.setModel(self.model)
        self.localList.setColumnWidth(0,230)
        self.localList.setColumnWidth(1,230)
        self.localList.setColumnWidth(2,140)
        self.localList.horizontalHeader().setStretchLastSection(True)
        self.localList.setSelectionMode(0)  # NoSelection
        # settings for local iso names management
        self.localListNames=[]

#       settings for the remote list view
        self.listIsos.setModel(self.modelRemote)
        self.listIsos.setColumnWidth(0,160)
        self.listIsos.setColumnWidth(1,350)
        self.listIsos.setColumnWidth(2,120)
        self.listIsos.horizontalHeader().setStretchLastSection(True)
        self.listIsos.setSelectionMode(2)  # MultiSelection
        self.listIsosNames=[]

    def add(self, path,iso,isoSize,date):
    #   Add an remote ISO in list
         itemPath=QStandardItem(path)
         itemPath.setData(path,3) # Add tooltip
         itemIso=QStandardItem(iso)
         itemIso.setData(iso,3) # Add tooltip
         if isoSize==0:
             itemSize=QStandardItem('--')
         else:
             formatedSize=isoSize.replace(","," ")
             itemSize=QStandardItem(formatedSize)
             itemSize.setData(formatedSize,3) # Add tooltip
         itemSize.setTextAlignment(QtCore.Qt.AlignVCenter|QtCore.Qt.AlignHCenter)

         itemDate=QStandardItem(date)
         itemDate.setData(date,3) # Add tooltip
         itemDate.setTextAlignment(QtCore.Qt.AlignVCenter|QtCore.Qt.AlignHCenter)
         self.modelRemote.appendRow([itemPath,itemIso,itemSize,itemDate,])
         self.listIsosNames.append([path,iso])

    def localAdd(self, path,iso,isoSize):
        #   Add an entry in local ISOs list, with indications about checking
        itemPath=QStandardItem(path)
        itemPath.setData(path,3) # Add tooltip
        itemIso=QStandardItem(iso)
        itemIso.setData(iso,3) # Add tooltip
        if isoSize==0:
            itemSize=QStandardItem('--')
        else:
            formatedSize='{:n}'.format(isoSize).replace(","," ")
            itemSize=QStandardItem(formatedSize)
            itemSize.setData(formatedSize,3) # Add tooltip
        itemSize.setTextAlignment(QtCore.Qt.AlignVCenter|QtCore.Qt.AlignHCenter)
        itemCheck1=QStandardItem("--")
        itemCheck1.setTextAlignment(QtCore.Qt.AlignVCenter|QtCore.Qt.AlignHCenter)
        itemCheck5=QStandardItem("--")
        itemCheck5.setTextAlignment(QtCore.Qt.AlignVCenter|QtCore.Qt.AlignHCenter)
        self.model.appendRow([itemPath,itemIso,itemSize,itemCheck1, itemCheck5,])
        self.localListNames.append([path,iso])

    def setProgress(self, value):
        #   Update the progress bar
        self.IprogressBar.setValue(value)

    def setSpeed(self, value):
        #   Update the speed field
        self.speedLCD.display(value)

    def setSize(self, size):
        #   Update the size field
        self.Lsize.setText(size+self.tr(" bytes"))

    def setRemain(self,remainTime):
        content=QtCore.QTime.fromString(remainTime,"h:mm:ss")
        self.timeRemaining.setTime(content)

    def manualChecks(self):
        for iso in self.modelRemote.selectedItems():
            path,name=iso.text().split('/')
            try:
                #   Look for ISO in local list
                item=self.model.findItems(name,QtCore.Qt.MatchExactly,1)[0]
            except:
                #   Remote ISO is not yet in local directory. We add it in localList and create the directory
                self.localAdd(path,name,0)
                basedir=QtCore.QDir(self.destination)
                basedir.mkdir(path)
                item=self.model.findItems(name,QtCore.Qt.MatchExactly,1)[0]
            row=self.model.indexFromItem(item).row()
            self.checks(row)

    def checks(self,isoIndex):
        #   processes a checking for each iso
        #   launches a thread for each iso
        newThread=mageiaSyncExt.checkThread(self)
        self.checkThreads.append(newThread)
        self.checkThreads[-1].setup(self.destination,
            self.model.data(self.model.index(isoIndex,0)) ,
            self.model.data(self.model.index(isoIndex,1)),
            isoIndex)
        self.checkThreads[-1].md5Signal.connect(self.md5Check)
        self.checkThreads[-1].sha1Signal.connect(self.sha1Check)
#        self.checkThreads[-1].dateSignal.connect(self.dateCheck)
        self.checkThreads[-1].sizeFinalSignal.connect(self.sizeUpdate)
        self.checkThreads[-1].checkStartSignal.connect(self.checkStart)
        self.checkThreads[-1].start()

    def checkStart(self,isoIndex):
        #   the function indicates that checking is in progress
        #   the hundred contains index of the value to check, the minor value contains the row
        col=(int)(isoIndex/100)
        row=isoIndex-col*100
        self.model.setData(self.model.index(row, col, QtCore.QModelIndex()), self.tr("Checking"))

    def md5Check(self,check):
        if check>=128:
            val=self.tr("OK")
            row=check-128
        else:
            val=self.tr("Failed")
            row=check
        self.model.setData(self.model.index(row, 4, QtCore.QModelIndex()), val)

    def sha1Check(self,check):
        if check>=128:
            val=self.tr("OK")
            row=check-128
        else:
            val=self.tr("Failed")
            row=check
        self.model.setData(self.model.index(row, 3, QtCore.QModelIndex()), val)

    def dateCheck(self,check):
        if check>=128:
            val=self.tr("OK")
            row=check-128
        else:
            val=self.tr("Failed")
            row=check
        self.model.setData(self.model.index(row, 3, QtCore.QModelIndex()), val)

    def sizeUpdate(self,signal,isoSize):
        col=(int)(signal/100)
        row=signal-col*100
        self.model.setData(self.model.index(row, col, QtCore.QModelIndex()), isoSize)

    def syncEnd(self, rc):
        if rc==1:
            self.lvMessage(self.tr("Command rsync not found"))
        elif rc==2:
            self.lvMessage(self.tr("Error in rsync parameters"))
        elif rc==3:
            self.lvMessage(self.tr("Unknown error in rsync"))
        self.IprogressBar.setEnabled(False)
        self.syncGo.setEnabled(True)
        self.listIsos.setEnabled(True)
        self.selectAll.setEnabled(True)
        self.stop.setEnabled(False)

    def prefsInit(self):
    #   Load the parameters at first
        params=QtCore.QSettings("Mageia","mageiaSync")
        paramRelease=""
        try:
            paramRelease=params.value("release", type="QString")
            #   the parameters already initialised?
        except:
            pass
        if paramRelease =="":
            # Values are not yet set
            self.pd0=prefsDialog0()
            self.pd0.user.setFocus()
            answer=self.pd0.exec_()
            if answer:
                #   Update params
                self.user=self.pd0.user.text()
                self.password=self.pd0.password.text()
                self.location=self.pd0.location.text()
                params=QtCore.QSettings("Mageia","mageiaSync")
                params.setValue("user",self.user)
                params.setValue("password",self.password)
                params.setValue("location",self.location)
            else:
                pass
#                answer=QDialogButtonBox(QDialogButtonBox.Ok)
                # the user must set values or default values
            self.pd0.close()
            self.pd=prefsDialog()
            if self.password !="":
                code,list=mageiaSyncExt.findRelease('rsync://'+self.user+'@bcd.mageia.org/isos/',self.password)
                if code==0:
                    for item in list:
                        self.pd.release.addItem(item)
            self.pd.password.setText(self.password)
            self.pd.user.setText(self.user)
            self.pd.location.setText(self.location)
            self.pd.selectDest.setText(QtCore.QDir.currentPath())
            self.pd.release.setFocus()
            answer=self.pd.exec_()
            if answer:
                #   Update params
                self.user=self.pd.user.text()
                self.password=self.pd.password.text()
                self.location=self.pd.location.text()
                params=QtCore.QSettings("Mageia","mageiaSync")
                self.release= self.pd.release.currentText()
                self.destination=self.pd.selectDest.text()
                self.bwl=self.pd.bwl.value()
                params.setValue("release", self.release)
                params.setValue("user",self.user)
                params.setValue("password",self.password)
                params.setValue("location",self.location)
                params.setValue("destination",self.destination)
                params.setValue("bwl",str(self.bwl))
            else:
                pass
#                answer=QDialogButtonBox(QDialogButtonBox.Ok)
                print(self.tr("the user must set values or default values"))
            self.pd.close()
        else:
            self.release=params.value("release", type="QString")
            self.user=params.value("user", type="QString")
            self.location=params.value("location", type="QString")
            self.password=params.value("password", type="QString")
            self.destination=params.value("destination", type="QString")
            self.bwl=params.value("bwl",type=int)
        self.localDirLabel.setText(self.tr("Local directory: ")+self.destination)
        if self.location !="":
            self.remoteDirLabel.setText(self.tr("Remote directory: ")+self.location)


    def selectDestination(self):
        #   dialog box to select the destination (local directory)
        directory = QFileDialog.getExistingDirectory(self, self.tr('Select a directory'),'~/')
        isosSync.destination = directory
        self.pd.selectDest.setText(isosSync.destination)


    def selectAllIsos(self):
        #   Select or unselect the ISOs in remote list
        if self.selectAllState :
            selectMode = QItemSelectionModel.Select
            self.selectAll.setText(self.tr("Unselect &All"))
        else:
            selectMode = QItemSelectionModel.Deselect
            self.selectAll.setText(self.tr("Select &All"))
        for i in range(self.modelRemote.rowCount()):
            self.listIsos.selectionModel().select(self.modelRemote.index(i,0),QItemSelectionModel.Rows|
            selectMode)   # set flag to selected or deselected
        self.selectAllState=not self.selectAllState

    def connectActions(self):
        self.actionQuit.triggered.connect(app.quit)
        self.quit.clicked.connect(app.quit)
        self.actionRename.triggered.connect(self.rename)
        self.actionUpdate.triggered.connect(self.updateList)
        self.actionCheck.triggered.connect(self.manualChecks)
        self.actionPreferences.triggered.connect(self.prefs)
        self.syncGo.clicked.connect(self.launchSync)
        self.selectAll.clicked.connect(self.selectAllIsos)
        self.actionAbout.triggered.connect(self.about)
        self.actionOnline_help.triggered.connect(self.help)

    def updateList(self):
        # From the menu entry
        self.lw = LogWindow()
        self.lw.show()
        self.modelRemote.removeRows(0,self.modelRemote.rowCount())
        self.model.removeRows(0,self.model.rowCount())
        if self.location  == "" :
            self.nameWithPath='rsync://'+self.user+'@bcd.mageia.org/isos/'+self.release+'/'
#            print self.nameWithPath
        else:
            self.nameWithPath=self.location+'/'
        self.lvMessage(self.tr("Source: ")+self.nameWithPath)
        self.fillList = mageiaSyncExt.findIsos()
        self.fillList.setup(self.nameWithPath, self.password,self.destination)
        self.fillList.endSignal.connect(self.closeFill)
        self.fillList.start()
        # Reset the button
        self.selectAll.setText(self.tr("Select &All"))
        self.selectAllState=True

    def lvMessage( self,message):
        #   Add a line in the logview
        self.lvText.append(message)

    def renameDir(self):
        #   Choose the directory where isos are stored
        directory = QFileDialog.getExistingDirectory(self, self.tr('Select a directory'),self.destination)
        self.rd.chooseDir.setText(directory)

    def rename(self):
        #   rename old isos and directories to a new release
        self.rd=renameDialog()
        loc=[]
        loc=self.location.split('/')
        self.rd.oldRelease.setText(loc[-1])
        self.rd.chooseDir.setText(self.destination)
        answer=self.rd.exec_()
        if answer:
            returncode=mageiaSyncExt.rename(self.rd.chooseDir.text(),self.rd.oldRelease.text(),str(self.rd.newRelease.text()))
            if returncode==1:
                returnMsg="Error in renaming"
            else:
                returnMsg=self.tr("Success in renaming")
            self.lvMessage(returnMsg)
        self.updateList()
        self.rd.close()

    def prefs(self):
        # From the menu entry
        self.pd=prefsDialog()
        self.pd.release.addItem(self.release)
        self.pd.password.setText(self.password)
        self.pd.user.setText(self.user)
        self.pd.location.setText(self.location)
        self.pd.selectDest.setText(self.destination)
        self.pd.bwl.setValue(self.bwl)
        params=QtCore.QSettings("Mageia","mageiaSync")
        answer=self.pd.exec_()
        if answer:
            params.setValue("release", self.pd.release.currentText())
            params.setValue("user",self.pd.user.text())
            params.setValue("password",self.pd.password.text())
            params.setValue("location",self.pd.location.text())
            params.setValue("destination",self.pd.selectDest.text())
            params.setValue("bwl",str(self.pd.bwl.value()))
        self.prefsInit()
        self.updateList()
        self.pd.close()

    def about(self):
        ad = aboutDialog()
        answer=ad.exec_()
        if answer:
            ad.close()

    def credits(self):
        ad = creditsDialog()
        answer=ad.exec_()
        if answer:
            ad.close()

    def help(self):
        # Open page in browser
        l = QDesktopServices.openUrl(QUrl('http://wiki.mageia.org/en/MageiaSync'))

    def launchSync(self):
        self.IprogressBar.setEnabled(True)
        self.stop.setEnabled(True)
        self.syncGo.setEnabled(False)
        self.listIsos.setEnabled(False)
        self.selectAll.setEnabled(False)
        # Connect the button Stop
        self.stop.clicked.connect(self.stopSync)
        self.rsyncThread.params(self.password, self.bwl)
        remoteRow=-1
        for isoIndex in self.listIsos.selectionModel().selectedIndexes():
            if remoteRow != isoIndex.row():
                remoteRow = isoIndex.row()
                path = self.model.data(self.model.index(remoteRow,0))
                name = self.model.data(self.model.index(remoteRow,1))
                try:
                    #   Look for ISO in local list
                    item=self.model.findItems(name,QtCore.Qt.MatchExactly,1)[0]
                except:
                    #   Remote ISO is not yet in local directory. We add it in localList and create the directory
                    self.localAdd(path,name,0)
                    basedir=QtCore.QDir(self.destination)
                    basedir.mkdir(path)
                    item=self.model.findItems(name,QtCore.Qt.MatchExactly,1)[0]
                row=self.model.indexFromItem(item).row()
                if self.location  == "" :
                    self.nameWithPath='rsync://'+self.user+'@bcd.mageia.org/isos/'+self.release+'/'+path
                else:
                    self.nameWithPath=self.location+path
                if (not str(path).endswith('/')):
                        self.nameWithPath+='/'
                self.rsyncThread.setup(self.nameWithPath, self.destination+'/'+path+'/',row)
        self.rsyncThread.start()             # start the thread
        # Pour les tests uniquement
            #rsync://$user@bcd.mageia.org/isos/$release/
        #self.nameWithPath='rsync://ftp5.gwdg.de/pub/linux/mageia/iso/4.1/Mageia-4.1-LiveCD-GNOME-en-i586-CD/'

    def closeFill(self,code):
        if code==0: #   list returned
            list=self.fillList.getList()
            for size, date,longName in list:
                path=longName.split('/')
                self.add(path[0], path[-1], size,date)
        elif code==1:
            self.lvMessage(self.tr("Command rsync not found"))
        elif code==2:
            self.lvMessage(self.tr("Error in rsync parameters"))
        elif code==3:
            self.lvMessage(self.tr("Unknown error in rsync"))
        list=self.fillList.getLocal()
        for path,iso,isoSize in list:
            self.localAdd(path,iso, isoSize)
        self.fillList.quit()
        self.lw.hide()

    def stopSync(self):
        self.rsyncThread.stop()
        self.IprogressBar.setEnabled(False)
        self.stop.setEnabled(False)
        self.syncGo.setEnabled(True)
        self.listIsos.setEnabled(True)
        self.selectAll.setEnabled(True)


    def main(self):
        self.show()
        #   Load or look for intitial parameters
        self.prefsInit()
        # look for Isos list and add it to the isoSync list. Update preferences
        self.updateList()

    def close(self):
        self.rsyncThread.stop()
        exit(0)

if __name__=='__main__':
    app = QApplication(sys.argv)
    locale = QtCore.QLocale.system().name()
    qtTranslator = QtCore.QTranslator()
    if qtTranslator.load("qt_" + locale,QLibraryInfo.location(QLibraryInfo.TranslationsPath)):
        app.installTranslator(qtTranslator)
    appTranslator = QtCore.QTranslator()
    if appTranslator.load("mageiaSync_" + locale,'/usr/share/mageiasync/translations'):
        app.installTranslator(appTranslator)
    isosSync = IsosViewer()
    isosSync.main()
    sys.exit(app.exec_())