File: Admin.py

package info (click to toggle)
mobyle 1.5.5%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,288 kB
  • sloc: python: 22,709; makefile: 35; sh: 33; ansic: 10; xml: 6
file content (319 lines) | stat: -rw-r--r-- 9,886 bytes parent folder | download | duplicates (3)
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
########################################################################################
#                                                                                      #
#   Author: Bertrand Neron,                                                            #
#   Organization:'Biological Software and Databases' Group, Institut Pasteur, Paris.   #
#   Distributed under GPLv2 Licence. Please refer to the COPYING.LIB document.         #
#                                                                                      #
########################################################################################


import os , sys
import logging
from time import localtime, strftime , strptime

a_log = logging.getLogger( __name__ )

from Mobyle.MobyleError import MobyleError

class Admin:
    """
    manage the informations in the .admin file.
    be careful there is no management of concurrent access file
    thus if there is different instance of Admin with the same path
    it could be problem of data integrity
    """
    FIELDS = ( 'DATE'    ,
               'EMAIL'   ,
               'REMOTE'  ,
               'SESSION' ,
               'WORKFLOWID' ,
               'JOBNAME' ,
               'JOBID'   ,
               'MD5'     ,
               'EXECUTION_ALIAS' ,
               'NUMBER'  ,
               'QUEUE'
               )
    FILENAME = '.admin'

    def __init__( self, path ):
        self.me = {}
        path = os.path.abspath( path )

        if os.path.exists( path ):
            if os.path.isfile( path ):
                self.path = path
                self._parse()
            elif os.path.isdir( path ):
                self.path = os.path.join( path , Admin.FILENAME )
                if os.path.isfile( self.path ):
                    self._parse()
                else:
                    raise MobyleError , "invalid job path : " + self.path
        else:
            raise MobyleError , "invalid job path : " + path


    @staticmethod
    def create( path , remote , jobName , jobID , userEmail =None , sessionID = None , workflowID = None ):
        """create a minimal admin file"""
        adm_file = os.path.join( path , Admin.FILENAME )
        if os.path.exists( adm_file ):
            raise MobyleError , "an \"admin\" file already exist in %s. can't create a new one" % ( path )

        args = {'DATE' : strftime( "%x %X" , localtime() ) ,
                'REMOTE' : remote ,
                'JOBNAME' : jobName ,
                'JOBID' : jobID ,
                }
        if userEmail:
            args[ 'EMAIL'] = userEmail
        if sessionID:
            args[ 'SESSION' ] = sessionID
        if workflowID:
            args[ 'WORKFLOWID' ] = workflowID

        adminFile = open( adm_file , "w" )
        for key in Admin.FIELDS:
            try:
                value = args[ key ]
            except KeyError :
                continue
            adminFile.write( "%s : %s\n" %( key , value ) )
        adminFile.close()


    def __str__( self ):
        res = ''
        for key in self.__class__.FIELDS:
            try:
                if key == 'DATE':
                    value = strftime( "%x %X" , self.me['DATE'] )
                else:
                    value =  self.me[ key ]
            except KeyError :
                continue
            res = res + "%s : %s\n" % ( key , value )
        return res


    def _parse ( self ):
        """
        parse the file .admin
        """
        try:
            fh = open( self.path , 'r' )

            for line in fh:
                datas = line[:-1].split( ' : ' )
                key = datas[0]
                value = ' : '.join( datas[1:] )
                if key == 'DATE':
                    value = strptime( value , "%x %X" )
                self.me[ key ] = value
        except Exception , err :
            a_log.critical( "an error occured during %s/.admin parsing (call by: %s) : %s " %( self.path ,
                                                                                               os.path.basename( sys.argv[0] ) ,
                                                                                               err ) ,
                                                                                               exc_info = True  )
            raise MobyleError , err
        finally:
            try:
                fh.close()
            except:
                pass
            if not self.me:
                msg = "admin %s object cannot be instantiated: is empty ( call by %s ) " % ( self.path , os.path.basename( sys.argv[0] ) )
                a_log.critical( msg)


    def refresh( self ):
        self._parse()

    def commit( self ):
        """
        Write the string representation of this instance on the file .admin
        """
        if not self.me.values():
            msg = "cannot commit admin file %s admin instance have no values (call by %s) " % ( self.path ,  os.path.basename( sys.argv[0] ) )
            a_log.critical( msg )
        try:
            tmpFile = open( "%s.%d" %( self.path , os.getpid() ) , 'w' )
            tmpFile.write( self.__str__() )
            os.rename( tmpFile.name , self.path )
        except Exception , err :
            a_log.critical( "an error occured during %s/.admin commit (call by: %s) : %s " %( self.path ,
                                                                                               os.path.basename( sys.argv[0] ) ,
                                                                                               err ) ,
                                                                                               exc_info = True  )
            raise MobyleError , err
        finally:
            try:
                tmpFile.close()
            except:
                pass

    def getDate( self ):
        """
        @return: the date of the job submission
        @rtype: time.struct_time
        """
        try:
            return  self.me[ 'DATE' ]
        except KeyError :
            return None

    def getEmail( self ):
        """
        @return: the email of the user who run the job
        @rtype: string
        """
        try:
            return self.me[ 'EMAIL' ]
        except KeyError :
            return None
        
    def setEmail( self , email):
        """
        set the email of the user
        """
        self.me[ 'EMAIL' ] = email
        
    def getRemote( self ):
        """
        @return: the remote of the job
        @rtype: string
        """
        try:
            return self.me[ 'REMOTE' ]
        except KeyError :
            return None

    def getSession( self ):
        """
        @return: the Session key of the job
        @rtype: string
        """
        try:
            return self.me[ 'SESSION' ]
        except KeyError :
            return None
        
    def setSession(self , session_key ):
        """
        set the session key of the job
        """
        self.me[ 'SESSION' ] = session_key
        
        
    def getWorkflowID( self , default= None ):
        """
        @return: the Workflow owner the job
        @rtype: string
        """
        try:
            return self.me[ 'WORKFLOWID' ]
        except KeyError :
            return default


    def getJobName( self ):
        """
        @return: the name of the job ( blast2 , toppred )
        @rtype: string
        """
        try:
            return self.me[ 'JOBNAME' ]
        except KeyError :
            return None

    def getJobID( self ):
        """
        @return: the job identifier.
        @rtype: string
        """
        try:
            return self.me[ 'JOBID' ]
        except KeyError :
            return None

    def getMd5( self ):
        """
        @return: the md5 of the job
        @rtype: string
        """
        try:
            return self.me[ 'MD5' ]
        except KeyError :
            return None

    def setMd5( self , md5 ):
        """
        set the md5 of the job
        @param : the identifier of the job
        @type : string
        """
        self.me[ 'MD5' ] = md5

    def getExecutionAlias( self ):
        """
        @return: the ExecutionConfig alias corresponding to the Execution object used to run the job
        @rtype: string
        """
        try:
            return self.me[ 'EXECUTION_ALIAS' ]
        except KeyError :
            return None

    def setExecutionAlias( self , execution_alias ):
        """
        set the execution class name  used to run of the job
        @param :  alias of the ExecutionConfig its value must belong to
         ExecutionConfig in Config.EXECUTION_CONFIG_ALIAS keys.
        @type : string
        """
        self.me[ 'EXECUTION_ALIAS' ] = execution_alias

    def getNumber( self ):
        """
        @return: the number the job the meaning of this value depend of BATCH value.
         - BATCH = Sys number is the job pid
         - BATCH = SGE number is the result of qstat
        @rtype: string
        """
        try:
            return self.me[ 'NUMBER' ]
        except KeyError :
            return None


    def setNumber( self , number ):
        """
        set the number of the job this number depend of the batch value
        if BATCH = Sys number is the pid
        if BATCH = SGE number is the
        @param : the number of the job
        @type : string
        """
        self.me[ 'NUMBER' ] = number

    def getQueue( self ):
        """
        @return: return the queue name of the job
        @rtype: string
        """
        try:
            return self.me[ 'QUEUE' ]
        except KeyError :
            return None


    def setQueue( self, queue ):
        """
        set the queue name of the job
        @param : the queuename of the job
        @type : string
        """
        self.me[ 'QUEUE' ] = queue