123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- import sys
- import os
- import urllib3
- import shutil
- import zipfile
- import pathlib
- import json
- import getpass
- import chardet
- import base64
- #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 buildGITURL(server,project,path,branch='master'):
- projectURL='%2f'.join(project)
- pathURL='%2f'.join(path)
- return server+'/api/v4/projects/'+projectURL+'/repository/files/'+pathURL+'?ref='+branch
- def getResources():
- server='http://wiscigt.powertheword.com'
- project=['labkey','nixsuite']
- path=['remoteResources','resources.json']
- remoteSourcesURL=buildGITURL(server,project,path)
- http = urllib3.PoolManager()
- r = http.request('GET', remoteSourcesURL)
- #returns a JSON
- encoding=chardet.detect(r.data)['encoding']
- jsonData=json.loads(r.data.decode(encoding))
- #we are interested in content, do looped way of decoding it
- b64_bytes=jsonData['content'].encode('ascii')
- m_bytes=base64.b64decode(b64_bytes)
- m=m_bytes.decode('ascii')
- return json.loads(m)
- def loadModule(slicer,qt,name,moduleName):
- loadLibrary(name)
- modulePath=os.path.join(getSuitePath(),name,'slicerModules',moduleName+'.py')
- factoryManager = slicer.app.moduleManager().factoryManager()
- factoryManager.registerModule(qt.QFileInfo(modulePath))
- factoryManager.loadModules([moduleName,])
- slicer.util.selectModule(moduleName)
- def loadLibrary(name,doReload=True):
- installDir=getSuitePath()
- finalName=os.path.join(installDir,name)
- if os.path.isdir(finalName):
- if not doReload:
- #1 keep existing copy, return
- sys.path.append(finalName)
- return
- else:
- #1 remove existing copy
- shutil.rmtree(finalName)
-
- #load library from git, store it at a default location and
- #add path to the python sys
- remoteSources=getResources()
- #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()
- rsource=remoteSources[name]
- r = http.request('GET', rsource['url'], 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
- 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
- try:
- zipName=name.lower()+'-'+rsource['branch']
- os.rename(os.path.join(installDir,zipName),finalName)
- except FileNotFoundError:
- zipName=name+'-'+rsource['branch']
- 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')
|