|
@@ -0,0 +1,160 @@
|
|
|
+import sys
|
|
|
+import os
|
|
|
+import urllib3
|
|
|
+import shutil
|
|
|
+import zipfile
|
|
|
+import pathlib
|
|
|
+import json
|
|
|
+import getpass
|
|
|
+#this is meant to be used as a startup script
|
|
|
+#all commands will be executed when python -m script -i is used
|
|
|
+
|
|
|
+
|
|
|
+#generic function to load library from gitlab
|
|
|
+def getSuitePath():
|
|
|
+ installDir=os.path.join(os.path.expanduser('~'),'.labkey','software','src')
|
|
|
+ if not os.path.isdir(installDir):
|
|
|
+ os.makedirs(installDir)
|
|
|
+ return installDir
|
|
|
+
|
|
|
+
|
|
|
+def loadModule(slicer,name,moduleName):
|
|
|
+ loadLibrary(name)
|
|
|
+ modulePath=os.path.join(getSuitePath(),name,'slicerModule',moduleName+'.py')
|
|
|
+ factoryManager = slicer.app.moduleManager().factoryManager()
|
|
|
+
|
|
|
+ factoryManager.registerModule(qt.QFileInfo(modulePath))
|
|
|
+
|
|
|
+ factoryManager.loadModules([moduleName,])
|
|
|
+ slicer.util.selectModule(moduleName)
|
|
|
+
|
|
|
+
|
|
|
+def loadLibrary(name):
|
|
|
+ #load library from git, store it at a default location and
|
|
|
+ #add path to the python sys
|
|
|
+
|
|
|
+ remoteSourcesURL={
|
|
|
+ "labkeyInterface":\
|
|
|
+ "http://wiscigt.powertheword.com/labkey/labkeyinterface/-/archive/master/labkeyinterface-master.zip"
|
|
|
+ "iraeMM":
|
|
|
+
|
|
|
+ }
|
|
|
+ #two steps:
|
|
|
+ #1 Download
|
|
|
+
|
|
|
+ tempDir=os.path.join(os.path.expanduser('~'),'temp')
|
|
|
+ if not os.path.isdir(tempDir):
|
|
|
+ os.mkdir(tempDir)
|
|
|
+ tempFile=os.path.join(tempDir,name+'.zip')
|
|
|
+
|
|
|
+ http = urllib3.PoolManager()
|
|
|
+ r = http.request('GET', remoteSources[name], preload_content=False)
|
|
|
+ chunk_size=65536
|
|
|
+ with open(tempFile, 'wb') as out:
|
|
|
+ while True:
|
|
|
+ data = r.read(chunk_size)
|
|
|
+ if not data:
|
|
|
+ break
|
|
|
+ out.write(data)
|
|
|
+
|
|
|
+ r.release_conn()
|
|
|
+
|
|
|
+ #2 Unzip
|
|
|
+ installDir=getSuitePath()
|
|
|
+
|
|
|
+ #3 remove existing copy
|
|
|
+ finalName=os.path.join(installDir,name)
|
|
|
+ if os.path.isdir(finalName):
|
|
|
+ shutil.rmtree(finalName)
|
|
|
+
|
|
|
+ #unzip
|
|
|
+ with zipfile.ZipFile(tempFile,'r') as zip_ref:
|
|
|
+ zip_ref.extractall(installDir)
|
|
|
+
|
|
|
+ #cleanup
|
|
|
+ os.remove(tempFile)
|
|
|
+
|
|
|
+ #rename
|
|
|
+ #this is the best guess of the relation between zip directory and name
|
|
|
+ zipName=name.lower()+'-master'
|
|
|
+ os.rename(os.path.join(installDir,zipName),finalName)
|
|
|
+
|
|
|
+ sys.path.append(finalName)
|
|
|
+
|
|
|
+def setConfig(labkeyUser,labkeyServer='https://merlin.fmf.uni-lj.si',\
|
|
|
+ certificateZip=None):
|
|
|
+ connectionConfig={}
|
|
|
+ if certificateZip!=None:
|
|
|
+ zpath=pathlib.Path(certificateZip)
|
|
|
+ user=zpath.stem
|
|
|
+ userDir=os.path.join(os.path.expanduser('~'),'.labkey',user)
|
|
|
+ if not os.path.isdir(userDir):
|
|
|
+ os.makedirs(userDir)
|
|
|
+ caName=None
|
|
|
+ with zipfile.ZipFile(certificateZip,'r') as zipObj:
|
|
|
+ zipObj.extract(user+'.crt',userDir)
|
|
|
+ zipObj.extract(user+'.key',userDir)
|
|
|
+ zipList=zipObj.namelist()
|
|
|
+ for f in zipList:
|
|
|
+ if f.find('CA')>-1:
|
|
|
+ caName=f
|
|
|
+ zipObj.extract(f,userDir)
|
|
|
+
|
|
|
+ #setup SSL
|
|
|
+ sslSetup={}
|
|
|
+ sslSetup['user']=os.path.join(userDir,user+'.crt')
|
|
|
+ sslSetup['key']=os.path.join(userDir,user+'.key')
|
|
|
+ sslSetup['ca']=os.path.join(userDir,caName)
|
|
|
+ sslSetup['keyPwd']='notUsed'
|
|
|
+ connectionConfig['SSL']=sslSetup
|
|
|
+
|
|
|
+ connectionConfig["host"]=labkeyServer
|
|
|
+ connectionConfig["context"]="labkey"
|
|
|
+ labkeySetup={}
|
|
|
+ labkeySetup['user']=labkeyUser
|
|
|
+ labkeyPwd='guest'
|
|
|
+ if not labkeyUser=='guest':
|
|
|
+ labkeyPwd=getpass.getpass(prompt='Enter labkey password:')
|
|
|
+ labkeySetup['password']=labkeyPwd
|
|
|
+ connectionConfig['labkey']=labkeySetup
|
|
|
+
|
|
|
+ orthancSetup={}
|
|
|
+ if connectionConfig['host'].find('merlin')>-1:
|
|
|
+ orthancSetup['server']='https://orthanc.fmf.uni-lj.si'
|
|
|
+ if connectionConfig['host'].find('onko-nix')>-1:
|
|
|
+ orthancSetup['server']='http://onko-nix.onko-i.si:8042'
|
|
|
+ orthancSetup['user']='ask'
|
|
|
+ orthancSetup['password']='askPassword'
|
|
|
+ connectionConfig['orthanc']=orthancSetup
|
|
|
+ net=labkeyInterface.labkeyInterface()
|
|
|
+ net.connectionConfig=connectionConfig
|
|
|
+ net.initRemote()
|
|
|
+ print(net.getUserId())
|
|
|
+ return net
|
|
|
+
|
|
|
+def testCertificate(certificateZip):
|
|
|
+ return setConfig('guest',certificateZip=certificateZip)
|
|
|
+
|
|
|
+def testConnection(labkeyUser,labkeyServer='https://merlin.fmf.uni-lj.si',\
|
|
|
+ certificateZip=None):
|
|
|
+ return setConfig(labkeyUser,labkeyServer,certificateZip)
|
|
|
+
|
|
|
+def getDefaultConfig(configFile):
|
|
|
+ #if configFile is None, this will set it to default and leave unchanged otherwise
|
|
|
+ if configFile==None:
|
|
|
+ return os.path.join(os.path.expanduser('~'),'.labkey','network.json')
|
|
|
+ return configFile
|
|
|
+
|
|
|
+def storeConfig(net,configFile=None):
|
|
|
+ #if configFile is None, this will set it to default and leave unchanged otherwise
|
|
|
+ with open(getDefaultConfig(configFile),'w') as f:
|
|
|
+ json.dump(net.connectionConfig,f,indent='\t')
|
|
|
+
|
|
|
+def getInterface(configFile=None):
|
|
|
+ net=labkeyInterface.labkeyInterface()
|
|
|
+ #if configFile is None, this will set it to default and leave unchanged otherwise
|
|
|
+ net.init(getDefaultConfig(configFile))
|
|
|
+ return net
|
|
|
+
|
|
|
+#loadLibrary('labkeyInterface')
|
|
|
+
|