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 """Module defining class for creating, querying and renewing AFS token"""
88
89 __author__ = "K.Harrison <Harrison@hep.phy.cam.ac.uk>"
90 __date__ = "05 November 2009"
91 __version__ = "1.16"
92
93 import os
94 import time
95
96 from ICredential import ICommandSet
97 from ICredential import ICredential
98 from ICredential import registerCommandSet
99 from Ganga.GPIDev.Schema import SimpleItem
100 from Ganga.Runtime import Repository_runtime
101 from Ganga.Utility.Config import getConfig
102 from Ganga.Utility.Config.Config import ConfigError
103 from Ganga.Utility.files import fullpath
104 from Ganga.Utility.logging import getLogger
105
106 logger = getLogger()
107
109 """
110 Class used to define shell commands and options for working with AFS token
111 """
112
113 _schema = ICommandSet._schema.inherit_copy()
114 _schema['init']._meta['defvalue'] = "klog"
115 _schema['info']._meta['defvalue'] = "tokens"
116 _schema['destroy']._meta['defvalue'] = "unlog"
117 _schema['init_parameters']._meta['defvalue'] = { "pipe" : "-pipe", "valid" : "-lifetime", \
118 "username" : "-principal", "cell" : "-cell" }
119 _schema['destroy_parameters']._meta['defvalue'] = { "cell" : "-cell" }
120
121 _name = "AfsCommand"
122 _hidden = 1
123 _enable_config = 1
124
126 super( AfsCommand, self ).__init__()
127 self.currentOpts = {}
128 self.infoOpts = {}
129 self.destroyOpts = {}
130 return
131
132 registerCommandSet( AfsCommand )
133
135 """
136 Class for working with AFS token
137 """
138
139 _schema = ICredential._schema.inherit_copy()
140 _schema.datadict[ "cell" ] = SimpleItem( defvalue = "", doc = \
141 "AFS cell with which token is used [empty string implies local cell]" )
142 _schema.datadict[ "username" ] = SimpleItem( defvalue = "",
143 doc = "AFS username with which token is used [defaults to login id]" )
144 _name = "AfsToken"
145 _hidden = 1
146 _enable_config = 1
147
156
157
158
159 - def buildOpts( self, command, clear = True):
177
178 - def create( self, validity = "", maxTry = 0, minValidity = "", check = False ):
181
182 - def destroy( self, allowed_exit = [ 0 ] ):
185
187
188 if self.cell:
189 available = True
190 else:
191 available = False
192
193 if not available:
194
195
196
197 available = Repository_runtime.requiresAfsToken()
198
199 if available:
200 infoCommand = self.command.info.split()[ 0 ]
201 available = False
202 try:
203 pathList = os.environ[ "PATH" ].split( os.pathsep )
204 except KeyError:
205 pathList = []
206 for searchDir in pathList:
207 try:
208 fileList = os.listdir( searchDir )
209 except OSError:
210 fileList = []
211 if infoCommand in fileList:
212 available = True
213 break
214 if available:
215 logger.debug( "Command '%s' found in directory '%s'" % \
216 ( infoCommand, searchDir ) )
217 else:
218 logger.debug( "Unable to find command '%s'" % infoCommand )
219
220 if available:
221 timeleft = self.timeleft()
222 if not timeleft:
223 available = False
224
225 return available
226
227 - def isValid( self, validity = "", log = False, force_check = False ):
229
231 """
232 Dummy method - returns empty string
233 """
234 return ""
235
236 - def renew( self, validity = "", maxTry = 0, minValidity = "", check = True ):
238
239 - def timeleft( self, units = "hh:mm:ss", force_check = False ):
241
243
244 localTuple = time.localtime()
245 status, output, message = self.shell.cmd1( self.command.info )
246
247 timeRemaining = "00:00:00"
248
249 if status:
250 if ( 1 + output.lower().find( "command not found" ) ):
251 logger.warning( "Command '" + self.command.info + "' not found" )
252 logger.warning( "Unable to obtain information on AFS tokens" )
253 timeRemaining = ""
254
255 if timeRemaining:
256 timeString = ""
257 lineList = output.split( "\n" )
258 timeRemaining = "-1"
259 for line in lineList:
260 if ( 1 + line.lower().find( "tokens for" ) ):
261 elementList = line.rstrip( "]" ).split( "[" )[ 1 ].split()
262 if self.cell:
263 afsString = "".join( [ " afs@", self.cell, " " ] )
264 else:
265 afsString = "afs@"
266 if not ( 1 + line.find( afsString ) ):
267 elementList = []
268 if len( elementList ) > 1:
269 elementList.append( str( localTuple[ 0 ] ) )
270 timeString = " ".join( elementList[ 1 : ] )
271 timeTuple = time.strptime( timeString, "%b %d %H:%M %Y" )
272 timeList = list( timeTuple )
273 if localTuple[ 1 ] > timeTuple[ 1 ]:
274 timeList[ 0 ] = 1 + localTuple[ 0 ]
275 timeList[ 8 ] = localTuple[ 8 ]
276 timeTuple = tuple( timeList )
277 timeLeft = int( time.mktime( timeTuple ) - time.time() )
278 hours = timeLeft / ( 60 * 60 )
279 minutes = ( timeLeft - hours * 60 * 60 ) / 60
280 seconds = timeLeft - hours * 60 * 60 - minutes * 60
281 minString = str( minutes )
282 if len( minString ) < 2:
283 minString = "0" + minString
284 secString = str( seconds )
285 if len( secString ) < 2:
286 secString = "0" + secString
287 timeRemaining = "%s:%s:%s" % \
288 ( str( hours ), minString, secString )
289 if ( timeRemaining != "-1" ):
290 break
291
292 return timeRemaining
293
294
295 for method in \
296 [ create, destroy, isAvailable, isValid, renew, timeleft, timeleftInHMS ]:
297 if hasattr( ICredential, method.__name__ ):
298 baseMethod = getattr( ICredential, method.__name__ )
299 setattr( method, "__doc__",\
300 baseMethod.__doc__.replace( "credential", "AFS token" ) )
301