/
fssvn.py
446 lines (362 loc) · 14.4 KB
/
fssvn.py
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
# -*- coding: utf-8 -*-
######## TODO
###### DANGER INSTALL : NECCESSTE SVN FRANCAIS
###### certain de grep fonctionne avec du svn Francais !!!
###### faire patch pour que cela fonctionne dans les autres langues...
###### LOGS
###### sortie des commande svn du nohup vers les logs
###### change auteur de la modification ??!! si possible avec openid !
###### remarque, il semble possible gerer l'authentification par pam
###### et de regler un truc specifique pour ca !!
###### Le semaphore devrai englober l'action svn en plus du commit et du get revision.
###### la on peux avoir une action qui se deplace dans un autre commit par erreur...
import os
import popen2
import tempfile
import threading
import datetime
import Image
from tekio.utils import system_ret, system_no_ret
from tekio.utils import format_path, path_to_liste
from tekio.utils import name_from_path, path_pere
from tekio.utils import utos
from tekio.utils import size_human_file
class FS_SVN:
def __init__(self,reposvn,dataread,log):
self.reposvn=reposvn
self.dataread=format_path(dataread)
self.semaphore_svn=threading.Semaphore()
self.log=log
def prepare_fstmp(self,repref=None):
fsreptmp = tempfile.mkdtemp()
if repref!=None:
system_no_ret("""cp -r "%s%s/.svn" "%s" """ % (self.dataread,repref,fsreptmp))
return fsreptmp
def creation(self,initdata,create_server=True,delete_trash=True):
if create_server:
system_no_ret("""rm -Rf "%s" """ % self.reposvn )
system_no_ret("""mkdir -p "%s" """ % self.reposvn )
system_no_ret("""svnadmin create "%s" """ % self.reposvn)
if initdata:
msg='import initial'
if delete_trash:
system_no_ret("""rm -Rf "%s/trash" """ % initdata )
system_no_ret("""mkdir -p "%s/trash" """ % initdata )
system_no_ret("""cd "%s" && recursif "rm -Rf .svn" """ % initdata)
system_no_ret("""svn import "%s" "file://%s" -m "%s" """ % (initdata,self.reposvn,msg))
system_no_ret("""rm -Rf "%s" """ % self.dataread)
system_no_ret("""mkdir -p "%s" """ % self.dataread)
system_no_ret("""svn checkout "file://%s" "%s" """ % (self.reposvn,self.dataread))
def get_rev(self,path):
path=format_path(path)
ret=system_ret("""svn info "%s%s" |grep vision| grep difica""" % (self.dataread,path))
#grep francophone !!!
try:
r=int(ret.split(':',1)[1].split('/n')[0].strip())
except:
self.log.debug(path)
self.log.debug(ret)
r=0
return r
def date(self,path,rev=None):
path=format_path(path)
if rev:
txtrev="-r %s " % rev
else:
txtrev=""
filename='%s%s' % (self.dataread,path)
ret=system_ret("""svn info %s "%s" |grep Date| grep difica""" % (txtrev,filename))
#grep francophone !!!
datestr=ret.split(':',1)[1].split('(')[0].strip()
return datestr
def get_revs(self,path):
path=format_path(path)
filename='%s%s' % (self.dataread,path)
ret=system_ret("""svn log "%s" """ % filename )
mods=ret.split('------------------------------------------------------------------------')
ids=[]
for m in mods:
try:
v=int(m.split('|',1)[0].strip()[1:])
ids.append(v)
except:
pass
return ids
def diff(self,path,r1,r2):
path=format_path(path)
if r1>r2:
r3=r1
r1=r2
r2=r3
try:
filename='%s/%s' % (self.dataread,path)
ret=system_ret("""svn diff -r %s:%s "%s" """ % (r1,r2,filename) )
diff=ret.split('\n',4)[4]
return diff
except:
return None
def commit(self,actions,message,tmpcommit,tmpbase):
self.semaphore_svn.acquire()
new_rev=None
#test
#try:
if 1:
for action in actions:
self.log.debug(action)
system_no_ret(action)
if not message:
message="commit %s " % str((actions,message,tmpcommit)).replace('"',"%")
if type(tmpcommit)==type(u''):
tmpcommit=utos(tmpcommit)
if type(tmpcommit)==type(''):
cmd=""" svn commit -m "%s" "%s" """ % (message,tmpcommit)
self.log.debug(cmd)
system_no_ret(cmd)
elif type(tmpcommit)==type([]):
for tc in tmpcommit:
cmd="""svn commit -m "%s" "%s" """ % (message,tc)
self.log.debug(cmd)
system_no_ret(cmd)
cmd="""svn update "%s" """ % self.dataread
self.log.debug(cmd)
system_no_ret(cmd)
new_rev=self.get_rev("")
#except:
# message=""
# try:
# message="ERROR commit %s " % str((actions,message,tmpcommit)).replace('"',"%")
# except:
# message='ERROR COMMIT'
# new_rev=None
# self.semaphore_svn.release()
# raise(message)
self.semaphore_svn.release()
#system_no_ret('rm -Rf %s' % tmpbase, check=False)
return new_rev
def get(self,path,rev=None):
path=format_path(path)
if not rev:
try:
f=open("%s%s" % (self.dataread,path))
ret=f.read()
f.close()
return ret
except:
return None
else:
fsreptmp = tempfile.mkdtemp()
system_no_ret("""svn export -r %s "%s%s" "%s/file" """ % (rev,self.dataread,path,fsreptmp))
f=open("%s/file" % (fsreptmp))
ret=f.read()
f.close()
return ret
def get_image(self,path):
f=Image.open("%s%s" % (self.dataread,path))
return f
def set_image(self,obj_img,path):
#l'image n'est pas dans le svn !!
obj_img.save("%s%s" % (self.dataread,path))
def modif(self,path,data,msg=None,binary=False):
self.log.debug("MODIF")
path=format_path(path)
if path=='':
#la racine n'est pas un fichier.
return
pere=path_pere(path)
name=name_from_path(path)
fsreptmp=self.prepare_fstmp(pere)
if binary:
acces='wb'
else:
acces='w'
f=open('%s/%s' % (fsreptmp,name),acces)
f.write(data)
f.close()
actions=[]
message=msg
if not message:
message = "Modifie fichier %s " % path
tmpcommit=fsreptmp+'/'+name
new_rev=self.commit(actions,message,tmpcommit,fsreptmp)
return new_rev
def add_folder(self,path,msg=None):
self.log.debug("ADDFOLDER")
path=format_path(path)
if path=='':
#on ne peux creer la racine (elle existe deja)
return
pere=path_pere(path)
name=name_from_path(path)
fsreptmp=self.prepare_fstmp(pere)
system_no_ret("""mkdir "%s/%s" """ % (fsreptmp,name))
actions=[("""svn add "%s/%s" """ % (fsreptmp,name)),]
message=msg
if not message:
message = "Ajouter Repertoire %s " % path
tmpcommit=fsreptmp+'/'+name
new_rev=self.commit(actions,message,tmpcommit,fsreptmp)
return new_rev
def add_link(self,pathlinkinit,pathlinkfinal,msg=None):
self.log.debug("ADDLINK")
pathlinkinit=format_path(pathlinkinit)
pathlinkfinal=format_path(pathlinkfinal)
perefinal=path_pere(pathlinkfinal)
namefinal=name_from_path(pathlinkfinal)
fsreptmp = self.prepare_fstmp(perefinal)
#TODO BEST : lien en relatif et pas en absolu
# pour permettre le deplacement dans un autre repertoire du contenu.
system_no_ret("""ln -s "%s/%s" "%s/%s" """ % (self.dataread,pathlinkinit,fsreptmp,namefinal))
actions=[("""svn add "%s/%s" """ % (fsreptmp,namefinal)),]
message=msg
if not message:
message="Ajout lien symbolique %s => %s " % (pathlinkinit,pathlinkfinal)
tmpcommit=fsreptmp+'/'+namefinal
new_rev=self.commit(actions,message,tmpcommit,fsreptmp)
return new_rev
def add(self,path,data,msg=None,binary=False):
path=format_path(path)
if path=='':
return
pere=path_pere(path)
name=name_from_path(path)
fsreptmp=self.prepare_fstmp(pere)
if binary:
acces='wb'
else:
acces='w'
f=open('%s/%s' % (fsreptmp,name),acces)
f.write(data)
f.close()
actions=[("""svn add "%s/%s" """ % (fsreptmp,name)),]
message=msg
if not message:
message="Ajouter fichier %s " % path
tmpcommit=fsreptmp+'/'+name
new_rev=self.commit(actions,message,tmpcommit,fsreptmp)
return new_rev
def delete(self,path,msg=None):
self.log.debug("DELETE")
### Ne pas appeler directemer dans les modules,
### prefere appeler trash pour garder le contenu.
path=format_path(path)
if path=='':
#on ne peux detruire la racine
return
pere=path_pere(path)
name=name_from_path(path)
fsreptmp=self.prepare_fstmp(pere)
system_no_ret("""cp -r "%s%s" "%s" """ % (self.dataread,path,fsreptmp))
actions=[("""svn del "%s/%s" """ % (fsreptmp,name)),]
message=msg
if not message:
message="Detruit fichier %s " % path
tmpcommit=fsreptmp+'/'+name
new_rev=self.commit(actions,message,tmpcommit,fsreptmp)
return new_rev
def move(self,pathinit,pathfinal,msg=None):
self.log.debug("MOVE")
pathinit=format_path(pathinit)
pathfinal=format_path(pathfinal)
if (pathinit==pathfinal):
# destination = arrive !! pas de deplacement
return
if not self.exist(pathinit):
# l'objet a deplacer n existe pas dans le fs.
return
if (pathinit==''):
# on ne peux deplacer la racine
return
li=path_to_liste(pathinit) #liste path init
lf=path_to_liste(pathfinal) #liste path final
lc=[] # liste path commun
acontinuer=True
while acontinuer:
if len(li)>0 and len(lf)>0 and li[0]==lf[0]:
lc.append(li.pop(0))
lf.pop(0)
else:
acontinuer=False
ri=format_path('/'.join(li)) # path repertoire initial sans le commun.
rf=format_path('/'.join(lf)) # path repertoire final sans le commun.
rc=format_path('/'.join(lc)) # path repertoire commun
if ri=='':
#ne peux pas deplacer un repertoire dans lui meme....
return
fscommun='%s%s' % (self.dataread,rc)
# cree la structure temporaire svn dans
fsreptmp=self.prepare_fstmp(rc)
# cree la structure temportaire des repertoire initaux
for i in range(len(li)-1):
rep=format_path('/'.join(li[:i+1]))
if rep!='':
self.log.debug("prepare inital %s " % rep)
system_no_ret("""mkdir "%s%s" """ % (fsreptmp,rep))
system_no_ret("""cp -r "%s%s/.svn" "%s%s" """ % (fscommun,rep,fsreptmp,rep))
# copie le repertoire ou le fichier a deplacer
if self.isdir(pathinit):
self.log.debug("copie dir %s " % ri)
system_no_ret("""cp -r "%s%s" "%s%s" """ % (fscommun,ri,fsreptmp,ri))
else:
self.log.debug("copie file %s " % ri)
system_no_ret("""cp "%s%s" "%s%s" """ % (fscommun,ri,fsreptmp,ri))
# cree la structure temportaire des repertoire finaux
for i in range(len(lf)-1):
rep=format_path('/'.join(lf[:i+1]))
if rep!='':
self.log.debug("prepare final %s " % rep)
if not self.exist(rep):
self.add_folder(rep)
system_no_ret("""mkdir "%s%s" """ % (fsreptmp,rep))
system_no_ret("""cp -r "%s%s/.svn" "%s%s" """ % (fscommun,rep,fsreptmp,rep))
else:
system_no_ret("""cp -r "%s/.svn" "%s" """ % (fscommun,fsreptmp))
# si la destination est un repertoire, preparation structure.
if self.exist(pathfinal):
if self.isdir(pathfinal):
self.log.debug("prepare final folder %s " % rf)
system_no_ret("""mkdir "%s%s" """ % (fsreptmp,rf))
system_no_ret("""cp -r "%s%s/.svn" "%s%s" """ % (fscommun,rf,fsreptmp,rf))
#definition svn move et commit
actions=[("""svn move "%s%s" "%s%s" """ % (fsreptmp,ri,fsreptmp,rf)),]
self.log.debug(str(actions))
message=msg
if not message:
message="move %s => %s" % (pathinit,pathfinal)
tmpcommit=[fsreptmp+ri,fsreptmp+rf]
self.log.debug('commits')
self.log.debug(str(tmpcommit))
new_rev=self.commit(actions,message,tmpcommit,fsreptmp)
# returne la revision actuelle.
return new_rev
def trash(self,path,msg=None):
self.log.debug("TRASH")
path=format_path(path)
if path=='':
#on ne peux pas mettre la racine dans trash
return
pere=path_pere(path)
name=name_from_path(path)
dt=datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
pathtrash="/trash/%s" % dt
self.log.debug('create %s' % pathtrash)
new_rev=self.add_folder(pathtrash)
pathtrashobj="%s/%s" % (pathtrash,name)
message="Trash : %s => %s " % (path,pathtrashobj)
self.log.debug(message)
new_rev=self.move(path,pathtrashobj)
return (new_rev,dt)
def listdir(self,path):
path=format_path(path)
filename='%s%s' % (self.dataread,path)
return os.listdir(filename)
def exist(self,path):
path=format_path(path)
filename='%s%s' % (self.dataread,path)
return os.path.lexists(filename) and os.access(filename,os.F_OK) and os.access(filename,os.R_OK)
def isdir(self,path):
path=format_path(path)
filename='%s%s' % (self.dataread,path)
return os.path.isdir(filename)
def size_human_file(self,path):
filename='%s%s' % (self.dataread,path)
return size_human_file(filename)