def getTrueFilePath(fnpath): ''' Find absolute file path if exists. :param: fnpath [str] - file path to check. :return: absolute path to existing file [str]. :raise: [IOError] if can't find or read. ''' fpath = fnpath.strip() resolvedPath = None assert isinstance(fpath, (str, unicode)), \ 'Error type not str: send str file path' if os.getcwd() not in fpath: fpath = opab(fpath) dfn = opb(opn(fpath)) dfdir = opd(opn(opab(fpath))) dfpath = None dirfs = os.listdir(opd(opn(opab(fpath)))) dfpath = opn(op.join(dfdir, dfn)) if dfn in dirfs and op.isfile(dfpath): # log.warn('\n dfpath {}'.format(dfpath)) resolvedPath = dfpath return resolvedPath elif _validateFileIsPath(dfpath): # log.warn('\n signle filepath return {}'.format(dfpath)) return dfpath raise IOError('file | filepath not resolved: {}'.format(fpath))
def _fetchLib(): assert compiler.isStdLib, \ 'need a Stdlib file in:\n {}'.format(compiler.stdlibsource) if not compiler.f_libembed and not compiler.isReleasedStdLib: copystr = ['cmd', '/C', 'copy', compiler.stdlibsource, \ opd(compiler.stdlibrelease)] if _subprocCopy(copystr, compiler.stdlibsource): log.FILE('Copied {}'.format(compiler.stdlibrelease)) if not compiler.haveStdLib: copystr = ['cmd', '/C', 'copy', compiler.stdlibsource, os.getcwd()] if _subprocCopy(copystr, compiler.stdlibsource): log.FILE('Copied {}'.format(opj(os.getcwd(), 'StdLib.dll'))) libsaved = [] if op.isfile(compiler.stdlibsource): libsaved.append(compiler.stdlibsource) if op.isfile(compiler.stdlibrelease): libsaved.append(compiler.stdlibrelease) if op.isfile(opj(os.getcwd(), 'StdLib.dll')): libsaved.append(opj(os.getcwd(), 'StdLib.dll')) if gsBuild.Verbose: log.info(('\nStdLib exists/saved in:\n' + '{}\n'*len(libsaved)) \ .format(*libsaved)) return raise NotImplementedError('Need ironpython 2.7 distribution ' + \ 'in something like ../ironpython path')
def _subprocPYC(strCmd, cmpfile, dotExt='.dll'): clr = None # keep out global clr try: import clr sys.path.append(oprel(compiler.pycpath)) import pyc except Exception as exi: pass args = None if clr: args = strCmd[2:] try: pyc.Main(args) except Exception as ex: if ex: print('type', type(ex)) try: print(ex) print(ex.message) except Exception: pass log.warn('pyc.Main err:\n' + ex.ToString()) else: log.warn('pyc.Main err:\n') return False else: po = subprocess.Popen(strCmd, stdout=PIPE, stderr=PIPE) stdout, stderr = po.communicate() po.wait() if stderr: log.warn('ERR\n {}:\n\t compiling with {}'.format(stderr, cmpfile)) po.kill() return False else: if gsBuild.Verbose or not gsBuild.INFO: log.info('\n - STDOUT - \n{}'.format(stdout)) po.kill() if dotExt and opex( opj(opd(compiler.mainoutdir), opb(cmpfile).replace('.py', dotExt))): log.FILE('Build Created: {}'.format(cmpfile.replace('.py', dotExt))) elif opex(cmpfile): log.FILE('Build Created: {}'.format(cmpfile)) else: return False return True
def SlashArgs(config, name, jsn, out): ''' Runs SlashControl and BasePath for each user path arg: :params: (configPath, mainName, jsonp, outDir) :return: (argc, argm, argj, argo) [tuple] ''' argc = argm = argj = argo = None if config == ' ': config = None if config: config = SlashContrl(config) argc = BasePathDir(config) if name: name = SlashContrl(name) argm = BasePathDir(name) if jsn: jsn = SlashContrl(jsn) argj = BasePathDir(jsn) if not out or out == '' or out == ' ': if argm and argm[0]: out = argm[0] + '\\release' elif 'builder' == opb(opd(os.getcwd())): out = os.getcwd() + '\\UserDefaulted\\release' else: out = os.getcwd() + '\\release' out = SlashContrl(out) if not op.isdir(out): if not op.isdir(opd(out)): os.mkdir(opd(out)) log.FILE('{}'.format(opd(out))) if not op.isdir(out) and not op.isfile(out): os.mkdir(out) log.FILE('{}'.format(out)) argo = BasePathDir(out) return (argc, argm, argj, argo)
def registerTemplates(_file_, templateDir='templates'): """ Add a template directory to the template search path. In __init__.py: registerTemplates(__file__) registerTemplates(__file__, 'templates') """ path = opn(opj(opd(_file_), templateDir)) templatePaths.append(path) print "templates:", path
def registerStatic(_file_, staticDir='static'): """ Add a directory of static files to the web server. In __init__.py: registerStatic(__file__) registerStatic(__file__, 'static') """ path = opn(opj(opd(_file_), staticDir)) staticPaths.append(path) print "static:", path
def FindIronPython(): ''' Walk directories to find IronPython Install :return: IronPython install path [str] - or - if not main sets gsBuild.IPATH :raise: NotImplementedError ''' clr = None try: import clr except Exception as ex: pass SEARCHUSER = opn(os.path.expanduser('~')) SEARCHCWD = opd(os.getcwd()) if 'Tests' in os.getcwd(): SEARCHCWD = opd(SEARCHCWD) SEARCHMODSRC = opn(opd(opd(os.__file__))) SEARCHROOT = os.path.splitdrive(SEARCHCWD)[0] searchList = [SEARCHCWD, SEARCHUSER, SEARCHMODSRC, SEARCHROOT] for direct in searchList: for root, dirs, files in os.walk(direct, topdown=False): for name in files: if gsBuild.IRONPYTHON in name: with open(opn(opab(opj(os.getcwd(), 'defaults\\ipath.txt'))), 'w') as tw: tw.write(opd(opn(opj(root, name)))) return opd(opn(opj(root, name))) if __name__ == '__main__': raise NotImplementedError('Failed to Find/*Access' + \ ' loadable IronPython Distribution') if clr: #print str(clr.References) with open(opn(opab(opj(os.getcwd(), 'defaults\\ipath.txt'))), 'w') as tw: tw.write('clr') print 'Err in clr req' return 'clr' if not clr:# or (clr and clr.References and not any('Iron' in clref for clref in str(clr.References).split(','))): log.warn(('\nFailed to find loadable IronPython Distribution:\n Used "{}"' + \ ' name to search for directory.\n\n' + ' Searched ordered from base:\n {}\n {}\n {}\n {}\n') \ .format(gsBuild.IRONPYTHON, *searchList)) log.info('\nCheck Access and/or install IronPython to loadable path.') raise FatalError('NotImplementedError', 'Failed to Find/*Access' + \ ' loadable IronPython Distribution') return
def checkRequiredIP(): if gsBuild.IPATH: return gsBuild.HAVELIB gsBuild.HAVELIBDLL ironPythonPath = FindIronPython() if opex(ironPythonPath): gsBuild.IPATH = ironPythonPath userReqBaseLst = os.listdir(ironPythonPath) userReqLst = [ opn(opj(ironPythonPath, urf)) for urf in os.listdir(ironPythonPath) ] if opex(opj(ironPythonPath, 'Lib')): log.FILE('Exists: {}'.format(opj(ironPythonPath, 'Lib'))) gsBuild.HAVELIB = True # TODO check for downloaded StdLib if op.isfile(opn(opj(ironPythonPath, 'StdLib.dll'))): gsBuild.HAVELIBDLL = True if not all(rf in userReqBaseLst for rf in DefaultReqList): try: raise NotImplementedError except NotImplementedError as ex: msg = 'Failed to find all required IronPython Distribution files' partialError(ex, msg) log.FILE('\n Exists required path {}'.format( opj(os.getcwd(), gsBuild.requiredPath))) WD = os.getcwd() if 'Tests' in WD: WD = opd(WD) with open(opj(os.getcwd(), gsBuild.requiredPath), 'w') as tw: for f in userReqLst: relp = opn(op.relpath(f)) # skip dirs if op.isfile(relp): tw.write(opn(os.path.relpath(f)) + '\n') log.FILE('Exists {}'.format(gsBuild.requiredPath)) return
def searchpy(current): while 1: last = current initpy = join(current, '__init__.py') if not exists(initpy): pydir = join(current, 'py') # recognize py-package and ensure it is importable if exists(pydir) and exists(join(pydir, '__init__.py')): #for p in sys.path: # if p == current: # return True if current != sys.path[0]: # if we are already first, then ok print >> sys.stderr, "inserting into sys.path:", current sys.path.insert(0, current) return True current = opd(current) if last == current: return False
def searchpy(current): while 1: last = current initpy = join(current, '__init__.py') if not exists(initpy): pydir = join(current, 'py') # recognize py-package and ensure it is importable if exists(pydir) and exists(join(pydir, '__init__.py')): #for p in sys.path: # if p == current: # return True if current != sys.path[0]: # if we are already first, then ok print >>sys.stderr, "inserting into sys.path:", current sys.path.insert(0, current) return True current = opd(current) if last == current: return False
def _setupBaseDll(config, dllNames, exedllName): gbm = [] gbm.append("/main:" + exedllName) if isinstance(compiler.lstexe, list): for resfile in compiler.lstexe: if '.py' in resfile: gbm.append(resfile) if dllNames: for dll in dllNames: gbm.append(dll) gbm.append("/out:" + opd(compiler.mainoutdir)) gbm.append("/target:dll") return list(gbm)
def _fetchLib(): if gsBuild.IPATH != 'clr': assert compiler.isStdLib, \ 'need a Stdlib file in:\n {}'.format(compiler.stdlibsource) if not compiler.f_libembed and not compiler.isReleasedStdLib: if compiler.stdlibsource: copystr = ['cmd', '/C', 'copy', compiler.stdlibsource, \ opd(compiler.stdlibrelease)] if _subprocCopy(copystr,compiler.stdlibsource): log.FILE('Copied {}'.format(compiler.stdlibrelease)) if not compiler.haveStdLib and compiler.stdlibsource: copystr = ['cmd', '/C', 'copy', compiler.stdlibsource, os.getcwd()] if _subprocCopy(copystr,compiler.stdlibsource): log.FILE('Copied {}'.format(opj(os.getcwd(), 'StdLib.dll'))) libsaved = [] if compiler.stdlibsource and op.isfile(compiler.stdlibsource): libsaved.append(compiler.stdlibsource) if compiler.stdlibrelease and op.isfile(compiler.stdlibrelease): libsaved.append(compiler.stdlibrelease) if op.isfile(opj(os.getcwd(), 'StdLib.dll')): libsaved.append(opj(os.getcwd(), 'StdLib.dll')) if gsBuild.Verbose and libsaved: log.info(('\nStdLib exists/saved in:\n' + '{}\n'*len(libsaved)) \ .format(*libsaved)) return elif libsaved: return elif clr: log.error("\nCouldn't create StdLib - Continuing, will try " + \ "\ninternal lib: exe fail = Need ironpython 2.7 distribution") return raise NotImplementedError('Need ironpython 2.7 distribution ' + \ 'in something like ../ironpython path')
def zipDirGCA(zips): ops = os.path.splitdrive zdirs = dict(zipDrives(zips)) drives = zdirs.keys() for drv in drives: drvzips = [] for zfl in zips: curFileDrive = ops(zfl)[0] if curFileDrive == drv: drvzips.append(zfl) root = opd(op.commonprefix(drvzips)) if zdirs[drv]['baseroot']: zdirs[drv]['GCA'] = root else: # log.error('\n wrote GCA {}'.format('from_' + drv.replace(':','') + ops(root)[1])) zdirs[drv]['GCA'] = 'from_' + drv.replace(':', '') + ops(root)[1] return zdirs
def __autoimport(use_only=None): """ Import all submodules with a defined `__all__` attribute from this directory level. An explicit sequence of modules can be provided in the `use_only` parameter to limit the import to these files. """ import os from os.path import join as opj, dirname as opd ismodule = lambda x: x.endswith('.py') and not x.startswith( '__init__') and not x.startswith('setup') here = opd(__file__) ret = [] modules = [os.path.splitext(i)[0] for i in os.listdir(here) if ismodule(i)] if use_only: modules = [i for i in modules if i in use_only] for module in modules: tmp = __import__(module, globals=globals(), fromlist=['*'], level=1) if hasattr(tmp, '__all__'): ret += tmp.__all__ for name in tmp.__all__: globals()[name] = vars(tmp)[name] return ret
import sys from os.path import dirname as opd, realpath as opr import os basedir = opd(opd(opr(__file__))) sys.path.append(os.path.join(basedir, "src")) from sharkradar.Config.Config import Config def test_001_get_dbpath_without_set(): """ Test blank DB Path """ assert Config.getDbPath() == "" def test_002_get_dbpath_with_set(): """ Test blank DB Path """ Config.setDbPath("ABC") assert Config.getDbPath() == "ABC/sharkradar_service.db" def test_002_get_algo_without_set(): """ Test blank Algo """ assert Config.getAlgorithm() == "" def test_002_get_algo_with_set(): """ Test blank Algo """ Config.setAlgorithm("ABC") assert Config.getAlgorithm() == "ABC"
def _subprocPYC(strCmd, cmpfile, dotExt='.dll'): clr = None # keep out global clr try: import clr except Exception as ex: pass if clr: try: sys.path.append(oprel(compiler.pycpath)) import pyc except Exception as ex: pass try: clr.AddReference("StdLib") except System.IO.IOException as ex: print('StdLib.dll reference error:\n\t' + \ 'check file | filepath') try: clr.AddReference("IronPython") except System.IO.IOException as ex: print('IronPython reference error:\n\t' + \ 'check file | filepath') f_ipy = False try: import ipyver rs = ipyver.ReferenceStatus() f_ipy = rs.RefStatusIPMS()['ipy']['isLocal'] except System.IO.IOException as ex: pass try: clr.AddReference("ipybuild") except System.IO.IOException as ex: try: clr.AddReference("ipybuilder") except System.IO.IOException as ex: if f_ipy: print('IF .exe: ipybuild(er) reference error:\n\t' + \ 'check file | filepath') args = None rslt = None args = strCmd[2:] try: rslt = pyc.Main(args) except Exception as ex: errtyp = '' gsBuild.OK = False try: errtyp = ex.GetType().ToString() except Exception as exf: pass if ex: log.warn('pyc.Main err:\n' + ex.ToString()) log.warn('type {} or System Type {}'.format(type(ex), errtyp)) log.warn('Error: {}'.format(ex)) print 'ex-err' return False if rslt: return True else: po = subprocess.Popen(strCmd, stdout=PIPE, stderr=PIPE) stdout, stderr = po.communicate() po.wait() if stderr: log.warn('ERR\n {}:\n\t compiling with {}'.format(stderr, cmpfile)) po.kill() gsBuild.OK=False return False else: if gsBuild.Verbose or not gsBuild.INFO: log.info('\n - STDOUT - \n{}'.format(stdout)) po.kill() if dotExt and opex(opj(opd(compiler.mainoutdir), opb(cmpfile).replace('.py', dotExt))): log.FILE('Build Created: {}'.format(cmpfile.replace('.py', dotExt))) return True elif opex(cmpfile): log.FILE('Build Created: {}'.format(cmpfile)) return True else: gsBuild.OK = False return False
from os.path import dirname as opd, exists, join, basename, abspath def searchpy(current): while 1: last = current initpy = join(current, '__init__.py') if not exists(initpy): pydir = join(current, 'py') # recognize py-package and ensure it is importable if exists(pydir) and exists(join(pydir, '__init__.py')): #for p in sys.path: # if p == current: # return True if current != sys.path[0]: # if we are already first, then ok print >>sys.stderr, "inserting into sys.path:", current sys.path.insert(0, current) return True current = opd(current) if last == current: return False if not searchpy(abspath(os.curdir)): if not searchpy(opd(abspath(sys.argv[0]))): if not searchpy(opd(__file__)): pass # let's hope it is just on sys.path import py if __name__ == '__main__': print "py lib is at", py.__file__
def makeBuild(config): ''' Compile the exe or dll files based on: - user args - user assembly flags (f_embed, f_libembed, f_standalone) ''' _setCompilerClass(config) if gsBuild.Verbose: log.info('\n compiler {}'.format( json.dumps(compiler, default=lambda x: x.__dict__, indent=4))) _fetchLib() if compiler.f_parerr: return False gbm = [] dllNames = list(createUserLibs(config)) exedllName = opn( opj(compiler.mainoutdir, compiler.mainout).replace('.exe', '.dll')) # if dllNames: # log.error(('\n dllNames:\n ' + '{}\n'*len(dllNames)).format(*dllNames)) # log.error('\n exedllName:{}'.format(exedllName)) # std stub useless unless just an exe way to run a few single python modules if compiler.ext.replace('.', '') == 'exe': gbm = gbm + _setupBaseExe(config, dllNames) if gsBuild.Verbose: gbm = gbm + ['-v'] # pass verbose to pyc ipystr = [compiler.ipath + '/ipy '] + [compiler.pycpath] + gbm _subprocPYC(ipystr, compiler.mainfile, dotExt='.exe') # ADD, SO DELETED if compiler.f_embed or compiler.f_standalone: dllNames.append(compiler.mainoutdir + '.dll') if os.path.isfile(compiler.mainoutdir + '.IPDLL'): try: os.remove(compiler.mainoutdir + '.IPDLL') except (IOError, Exception) as ex: if ex == IOError: pass elif ex == System.IO.IOException: pass else: raise ex if (compiler.f_embed or compiler.f_standalone) and compiler.f_libembed: if os.path.isfile(opd(compiler.mainoutdir) + 'StdLib.dll'): try: os.remove(opd(compiler.mainoutdir) + 'StdLib.dll') except IOError as ex: pass if gsBuild.Verbose or not gsBuild.INFO: pycInfo() #pyc does not send .exe to mainoutdir if 'dll' not in compiler.ext: movestr = [ 'cmd', '/C', 'move', compiler.pycdir, opn(opd(compiler.mainoutdir) + '\\') ] _subprocMove(movestr, compiler.pycdir, compiler.mainoutdir, compiler.ext) if compiler.f_embed or compiler.f_standalone: for dll in dllNames: if os.path.isfile(dll) and '.dll' in dll and\ 'IronPython' not in dll: #some ver deleted probably not needed try: os.remove(dll) except (IOError, Exception) as ex: if ex == IOError: pass elif ex == System.IO.IOException: pass else: raise ex #TODO debug simple dll compile elif compiler.ext.replace('.', '') == 'dll': _setupBaseDll(config, dllNames, exedllName)
def searchpy(current): while 1: last = current initpy = join(current, '__init__.py') if not exists(initpy): pydir = join(current, 'py') # recognize py-package and ensure it is importable if exists(pydir) and exists(join(pydir, '__init__.py')): #for p in sys.path: # if p == current: # return True if current != sys.path[0]: # if we are already first, then ok print >> sys.stderr, "inserting into sys.path:", current sys.path.insert(0, current) return True current = opd(current) if last == current: return False if not searchpy(abspath(os.curdir)): if not searchpy(opd(abspath(sys.argv[0]))): if not searchpy(opd(__file__)): pass # let's hope it is just on sys.path import py if __name__ == '__main__': print "py lib is at", py.__file__
def createUserLibs(config): ''' Loads files from user arg "listdll". If .py file creates .dll then/else adds created or listdll .dll file to to compiler config.dlls. If assembly f_embed or f_standalone: is true: Agglomerates all dll libraries into one dll that is embeded and then removed from user arg "outDir" or or auto-named outdir. else: Add each lib .dll file to "outDir" or outdir. ''' dllNames = [] if not compiler.lstdll: return [] dllName = None gb = [] if isinstance(compiler.lstdll, list): for resfile in compiler.lstdll: if '.py' not in resfile: #skip compile if '.dll' in resfile: dllNames.append(resfile) continue if resfile and '.py' in resfile: dllName = opj(opd(compiler.mainoutdir), opb(resfile).replace('.py', '')) dllNames.append(dllName + '.dll') gb.append(resfile) if not compiler.f_embed and not compiler.f_standalone: gb.extend(_getAssembly(config)) gb.append("/out:" + dllName) gb.append("/target:dll") gb = nullList(gb) ipystr = [compiler.ipath + '/ipy'] + [compiler.pycpath] + gb # for gbs in gb: # log.warn('\ndll gbs {}'.format(gbs)) _subprocPYC(ipystr, dllName, '.dll') gb = [] continue # log.error('\n conpiled dllNames:\n {}'.format(dllNames)) # make one lib dll to embed if compiler.f_embed or compiler.f_standalone: dllNames = [] gb.extend(_getAssembly(config)) dllName = opj(opd(compiler.mainoutdir), ('.').join(compiler.mainout.split('.')[:-1]) + 'DLL') dllNames.append(dllName + '.dll') gb.append("/out:" + dllName) gb.append("/target:dll") gb = nullList(gb) ipystr = [compiler.ipath + '/ipy'] + [compiler.pycpath] + gb _subprocPYC(ipystr, dllName, '.dll') # if not dllName: # dllName = opj(opd(compiler.mainoutdir), # ('.').join(compiler.mainout.split('.')[:-1])) + 'DLL.dll' # log.error('\n returing dllnames {}'.format(dllNames)) return dllNames return None
import pytest import re import sys import traceback from f5_openstack_agent.lbaasv2.drivers.bigip.icontrol_driver import \ iControlDriver from .bigip_interaction import BigIpInteraction from .testlib.bigip_client import BigIpClient from .testlib.fake_rpc import FakeRPCPlugin from .testlib.resource_validator import ResourceValidator DEBUG = True # sys.flags.debug DATADIR = opj(opd(opd(os.path.abspath(__file__))), "testdata") CONFIGDIR = opj(opd(DATADIR), "config") ESD_dir = opj(DATADIR, "esds") req_services_dir = opj(DATADIR, "service_requests") OSLOCONF = 'overcloud_basic_agent_config.json' class TestConfig(object): """A configuration tool, Experiment fixtures use the public interface.""" def __init__(self, service_requests_file, oslo_config_file): """Neutronless tests expect the state specified here. self.oslo_config: A faked interface to the oslo configuration system
# -*- coding: utf-8 -*- import sys import numpy as np from os.path import dirname as opd from os.path import abspath as opa TEST_PATH = opa(opd(opd(__file__))) PRJ_PATH = opd(TEST_PATH) sys.path.insert(0, PRJ_PATH) from pycontour.transform import smooth_cnt from pycontour.img import build_cnt_mask def test_smooth_cnt(): cnt1 = np.array([[300, 400, 450, 400, 300, 200, 0, 50, 100, 200], [100, 100, 200, 300, 400, 500, 500, 400, 300, 200]]) # img1 = build_cnt_mask(cnt1) smooth_cnt1 = smooth_cnt(cnt1, sigma=10) smooth_img1 = build_cnt_mask(smooth_cnt1) import matplotlib.pyplot as plt plt.imshow(smooth_img1)
def CheckConfig(uconfig): ''' Check for valid config entries after parse/loading :param: config Dict from LoadConfig [OrderedDict] :return: userconfig [config Dict] - as written to file path shown in writefiles.log :raise: FatalError or partialError [Exception] or [error sink list] *user can re-run ipybuild on a partialError fix* *why FatalError - a user messed with defaults - error vs program error* ''' userfig = ndcDict(uconfig) if not len([val for val in userfig if val or val != '']) >= 1: raise ValueError('need at least one parameter for config') if not userfig['MAKEDIR'] or not opex(opd(userfig['MAKEDIR'])): raise NameError('bad path to config file {}' \ .format(opd(userfig['MAKEDIR']))) if not all(k in userfig.keys() for k in defaultconfig.keys()): log.error('\n keys {}\n\t{}'.format(str(userfig.keys()), \ str(defaultconfig.keys()))) raise FatalError('KeyError', 'bad userconfig key set') if not userfig['MAINFILE'] or userfig['MAINFILE'] == '': raise FatalError('NameError', 'need main name.py') if not '.py' in userfig['MAINFILE'] or not opex(userfig['MAINFILE']): try: raise NameError except NameError as ex: msg = ' main name "{}" must be .py file and exist' \ .format(userfig['MAINFILE']) partialError(ex, msg) if not isinstance(userfig['MAKEEXE'], bool) and \ str(userfig['MAKEEXE']).upper() not in ['TRUE', 'FALSE']: try: raise ValueError except ValueError as ex: msg = 'makeEXE {}: need type bool in params (no key needed)' \ .format(userfig['MAKEEXE']) partialError(ex, msg) if not opex(userfig['JSONPATH']): try: raise NameError except NameError as ex: msg = 'bad path to assembly.json file "{}"' \ .format(userfig['JSONPATH']) partialError(ex, msg) if not opex(userfig['CONFIGPATH']): try: raise NameError except NameError as ex: msg = 'bad path to assembly.json file "{}"' \ .format(userfig['CONFIGPATH']) partialError(ex, msg)
log = dynfile(__name__) log.debug('\n---------------- ' + str(__name__) + \ ' loaded --------------') if not opex(opn(opab(opj(os.getcwd(), 'defaults')))): print('no exist {}'.format(opn(opab(opj(os.getcwd(), 'defaults\\'))))) try: os.mkdir(opn(opab(opj(os.getcwd(), 'defaults')))) except Exception as ex: print(ex) #opn is key to run in clr? defaultcfg = opn(opj(os.getcwd(), 'defaults\\default_config.config')) defaultjson = opn(opj(os.getcwd(), 'defaults\\default_assembly.json')) if 'Tests' in os.getcwd(): defaultjson = opj(opd(os.getcwd()), 'defaults\\default_assembly.json') defaultcfg = opj(opd(os.getcwd()), 'defaults\\default_config.config') defaultconfig = { 'MAKEEXE': False, 'MAKEDIR': None, 'OUTDIR': None, 'MAINFILE': None, 'LISTFILES': { 'exe': [None], 'dll': [None], 'zip': [None] }, 'JSONPATH': None, 'ASSEMBLY': { "standalone": False,
def BasePathDir(dp): ''' Parse a file path and return dict of info about path :param: dp [str] - user arg path :return: - (main, base, basetype, isFile, isdefault) [tuple] - main [str] - fullpath parent - base [str] - base of path dir or file path - basetype [basetype [python, json, config, None]] - isFile [bool] - isdefault [ bool] - True if output is going to UserDefaulted/ ''' dp = dp.strip() base = None main = None dpex = op.exists(opab(dp)) main = opd(dp) mainex = op.exists(main) base = opb(dp) hasdot = '.' in base basetype = None isFile = False isdefault = False if dpex: isFile = op.isfile(opab(dp)) if hasdot: if '.py' in base: basetype = 'python' elif '.json' in base: basetype = 'json' elif '.config' in base: basetype = 'config' if (opb(main)) == 'builder': main = opn(opj(main, 'UserDefaulted')) isdefault = True if not hasdot and base == opd(main): return (main, None, None, isFile, isdefault) elif not hasdot and base in os.getcwd(): if base == 'builder': base = opn(opj(base, 'UserDefaulted')) isdefault = True return (opn(opj(opab(main), base)), None, None, isFile, isdefault) elif not mainex: if op.exists(opn(opab(opd(main)))): isdefault = True return (opn(opj(opab(opd(main)), 'UserDefaulted')), base, basetype, isFile, isdefault) isdefault = True return (opn(opab(opj(opd(main), 'UserDefaulted'))), base, basetype, isFile, isdefault) return (main.strip(), base.strip(), basetype, isFile, isdefault)
def _setCompilerClass(rconfig): config = ndcDict(rconfig) f_standalone = None f_embed = None f_libembed = None f_parerr = None # f_parerr - Stop build on partialError that is Fatal to Compile # Let makeBuild finish so that user can fix partialErrs with open(config['CONFIGPATH'], 'r') as jbcr: config = ndcDict(json.load(jbcr)) if gsBuild.Verbose or not gsBuild.INFO: log.info('\n Building from CONFIG:\n {}\n'.format( json.dumps(config, indent=4))) if not opex(config['MAINFILE']): try: raise IOError except IOError as ex: msg = 'File Filepath does Not exist:\n "{}"' \ .format(config['MAINFILE']) partialError(ex, msg) f_parerr = True if not f_parerr: log.FILE('Build Loaded: {}'.format(config['MAINFILE'])) assemInfo = config['ASSEMBLY'] if isinstance(assemInfo['standalone'], bool) or \ str(assemInfo['standalone']).upper() in ['TRUE', 'FALSE']: f_standalone = True if str(assemInfo['standalone']).upper() == \ 'TRUE' else False if isinstance(assemInfo['embed'], bool) or \ str(assemInfo['embed']).upper() in ['TRUE', 'FALSE']: f_embed = True if str(assemInfo['embed']).upper() == 'TRUE' else False if isinstance(assemInfo['libembed'], bool) or \ str(assemInfo['libembed']).upper() in ['TRUE', 'FALSE']: f_libembed = True if str(assemInfo['libembed']).upper() \ == 'TRUE' else False ext = '.dll' if config['MAKEEXE'] == True or \ str(config['MAKEEXE']).upper() == 'TRUE': ext = '.exe' if f_standalone and not config['MAKEEXE']: log.warn('\n** Switching to exe /stanalone == true in Assembly:' + \ '\n {}\n Overrides default or makeEXE input arg == False' \ .format(config['JSONPATH'])) MAINOUT = opn(opj(config['OUTDIR'], ('.').join(opb(config['MAINFILE']) \ .split('.')[:-1])) + ext) IPATH = gsBuild.IPATH STDLIBSOURCE = opabs(opj(IPATH, 'StdLib.dll')) LIBPATH = opabs(opj(IPATH, 'Lib')) compiler.pycpath = (opn(opd(opabs(gsBuild.IPYBLDPATH)))) + '\pyc.py' compiler.stdlibsource = STDLIBSOURCE compiler.ipath = IPATH compiler.libpath = LIBPATH if not op.isfile(STDLIBSOURCE): _createStdLib() MAINOUTDIR = ('.').join(MAINOUT.split('.')[:-1]) PYCDIR = opn(opj(os.getcwd(), opb(MAINOUTDIR)) + ext) STDLIBRELEASE = opj(opd(MAINOUTDIR), 'StdLib.dll') MAINFILE = config['MAINFILE'] isLib = opex(LIBPATH) isStdLib = op.isfile(STDLIBSOURCE) haveStdLib = op.isfile(opj(os.getcwd(), 'StdLib.dll')) isReleasedStdLib = op.isfile(STDLIBRELEASE) lstdll = [] if config['LISTFILES']['dll']: if isinstance(config['LISTFILES']['dll'], list): for lfile in config['LISTFILES']['dll']: if lfile and '__init__' not in lfile: lstdll.append(lfile) else: lstdll.append(config['LISTFILES']['dll']) lstexe = [] if config['LISTFILES']['exe']: if isinstance(config['LISTFILES']['exe'], list): for xfile in config['LISTFILES']['exe']: if xfile and '__init__' not in xfile: lstexe.append(xfile) else: lstexe.append(config['LISTFILES']['exe']) lstexe = nullList(lstexe) compiler.f_standalone = f_standalone compiler.f_embed = f_embed compiler.f_libembed = f_libembed compiler.f_parerr = f_parerr compiler.mainout = MAINOUT compiler.ipath = IPATH compiler.mainoutdir = MAINOUTDIR compiler.pycdir = PYCDIR compiler.stdlibrelease = STDLIBRELEASE compiler.stdlibsource = STDLIBSOURCE compiler.libpath = LIBPATH compiler.mainfile = MAINFILE compiler.isLib = isLib compiler.isStdLib = isStdLib compiler.haveStdLib = haveStdLib compiler.isReleasedStdLib = isReleasedStdLib compiler.lstdll = lstdll compiler.lstexe = lstexe compiler.ext = ext compiler.lstexedlls = None if not opex(opd(compiler.pycdir)): raise IOError('FilePath {}:\t Use absolute or relative to:\n\t {}' \ .format(opd(compiler.pycdir), os.getcwd())) if compiler.f_standalone: if gsBuild.Verbose or not gsBuild.INFO: log.info('\nNew {} compile standalone from:\n {}' \ .format(ext.upper().replace('.', ''), config['MAINFILE'])) else: mfn = 'application/lib' if config['MAINFILE']: mfn = opb(config['MAINFILE']) if gsBuild.Verbose or not gsBuild.INFO: log.info(("\nNew {} compile from: \n {}" + \ "\n\tAs Required: add your {}, project, and ironpython"+ \ "\n\tdll(s) to path:\n\t{}\n\n") .format(ext.upper().replace('.', ''), config['MAINFILE'], mfn, config['OUTDIR'])) if gsBuild.Verbose or not gsBuild.INFO: log.info('\n Lib source path {}'.format(LIBPATH)) log.info('\n "IF" set "True", f_libembed adds ~23mb to file:' + \ 'now set as {}'.format(compiler.f_libembed)) if compiler.f_libembed and compiler.isStdLib: if gsBuild.Verbose or not gsBuild.INFO: if compiler.isReleasedStdLib: log.info('\nOK - "StdLib.dll" exists delete'+ \ ' or move to update:\n{}'.format(STDLIBRELEASE)) else: log.info('\nOK - "StdLib.dll" exists delete'+ \ ' or move to update:\n{}'.format(STDLIBSOURCE)) elif not compiler.isStdLib and compiler.f_libembed and \ not compiler.isReleasedStdLib and compiler.isLib: _createStdLib() if not compiler.isStdLib: raise NotImplementedError('StdLib: Need ironpython2.7 distribution' + \ ' in something like ../ironpython path')
from os.path import join as opj from os.path import dirname as opd from flask import Flask, request, Response from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.admin.contrib.sqla import ModelView from flask.ext.restful import Resource, Api app = Flask(__name__, template_folder=opj(opd(__file__), '..', 'templates'), static_folder=opj(opd(__file__), '..', 'static')) import config app.config.from_object(config) db = SQLAlchemy(app) from vicequiz.models import * from vicequiz.views import *
# -*- coding: utf-8 -*- # __author__ = 'XingHuan' # 6/17/2018 from os.path import dirname as opd from os.path import join as opj from sins.module.sqt import QSettings, to_unicode # Global_Setting = QSettings(QSettings.IniFormat, QSettings.SystemScope, 'Sins', 'sins') Global_Setting = QSettings(opj(opd(opd(opd(opd(__file__)))), 'sins.ini'), QSettings.IniFormat) User_Setting = QSettings(QSettings.IniFormat, QSettings.UserScope, 'Sins', 'sins') def setting_to_unicode(setting): if isinstance(setting, basestring): return to_unicode(setting) else: return to_unicode(setting.toString()) def setting_to_int(setting): if isinstance(setting, basestring): return int(setting) else: return setting.toInt()[0] def convert_setting(setting, to_type='unicode'): if to_type == 'unicode': return setting_to_unicode(setting) elif to_type == 'int':
""" API endpoints definition related functions for the project """ import sys from os.path import dirname as opd, realpath as opr import os import time import json from flask import Flask, request, render_template basedir = opd(opd(opd(opr(__file__)))) sys.path.append(basedir) from sharkradar.Util import sharkradarDbutils from sharkradar.Service.Health import Health from sharkradar.Service.Discovery import Discovery from sharkradar.Service.MonitorRealTime import MonitorRealTime sharkradarDbutils.createTableIfNotExists() STATIC_DIR = os.path.join(opd(opd(opr(__file__))), 'NonPy_UI_Build') app = Flask(__name__, static_folder=os.path.join(STATIC_DIR, 'build/static'), template_folder=os.path.join(STATIC_DIR, 'build')) @app.route("/health", methods=['PUT']) def health(): """ API Endpoint to send health report of micro-services to Service R/D @method: PUT @params: A json body containing the following key-value pairs about a micro-service.
import pytorch_lightning as pl import hydra import os from os.path import join as opj from os.path import dirname as opd from Data import DataModule from Model import ModelModule @hydra.main(config_path=opj(opd(__file__),'opt'),config_name='train') def main(opt): pl.seed_everything(1) data=DataModule(opt.Data) model = ModelModule(opt.Model) trainer = pl.Trainer(**opt.trainer) trainer.fit(model,data) trainer.test(datamodule=data) if __name__ == '__main__': main()
import sys import os from pathlib import Path from os.path import dirname as opd from os.path import realpath as opr sys.path.insert(0, opd(opd(opr(__file__)))) # place /path/to/hoad/ in sys.path import csv import pickle import numpy as np from imitator_agents.data_creation_wrappers.create_agent_data import DataCreator import argparse parser = argparse.ArgumentParser() parser.add_argument('--num_games', '--n', type=int, default=25000, help='The number of games to run per agent-agent combo.') parser.add_argument( '--agent', '--a', type=str, default='iggi', help='Imitator agent for which to create data. Must have model' 'present in ./saved_models/<agent>.save/best.h5') parser.add_argument('--batch_num', '--b', type=int, required=True, help='The subdirectory where the games will be saved.') args = parser.parse_args()
def rename_vol(input_vol, subject_id, subj_obj, visit=None, subtype=None): """ Convenience function that makes a copy of a volume file with a nice name, to be used as a Nipype node. Parameters ---------- input_vol :str Absolute path to the input volume subject_id : str self-explanatory subj_obj: explore.hf subject object subject object visit: int, default: None Visit number. If None, all visits are assumed. CURRENTLY MUST PROVIDE A VISIT NUMBER FOR NAME TO BE OK. subtype: ste, default: None Scan subtype as in mri.csv. If None, all subtypes are assumed. CURRENTLY MUST PROVIDE A SUBTYPE STR FOR NAME TO BE OK. Returns ------- out_file : str Absolute path to nicely named volume. """ from os.path import join as opj from os.path import dirname as opd from os.path import isfile as isf from os.path import splitext from os import remove import pandas as pd import shutil def splitext2(path): for ext in ['.nii.gz']: if path.endswith(ext): path, ext = path[:-len(ext)], path[-len(ext):] break else: path, ext = splitext(path) return ext if 'DTI' in subtype: if hasattr(subtype, 'extend'): subtype.extend('Generic') else: subtype = [subtype, 'Generic'] idx = pd.IndexSlice mri_df = subj_obj.mri if not mri_df.index.is_lexsorted(): mri_df = mri_df.sort_index(in_place=True) if visit is None: if subtype is None: used_df = mri_df else: used_df = mri_df.loc[idx[subtype, :], :] else: if subtype is None: used_df = mri_df.loc[idx[:, 'Visit ' + str(visit)], :] else: used_df = mri_df.loc[idx[subtype, 'Visit ' + str(visit)], :] scanid = ['S' + scid for scid in used_df[used_df['scanstatus'] == 1]['scanid'].values.astype(str)] scanid = [s for s in scanid if s in input_vol][0] fext = splitext2(input_vol) dn = opd(input_vol) if hasattr(subtype, 'extend'): used_subtype = subtype[0] else: used_subtype = subtype used_subtype = used_subtype.replace(' ', '_') fn = '_'.join([subject_id, 'visit', str(visit), used_subtype, scanid]) out_file = opj(dn, fn + fext) if isf(out_file): remove(out_file) # rename(input_vol,out_file) shutil.copyfile(input_vol, out_file) return out_file
def makeBuild(config): ''' Compile the exe or dll files based on: - user args - user assembly flags (f_embed, f_libembed, f_standalone) ''' gsBuild.OK = True _setCompilerClass(config) if gsBuild.Verbose: log.info('\n compiler {}'.format(json.dumps(compiler, default=lambda x: x.__dict__, indent=4))) _fetchLib() if compiler.f_parerr: return False gbm = [] dllNames = [] if compiler.lstdll: dllNames = list(createUserLibs(config)) # std stub useless unless just an exe way to run a few single python modules if compiler.ext.replace('.', '') == 'exe': gbm = gbm + _setupBaseExe(config, dllNames) if gsBuild.Verbose: gbm = gbm + ['-v'] # pass verbose to pyc ipystr = [compiler.ipath + '/ipy '] + [compiler.pycpath] + gbm _subprocPYC(ipystr, compiler.mainfile, dotExt='.exe') # ADD, SO DELETED if compiler.f_embed or compiler.f_standalone: dllNames.append(compiler.mainoutdir + '.dll') # THIS IS OK if os.path.isfile(compiler.mainoutdir + '.IPDLL'): # log.error('\n trying to remove {}'.format(compiler.mainoutdir + '.IPDLL')) try: os.remove(compiler.mainoutdir + '.IPDLL') except (IOError, Exception) as ex: log.error('\n Failed to del/remove:\n {}'.format(compiler.mainoutdir + '.IPDLL')) if ex == IOError: print(ex)#pass elif ex == System.IO.IOException: print(ex)#pass elif ex == WindowsError: print 'Type Err: {}'.format(type(ex)) print(ex)#pass else: print 'Type Err: {}'.format(type(ex)) print(ex) #raise ex #TODO checkjust use compiler.f_libembed if (compiler.f_embed or compiler.f_standalone) and compiler.f_libembed: if os.path.isfile(opd(compiler.mainoutdir) + 'StdLib.dll'): try: os.remove(opd(compiler.mainoutdir) + 'StdLib.dll') except IOError as ex: pass if gsBuild.Verbose or not gsBuild.INFO: pycInfo() #pyc does not send .exe to mainoutdir if 'dll' not in compiler.ext: movestr = ['cmd', '/C', 'move', compiler.pycdir, opn(opd(compiler.mainoutdir) + '\\')] _subprocMove(movestr, compiler.pycdir, compiler.mainoutdir, compiler.ext) if compiler.f_embed or compiler.f_standalone: notDelDll = [] for dll in dllNames: if os.path.isfile(dll) and dll.find('.dll') != -1 and \ dll.find('IronPython') == -1: #some ver deleted probably not needed # log.error('\n trying to remove {}'.format(dll)) try: os.remove(dll) # log.error('\n successfully removed {}'.format(dll)) except (IOError, Exception) as ex: if ex == IOError: print(ex)#pass elif ex == System.IO.IOException: print(ex)#pass elif ex == WindowsError: print 'Type Err: {}'.format(type(ex)) print(ex)#pass else: print 'Type Err: {}'.format(type(ex)) print(ex) #raise ex notDelDll.append(dll) if notDelDll: gsBuild.DELDLLS = notDelDll elif compiler.ext.replace('.', '') == 'dll': gbm = [] gbm = gbm + _setupBaseDll(config, dllNames) if gsBuild.Verbose: gbm = gbm + ['-v'] # pass verbose to pyc ipystr = [compiler.ipath + '/ipy '] + [compiler.pycpath] + gbm _subprocPYC(ipystr, compiler.mainfile)