nixWrapper.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import sys
  2. import os
  3. import urllib3
  4. import shutil
  5. import zipfile
  6. import pathlib
  7. import json
  8. import getpass
  9. #this is meant to be used as a startup script
  10. #all commands will be executed when python -m script -i is used
  11. #generic function to load library from gitlab
  12. def getSuitePath():
  13. installDir=os.path.join(os.path.expanduser('~'),'.labkey','software','src')
  14. if not os.path.isdir(installDir):
  15. os.makedirs(installDir)
  16. return installDir
  17. def loadModule(slicer,name,moduleName):
  18. loadLibrary(name)
  19. modulePath=os.path.join(getSuitePath(),name,'slicerModule',moduleName+'.py')
  20. factoryManager = slicer.app.moduleManager().factoryManager()
  21. factoryManager.registerModule(qt.QFileInfo(modulePath))
  22. factoryManager.loadModules([moduleName,])
  23. slicer.util.selectModule(moduleName)
  24. def loadLibrary(name):
  25. #load library from git, store it at a default location and
  26. #add path to the python sys
  27. remoteSourcesURL={
  28. "labkeyInterface":\
  29. "http://wiscigt.powertheword.com/labkey/labkeyinterface/-/archive/master/labkeyinterface-master.zip"
  30. "iraeMM":
  31. }
  32. #two steps:
  33. #1 Download
  34. tempDir=os.path.join(os.path.expanduser('~'),'temp')
  35. if not os.path.isdir(tempDir):
  36. os.mkdir(tempDir)
  37. tempFile=os.path.join(tempDir,name+'.zip')
  38. http = urllib3.PoolManager()
  39. r = http.request('GET', remoteSources[name], preload_content=False)
  40. chunk_size=65536
  41. with open(tempFile, 'wb') as out:
  42. while True:
  43. data = r.read(chunk_size)
  44. if not data:
  45. break
  46. out.write(data)
  47. r.release_conn()
  48. #2 Unzip
  49. installDir=getSuitePath()
  50. #3 remove existing copy
  51. finalName=os.path.join(installDir,name)
  52. if os.path.isdir(finalName):
  53. shutil.rmtree(finalName)
  54. #unzip
  55. with zipfile.ZipFile(tempFile,'r') as zip_ref:
  56. zip_ref.extractall(installDir)
  57. #cleanup
  58. os.remove(tempFile)
  59. #rename
  60. #this is the best guess of the relation between zip directory and name
  61. zipName=name.lower()+'-master'
  62. os.rename(os.path.join(installDir,zipName),finalName)
  63. sys.path.append(finalName)
  64. def setConfig(labkeyUser,labkeyServer='https://merlin.fmf.uni-lj.si',\
  65. certificateZip=None):
  66. connectionConfig={}
  67. if certificateZip!=None:
  68. zpath=pathlib.Path(certificateZip)
  69. user=zpath.stem
  70. userDir=os.path.join(os.path.expanduser('~'),'.labkey',user)
  71. if not os.path.isdir(userDir):
  72. os.makedirs(userDir)
  73. caName=None
  74. with zipfile.ZipFile(certificateZip,'r') as zipObj:
  75. zipObj.extract(user+'.crt',userDir)
  76. zipObj.extract(user+'.key',userDir)
  77. zipList=zipObj.namelist()
  78. for f in zipList:
  79. if f.find('CA')>-1:
  80. caName=f
  81. zipObj.extract(f,userDir)
  82. #setup SSL
  83. sslSetup={}
  84. sslSetup['user']=os.path.join(userDir,user+'.crt')
  85. sslSetup['key']=os.path.join(userDir,user+'.key')
  86. sslSetup['ca']=os.path.join(userDir,caName)
  87. sslSetup['keyPwd']='notUsed'
  88. connectionConfig['SSL']=sslSetup
  89. connectionConfig["host"]=labkeyServer
  90. connectionConfig["context"]="labkey"
  91. labkeySetup={}
  92. labkeySetup['user']=labkeyUser
  93. labkeyPwd='guest'
  94. if not labkeyUser=='guest':
  95. labkeyPwd=getpass.getpass(prompt='Enter labkey password:')
  96. labkeySetup['password']=labkeyPwd
  97. connectionConfig['labkey']=labkeySetup
  98. orthancSetup={}
  99. if connectionConfig['host'].find('merlin')>-1:
  100. orthancSetup['server']='https://orthanc.fmf.uni-lj.si'
  101. if connectionConfig['host'].find('onko-nix')>-1:
  102. orthancSetup['server']='http://onko-nix.onko-i.si:8042'
  103. orthancSetup['user']='ask'
  104. orthancSetup['password']='askPassword'
  105. connectionConfig['orthanc']=orthancSetup
  106. net=labkeyInterface.labkeyInterface()
  107. net.connectionConfig=connectionConfig
  108. net.initRemote()
  109. print(net.getUserId())
  110. return net
  111. def testCertificate(certificateZip):
  112. return setConfig('guest',certificateZip=certificateZip)
  113. def testConnection(labkeyUser,labkeyServer='https://merlin.fmf.uni-lj.si',\
  114. certificateZip=None):
  115. return setConfig(labkeyUser,labkeyServer,certificateZip)
  116. def getDefaultConfig(configFile):
  117. #if configFile is None, this will set it to default and leave unchanged otherwise
  118. if configFile==None:
  119. return os.path.join(os.path.expanduser('~'),'.labkey','network.json')
  120. return configFile
  121. def storeConfig(net,configFile=None):
  122. #if configFile is None, this will set it to default and leave unchanged otherwise
  123. with open(getDefaultConfig(configFile),'w') as f:
  124. json.dump(net.connectionConfig,f,indent='\t')
  125. def getInterface(configFile=None):
  126. net=labkeyInterface.labkeyInterface()
  127. #if configFile is None, this will set it to default and leave unchanged otherwise
  128. net.init(getDefaultConfig(configFile))
  129. return net
  130. #loadLibrary('labkeyInterface')