#!/usr/bin/python
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------------------------------------
# Author : Ulrich Carmesin
# Source : gitea.ucarmesin.de
# ---------------------------------------------------------------------------------------------------------
""" In diesem Modul werden alle Funktionen zusammengefasst zur Generierung und Ermittlung von pathsn """
import os . path
import sys
import basic . program
import utils . config_tool
import re
import basic . constants as B
import utils . path_const as P
TOOL_NAME = " path_tool "
def getHome ( ) :
home = os . getcwd ( )
if home [ - 4 : ] == " test " and home [ - 6 : ] != " datest " :
home = home [ 0 : - 5 ]
if home [ - 10 : ] == " components " :
home = home [ 0 : - 11 ]
if home [ - 6 : ] == " datest " :
prgdir = home [ - 6 : ]
home = home [ 0 : - 7 ]
elif home [ - 7 : ] == " program " :
prgdir = home [ - 7 : ]
home = home [ 0 : - 8 ]
return home
def getBasisConfigPath ( ) :
home = os . getcwd ( )
a = home . split ( os . path . sep )
for i in range ( 0 , len ( a ) ) :
path = os . path . sep . join ( a [ 0 : - i ] )
path = os . path . join ( path , P . VAL_CONFIG , B . BASIS_FILE )
for format in utils . config_tool . CONFIG_FORMAT :
filepath = path + " . " + format
if os . path . isfile ( filepath ) :
return filepath
if os . path . exists ( filepath ) :
return filepath
raise Exception ( " no basis-configuration found " )
def getKeyValue ( job , key , comp = None ) :
"""
this function gets the value for the key which relates to an attribute in the job or in the component
: param key :
: param comp :
: return :
"""
#job = basic.program.Job.getInstance()
verify = job . getDebugLevel ( TOOL_NAME ) - 4
pt = PathConf . getInstance ( job )
job . debug ( verify , " getKeyValue " + key )
if ' job.par ' in key :
val = job . getParameter ( key [ 8 : ] )
return val
elif ' job.conf ' in key :
val = job . conf . confs [ B . SUBJECT_PATH ] [ key [ 9 : ] ]
job . debug ( verify , val )
return val
# return job.conf.confs["paths"][key[9:]]
elif ' comp. ' in key :
if comp is None :
raise Exception ( P . EXP_COMP_MISSING . format ( key ) )
if utils . config_tool . hasAttr ( comp . conf , key [ 5 : ] ) :
return utils . config_tool . getAttr ( comp . conf , key [ 5 : ] )
if utils . config_tool . hasAttr ( comp , key [ 5 : ] ) :
return utils . config_tool . getAttr ( comp , key [ 5 : ] )
return " "
elif ' env. ' in key :
if key [ 4 : ] in comp . conf [ " conn " ] :
return comp . conf [ " conn " ] [ key [ 4 : ] ]
pass
elif ( pt . pattern ) :
return pt . pattern [ key ]
job . debug ( verify , " pt exists " )
else :
return " xx- " + key + " -xx "
def composePath ( job , pathname , comp ) :
"""
this function composes a concrete path by the structured pathname
- the key of pathname is declared in path_const and the structure is configurated in config / path . yml .
: param pathname - plain keyword
: param comp :
: return :
"""
#job = basic.program.Job.getInstance()
verify = job . getDebugLevel ( TOOL_NAME )
pt = PathConf . getInstance ( job )
job . debug ( verify , " composePath " + pathname + " zu " + str ( pt ) + " mit " )
job . debug ( verify , str ( pt . pattern ) )
if pt . pattern [ pathname ] :
return composePattern ( job , pt . pattern [ pathname ] , comp )
else :
job . debug ( verify , " in Pattern nicht vorhanden: " + pathname )
def composePattern ( job , pattern , comp ) :
"""
the function composes the pattern to the standardarized path with the attributes
which are stored in the job and the component
- the key of pathname is declared in path_const and the structure is configurated in config / path . yml .
: param pattern : - keyword surroundet with { }
: param comp :
: return : path
"""
#job = basic.program.Job.getInstance()
verify = job . getDebugLevel ( TOOL_NAME )
verbose = False
job . debug ( verify , " composePattern " + pattern )
max = 5
l = re . findall ( ' \ { .*? \ } ' , pattern )
job . debug ( verify , l )
for pat in l :
if verbose : print ( str ( max ) + " : " + pattern + " : " + pat )
pit = getKeyValue ( job , pat [ 1 : - 1 ] , comp )
job . debug ( verify , str ( max ) + " : " + pattern + " : " + pat + " : " + pit )
pattern = pattern . replace ( pat , pit )
job . debug ( verify , str ( max ) + " : " + pattern + " : " + pat + " : " + pit )
while ( " { " in pattern ) :
max = max - 1
job . debug ( verify , str ( max ) + " : " + pattern + " : " + pat + " : " + pit )
pattern = composePattern ( job , pattern , comp )
job . debug ( verify , str ( max ) + " : " + pattern + " : " + pat + " : " + pit )
if ( max < 3 ) :
break
return pattern
def rejoinPath ( a , b = " " , c = " " , d = " " , e = " " , f = " " ) :
"""
this function concatenates the arguments to a path in the correct format for the operating - system
: param a :
: param b : optional
: param c : optional
: param d : optional
: param e : optional
: param f : optional
: return : path
"""
work = a + " / " + b + " / " + c + " / " + d + " / " + e + " / " + f
if a . find ( " :// " ) > 1 :
protocol = True
else :
protocol = False
work = re . sub ( r ' \\ ' , ' / ' , work )
work = re . sub ( r ' \ / ' , ' / ' , work )
work = re . sub ( r ' // ' , ' / ' , work )
while work [ - 1 : ] == " / " :
work = work [ 0 : - 1 ]
l = work . split ( " / " )
out = " "
for x in l :
if len ( x ) < 1 :
continue
if protocol :
if len ( out ) < 1 :
out = x
else :
out = out + " / " + x
else :
out = os . path . join ( out , x )
if out [ 1 : 2 ] == " : " and out [ 2 : 3 ] != " \\ " :
out = out [ 0 : 2 ] + " \\ " + out [ 2 : ]
elif protocol :
if " :// " not in out or out . index ( " :// " ) > 8 or out . index ( " :// " ) < 1 :
i = out . index ( " :/ " )
out = out [ 0 : i + 1 ] + " / " + out [ i + 1 : ]
pass
if not protocol and out . count ( " \\ " ) < 1 and out [ 0 : 1 ] != " / " and out [ 0 : 2 ] != " .. " :
out = " / " + out
return out
def extractPattern ( job , pathtyp , comp = None ) :
"""
this function extracts recoursively all parts of the pathstrucure as key and gets the values from the
job - parameter and job - configuration
: param pathtyp : the name of the path - structure
: param comp :
: return : dictionary of all part ( key ) with their valuess
"""
#job = basic.program.Job.getInstance()
verify = job . getDebugLevel ( TOOL_NAME )
out = [ ]
pt = PathConf . getInstance ( job )
pattern = pt . pattern [ pathtyp ]
work = pattern
while " { " in work :
i = work . index ( " { " )
j = work . index ( " } " )
pre = work [ 0 : i ]
pat = work [ i + 1 : j ]
job . debug ( verify , work + " von " + str ( i ) + " - " + str ( j ) + " pre " + pre + " pat " + pat )
pit = getKeyValue ( job , pat , comp )
tup = ( pre , pat , pit )
out . append ( tup )
work = work [ j + 1 : ]
return out
def extractPath ( job , pathtyp , path ) :
"""
this function extracts parts of a concrete structered path and stores the parts
as attributes into the actual job . So these attributes can read from the concrete
path instead of the related parameter - arguments .
It stores the values into the job - parameter
: param pathtyp : the structure of the concrete path
: param path : the concrete path - it should be the directory in the parameter of the job
: return :
"""
#job = basic.program.Job.getInstance()
patterlist = extractPattern ( job , pathtyp )
verbose = False
work = path
i = 0
if verbose : print ( " -- extractPatternList -- " + pathtyp + " : " + str ( patterlist ) )
for p in patterlist :
if len ( p ) < 1 : continue
delim = p [ 0 ]
key = p [ 1 ]
val = p [ 2 ]
nextdelim = " "
if i > = len ( patterlist ) - 1 :
nextdelim = " "
else :
nextdelim = patterlist [ i + 1 ] [ 0 ]
if verbose : print ( " xPath delim " + delim + " " + str ( len ( delim ) ) + " , " + nextdelim + " work " + work )
work = work [ len ( delim ) : ]
if verbose : print ( " xPath key " + key + " i " + str ( i ) + " work " + work )
if val is not None :
if verbose : print ( " val not none " + val )
if val in work :
if verbose : print ( " val ok " )
work = work . replace ( val , " " )
elif " time " in key and " job.par " in key :
prop = " "
if i < len ( patterlist ) - 1 :
prop = work [ 0 : work . index ( nextdelim ) ]
else :
prop = work
key = key [ 8 : ]
if verbose : print ( " setprop " + key + " = " + prop )
if hasattr ( job . par , key ) : delattr ( job . par , key )
setattr ( job . par , key , val )
else :
if verbose : print ( " val not not ok " + val + " zu " + key )
elif " job.par " in key :
prop = " "
if i < len ( patterlist ) - 1 :
if verbose : print ( " job.par nextdelim " + nextdelim )
prop = work [ 0 : work . index ( nextdelim ) ]
else :
prop = work
key = key [ 8 : ]
if verbose : print ( " setprop " + key + " = " + prop )
if hasattr ( job . par , key ) : delattr ( job . par , key )
setattr ( job . par , key , prop )
work = work . replace ( prop , " " )
else :
if verbose : print ( " val is none " + key )
i = i + 1
class PathConf :
"""
this class contains the structure - informations of the testrelevant directories
"""
__instance = None
def __init__ ( self , job = None ) :
#print('init pathConf')
confs = utils . config_tool . getConfig ( job , " tool " , " path " )
self . pattern = confs [ " pattern " ]
#print(self.pattern)
PathConf . __instance = self
@staticmethod
def getInstance ( job = None ) :
#print("PathConf getInstance " + str(PathConf.__instance))
if ( PathConf . __instance is None ) :
PathConf ( job )
#print("PathConf getInstance " + str(PathConf.__instance))
return PathConf . __instance