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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
|
########################################################################################
# #
# 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
import sys
import fcntl
import pickle
from lxml import etree
from time import strptime , strftime , time , sleep
from logging import getLogger
from Mobyle.Parser import parseType
from Mobyle.Classes.DataType import DataTypeFactory
from Mobyle.MobyleError import MobyleError , SessionError , ParserError
#from Mobyle.Utils import indent, parse_xml_file
from Mobyle.Utils import indent
from Mobyle.Status import Status
class Transaction( object ):
"""
This class defines a session, that stores all the information
about a user that should be persistent on the server
@author: Bertrand Neron
@organization: Institut Pasteur
@contact:mobyle@pasteur.fr
"""
__ref = {}
WRITE = fcntl.LOCK_EX
READ = fcntl.LOCK_SH
def __new__( cls , fileName , lockType ):
if not cls.__ref.has_key( fileName ) :
self = super( Transaction , cls ).__new__( cls )
self._log = getLogger( 'Mobyle.Session.Transaction' )
fileName = os.path.normpath( fileName )
try:
if lockType == self.READ :
self.__File = open( fileName , 'r' )
elif lockType == self.WRITE :
self.__File = open( fileName , 'r+' )
else:
raise MobyleError , 'invalid lockType : ' + str( lockType )
except IOError , err:
msg = "can't open session %s : %s" % ( os.path.basename( os.path.dirname( fileName ) ) , err )
self._log.critical( msg )
raise SessionError , msg
self.__lockType = lockType
self._lock() #try to acquire a lock
#If I do not got a lock a IOError is raised
try:
parser = etree.XMLParser( no_network = False )
#self._doc = parse_xml_file(self.__File,parser)
self._doc = etree.parse( self.__File , parser )
self._root = self._doc.getroot()
except Exception , err:
import shutil
shutil.copy2( self.__File.name , "%s.copy.%d" %( self.__File.name , int( time() ) ))
msg = " an error occured during the session %s parsing : %s " % ( os.path.basename( os.path.dirname( fileName ) ) , err )
self._log.error( msg , exc_info = True )
raise SessionError , err
self._modified = False
cls.__ref[ self.__File.name ] = True
return self
else:
raise SessionError , "try to open 2 transactions on the same file at the same time"
@staticmethod
def create( fileName , authenticated , activated , activatingKey = None , userEmail = None , passwd = None):
"""
create a minimal xml session
@param fileName: the absolute path of the xml session file
@type fileName: string
@param authenticated: True if the session is authenticated , false otherwise
@type authenticated: boolean
@param activated: True if the session is activated, False otherwise.
@type activated: boolean
@param activatingKey: the key send to the user to activate this session
@type activatingKey: boolean
@param userEmail: the userEmail of the user
@type userEmail: string
"""
id = os.path.basename( os.path.dirname( fileName ))
if authenticated and not passwd:
msg = "cannot create authenticated session %s for %s without passwd" % ( id , userEmail )
raise SessionError, msg
if not authenticated and passwd:
msg = "cannot create anonymous session %s for %s with passwd" % ( id , userEmail )
raise SessionError, msg
if authenticated and not userEmail:
msg = "cannot create authenticated session %s without userEmail" % ( id )
raise SessionError, msg
root = etree.Element( "userSpace" , id = id )
#doc = etree.ElementTree( root )
authenticatedNode = etree.Element( "authenticated" )
if authenticated:
authenticatedNode.text = 'true'
ticketNode = etree.Element( "ticket" )
ticket_idNode = etree.Element( "id" )
exp_dateNode = etree.Element( "exp_date" )
ticketNode.append( ticket_idNode )
ticketNode.append( exp_dateNode )
root.append( ticketNode )
else:
authenticatedNode.text = 'false'
root.append( authenticatedNode )
activatedNode = etree.Element( "activated" )
if activated:
activatedNode.text = 'true'
else:
activatedNode.text = 'false'
root.append( activatedNode )
if userEmail:
emailNode = etree.Element( "email" )
emailNode.text = str( userEmail )
root.append( emailNode )
if passwd:
passwdNode = etree.Element( "passwd" )
passwdNode.text = passwd
root.append( passwdNode )
if activatingKey:
activatingKeyNode = etree.Element( "activatingKey" )
activatingKeyNode.text = activatingKey
root.append( activatingKeyNode )
if os.path.exists( fileName ):
msg = "cannot create session %s, file already exists" % fileName
raise SessionError, msg
xmlFile = open( fileName , 'w' )
xmlFile.write( etree.tostring( root , xml_declaration=True , encoding='UTF-8', pretty_print= True ))
xmlFile.close()
def _lock(self):
"""
try to acquire a lock of self.__lockType on self.__File
@raise IOError: when it could not acquire a lock
"""
IGotALock = False
ID = os.path.basename( os.path.dirname( self.__File.name ))
self._log.debug( "%f : %s : _lock Type= %s ( call by= %s )" %( time() ,
ID,
( 'UNKNOWN LOCK', 'READ' , 'WRITE' )[ self.__lockType ] ,
os.path.basename( sys.argv[0] ) ,
))
for attempt in range( 4 ):
try:
fcntl.lockf( self.__File , self.__lockType | fcntl.LOCK_NB )
IGotALock = True
self._log.debug( "%f : %s : _lock IGotALock = True" %(time() , ID ))
break
except IOError , err:
self._log.debug( "%f : %s : _lock IGotALock = False" %(time() , ID))
sleep( 0.2 )
if not IGotALock :
self._log.error( "%s : %s" %( ID , err ) )
self._log.debug( "%f : %s : _lock Type= %s ( call by= %s )" %( time() ,
ID ,
( 'UNKNOWN LOCK', 'READ' , 'WRITE' )[ self.__lockType ] ,
os.path.basename( sys.argv[0] )
))
self.__File.close()
self.__File = None
self.__lockType = None
raise IOError , err
def _setModified(self , modified ):
"""
to avoid that a 1rst method set modified to True and a 2 method reset it to False
"""
if self._modified is False:
self._modified = modified
def _decodeData( self , data ):
"""
convert the data in right encoding and replace windows end of line by unix one.
"""
# trying to guess the encoding, before converting the data to ascii
try:
# trying ascii
data = unicode(data.decode('ascii','strict'))
except:
try:
# utf8 codec with BOM support
data = unicode(data,'utf_8_sig')
except:
try:
# utf16 (default Windows Unicode encoding)
data = unicode(data,'utf_16')
except:
# latin1
data = unicode(data,'latin1')
# converting the unicode data to ascii
data = data.encode('ascii','replace')
return data
def _addTextNode( self, node , nodeName , content , attr = None):
"""
add a text node named nodeName with content to the node node
@param node: the node on which the new node will append
@type node: node element
@param nodeName: the name of the new node
@type nodeName: string
@param content: the content of the new node
@type content: string
"""
newNode = etree.Element( nodeName )
if attr:
for attrName in attr.keys():
newNode.set( attrName , str( attr[ attrName ] ) )
try:
newNode.text = content
except Exception:
data = self._decodeData( content )
try:
newNode.text = data
except Exception:
if nodeName != 'headOfData':
self._log.error( "%s :node = %s have not printable characters they will be replaced by'_': %s " %(
self.getID() ,
nodeName
) )
from string import printable
for char in data :
if char not in printable :
data = data.replace( char , '_')
try:
newNode.text = data
except Exception, err:
self._log.warning( "%s : cannot add text to textNode nodeName= %s : %s " %(
self.getID() ,
nodeName ,
err ,
) )
newNode.text = '--- not printable ---'
node.append( newNode )
def _updateNode( self, node , nodeName , content ):
"""
replace the content of the text_node child of a node by content. if the node nodeName doesn't exist, it make it
@param nodeName: the name of the Node to update
@type nodeName: String
@param content: the content of the text-node child of nodeName
@type content: String
"""
# convert non-string node contents to str (e.g. floats...)
if not(isinstance(content,basestring)):
content = str(content)
#handle utf8 data (renamed jobs and data)
try:
content = unicode(content, 'utf-8')
except TypeError:
pass
node2update = node.find( "./" + nodeName )
if node2update is not None:
if node2update.text == content:
return False
else:
node2update.text = content
return True
else:
newNode = etree.Element( nodeName )
newNode.text = content
node.append( newNode )
return True
def commit(self):
"""
if the transaction was open in WRITE
- write the dom in xml file if it was modified
and release the lock and close the session File this transaction could not be used again
"""
if self.__lockType == self.READ :
logType = 'READ'
elif self.__lockType == self.WRITE :
logType = 'WRITE'
else:
logType = 'UNKNOWN LOCK( ' + str( self.__lockType ) +' )'
self._log.debug( "%f : %s : _commit Type= %s ( call by= %s )" %( time(),
self.getID() ,
logType ,
os.path.basename( sys.argv[0] )
))
try:
if self.__lockType == self.WRITE and self._modified :
try:
tmpFile = open( "%s.%d" %( self.__File.name, os.getpid() ) , 'w' )
indent(self._root )
tmpFile.write(etree.tostring( self._doc, xml_declaration=True , encoding='UTF-8'))
tmpFile.close()
os.rename(tmpFile.name , self.__File.name)
except IOError , err:
msg = "can't commit this transaction: " + str( err )
self._log.error( msg )
raise SessionError , msg
except Exception , err:
self._log.error( "session/%s self.__File = %s" %( self.getID(), self.__File ) ,
exc_info = True )
raise err
finally:
key2del = self.__File.name
try:
self._log.debug("%f : %s : commit UNLOCK type= %s modified = %s" %(time() ,
self.getID() ,
logType,
self._modified
))
fcntl.lockf( self.__File , fcntl.LOCK_UN )
self.__File.close()
self.__File = None
self.__lockType = None
self._modified = False #reset _modifiedTransaction to false for the next transaction
del( Transaction.__ref[ key2del ] )
except IOError , err :
self._log.error( "session/%s : cannot UNLOCK transaction type= %s modified= %s: %s" %(self.getID(),
logType,
self._modified,
err ) )
del( Transaction.__ref[ key2del ] )
def rollback(self):
"""
release the lock , close the file without modification of the xml file.
this transaction could not be used again
"""
if self.__lockType == self.READ :
logType = 'READ'
elif self.__lockType == self.WRITE :
logType = 'WRITE'
else:
logType = 'UNKNOWN LOCK( ' + str( self.__lockType ) +' )'
self._log.debug( "%f : %s : _close Type= %s ( call by= %s )" %( time(),
self.getID() ,
logType ,
os.path.basename( sys.argv[0] )
))
try:
key2del = self.__File.name
sys.stdout.flush()
fcntl.lockf( self.__File , fcntl.LOCK_UN )
self.__File.close()
self.__File = None
self.__lockType = None
self._modified = False
self._modified = False
except IOError , err :
self._log.error( "session/%s : an error occured during closing transaction : %s" %( self.getID() , err ))
# a gerer
except Exception , err :
msg = "session/%s : an error occured during closing transaction : %s" % ( self.getID() , err )
self._log.error( msg )
self._log.debug( "%f : %s : self.__File = %s" %(time() , self.getID(), self.__File ))
self._log.debug( "%f : %s : self.__lockType = %s" %(time() , self.getID(), logType ))
raise SessionError , msg
finally:
try:
del( Transaction.__ref[ key2del ] )
except :
pass
def getID(self):
"""
@return: the id of the session
@rtype: string
"""
id = self._root.get( 'id' )
if id:
return id
else:
msg = "the session %s has no identifier" % self.__File.name
self._log.error( msg )
raise SessionError , msg
def setID(self, sessionKey):
"""
set the id of the session
@param sessionKey: the sessionKey wchi will used to set the id
@type sessionKey: string
"""
self._root.set( 'id', sessionKey )
self._setModified( True )
def getEmail(self):
"""
@return: the email of the user session
@rtype: string
"""
try:
return self._root.find( 'email' ).text
except AttributeError:
return None
def setEmail( self , email ):
"""
set the user email of this session
@param email: the email of the user of this session
@type email: string
"""
modified = self._updateNode( self._root , 'email' , email )
self._setModified( modified )
def isActivated( self ):
"""
@return: True if this session is activated, False otherwise.
@rtype: boolean
"""
try:
activated = self._root.find( 'activated' ).text
if activated == 'true':
return True
else:
return False
except AttributeError:
msg = "the session %s has no tag 'activated'" % self.__File.name
self._log.error( msg )
raise SessionError , msg
def activate(self):
"""
activate this session
"""
modified = self._updateNode( self._root , 'activated' , 'true' )
self._setModified( modified )
def inactivate(self):
"""
activate this session
"""
modified = self._updateNode( self._root , 'activated' , 'false' )
self._setModified( modified )
def getActivatingKey(self):
"""
@return: the key needed to activate this session ( which is send by email to the user )
@rtype: string
"""
try:
return self._root.find( 'activatingKey' ).text
except AttributeError:
return None
def isAuthenticated(self):
"""
@return: True if this session is authenticated, False otherwise.
@rtype: boolean
"""
try:
auth = self._root.find( 'authenticated').text
if auth == 'true':
return True
else:
return False
except AttributeError:
return None
def getPasswd(self):
"""
@return: the password of the user ( encoded )
@rtype: string
"""
try:
return self._root.find( 'passwd' ).text
except AttributeError:
return None
def setPasswd( self , passwd ):
"""
set the session user pass word to passwd
@param passwd: the encoded user password
@type passwd: string
"""
modified = self._updateNode( self._root , 'passwd' , passwd )
self._setModified( modified )
def getTicket(self):
"""
get the currently allocated ticket and its expiration date
@return: the ticket of the user and the expiration date
@rtype: tuple
"""
try:
tck = self._root.find( 'ticket' )
ticket_id = tck.find('id').text
exp_date = tck.find('exp_date' ).text
return (ticket_id, exp_date)
except AttributeError, ae:
self._log.error(ae, exc_info=True)
return None
def setTicket( self , ticket_id, exp_date ):
"""
set the currently allocated ticket and its expiration date
@param ticket_id: the ticket
@type ticket_id: string
@param exp_date: the expiration date
@type exp_date: date
"""
tck = self._root.find( 'ticket' )
if tck is not None:
m1 = self._updateNode(tck, 'id', ticket_id)
m2 = self._updateNode(tck, 'exp_date', exp_date)
modified = m1 or m2
self._setModified( modified )
else:
msg = "try to set id and exp-date to a ticket but ticket tag does not exist (%s)" % self.__File.name
self._log.error( msg )
raise SessionError , msg
def getCaptchaSolution( self ):
"""
@return: the solution of the captcha problem submitted to the user.
@rtype: string
"""
try:
return self._root.find( 'captchaSolution' ).text
except AttributeError:
return None
def setCaptchaSolution( self , solution ):
"""
store the solution of the captcha problem submitted to the user.
@param solution: the solution of the captcha
@type solution: string
"""
modified = self._updateNode( self._root , 'captchaSolution' , solution )
self._setModified( modified )
################################
#
# operations on Data
#
#################################
def hasData(self , dataID ):
"""
@param dataID: the identifier of the data in the session ( mdm5 name + ext )
@type dataID: string
@return: True if the session structure has an entry for the data corresponding to this ID, False otherwise.
@rtype: boolean
"""
try:
self._root.xpath( 'dataList/data[@id = "%s"]'% dataID )[0]
return True
except IndexError:
return False
def renameData( self , dataID , newUserName ):
"""
change the user name of the data corresponding to the dataID.
@param dataID: the identifier of the data in the session ( mdm5 name + ext )
@type dataID: string
@param newUserName: the new user name of the data.
@type newUserName: string
@raise ValueError: if dataID does not match any entry in session.
"""
dataNode = self._getDataNode( dataID )
modified = self._updateNode( dataNode , 'userName' , newUserName )
self._setModified( modified )
def getData(self , dataID ):
"""
@param dataID: the identifier of the data in the session ( mdm5 name + ext )
@type dataID: string
@return: the data corresponding to the dataID
@rtype: { 'dataName' = string ,
'userName' = string ,
'size' = int ,
'Type' = Mobyletype instance,
'dataBegin' = string ,
'inputModes' = [ string inputMode1 , string inputMode2 ,... ],
'producedBy' = [ string jobID1 , string jobID2 , ... ],
'usedBy' = [ string jobID1 , string jobID2 , ... ]
}
@raise ValueError: if dataID does not match any entry in session.
"""
dataNode = self._getDataNode( dataID )
dtf = DataTypeFactory()
return self._dataNode2dataDict( dataNode , dtf )
def getAllData(self):
"""
@return: all data in this session
@rtype: [ {'dataName' = string ,
'userName' = string ,
'size' = int ,
'Type' = Mobyletype instance,
'dataBegin' = string ,
'inputModes' = [ string inputMode1 , string inputMode2 ,... ],
'producedBy' = [ string jobID1 , string jobID2 , ... ],
'usedBy' = [ string jobID1 , string jobID2 , ... ]
} , ... ]
"""
datas = []
dtf = DataTypeFactory()
for dataNode in self._root.xpath( 'dataList/data') :
data = self._dataNode2dataDict( dataNode , dtf )
datas.append( data )
return datas
def removeData(self , dataID ):
"""
remove the data entry corresponding to dataID
@param dataID: the identifier of the data in the session ( mdm5 name + ext )
@type dataID: string
@raise ValueError: if dataID does not match any entry in session.
"""
dataNode = self._getDataNode( dataID )
dataListNode = dataNode.getparent()
dataListNode.remove( dataNode )
## remove the ref of this data from the jobs
usedBy = dataNode.xpath('usedBy/@ref' )
for jobID in usedBy:
try:
jobNode = self._getJobNode( jobID )
except ValueError:
continue
else:
try:
dataUsedNode = jobNode.xpath( 'dataUsed[@ref="%s"]' %dataID )[0]
except IndexError:
continue
else:
jobNode.remove( dataUsedNode )
producedBy = dataNode.xpath('producedBy/@ref' )
for jobID in producedBy:
try:
jobNode = self._getJobNode( jobID )
except ValueError:
continue
else:
try:
dataProducedNode = jobNode.xpath( 'dataProduced[@ref="%s"]' %dataID )[0]
except IndexError:
continue
else:
jobNode.remove( dataProducedNode )
self._setModified( True )
def linkJobInput2Data(self , datasID , jobsID ):
"""
add a ref of each job using this data and a ref of this data in the corresponding job
@param datasID: the IDs of data which are used by these jobsID
@type datasID: list of string
@param jobsID: the IDs of job which has used these data
@type jobsID: list of string
@raise ValueError: if one dataID or jobID does not match any entry in session.
"""
for dataID in datasID:
dataNode = self._getDataNode( dataID )
for jobID in jobsID :
alreadyUsedBy = dataNode.xpath('usedBy/@ref' )
if jobID not in alreadyUsedBy :
newUsedByNode = etree.Element( 'usedBy' , ref = jobID )
dataNode.append( newUsedByNode )
self._setModified( True )
try:
jobNode = self._getJobNode( jobID )
except ValueError:
continue
dataUsed = jobNode.xpath( 'dataUsed/@ref' )
if dataID not in dataUsed:
newDataUsedNode = etree.Element( 'dataUsed' , ref = dataID )
jobNode.append( newDataUsedNode )
self._setModified( True )
def linkJobOutput2Data(self , datasID , jobsID ):
"""
add a ref of each job producing these data and a ref of these data in corresponding jobs.
@param dataID: the list of the data ID
@type dataID: list of string
@param jobsID: the IDs of job which has produced this data
@type jobsID: list of string
@raise ValueError: if one dataID or jobID does not match any entry in session.
"""
for dataID in datasID:
dataNode = self._getDataNode( dataID )
for jobID in jobsID:
alreadyDataProduced = dataNode.xpath('producedBy/@ref' )
if jobID not in alreadyDataProduced :
newUsedByNode = etree.Element( 'producedBy' , ref = jobID)
dataNode.append( newUsedByNode )
self._setModified( True )
try:
jobNode = self._getJobNode( jobID )
except ValueError:
continue
dataProduced = jobNode.xpath( 'dataProduced/@ref' )
if dataID not in dataProduced:
newDataProducedNode = etree.Element('dataProduced' , ref = dataID )
jobNode.append( newDataProducedNode )
self._setModified( True )
def addInputModes(self , dataID , inputModes ):
"""
add an inputmode in the list of inputModes of the data corresponding to the dataID
@param dataID: The identifier of one data in this session
@type dataID: string
@param inputModes: the list of inputMode to add the accepted value are 'db' , 'paste' , 'upload' , 'result'
@type inputModes: list of string
@raise ValueError: if dataID does not match any entry in session.
"""
dataNode = self._getDataNode( dataID )
modesNode = dataNode.find( 'inputModes')
if modesNode is not None:
oldModes = modesNode.xpath( 'inputMode/text()')
else:
modesNode = etree.Element( 'inputModes' )
oldModes = []
for newMode in inputModes:
if newMode in oldModes:
continue
else:
newInputNode = etree.Element( 'inputMode' )
newInputNode.text = newMode
modesNode.append( newInputNode )
self._setModified( True )
def createData( self , dataID , userName , size , mobyleType , dataBegining , inputModes , producedBy = [] , usedBy = []):
"""
add a new data entry in the session.
@param dataID: The identifier of one data in this session
@type dataID: string
@param userName: the name given by the user to this data
@type userName: string
@param size: the size of the file corresponding to this data
@type size: int
@param Type: the MobyleType of this data
@type Type: L{MobyleType} instance
@param dataBegining: the 50 first char of the data (if data inherits from Binary a string)
@type dataBegining: string
@param inputModes: the input modes of this data the accepted data are: 'paste' , 'db' , 'upload' , 'result'
@type inputModes: list of string
@param format: the format , count , of this data
@type format: ( string format , int count , string 'to fill the api (unused)' )
@param producedBy: the jobs Id which produced this data
@type producedBy: list of string
@param usedBy: the jobs ID which used this data
@type usedBy: list of string
"""
dataListNode = self._root.find( 'dataList')
if dataListNode is None:
dataListNode = etree.Element( 'dataList')
self._root.append( dataListNode )
dataNode = etree.Element( 'data' , id = dataID , size = str( size ))
self._addTextNode( dataNode , "userName" , userName )
typeNode = mobyleType.toDom()
dataNode.append( typeNode )
self._addTextNode( dataNode , "headOfData" , dataBegining )
if inputModes:
inputModesNode = etree.Element( 'inputModes' )
for inputMode in inputModes:
self._addTextNode( inputModesNode , "inputMode" , inputMode )
dataNode.append( inputModesNode )
for jobId in producedBy:
producedByNode = etree.Element( "producedBy" , ref = jobId )
dataNode.append( producedByNode )
for jobId in usedBy:
usedByNode = etree.Element( "usedBy" , ref = jobId )
dataNode.append( usedByNode )
dataListNode.append( dataNode )
self._setModified( True )
def addWorkflowLink( self , id):
"""
add a new workflow definition in the session.
@param id: The identifier of the workflow
@type id: string
"""
workflowsListNode = self._root.find( 'workflowsLinkList')
if workflowsListNode is None:
workflowsListNode = etree.Element( 'workflowsLinkList')
self._root.append( workflowsListNode )
workflowNode = etree.Element( 'workflowLink' , id = str(id))
workflowsListNode.append( workflowNode )
self._setModified( True )
def getWorkflows( self):
"""
get the list of workflow definitions in the session.
@return: workflow definition identifiers list
@rtype: [ {'id':string } , ... ]
"""
list = []
for workflow in self._root.xpath( 'workflowsLinkList/workflowLink') :
list.append({'id':int(workflow.get('id'))})
return list
def _getDataNode(self , dataID ):
"""
@param dataID: the identifier of one data in this session
@type dataID: string
@return: the Node corresponding to the dataID
@rtype: Node instance
@raise ValueError: if dataID does not match any entry in this session.
"""
try:
dataNode = self._root.xpath( 'dataList/data[@id = "%s"]' % dataID )[0]
return dataNode
except IndexError:
msg = "the data %s does not exist in the session %s" % ( dataID , self.getID() )
raise ValueError , msg
def _dataNode2dataDict(self, dataNode , dtf ):
"""
@param dataNode: a node representing a data
@type dataNode: Node instance
@param dtf: a dataTypeFactory
@type dtf: a L{Mobyle.Classes.Core.DataType.DataTypeFactory} instance
"""
data = {}
data[ 'dataName' ] = dataNode.get( 'id' )
try:
data[ 'userName' ] = dataNode.find( 'userName' ).text
except AttributeError:
msg = "data %s has no userName" % data[ 'dataName' ]
self._log.error( msg )
SessionError , msg
try:
data[ 'Type' ] = parseType( dataNode.find( 'type' ) , dataTypeFactory = dtf )
except ( ParserError , MobyleError ) :
msg = "error in type parsing for data %s "% data[ 'dataName' ]
self._log.error( msg , exc_info = True )
raise SessionError , msg
try:
data[ 'dataBegin' ] = dataNode.find( 'headOfData').text
except AttributeError:
data[ 'dataBegin' ] = ''
try:
data[ 'inputModes' ] = [ inputmode.text for inputmode in dataNode.findall( 'inputModes/inputMode' ) ]
except AttributeError:
pass
data[ 'producedBy' ] = dataNode.xpath( 'producedBy/@ref' )
data[ 'usedBy' ] = dataNode.xpath( 'usedBy/@ref' )
data[ 'size' ] = int( dataNode.get( 'size' ) )
return data
#####################
#
# jobs methods
#
#####################
def _get_userAnnotation_node(self , jobID ):
"""
@return: the userAnnotationNode of job with jobID.
if userAnnotationNode doesnot exist make it
@rtype: etree.element instance
@param jobID: the job identifer (it's url) of a job
@type jobID: string
@raise ValueError: if there is no job with jobID in the session
"""
jobNode = self._getJobNode( jobID )
userAnnotation_node = jobNode.find( 'userAnnotation' )
if userAnnotation_node is not None:
self._setModified( False )
else:
userAnnotation_node = etree.Element( 'userAnnotation' )
jobNode.append( userAnnotation_node )
self._setModified( True )
return userAnnotation_node
def getAllUniqueLabels(self):
labelsList = []
#rather to use recursive search
#I used direct path from root
for labelNode in self._doc.findall('/jobList/job/userAnnotation/labels/label') :
if not labelNode.text in labelsList :
labelsList.append( labelNode.text )
return labelsList
def getJobDescription( self, jobID):
"""
@return: the description for job with identifier jobID
@rtype: string or None if there is no description for this job
@param jobID: the job identifer (url) of a job
@type jobID: string
@raise ValueError: if there is no job with jobID in the session
"""
user_annot_node = self._get_userAnnotation_node( jobID )
try:
description = user_annot_node.find( 'description' ).text
return description
except AttributeError:
return None
def getJobUserName( self, jobID):
"""
@return: the user name for job with identifier jobID
@rtype: string or None if there is no description for this job
@param jobID: the job identifer (url) of a job
@type jobID: string
@raise ValueError: if there is no job with jobID in the session
"""
user_annot_node = self._get_userAnnotation_node( jobID )
try:
description = user_annot_node.find( 'userName' ).text
return description
except AttributeError:
return None
def setJobDescription( self, jobID , description):
"""
set description for the job with jobId
@param jobID: the job identifer (it's url) of a job
@type jobID: string
@param description: the description of this job
@type description: string
@raise ValueError: if there is no job with jobID in the session
"""
user_annot_node = self._get_userAnnotation_node( jobID )
modified = self._updateNode( user_annot_node , 'description' , description )
self._setModified( modified )
def setJobLabels( self, jobID, inputLabels):
"""
set labels for job with jobId
@param jobID: the job identifer (it's url) of a job
@type jobID: string
@param description: the description of this job
@type description: string
@raise ValueError: if there is no job with jobID in the session
"""
user_annot_node = self._get_userAnnotation_node( jobID )
labels_node = user_annot_node.find( 'labels' )
if labels_node is not None:
user_annot_node.remove( labels_node )
new_labels_node = etree.Element( 'labels' )
for new_label in inputLabels:
new_label_node = etree.Element( 'label' )
new_label_node.text = new_label
new_labels_node.append( new_label_node )
user_annot_node.append( new_labels_node )
self._setModified( True )
def getJobLabels( self, jobID):
"""
@return: the labels for job with identifier jobID
@rtype: list of strings , [ string label1 ,string label2 ,...] or None if this jobs has no labels.
@param jobID: the job identifer (it's url) of a job
@type jobID: string
@raise ValueError: if there is no job with jobID in the session
"""
user_annot_node = self._get_userAnnotation_node( jobID )
labels_node = user_annot_node.find( 'labels' )
if labels_node is None:
return []
else:
labels = [ labelNode.text for labelNode in labels_node.findall( 'label' ) ]
return labels
def hasJob( self , jobID ):
"""
@param jobID: the identifier of the job in the session ( the url without index.xml )
@type jobID: string
@return: True if the session structure has an entry for the job corresponding to this ID, False otherwise.
@rtype: boolean
"""
try:
self._root.xpath( 'jobList/job[@id="%s"]'% jobID )[0]
return True
except IndexError:
return False
def getJob(self , jobID ):
"""
@param jobID: the identifier of the job in the session ( the url without index.xml )
@type jobID: string
@return: the job corresponding to the jobID
@rtype: {'jobID' : string ,
'userName' : string ,
'programName' : string ,
'status' : Status object ,
'date' : time struct ,
'dataProduced': [ string dataID1 , string dataID2 , ...] ,
'dataUsed' : [ string dataID1 , string dataID2 , ...] ,
}
@raise ValueError: if the jobID does not match any entry in this session
"""
jobNode = self._getJobNode( jobID )
return self._jobNode2jobDict( jobNode )
def getAllJobs(self):
"""
@return: the list of jobs in this session
@rtype: [ {'jobID' : string ,
'userName' : string ,
'programName' : string ,
'status' : Status object ,
'date' : time struct ,
'dataProduced': [ string dataID1 , string dataID2 , ...] ,
'dataUsed' : [ string dataID1 , string dataID2 , ...] ,
} , ... ]
"""
jobs = []
jobList = self._root.findall( 'jobList/job' )
for jobNode in jobList:
job = self._jobNode2jobDict( jobNode )
jobs.append( job )
return jobs
#OPENID
def addOpenIdAuthData(self, authsession):
"""
@param authsession: the session used by OpenId library
@type authsession: Session
@raise SessionError : if data cannot be saved
"""
authNode = self._root.find( 'authOpenIdData' )
if authNode:
self._root.remove( authNode )
authNode = etree.Element( 'authOpenIdData')
for name in authsession.keys():
self._addTextNode(authNode, name, pickle.dumps(authsession[name]))
self._root.append(authNode)
self._setModified( True )
#OPENID
def getOpenIdAuthData(self):
"""
@return : Session with openid data
@raise SessionError : if data cannot be read
"""
authNode = self._root.find( 'authOpenIdData' )
if not authNode:
msg = "No auth data available"
raise ValueError , msg
authsession = {}
#Parse sessionData to get name and value
for child in authNode.getchildren():
if(child.tag!="#text"):
authsession[child.tag] = pickle.loads(child.text)
return authsession
def createJob(self , jobID , userName , programName , status , date , dataUsed , dataProduced ):
"""
add a new job entry in this session
@param jobID: the identifier of the job in the session ( the url without index.xml )
@type jobID: string
@param userName: the name of this job given by the user
@type userName: string
@param programName: the name of the program which generate this job
@type programName: string
@param status: the status of this job
@type status: L{Status.Status} instance
@param date: the date of job submission
@type date: time struct
@param dataUsed: the list of data stored in this session used by this job.
@type dataUsed: list of strings
@param dataProduced: the list of data stored in this session and produced by this job.
@type dataProduced: list of strings
"""
jobListNode = self._root.find( 'jobList' )
if jobListNode is None:
jobListNode = etree.Element('jobList')
self._root.append( jobListNode )
jobexist = self._doc.xpath( 'jobList/job[@id="%s"]' % jobID )
if jobexist:
raise ValueError , "can't create a new job entry in session %s :the jobID %s already exist" % ( self.getID() , jobID )
jobNode = etree.Element( 'job' , id = jobID )
self._addTextNode( jobNode, 'userName' , userName )
self._addTextNode( jobNode, 'programName' , programName )
self._addTextNode( jobNode, 'date' , strftime( "%x %X" , date ) )
self._addTextNode( jobNode, 'status' , str( status ) )
if status.message :
self._addTextNode( jobNode, 'message' , status.message )
for dataID in dataUsed:
dataUsedNode = etree.Element( "dataUsed" , ref = dataID )
jobNode.append( dataUsedNode )
for dataID in dataProduced:
dataProducedNode = etree.Element( "dataProduced" , ref = dataID )
jobNode.append( dataProducedNode )
jobListNode.append( jobNode )
self._setModified( True )
def removeJob(self , jobID ):
"""
remove the entry corresponding to this jobID
@param jobID: the identifier of the job in the session ( the url without index.xml )
@type jobID: string
@return: the job corresponding to jobID as a dict
@rtype: dictionary
@raise ValueError: if the jobID does not match any entry in this session
"""
jobNode = self._getJobNode(jobID)
jobDict = self._jobNode2jobDict( jobNode )
jobListNode = jobNode.getparent()
jobListNode.remove( jobNode )
## remove the ref of this job from the data
dataUsedNodes = jobNode.findall( 'dataUsed' )
for dataUsedNode in dataUsedNodes:
dataID = dataUsedNode.get( 'ref' )
try:
dataNode = self._getDataNode( dataID )
except ValueError:
continue
else:
try:
usedByNode = dataNode.xpath( 'usedBy[@ref="%s"]' %jobID )[0]
except IndexError:
continue
else:
dataNode.remove( usedByNode )
dataProducedNodes = jobNode.findall( 'dataProduced' )
for dataProducedNode in dataProducedNodes:
dataID = dataProducedNode.get( 'ref' )
try:
dataNode = self._getDataNode( dataID )
except ValueError:
continue
else:
try:
producedByNode = dataNode.xpath( 'producedBy[@ref="%s"]' %jobID )[0]
except IndexError:
continue
else:
dataNode.remove( producedByNode )
self._setModified( True )
return jobDict
def renameJob( self , jobID , newUserName ):
"""
change the user name of the job corresponding to the jobID.
@param jobID: the identifier of the job in the session ( url without index.xml )
@type jobID: string
@param newUserName: the new user name of the job.
@type newUserName: string
@raise ValueError: if jobID does not match any entry in session.
"""
jobNode = self._getJobNode( jobID )
modified = self._updateNode( jobNode , 'userName' , newUserName )
self._setModified( modified )
def updateJobStatus( self , jobID , status ):
"""
update the status of the job corresponding to the jobID
@param jobID: the identifier of the job in the session ( url without index.xml )
@type jobID: string
@param status: the status of this job
@type status: L{Status.Status} instance
@raise ValueError:
"""
jobNode = self._getJobNode( jobID )
modified = self._updateNode( jobNode , 'status' , str( status ) )
if status.message:
modified = self._updateNode( jobNode , 'message' , status.message )
self._setModified( modified )
def _getJobNode( self , jobID ):
"""
@param jobID: the identifier of one job in this session
@type jobID: string
@return: the Node corresponding to the jobID
@rtype: Node instance
@raise ValueError: if jobID does not match any entry in this session.
"""
try:
return self._root.xpath( 'jobList/job[@id="%s"]' % jobID )[0]
except IndexError:
msg = "the job %s does not exist in the session %s" % ( jobID , self.getID() )
raise ValueError , msg
def _jobNode2jobDict( self , jobNode ):
"""
@param jobNode: a node representing a job
@type jobNode: Node instance
"""
job = {}
job [ 'jobID' ] = jobNode.get( 'id' )
try:
job[ 'userName' ] = jobNode.find( 'userName' ).text
except AttributeError:
self._log.error( "the job %s in session %s has no userName" %( job[ 'jobID' ] , self.getID()) )
try:
job[ 'programName' ] = jobNode.find( 'programName' ).text
except AttributeError :
self._log.error( "the job %s in session %s has no programName" % ( job[ 'jobID' ] , self.getID()) )
try:
statusString = jobNode.find( 'status' ).text
except AttributeError :
msg = "the job %s in session %s has no status" % ( job[ 'jobID' ] , self.getID())
self._log.error( msg )
raise MobyleError( msg )
else:
try:
message = jobNode.find( 'message' ).text
except AttributeError:
message = ''
try:
job[ 'status' ] = Status( string= statusString , message = message )
except KeyError:
msg = "error in status %s for job %s in session %s" % ( statusString ,
job[ 'jobID' ] ,
self.getID()
)
self._log.error( msg )
raise MobyleError , msg
try:
job[ 'date' ] = strptime( jobNode.find( 'date' ).text , "%x %X")
except IndexError:
pass
job[ 'dataProduced' ] = jobNode.xpath( 'dataProduced/@ref' )
job[ 'dataUsed' ] = jobNode.xpath( 'dataUsed/@ref' )
return job
|