nixWrapper.py 5.4 KB

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