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 cleanDll(): try: cleandlls = gsBuild.DELDLLS except Exception as exd: print 'No Cleanup' print(exd) if cleandlls: cleanedDlls = list(cleandlls) for cleandll in cleandlls: if opex(cleandll): try: os.remove(cleandll) cleanedDlls.remove(cleandll) except Exception as ex: log.warn(str(ex)) else: cleanedDlls.remove(cleandll) log.error('\n No reason to cleanup {}.'.format(cleandll)) if cleanedDlls: for dll in cleanedDlls: delCmd = ['cmd /c del '] + [dll] try: subprocess.check_output(delCmd) except Exception as exd: log.warn("\nCan't cleanup in ipybuilder {}".format(cleandll))
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 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)
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)
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')
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 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 = {
from os.path import basename as opb from os.path import exists as opex from os.path import isdir as opisd 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\\'))))) #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,