Esempio n. 1
0
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))
Esempio n. 2
0
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 
Esempio n. 3
0
def AssignMakeZip(uconfig, args):

    config = ndcDict(uconfig)
    outzipPath = None
    cfz = config['ZIPPATH']

    if args['listzip']:
        zfiles = []  # missing zipfiles in listzip.txt or input
        zips = loadRes(getTrueFilePath(args['listzip']))

        uzips = None  # unique zips
        nuzips = []  #full path uniques

        # zip exists with cur name if have zipped before
        isautozip = False

        try:
            import zlib
            mode = zipfile.ZIP_DEFLATED
        except ImportError:
            mode = zipfile.ZIP_STORED

        config['LISTFILES']['zip'] = []
        if isinstance(zips, list):
            config['LISTFILES']['zip'].extend(zips)
        else:
            config['LISTFILES']['zip'].append(zips)

        # set outzip path
        manf = 'default.zip'
        if config['MAINFILE']:
            manf = ('.').join(opb(config['MAINFILE']).split('.')[:-1]) + '.zip'

        outzipPath = opj(config['OUTDIR'], manf)
        # stop trying to overwrite same file
        # current zip path
        cfzp = None
        if cfz:

            try:
                cfzp = getTrueFilePath(cfz)
                if opex(cfzp):
                    isautozip = True
            except IOError:
                pass
        # auto zip path
        elif outzipPath:
            try:
                cfzp = getTrueFilePath(outzipPath)
                if opex(cfzp):
                    isautozip = True
            except IOError:
                pass
        # update zip path
        config['ZIPPATH'] = cfzp
        cfz = cfzp

        # -- zipping ---
        # uzips
        if isautozip:
            infoExistZip(cfz, outzipPath)
            log.FILE('Confirmed: {}'.format(cfzp))
            zipsRelative = []

            for zipname in zips:
                relname = None
                relname = _getRelativeZipName(zips, zipname)
                if relname:
                    zipsRelative.append(relname)
                else:
                    zipsRelative.append(zipname)

            with zipfile.ZipFile(cfzp, 'a', mode) as ziprd:
                uzips = list(uniqueZips(zipsRelative, list(ziprd.namelist())))

                #getting back full path from relative
                for zfile in uzips:
                    if 'from' in zfile:
                        drv = zfile[5]
                        zfile = opn(zfile.replace('from_' + drv, drv + ':'))
                    else:
                        cwdlst = os.getcwd().split(os.sep)

                        cnt = 0
                        while cwdlst[-1] and not opex(
                                opab(opj((os.sep).join(cwdlst), zfile))):
                            cnt += 1
                            if cnt > 25: break
                            cwdlst.pop()

                        zfile = opab(opj((os.sep).join(cwdlst), zfile))

                    nuzips.append(zfile)

                    if not os.path.isfile(zfile):
                        log.error("can't find {}".format(zfile))
                        zfiles.append(zfile)
                        continue

                    arcname = _getRelativeZipName(nuzips, zfile)
                    if not arcname:
                        log.error("can't find arcname using {}".format(
                            oprel(zfile)))
                        zfiles.append(zfile)
                        continue
                    ziprd.write(zfile, arcname)
            ziprd.close()

            # changed if uzips
            if nuzips:
                if gsBuild.Verbose or not gsBuild.INFO:
                    log.info(('\nSome Files already zipped in:\n{}\n\t' + \
                              '- delete to replace existing' + \
                              '\nadding zip files to existing archive:\n' + \
                              '{}\n'*len(nuzips)) \
                            .format(cfz, *nuzips))

        # Need new zip with ZIPPATH/outzipPath as name
        elif not isautozip:
            warnZip(outzipPath)

            if isinstance(zips, list):

                with zipfile.ZipFile(cfz, 'a', mode) as zipr:
                    for zfile in list(set(zips)):
                        if not os.path.isfile(zfile):
                            zfiles.append(zfile)
                            continue
                        arcname = _getRelativeZipName(zips, zfile)
                        if not arcname:
                            arcname = oprel(zfile)
                        zipr.write(zfile, arcname)

                log.FILE('{}'.format(config['ZIPPATH']))
                zipr.close()

            if isinstance(zips, (str, unicode)):
                with zipfile.ZipFile(cfz, 'w', mode) as zipr:
                    arcname = oprel(zips)
                    zipr.write(zips, arcname)

                zipr.close()
                log.FILE('{}'.format(cfz))

        if zfiles:
            if gsBuild.Verbose or not gsBuild.INFO:
                log.warn(('\nFile | path does not exist - ' +\
                          'skipped adding zip files:\n\t' + \
                          '{} \n\t'*len(zfiles)).format(*zfiles))

            log.FILE(('*Missing zip: {}\n' * len(zfiles)).format(*zfiles))

            partialError('ValueError',
                         ('*Missing zip: {}\n' * len(zfiles)).format(*zfiles))

    return ndcDict(config)
Esempio n. 4
0
def setPath(configPath, mainName, jsonarg, argout, f_type='json'):
    ''' Well this doesn't end - way too complex '''
    #TODO auto-fix file re-naming on err fix

    basem = None
    basec = None
    basej = None
    mainp = None
    jsonp = None
    configp = None

    odir = None
    argc, argm, argj, argo = SlashArgs(configPath, mainName, jsonarg, argout)

    if f_type == 'mkdir':
        return setMkdir(argc, argm, argj)

    odir = opn(opj(argo[0], argo[1]))
    if f_type == 'odir':
        return odir

    if argc:
        configp, basec, typc, isFilec, isdefaultc = argc

    if configPath and f_type == 'config':
        return setConfig(configPath, mainName, jsonarg, argc, argm, argj)

    if argm:
        mainp, basem, typm, isFilem, isdefaultm = argm

    if mainName and f_type == 'name':
        return setMain(configPath, mainName, jsonarg, argc, argm, argj)

    if argj:
        jsonp, basej, typj, isFilej, isdefaultj = argj

    if mainName and f_type == 'json':
        if basec and '.' in basec:
            basec = ('.').join(basec.split('.')[:-1]).replace('_config', '')

    if basem and '.' in basem:
        basem = ('.').join(basem.split('.')[:-1])
        mainName = opn(opj(mainp, basem))

        if configp and not op.exists(mainp):
            mainName = opn(opj(configp.replace('_config', ''), basem))

    if jsonarg and f_type == 'json' and \
        op.basename(jsonarg) != 'default_assembly.json':

        if typj != 'json' and '.' in basej:
            basej = ('.').join(basej.split('.')[:-1])  #+ '_assembly.json'
        elif typj != 'json':
            basej = basej + '_assembly.json'
        if isFilej:
            jsonp = opn(opj(jsonp, basej))
        elif basej and not isdefaultj:
            jsonp = opn(opj(jsonp, basej))
        elif basej and mainp and not isdefaultm:
            jsonp = opn(opj(mainp, basej))
        elif basej and configp and not isdefaultc:
            jsonp = opn(opj(configp, basej))
        elif basej and mainp:
            jsonp = opn(opj(mainp, basej))
        elif basej and jsonp:
            jsonp = opn(opj(jsonp, basej))
        elif basej and configp:
            jsonp = opn(opj(mainp, basej))

        if jsonp:
            return jsonp
        else:
            jsonp = None

    if f_type == 'json':
        #falling backwards
        fallp = None
        if basem:
            basem = basem + '_assembly.json'
        defbase = 'default_assembly.json'

        if basem and mainp and op.exists(mainp) and not isdefaultm:
            fallp = opn(opj(mainp, basem))
        elif basem and configp and op.exists(configp):
            fallp = opn(opab(opj(configp.replace('_config', ''), basem)))
        elif basem and not configp and mainp:
            fallp = opn(opj(mainp, basem))
        elif configp and op.exists(configp):
            fallp = opn(opj(configp.replace('_config', ''), defbase))
        else:
            fallp = opn(opab(opj('./UserDefaulted', defbase)))

        return fallp
Esempio n. 5
0
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)
Esempio n. 6
0
from os.path import exists as opex
from os.path import isdir as opisd
from ipyrequirements import REQUIREMENTS as ipyreq
import re
from copy import deepcopy
from collections import OrderedDict
import json
import unittest
from regt import SlashContrl
from buildlogs import dynfile
from globalstate import gsBuild

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 = {