aboutsummaryrefslogtreecommitdiffstats
path: root/mageiaSync/mageiaSyncExt.py
blob: 499881304e7a74bf99a8695ef8cc107f848b9946 (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
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 12 21:42:56 2014

@author: yves
"""

import re
import os
import gnupg
import shutil

from subprocess import Popen, PIPE
from PyQt6.QtCore import QDir, QFileInfo, pyqtSignal, QThread

class checkThread(QThread):
    sha3Signal= pyqtSignal(int)
    dateSignal=pyqtSignal(int)
    sizeFinalSignal=pyqtSignal(int,str)
    checkStartSignal=pyqtSignal(int)

    def __init__(self, parent=None):
        super(checkThread, self).__init__(parent)

    def processSum(self,sumType):
        import hashlib
        checkvalue=0
        if sumType=='sha3':
            hashfunc = hashlib.sha3_512()
        #       Check if the sum file has a valid signature
        gpg = gnupg.GPG()
        gpg.encoding = 'utf-8'
        gpg.recv_keys('keyserver.ubuntu.com', 'EDCA7A90')
        sum_file = self.destination+'/'+self.path+'/'+self.name+'.'+sumType
        sig_file = sum_file +'.gpg'
        try:
            with open(sig_file, 'rb') as g:
                verified = gpg.verify_file(g,close_file=False)
                if verified.valid:
                    checkvalue=64
                    print('Valid signature')
                else:
                    verified = gpg.verify_file(g, sum_file)
                    if verified.valid:
                        checkvalue=64
                        print('Valid detached signature')
                    else:
                        print('Invalid signature for %s'%self.name+'.'+sumType)
                        print(verified.key_status)
                        return 0
        except:
            print('Signature file %s not found'%sig_file)
            checkvalue = 32
        try:
            fs=open(sum_file,'r')
        except:
            #   reference file not found
            return checkvalue
        try:
            with open(self.destination+'/'+self.path+'/'+self.name, 'rb') as f:
                while True:
                    block = f.read(2**10)
                    if not block: break
                    hashfunc.update(block)
                sumcalc=hashfunc.hexdigest()
        except:
            return checkvalue
        sumcheck=(fs.readline()).split()[0]
        if sumcalc.upper()==sumcheck:
            checkvalue += 128
        return checkvalue

    def setup(self, destination, path,name,isoIndex):
        self.destination=destination
        self.path=path
        self.name=name
        self.isoIndex=isoIndex

    def run(self):
        signal=200+self.isoIndex
        isoSize=QFileInfo(str(self.destination)+'/'+self.path+'/' +self.name).size()
        self.sizeFinalSignal.emit(signal, '{:,}'.format(isoSize).replace(',',' '))
        signal=300+self.isoIndex
        self.checkStartSignal.emit(signal)
        checkSha=self.processSum('sha3')
        self.sha3Signal.emit(self.isoIndex+checkSha)
        self.quit()


class syncThread(QThread):
    progressSignal = pyqtSignal(int)
    speedSignal= pyqtSignal(int)
    endSignal=pyqtSignal(int)
    remainSignal=pyqtSignal(str)
    checkSignal=pyqtSignal(int)
    sizeSignal=pyqtSignal(str)
    lvM=pyqtSignal(str)

    def __init__(self, parent=None):
        super(syncThread, self).__init__(parent)
        self.stopped=False
        self.list=[]

    def setup(self,nameWithPath, destination, size, row,):
        #   row is the index in 'model'
        iso={'nameWithPath':str(nameWithPath), 'destination':str(destination), 'row':row, 'size':size}
        self.list.append(iso)

    def params(self, password, bwl):
        self.password=password
        self.bwl=bwl    #   Bandwith limit

    def stop(self):
        self.stopped=True
        try:
            self.process.terminate()
            self.lvM.emit(self.tr("Process rsync stopped"))
            self.list=[]
        except:
            self.lvM.emit(self.tr("Process rsync already stopped"))
        # Init progressbar and speed counter
        self.endSignal.emit(0)

    def run(self):
        if len(self.list)==0:
            self.lvM.emit(self.tr("No entry selected"))
        for iso in self.list:
            # Check free space
            _, _, free = shutil.disk_usage( iso['destination'])
            if free <= iso["size"]:
                self.lvM.emit( self.tr("Space available {:.3f} Mb isn't enough ({:.3f} Mb needed)"). format(free/1024/1024, iso["size"]/1024/1024))
                continue
            errorOccured=True
            self.lvM.emit(self.tr("Starting rsync with ")+iso['nameWithPath'])
            if self.bwl!=0:
                commande=['rsync','-avP','--no-p','--modify-window=1',"--bwlimit="+str(self.bwl), iso['nameWithPath'], iso['destination']]
            else:
                commande=['rsync','-avP','--no-p','--modify-window=1', iso['nameWithPath'], iso['destination']]
            try:
                envir = os.environ.copy()
                envir['LC_ALL']="C"
                if self.password != "":
                    envir['RSYNC_PASSWORD']=str(self.password)
                self.process = Popen(commande, shell=False, stdout=PIPE, stderr=PIPE, env=envir)
                errorOccured=False
            except OSError as e:
                self.endSignal.emit(1)
#                Command rsync not found
            except ValueError as e:
                self.endSignal.emit(2)
#                Error in rsync parameters
            except Exception as e  :
                self.endSignal.emit(3)
#                Unknown error in rsync
            if not errorOccured:
                buf=''
                while not self.stopped:
                    letter=self.process.stdout.read(1).decode('unicode_escape')
                    buf=buf+letter
                    if letter=='\n' or letter=='\r':
                        progressL=re.findall("([0-9]*)%", buf)
                        speedK=re.findall("([0-9.]*)kB/s", buf)
                        speedM=re.findall("([0-9.]*)MB/s", buf)
                        remain=re.findall("[0-9]*:[0-9]*:[0-9]*",buf)
                        sizeB=re.findall("[1-9](?:\d{0,2})(?:,\d{3})*",buf)
                        failed=re.findall("\(28\)",buf)
                        if len(progressL) != 0:
                            progress= int(progressL[0])
                            self.progressSignal.emit(progress)
                            if len(sizeB) != 0:
                                self.sizeSignal.emit(sizeB[0].replace(","," "))
                        else:
                            if (len(buf) !=0):
                                self.lvM.emit(buf.rstrip())
                        if len(speedK) != 0:
                            speed= float(speedK[0])
                            self.speedSignal.emit(int(speed))
                        if len(speedM) != 0:
                            speed= 1024*float(speedM[0])
                            self.speedSignal.emit(int(speed))
                        if len(remain) != 0:
                            self.remainSignal.emit(remain[0])
                        if len(failed) != 0:
                            # no space left
                            self.stopped = True
                            self.lvM.emit(self.tr("Ending with {}, no space left").format(iso['nameWithPath']))
                        buf=""
                    self.process.poll()
                    if self.process.returncode != None:
                        break
                if self.stopped:
                    break
                else:
                    self.checkSignal.emit(iso['row'])
        self.endSignal.emit(0)
        self.speedSignal.emit(0)
        self.progressSignal.emit(0)
        self.sizeSignal.emit('0')
        self.remainSignal.emit('0')
        self.stopped=False
        self.list=[]
        self.quit()

def rename(directory,oldRelease,newRelease, nbf, nbr):
    # nbf: number of files renamed
    # nbr: number of directories renamed
    # the function is recursive
    for entry in os.listdir(directory):
        name = entry
        entry=os.path.join(directory,entry)
        if os.path.isfile(entry):
             if name.startswith(oldRelease):
                 # gpg keys are no more valid
                 if name.endswith('.gpg'):
                     os.remove(entry)
                 else:
                    newPath = os.path.join( os.path.dirname(entry) , name.replace(oldRelease, newRelease,1))
                    os.rename(entry, newPath)
                    nbf = nbf + 1
        if os.path.isdir(entry):
             nbf, nbr = rename(entry,oldRelease,newRelease, nbf, nbr)
             if name.startswith(oldRelease) :
                 newPath = os.path.join( os.path.dirname(entry) , name.replace(oldRelease, newRelease,1))
                 os.rename(entry, newPath)
                 nbr = nbr + 1
    return nbf, nbr

class findIsos(QThread):
    endSignal=pyqtSignal(int)
    lvM=pyqtSignal(str)

    def __init__(self, parent=None):
        super(findIsos, self).__init__(parent)
        self.list=[]
        self.localList=[]

    def setup(self,path, password, destination):
        self.path=path
        self.password=password
        self.destination=destination
        self.lvM.emit(path+password+destination)

    def getList(self):
        return self.list

    def getLocal(self):
        return self.localList

    def run(self):
        #   Lists ISO files in local directory
        root=QDir(self.destination)
        root.setFilter(QDir.Filter.AllDirs|QDir.Filter.NoDot|QDir.Filter.NoDotDot)
        dirs=root.entryList()
        for dir in dirs:
            sub=QDir(self.destination+'/'+dir)
            sub.setNameFilters(["*.iso"])
            sub.setFilter(QDir.Filter.Files)
            local=sub.entryList()
            if len(local)!=0:
                for iso in local:
                    isoSize=QFileInfo(sub.absolutePath()+'/' +iso).size()
                    self.localList.append([dir,iso,isoSize])
        #   List the remote directory
        commande = ['rsync', '-avHP', '--list-only',str(self.path)]
        try:
            envir = os.environ.copy()
            envir['LC_ALL']="C"
            if self.password != "":
                envir['RSYNC_PASSWORD']=str(self.password)
            process = Popen(commande, shell=False, stdout=PIPE, stderr=PIPE, env=envir)
        except OSError as e:
            self.lvM.emit(self.tr("Command rsync not found: ")+str(e))
            self.endSignal.emit(1)
            return
        except ValueError as e:
            self.lvM.emit(self.tr("Error in rsync parameters: ")+str(e))
            self.endSignal.emit(2)
            return
        except Exception as e:
            # Unknown error in rsync
            self.lvM.emit(self.tr("Error in rsync: ")+str(e))
            self.endSignal.emit(3)
            return
        process.poll()
        while True :
            item=process.stdout.readline().rstrip().decode('unicode_escape')
            self.lvM.emit(item)
            if str(item.lower()).endswith('.iso') :
                words = item.split()
                size = words[1]
                date = words[2]+' '+ words[3]
                self.list.append([size,date,words[-1]])
            process.poll()
            if process.returncode != None:
                break
        self.endSignal.emit(0)

def findRelease(releasePath, password):        #   List the remote list of releases
        releaseList =[]
        code=0
        commande = ['rsync', '--list-only',str(releasePath)]
        try:
            if password != "":
                envir = os.environ.copy()
                envir['RSYNC_PASSWORD']=str(password)
                process = Popen(commande, shell=False, stdout=PIPE, stderr=PIPE, env=envir)
            else:
                process = Popen(commande, shell=False, stdout=PIPE, stderr=PIPE)
        except OSError as e:
            code=1
            return code,self.tr("Command rsync not found: ")+str(e)
        except ValueError as e:
            code=2
            return code,self.tr("Error in rsync parameters: ")+str(e)
        except Exception as e:
            # Unknown error in rsync
            code=3
            return code,self.tr("Error in rsync: ")+str(e)
        process.poll()
        while True :
            item=process.stdout.readline().rstrip().decode('unicode_escape')
            words=item.split()
            if words !=[] and words[-1] != "." and words[0].startswith('d'):
                releaseList.append(words[-1])
            process.poll()
            if process.returncode != None:
                break
        return code, releaseList