nixWrapper.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 buildGITURL(server,project,path,branch='master'):
  20. if server.find('wiscigt')>-1:
  21. projectURL='%2f'.join(project)
  22. pathURL='%2f'.join(path)
  23. return server+'/api/v4/projects/'+projectURL+'/repository/files/'+pathURL+'?ref='+branch
  24. if server.find('fmf.uni-lj.si')>-1:
  25. projectURL='/'.join(project)#studen/nixSuite
  26. pathURL='/'.join(path)
  27. return '/'.join([server,projectURL,'raw',branch,pathURL])
  28. def getResources():
  29. server='http://wiscigt.powertheword.com'
  30. project=['labkey','nixsuite']
  31. path=['remoteResources','resources.json']
  32. server='https://git0.fmf.uni-lj.si'
  33. project=['studen','nixSuite']
  34. remoteSourcesURL=buildGITURL(server,project,path)
  35. print('remoteSourcesURL {}'.format(remoteSourcesURL))
  36. http = urllib3.PoolManager()
  37. r = http.request('GET', remoteSourcesURL)
  38. #returns a JSON
  39. encoding=chardet.detect(r.data)['encoding']
  40. dataDecoded=r.data.decode(encoding)
  41. jsonData=json.loads(dataDecoded)
  42. print(jsonData)
  43. #since gogs drops raw data, we already did the hard part
  44. if server.find('fmf.uni-lj.si')>-1:
  45. return jsonData
  46. #on gitlab a further decoding step is required
  47. #we are interested in content, do looped way of decoding it
  48. b64_bytes=jsonData['content'].encode('ascii')
  49. m_bytes=base64.b64decode(b64_bytes)
  50. m=m_bytes.decode('ascii')
  51. return json.loads(m)
  52. def loadModule(slicer,qt,name,moduleName):
  53. loadLibrary(name)
  54. modulePath=os.path.join(getSuitePath(),name,'slicerModules',moduleName+'.py')
  55. factoryManager = slicer.app.moduleManager().factoryManager()
  56. factoryManager.registerModule(qt.QFileInfo(modulePath))
  57. factoryManager.loadModules([moduleName,])
  58. slicer.util.selectModule(moduleName)
  59. def loadLibrary(name,doReload=True):
  60. print('loadLibrary')
  61. installDir=getSuitePath()
  62. finalName=os.path.join(installDir,name)
  63. if os.path.isdir(finalName):
  64. if not doReload:
  65. #1 keep existing copy, return
  66. sys.path.append(finalName)
  67. return
  68. else:
  69. #1 remove existing copy
  70. shutil.rmtree(finalName)
  71. #load library from git, store it at a default location and
  72. #add path to the python sys
  73. remoteSources=getResources()
  74. #two steps:
  75. #1 Download
  76. tempDir=os.path.join(os.path.expanduser('~'),'temp')
  77. if not os.path.isdir(tempDir):
  78. os.mkdir(tempDir)
  79. tempFile=os.path.join(tempDir,name+'.zip')
  80. http = urllib3.PoolManager()
  81. rsource=remoteSources[name]
  82. print(rsource)
  83. r = http.request('GET', rsource['url'], preload_content=False)
  84. chunk_size=65536
  85. with open(tempFile, 'wb') as out:
  86. while True:
  87. data = r.read(chunk_size)
  88. if not data:
  89. break
  90. out.write(data)
  91. r.release_conn()
  92. print('File {}: {}'.format(tempFile,os.path.isfile(tempFile)))
  93. #2 Unzip
  94. with zipfile.ZipFile(tempFile,'r') as zip_ref:
  95. zip_ref.extractall(installDir)
  96. #cleanup
  97. os.remove(tempFile)
  98. #rename
  99. #this is the best guess of the relation between zip directory and name
  100. try:
  101. zipName=name.lower()+'-'+rsource['branch']
  102. os.rename(os.path.join(installDir,zipName),finalName)
  103. except FileNotFoundError:
  104. try:
  105. zipName=name+'-'+rsource['branch']
  106. os.rename(os.path.join(installDir,zipName),finalName)
  107. except FileNotFoundError:
  108. #git0/gogs
  109. zipName=name.lower()
  110. os.rename(os.path.join(installDir,zipName),finalName)
  111. sys.path.append(finalName)
  112. updateSetup(name,finalName)
  113. return finalName
  114. def updateSetup(name,path):
  115. #update setup.json
  116. #set paths[name]=finalName
  117. #for use in slicer, use .labkey/slicer/setup.json
  118. setupDir=os.path.join(os.path.expanduser('~'),'.labkey','slicer')
  119. if not os.path.isdir(setupDir):
  120. os.makedirs(setupDir)
  121. setupFile=os.path.join(setupDir,'setup.json')
  122. #if f is not there, create an empty json
  123. try:
  124. with open(setupFile,'r') as f:
  125. setup=json.load(f)
  126. except OSError:
  127. setup={}
  128. try:
  129. setup['paths'][name]=path
  130. except KeyError:
  131. setup['paths']={}
  132. setup['paths'][name]=path
  133. with open(setupFile,'w') as f:
  134. json.dump(setup,f)
  135. def setConfig(labkeyUser,labkeyServer='https://merlin.fmf.uni-lj.si',\
  136. certificateZip=None):
  137. connectionConfig={}
  138. if certificateZip!=None:
  139. zpath=pathlib.Path(certificateZip)
  140. user=zpath.stem
  141. userDir=os.path.join(os.path.expanduser('~'),'.labkey',user)
  142. if not os.path.isdir(userDir):
  143. os.makedirs(userDir)
  144. caName=None
  145. with zipfile.ZipFile(certificateZip,'r') as zipObj:
  146. zipObj.extract(user+'.crt',userDir)
  147. zipObj.extract(user+'.key',userDir)
  148. zipList=zipObj.namelist()
  149. for f in zipList:
  150. if f.find('CA')>-1:
  151. caName=f
  152. zipObj.extract(f,userDir)
  153. #setup SSL
  154. sslSetup={}
  155. sslSetup['user']=os.path.join(userDir,user+'.crt')
  156. sslSetup['key']=os.path.join(userDir,user+'.key')
  157. sslSetup['ca']=os.path.join(userDir,caName)
  158. sslSetup['keyPwd']='notUsed'
  159. connectionConfig['SSL']=sslSetup
  160. connectionConfig["host"]=labkeyServer
  161. connectionConfig["context"]="labkey"
  162. labkeySetup={}
  163. labkeySetup['user']=labkeyUser
  164. labkeyPwd='guest'
  165. if not labkeyUser=='guest':
  166. labkeyPwd=getpass.getpass(prompt='Enter labkey password:')
  167. labkeySetup['password']=labkeyPwd
  168. connectionConfig['labkey']=labkeySetup
  169. orthancSetup={}
  170. if connectionConfig['host'].find('merlin')>-1:
  171. orthancSetup['server']='https://orthanc.fmf.uni-lj.si'
  172. if connectionConfig['host'].find('onko-nix')>-1:
  173. orthancSetup['server']='http://onko-nix.onko-i.si:8042'
  174. orthancSetup['user']='ask'
  175. orthancSetup['password']='askPassword'
  176. connectionConfig['orthanc']=orthancSetup
  177. net=labkeyInterface.labkeyInterface()
  178. net.connectionConfig=connectionConfig
  179. net.initRemote()
  180. print(net.getUserId())
  181. return net
  182. def testCertificate(certificateZip):
  183. return setConfig('guest',certificateZip=certificateZip)
  184. def testConnection(labkeyUser,labkeyServer='https://merlin.fmf.uni-lj.si',\
  185. certificateZip=None):
  186. return setConfig(labkeyUser,labkeyServer,certificateZip)
  187. def getDefaultConfig(configFile):
  188. #if configFile is None, this will set it to default and leave unchanged otherwise
  189. if configFile==None:
  190. return os.path.join(os.path.expanduser('~'),'.labkey','network.json')
  191. return configFile
  192. def storeConfig(net,configFile=None):
  193. #if configFile is None, this will set it to default and leave unchanged otherwise
  194. with open(getDefaultConfig(configFile),'w') as f:
  195. json.dump(net.connectionConfig,f,indent='\t')
  196. def getInterface(configFile=None):
  197. net=labkeyInterface.labkeyInterface()
  198. #if configFile is None, this will set it to default and leave unchanged otherwise
  199. net.init(getDefaultConfig(configFile))
  200. return net
  201. #loadLibrary('labkeyInterface')