-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirac-install.py
executable file
·1586 lines (1411 loc) · 60.4 KB
/
dirac-install.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
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""
The main DIRAC installer script
"""
import sys
import os
import getopt
import urllib2
import imp
import signal
import time
import stat
import shutil
import ssl
import hashlib
__RCSID__ = "$Id$"
executablePerms = stat.S_IWUSR | stat.S_IRUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH
def S_OK( value = "" ):
return { 'OK' : True, 'Value' : value }
def S_ERROR( msg = "" ):
return { 'OK' : False, 'Message' : msg }
############
# Start of CFG
############
class Params( object ):
def __init__( self ):
self.extensions = []
self.project = 'DIRAC'
self.installation = 'DIRAC'
self.release = ""
self.externalsType = 'client'
self.pythonVersion = '27'
self.platform = ""
self.basePath = os.getcwd()
self.targetPath = os.getcwd()
self.buildExternals = False
self.noAutoBuild = False
self.debug = False
self.externalsOnly = False
self.lcgVer = ''
self.useVersionsDir = False
self.installSource = ""
self.globalDefaults = False
self.timeout = 300
cliParams = Params()
###
# Release config manager
###
class ReleaseConfig( object ):
class CFG:
def __init__( self, cfgData = "" ):
self.__data = {}
self.__children = {}
if cfgData:
self.parse( cfgData )
def parse( self, cfgData ):
try:
self.__parse( cfgData )
except:
import traceback
traceback.print_exc()
raise
return self
def getChild( self, path ):
child = self
if isinstance( path, (list, tuple) ):
pathList = path
else:
pathList = [ sec.strip() for sec in path.split( "/" ) if sec.strip() ]
for childName in pathList:
if childName not in child.__children:
return False
child = child.__children[ childName ]
return child
def __parse( self, cfgData, cIndex = 0 ):
childName = ""
numLine = 0
while cIndex < len( cfgData ):
eol = cfgData.find( "\n", cIndex )
if eol < cIndex:
#End?
return cIndex
numLine += 1
if eol == cIndex:
cIndex += 1
continue
line = cfgData[ cIndex : eol ].strip()
#Jump EOL
cIndex = eol + 1
if not line or line[0] == "#":
continue
if line.find( "+=" ) > -1:
fields = line.split( "+=" )
opName = fields[0].strip()
if opName in self.__data:
self.__data[ opName ] += ', %s' % '+='.join( fields[1:] ).strip()
else:
self.__data[ opName ] = '+='.join( fields[1:] ).strip()
continue
if line.find( "=" ) > -1:
fields = line.split( "=" )
self.__data[ fields[0].strip() ] = "=".join( fields[1:] ).strip()
continue
opFound = line.find( "{" )
if opFound > -1:
childName += line[ :opFound ].strip()
if not childName:
raise Exception( "No section name defined for opening in line %s" % numLine )
childName = childName.strip()
self.__children[ childName ] = ReleaseConfig.CFG()
eoc = self.__children[ childName ].__parse( cfgData, cIndex )
cIndex = eoc
childName = ""
continue
if line == "}":
return cIndex
#Must be name for section
childName += line.strip()
return cIndex
def createSection( self, name, cfg = False ):
if isinstance( name, ( list, tuple ) ):
pathList = name
else:
pathList = [ sec.strip() for sec in name.split( "/" ) if sec.strip() ]
parent = self
for lev in pathList[:-1]:
if lev not in parent.__children:
parent.__children[ lev ] = ReleaseConfig.CFG()
parent = parent.__children[ lev ]
secName = pathList[-1]
if secName not in parent.__children:
if not cfg:
cfg = ReleaseConfig.CFG()
parent.__children[ secName ] = cfg
return parent.__children[ secName ]
def isSection( self, obList ):
return self.__exists( [ ob.strip() for ob in obList.split( "/" ) if ob.strip() ] ) == 2
def sections( self ):
return [ k for k in self.__children ]
def isOption( self, obList ):
return self.__exists( [ ob.strip() for ob in obList.split( "/" ) if ob.strip() ] ) == 1
def options( self ):
return [ k for k in self.__data ]
def __exists( self, obList ):
if len( obList ) == 1:
if obList[0] in self.__children:
return 2
elif obList[0] in self.__data:
return 1
else:
return 0
if obList[0] in self.__children:
return self.__children[ obList[0] ].__exists( obList[1:] )
return 0
def get( self, opName, defaultValue = None ):
try:
value = self.__get( [ op.strip() for op in opName.split( "/" ) if op.strip() ] )
except KeyError:
if defaultValue != None:
return defaultValue
raise
if defaultValue == None:
return value
defType = type( defaultValue )
if isinstance(defType, bool):
return value.lower() in ( "1", "true", "yes" )
try:
return defType( value )
except ValueError:
return defaultValue
def __get( self, obList ):
if len( obList ) == 1:
if obList[0] in self.__data:
return self.__data[ obList[0] ]
raise KeyError( "Missing option %s" % obList[0] )
if obList[0] in self.__children:
return self.__children[ obList[0] ].__get( obList[1:] )
raise KeyError( "Missing section %s" % obList[0] )
def toString( self, tabs = 0 ):
lines = [ "%s%s = %s" % ( " " * tabs, opName, self.__data[ opName ] ) for opName in self.__data ]
for secName in self.__children:
lines.append( "%s%s" % ( " " * tabs, secName ) )
lines.append( "%s{" % ( " " * tabs ) )
lines.append( self.__children[ secName ].toString( tabs + 1 ) )
lines.append( "%s}" % ( " " * tabs ) )
return "\n".join( lines )
def getOptions( self, path = "" ):
parentPath = [ sec.strip() for sec in path.split( "/" ) if sec.strip() ][:-1]
if parentPath:
parent = self.getChild( parentPath )
else:
parent = self
if not parent:
return []
return tuple( parent.__data )
def delPath( self, path ):
path = [ sec.strip() for sec in path.split( "/" ) if sec.strip() ]
if not path:
return
keyName = path[ -1 ]
parentPath = path[:-1]
if parentPath:
parent = self.getChild( parentPath )
else:
parent = self
if parent:
parent.__data.pop( keyName )
def update( self, path, cfg ):
parent = self.getChild( path )
if not parent:
self.createSection( path, cfg )
return
parent.__apply( cfg )
def __apply( self, cfg ):
for k in cfg.sections():
if k in self.__children:
self.__children[ k ].__apply( cfg.getChild( k ) )
else:
self.__children[ k ] = cfg.getChild( k )
for k in cfg.options():
self.__data[ k ] = cfg.get( k )
############################################################################
# END OF CFG CLASS
############################################################################
def __init__( self, instName = 'DIRAC', projectName = 'DIRAC', globalDefaultsURL = False ):
if globalDefaultsURL:
self.__globalDefaultsURL = globalDefaultsURL
else:
self.__globalDefaultsURL = "http://lhcbproject.web.cern.ch/lhcbproject/dist/DIRAC3/globalDefaults.cfg"
self.__globalDefaults = ReleaseConfig.CFG()
self.__loadedCfgs = []
self.__prjDepends = {}
self.__prjRelCFG = {}
self.__projectsLoadedBy = {}
self.__cfgCache = {}
self.__debugCB = False
self.__instName = instName
self.__projectName = projectName
def getInstallation( self ):
return self.__instName
def getProject( self ):
return self.__projectName
def setInstallation( self, instName ):
self.__instName = instName
def setProject( self, projectName ):
self.__projectName = projectName
def setDebugCB( self, debFunc ):
self.__debugCB = debFunc
def __dbgMsg( self, msg ):
if self.__debugCB:
self.__debugCB( msg )
def __loadCFGFromURL( self, urlcfg, checkHash = False ):
# This can be a local file
if os.path.exists( urlcfg ):
with open( urlcfg, 'r' ) as relFile:
cfgData = relFile.read()
else:
if urlcfg in self.__cfgCache:
return S_OK( self.__cfgCache[ urlcfg ] )
try:
cfgData = urlretrieveTimeout( urlcfg, timeout = cliParams.timeout )
if not cfgData:
return S_ERROR( "Could not get data from %s" % urlcfg )
except:
return S_ERROR( "Could not open %s" % urlcfg )
try:
#cfgData = cfgFile.read()
cfg = ReleaseConfig.CFG( cfgData )
except Exception, excp:
return S_ERROR( "Could not parse %s: %s" % ( urlcfg, excp ) )
#cfgFile.close()
if not checkHash:
self.__cfgCache[ urlcfg ] = cfg
return S_OK( cfg )
try:
md5path = urlcfg[:-4] + ".md5"
if os.path.exists( md5path ):
md5File = open( md5path, 'r' )
md5Data = md5File.read()
md5File.close()
else:
md5Data = urlretrieveTimeout( md5path, timeout = 60 )
md5Hex = md5Data.strip()
#md5File.close()
if md5Hex != hashlib.md5( cfgData ).hexdigest():
return S_ERROR( "Hash check failed on %s" % urlcfg )
except Exception, excp:
return S_ERROR( "Hash check failed on %s: %s" % ( urlcfg, excp ) )
self.__cfgCache[ urlcfg ] = cfg
return S_OK( cfg )
def loadInstallationDefaults( self ):
result = self.__loadGlobalDefaults()
if not result[ 'OK' ]:
return result
return self.__loadObjectDefaults( "Installations", self.__instName )
def loadProjectDefaults( self ):
result = self.__loadGlobalDefaults()
if not result[ 'OK' ]:
return result
return self.__loadObjectDefaults( "Projects", self.__projectName )
def __loadGlobalDefaults( self ):
self.__dbgMsg( "Loading global defaults from: %s" % self.__globalDefaultsURL )
result = self.__loadCFGFromURL( self.__globalDefaultsURL )
if not result[ 'OK' ]:
return result
self.__globalDefaults = result[ 'Value' ]
for k in ( "Installations", "Projects" ):
if not self.__globalDefaults.isSection( k ):
self.__globalDefaults.createSection( k )
self.__dbgMsg( "Loaded global defaults" )
return S_OK()
def __loadObjectDefaults( self, rootPath, objectName ):
basePath = "%s/%s" % ( rootPath, objectName )
if basePath in self.__loadedCfgs:
return S_OK()
#Check if it's a direct alias
try:
aliasTo = self.__globalDefaults.get( basePath )
except KeyError:
aliasTo = False
if aliasTo:
self.__dbgMsg( "%s is an alias to %s" % ( objectName, aliasTo ) )
result = self.__loadObjectDefaults( rootPath, aliasTo )
if not result[ 'OK' ]:
return result
cfg = result[ 'Value' ]
self.__globalDefaults.update( basePath, cfg )
return S_OK()
#Load the defaults
if self.__globalDefaults.get( "%s/SkipDefaults" % basePath, False ):
defaultsLocation = ""
else:
defaultsLocation = self.__globalDefaults.get( "%s/DefaultsLocation" % basePath, "" )
if not defaultsLocation:
self.__dbgMsg( "No defaults file defined for %s %s" % ( rootPath.lower()[:-1], objectName ) )
else:
self.__dbgMsg( "Defaults for %s are in %s" % ( basePath, defaultsLocation ) )
result = self.__loadCFGFromURL( defaultsLocation )
if not result[ 'OK' ]:
return result
cfg = result[ 'Value' ]
self.__globalDefaults.update( basePath, cfg )
#Check if the defaults have a sub alias
try:
aliasTo = self.__globalDefaults.get( "%s/Alias" % basePath )
except KeyError:
aliasTo = False
if aliasTo:
self.__dbgMsg( "%s is an alias to %s" % ( objectName, aliasTo ) )
result = self.__loadObjectDefaults( rootPath, aliasTo )
if not result[ 'OK' ]:
return result
cfg = result[ 'Value' ]
self.__globalDefaults.update( basePath, cfg )
self.__loadedCfgs.append( basePath )
return S_OK( self.__globalDefaults.getChild( basePath ) )
def loadInstallationLocalDefaults( self, fileName ):
try:
fd = open( fileName, "r" )
#TODO: Merge with installation CFG
cfg = ReleaseConfig.CFG().parse( fd.read() )
fd.close()
except Exception, excp :
return S_ERROR( "Could not load %s: %s" % ( fileName, excp ) )
self.__globalDefaults.update( "Installations/%s" % self.getInstallation(), cfg )
return S_OK()
def getInstallationCFG( self, instName = False ):
if not instName:
instName = self.__instName
return self.__globalDefaults.getChild( "Installations/%s" % instName )
def getInstallationConfig( self, opName, instName = False ):
if not instName:
instName = self.__instName
return self.__globalDefaults.get( "Installations/%s/%s" % ( instName, opName ) )
def isProjectLoaded( self, project ):
return project in self.__prjRelCFG
def getTarsLocation( self, project ):
defLoc = self.__globalDefaults.get( "Projects/%s/BaseURL" % project, "" )
if defLoc:
return S_OK( defLoc )
return S_ERROR( "Don't know how to find the installation tarballs for project %s" % project )
def getUploadCommand( self, project = False ):
if not project:
project = self.__projectName
defLoc = self.__globalDefaults.get( "Projects/%s/UploadCommand" % project, "" )
if defLoc:
return S_OK( defLoc )
return S_ERROR( "No UploadCommand for %s" % project )
def __loadReleaseConfig( self, project, release, releaseMode, sourceURL = False, relLocation = False ):
if project not in self.__prjRelCFG:
self.__prjRelCFG[ project ] = {}
if release in self.__prjRelCFG[ project ]:
self.__dbgMsg( "Release config for %s:%s has already been loaded" % ( project, release ) )
return S_OK()
if relLocation:
relcfgLoc = relLocation
else:
if releaseMode:
try:
relcfgLoc = self.__globalDefaults.get( "Projects/%s/Releases" % project )
except KeyError:
return S_ERROR( "Missing Releases file for project %s" % project )
else:
if not sourceURL:
result = self.getTarsLocation( project )
if not result[ 'OK' ]:
return result
siu = result[ 'Value' ]
else:
siu = sourceURL
relcfgLoc = "%s/release-%s-%s.cfg" % ( siu, project, release )
self.__dbgMsg( "Releases file is %s" % relcfgLoc )
result = self.__loadCFGFromURL( relcfgLoc, checkHash = not releaseMode )
if not result[ 'OK' ]:
return result
self.__prjRelCFG[ project ][ release ] = result[ 'Value' ]
self.__dbgMsg( "Loaded releases file %s" % relcfgLoc )
return S_OK( self.__prjRelCFG[ project ][ release ] )
def getReleaseCFG( self, project, release ):
return self.__prjRelCFG[ project ][ release ]
def dumpReleasesToPath( self, path ):
for project in self.__prjRelCFG:
prjRels = self.__prjRelCFG[ project ]
for release in prjRels:
self.__dbgMsg( "Dumping releases file for %s:%s" % ( project, release ) )
fd = open( os.path.join( cliParams.targetPath, "releases-%s-%s.cfg" % ( project, release ) ), "w" )
fd.write( prjRels[ release ].toString() )
fd.close()
def __checkCircularDependencies( self, key, routePath = False ):
if not routePath:
routePath = []
if key not in self.__projectsLoadedBy:
return S_OK()
routePath.insert( 0, key )
for lKey in self.__projectsLoadedBy[ key ]:
if lKey in routePath:
routePath.insert( 0, lKey )
route = "->".join( [ "%s:%s" % sKey for sKey in routePath ] )
return S_ERROR( "Circular dependency found for %s: %s" % ( "%s:%s" % lKey, route ) )
result = self.__checkCircularDependencies( lKey, routePath )
if not result[ 'OK' ]:
return result
routePath.pop( 0 )
return S_OK()
def loadProjectRelease( self, releases, project = False, sourceURL = False, releaseMode = False, relLocation = False ):
if not project:
project = self.__projectName
if not isinstance( releases, (list, tuple) ):
releases = [ releases ]
#Load defaults
result = self.__loadObjectDefaults( "Projects", project )
if not result[ 'OK' ]:
self.__dbgMsg( "Could not load defaults for project %s" % project )
return result
if project not in self.__prjDepends:
self.__prjDepends[ project ] = {}
for release in releases:
self.__dbgMsg( "Processing dependencies for %s:%s" % ( project, release ) )
result = self.__loadReleaseConfig( project, release, releaseMode, sourceURL, relLocation )
if not result[ 'OK' ]:
return result
relCFG = result[ 'Value' ]
#Calculate dependencies and avoid circular deps
self.__prjDepends[ project ][ release ] = [ ( project, release ) ]
relDeps = self.__prjDepends[ project ][ release ]
if not relCFG.getChild( "Releases/%s" % ( release ) ): # pylint: disable=no-member
return S_ERROR( "Release %s is not defined for project %s in the release file" % ( release, project ) )
initialDeps = self.getReleaseDependencies( project, release )
if initialDeps:
self.__dbgMsg( "%s %s depends on %s" % ( project, release, ", ".join( [ "%s:%s" % ( k, initialDeps[k] ) for k in initialDeps ] ) ) )
relDeps.extend( [ ( p, initialDeps[p] ) for p in initialDeps ] )
for depProject in initialDeps:
depVersion = initialDeps[ depProject ]
#Check if already processed
dKey = ( depProject, depVersion )
if dKey not in self.__projectsLoadedBy:
self.__projectsLoadedBy[ dKey ] = []
self.__projectsLoadedBy[ dKey ].append( ( project, release ) )
result = self.__checkCircularDependencies( dKey )
if not result[ 'OK' ]:
return result
#if it has already been processed just return OK
if len( self.__projectsLoadedBy[ dKey ] ) > 1:
return S_OK()
#Load dependencies and calculate incompatibilities
result = self.loadProjectRelease( depVersion, project = depProject )
if not result[ 'OK' ]:
return result
subDep = self.__prjDepends[ depProject ][ depVersion ]
#Merge dependencies
for sKey in subDep:
if sKey not in relDeps:
relDeps.append( sKey )
continue
prj, vrs = sKey
for pKey in relDeps:
if pKey[0] == prj and pKey[1] != vrs:
errMsg = "%s is required with two different versions ( %s and %s ) starting with %s:%s" % ( prj,
pKey[1], vrs,
project, release )
return S_ERROR( errMsg )
#Same version already required
if project in relDeps and relDeps[ project ] != release:
errMsg = "%s:%s requires itself with a different version through dependencies ( %s )" % ( project, release,
relDeps[ project ] )
return S_ERROR( errMsg )
return S_OK()
def getReleaseOption( self, project, release, option ):
try:
return self.__prjRelCFG[ project ][ release ].get( option )
except KeyError:
self.__dbgMsg( "Missing option %s for %s:%s" % ( option, project, release ) )
return False
def getReleaseDependencies( self, project, release ):
try:
data = self.__prjRelCFG[ project ][ release ].get( "Releases/%s/Depends" % release )
except KeyError:
return {}
data = [ field for field in data.split( "," ) if field.strip() ]
deps = {}
for field in data:
field = field.strip()
if not field:
continue
pv = field.split( ":" )
if len( pv ) == 1:
deps[ pv[0].strip() ] = release
else:
deps[ pv[0].strip() ] = ":".join( pv[1:] ).strip()
return deps
def getModulesForRelease( self, release, project = False ):
if not project:
project = self.__projectName
if not project in self.__prjRelCFG:
return S_ERROR( "Project %s has not been loaded. I'm a MEGA BUG! Please report me!" % project )
if not release in self.__prjRelCFG[ project ]:
return S_ERROR( "Version %s has not been loaded for project %s" % ( release, project ) )
config = self.__prjRelCFG[ project ][ release ]
if not config.isSection( "Releases/%s" % release ):
return S_ERROR( "Release %s is not defined for project %s" % ( release, project ) )
#Defined Modules explicitly in the release
modules = self.getReleaseOption( project, release, "Releases/%s/Modules" % release )
if modules:
dMods = {}
for entry in [ entry.split( ":" ) for entry in modules.split( "," ) if entry.strip() ]: # pylint: disable=no-member
if len( entry ) == 1:
dMods[ entry[0].strip() ] = release
else:
dMods[ entry[0].strip() ] = entry[1].strip()
modules = dMods
else:
#Default modules with the same version as the release version
modules = self.getReleaseOption( project, release, "DefaultModules" )
if modules:
modules = dict( ( modName.strip() , release ) for modName in modules.split( "," ) if modName.strip() ) # pylint: disable=no-member
else:
#Mod = project and same version
modules = { project : release }
#Check project is in the modNames if not DIRAC
if project != "DIRAC":
for modName in modules:
if modName.find( project ) != 0:
return S_ERROR( "Module %s does not start with the name %s" % ( modName, project ) )
return S_OK( modules )
def getModSource( self, release, modName ):
if not self.__projectName in self.__prjRelCFG:
return S_ERROR( "Project %s has not been loaded. I'm a MEGA BUG! Please report me!" % self.__projectName )
modLocation = self.getReleaseOption( self.__projectName, release, "Sources/%s" % modName )
if not modLocation:
return S_ERROR( "Source origin for module %s is not defined" % modName )
modTpl = [ field.strip() for field in modLocation.split( "|" ) if field.strip() ] # pylint: disable=no-member
if len( modTpl ) == 1:
return S_OK( ( False, modTpl[0] ) )
return S_OK( ( modTpl[0], modTpl[1] ) )
def getExtenalsVersion( self, release = False ):
if 'DIRAC' not in self.__prjRelCFG:
return False
if not release:
release = list( self.__prjRelCFG[ 'DIRAC' ] )
release = max( release )
try:
return self.__prjRelCFG[ 'DIRAC' ][ release ].get( 'Releases/%s/Externals' % release )
except KeyError:
return False
def getLCGVersion( self, lcgVersion = "" ):
if lcgVersion:
return lcgVersion
for objName in self.__projectsLoadedBy:
try:
return self.__prjRelCFG[ self.__projectName ][ cliParams.release ].get( "Releases/%s/LcgVer" % cliParams.release, lcgVersion )
except KeyError:
pass
return lcgVersion
def getModulesToInstall( self, release, extensions = False ):
if not extensions:
extensions = []
extraFound = []
modsToInstall = {}
modsOrder = []
if self.__projectName not in self.__prjDepends:
return S_ERROR( "Project %s has not been loaded" % self.__projectName )
if release not in self.__prjDepends[ self.__projectName ]:
return S_ERROR( "Version %s has not been loaded for project %s" % ( release, self.__projectName ) )
#Get a list of projects with their releases
projects = list( self.__prjDepends[ self.__projectName ][ release ] )
for project, relVersion in projects:
try:
requiredModules = self.__prjRelCFG[ project ][ relVersion ].get( "RequiredExtraModules" )
requiredModules = [ modName.strip() for modName in requiredModules.split( "/" ) if modName.strip() ]
except KeyError:
requiredModules = []
for modName in requiredModules:
if modName not in extensions:
extensions.append( modName )
result = self.getTarsLocation( project )
if not result[ 'OK' ]:
return result
tarsPath = result[ 'Value' ]
self.__dbgMsg( "Discovering modules to install for %s (%s)" % ( project, relVersion ) )
result = self.getModulesForRelease( relVersion, project )
if not result[ 'OK' ]:
return result
modVersions = result[ 'Value' ]
try:
defaultMods = self.__prjRelCFG[ project ][ relVersion ].get( "DefaultModules" )
modNames = [ mod.strip() for mod in defaultMods.split( "," ) if mod.strip() ]
except KeyError:
modNames = []
for extension in extensions:
# Check if the version of the extension module is specified in the command line
extraVersion = None
if ":" in extension:
extension, extraVersion = extension.split( ":" )
modVersions[extension] = extraVersion
if extension in modVersions:
modNames.append( extension )
extraFound.append( extension )
if 'DIRAC' not in extension:
dextension = "%sDIRAC" % extension
if dextension in modVersions:
modNames.append( dextension )
extraFound.append( extension )
modNameVer = [ "%s:%s" % ( modName, modVersions[ modName ] ) for modName in modNames ]
self.__dbgMsg( "Modules to be installed for %s are: %s" % ( project, ", ".join( modNameVer ) ) )
for modName in modNames:
modsToInstall[ modName ] = ( tarsPath, modVersions[ modName ] )
modsOrder.insert( 0, modName )
for modName in extensions:
if modName.split(":")[0] not in extraFound:
return S_ERROR( "No module %s defined. You sure it's defined for this release?" % modName )
return S_OK( ( modsOrder, modsToInstall ) )
#################################################################################
# End of ReleaseConfig
#################################################################################
#platformAlias = { 'Darwin_i386_10.6' : 'Darwin_i386_10.5' }
platformAlias = {}
####
# Start of helper functions
####
def logDEBUG( msg ):
if cliParams.debug:
for line in msg.split( "\n" ):
print "%s UTC dirac-install [DEBUG] %s" % ( time.strftime( '%Y-%m-%d %H:%M:%S', time.gmtime() ), line )
sys.stdout.flush()
def logERROR( msg ):
for line in msg.split( "\n" ):
print "%s UTC dirac-install [ERROR] %s" % ( time.strftime( '%Y-%m-%d %H:%M:%S', time.gmtime() ), line )
sys.stdout.flush()
def logWARN( msg ):
for line in msg.split( "\n" ):
print "%s UTC dirac-install [WARN] %s" % ( time.strftime( '%Y-%m-%d %H:%M:%S', time.gmtime() ), line )
sys.stdout.flush()
def logNOTICE( msg ):
for line in msg.split( "\n" ):
print "%s UTC dirac-install [NOTICE] %s" % ( time.strftime( '%Y-%m-%d %H:%M:%S', time.gmtime() ), line )
sys.stdout.flush()
def alarmTimeoutHandler( *args ):
raise Exception( 'Timeout' )
def urlretrieveTimeout( url, fileName = '', timeout = 0 ):
"""
Retrieve remote url to local file, with timeout wrapper
"""
# NOTE: Not thread-safe, since all threads will catch same alarm.
# This is OK for dirac-install, since there are no threads.
logDEBUG( 'Retrieving remote file "%s"' % url )
urlData = ''
if timeout:
signal.signal( signal.SIGALRM, alarmTimeoutHandler )
# set timeout alarm
signal.alarm( timeout + 5 )
try:
# if "http_proxy" in os.environ and os.environ['http_proxy']:
# proxyIP = os.environ['http_proxy']
# proxy = urllib2.ProxyHandler( {'http': proxyIP} )
# opener = urllib2.build_opener( proxy )
# #opener = urllib2.build_opener()
# urllib2.install_opener( opener )
# Try to use insecure context explicitly, needed for python >= 2.7.9
try:
context = ssl._create_unverified_context()
remoteFD = urllib2.urlopen( url, context = context ) # pylint: disable=unexpected-keyword-arg
# the keyword 'context' is present from 2.7.9+
except AttributeError:
remoteFD = urllib2.urlopen( url )
expectedBytes = 0
# Sometimes repositories do not return Content-Length parameter
try:
expectedBytes = long( remoteFD.info()[ 'Content-Length' ] )
except Exception as x:
logWARN( 'Content-Length parameter not returned, skipping expectedBytes check' )
if fileName:
localFD = open( fileName, "wb" )
receivedBytes = 0L
data = remoteFD.read( 16384 )
count = 1
progressBar = False
while data:
receivedBytes += len( data )
if fileName:
localFD.write( data )
else:
urlData += data
data = remoteFD.read( 16384 )
if count % 20 == 0 and sys.stdout.isatty():
print '\033[1D' + ".",
sys.stdout.flush()
progressBar = True
count += 1
if progressBar and sys.stdout.isatty():
# return cursor to the beginning of the line
print '\033[1K',
print '\033[1A'
if fileName:
localFD.close()
remoteFD.close()
if receivedBytes != expectedBytes and expectedBytes > 0:
logERROR( "File should be %s bytes but received %s" % ( expectedBytes, receivedBytes ) )
return False
except urllib2.HTTPError, x:
if x.code == 404:
logERROR( "%s does not exist" % url )
if timeout:
signal.alarm( 0 )
return False
except urllib2.URLError:
logERROR( 'Timeout after %s seconds on transfer request for "%s"' % ( str( timeout ), url ) )
except Exception, x:
if x == 'Timeout':
logERROR( 'Timeout after %s seconds on transfer request for "%s"' % ( str( timeout ), url ) )
if timeout:
signal.alarm( 0 )
raise x
if timeout:
signal.alarm( 0 )
if fileName:
return True
else:
return urlData
def downloadAndExtractTarball( tarsURL, pkgName, pkgVer, checkHash = True, cache = False ):
tarName = "%s-%s.tar.gz" % ( pkgName, pkgVer )
tarPath = os.path.join( cliParams.targetPath, tarName )
tarFileURL = "%s/%s" % ( tarsURL, tarName )
tarFileCVMFS = "/cvmfs/dirac.egi.eu/installSource/%s" % tarName
cacheDir = os.path.join( cliParams.basePath, ".installCache" )
tarCachePath = os.path.join( cacheDir, tarName )
if cache and os.path.isfile( tarCachePath ):
logNOTICE( "Using cached copy of %s" % tarName )
shutil.copy( tarCachePath, tarPath )
elif os.path.exists( tarFileCVMFS ):
logNOTICE( "Using CVMFS copy of %s" % tarName )
tarPath = tarFileCVMFS
checkHash = False
cache = False
else:
logNOTICE( "Retrieving %s" % tarFileURL )
try:
if not urlretrieveTimeout( tarFileURL, tarPath, cliParams.timeout ):
logERROR( "Cannot download %s" % tarName )
return False
except Exception, e:
logERROR( "Cannot download %s: %s" % ( tarName, str( e ) ) )
sys.exit( 1 )
if checkHash:
md5Name = "%s-%s.md5" % ( pkgName, pkgVer )
md5Path = os.path.join( cliParams.targetPath, md5Name )
md5FileURL = "%s/%s" % ( tarsURL, md5Name )
md5CachePath = os.path.join( cacheDir, md5Name )
if cache and os.path.isfile( md5CachePath ):
logNOTICE( "Using cached copy of %s" % md5Name )
shutil.copy( md5CachePath, md5Path )
else:
logNOTICE( "Retrieving %s" % md5FileURL )
try:
if not urlretrieveTimeout( md5FileURL, md5Path, 60 ):
logERROR( "Cannot download %s" % tarName )
return False
except Exception, e:
logERROR( "Cannot download %s: %s" % ( md5Name, str( e ) ) )
return False
#Read md5
fd = open( os.path.join( cliParams.targetPath, md5Name ), "r" )
md5Expected = fd.read().strip()
fd.close()
#Calculate md5
md5Calculated = hashlib.md5()
fd = open( os.path.join( cliParams.targetPath, tarName ), "r" )
buf = fd.read( 4096 )
while buf:
md5Calculated.update( buf )
buf = fd.read( 4096 )
fd.close()
#Check
if md5Expected != md5Calculated.hexdigest():
logERROR( "Oops... md5 for package %s failed!" % pkgVer )
sys.exit( 1 )
#Delete md5 file
if cache:
if not os.path.isdir( cacheDir ):
os.makedirs( cacheDir )
os.rename( md5Path, md5CachePath )
else:
os.unlink( md5Path )
#Extract
#cwd = os.getcwd()
#os.chdir(cliParams.targetPath)
#tf = tarfile.open( tarPath, "r" )
#for member in tf.getmembers():
# tf.extract( member )
#os.chdir(cwd)
tarCmd = "tar xzf '%s' -C '%s'" % ( tarPath, cliParams.targetPath )
os.system( tarCmd )
#Delete tar
if cache:
if not os.path.isdir( cacheDir ):
os.makedirs( cacheDir )
os.rename( tarPath, tarCachePath )
else:
if tarPath != tarFileCVMFS:
os.unlink( tarPath )
postInstallScript = os.path.join( cliParams.targetPath, pkgName, 'dirac-postInstall.py' )
if os.path.isfile( postInstallScript ):
os.chmod( postInstallScript , executablePerms )
logNOTICE( "Executing %s..." % postInstallScript )
if os.system( "python '%s' > '%s.out' 2> '%s.err'" % ( postInstallScript,
postInstallScript,
postInstallScript ) ):
logERROR( "Post installation script %s failed. Check %s.err" % ( postInstallScript,
postInstallScript ) )
return True
def fixBuildPaths():
"""
At compilation time many scripts get the building directory inserted,
this needs to be changed to point to the current installation path:
cliParams.targetPath
"""
# Locate build path (from header of pydoc)
binaryPath = os.path.join( cliParams.targetPath, cliParams.platform )
pydocPath = os.path.join( binaryPath, 'bin', 'pydoc' )
try:
fd = open( pydocPath )
line = fd.readline()
fd.close()
buildPath = line[2:line.find( cliParams.platform ) - 1]
replaceCmd = "grep -rIl '%s' %s | xargs sed -i'.org' 's:%s:%s:g'" % ( buildPath,
binaryPath,
buildPath,
cliParams.targetPath )
os.system( replaceCmd )
except:
pass
def fixPythonShebang():
"""
Some scripts (like the gfal2 scripts) come with a shebang pointing to the system python.
We replace it with the environment one
"""
binaryPath = os.path.join( cliParams.targetPath, cliParams.platform )
try:
replaceCmd = "grep -rIl '#!/usr/bin/python' %s/bin | xargs sed -i'.org' 's:#!/usr/bin/python:#!/usr/bin/env python:g'" % binaryPath
os.system( replaceCmd )
except:
pass
def runExternalsPostInstall():