123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- # -*- coding: utf-8; mode: Python -*-
- # (c) Daniel Llorens - 2016, 2017-2019
- # This library is free software; you can redistribute it and/or modify it under
- # the terms of the GNU Lesser General Public License as published by the Free
- # Software Foundation; either version 3 of the License, or (at your option) any
- # later version.
- # Utilities for SConstructs
- import os, string
- # These are colorama names, but the dependence is a bother.
- class Fore: RED = '\x1b[31m'; YELLOW ='\x1b[33m'; RESET = '\x1b[39m'
- class Style: BRIGHT = '\x1b[1m'; RESET_ALL = '\x1b[0m';
- from os.path import join, abspath, split
- from subprocess import call
- def ensure_ext(s, ext):
- "if s doesn't end with ext, append it."
- p = s.rfind(ext)
- return s if p+len(ext) == len(s) else s + ext
- def remove_ext(s):
- "clip string from the last dot until the end."
- p = s.rfind('.')
- assert p>=0, 'source must have an extension'
- return s[0:p]
- def path_parts(path):
- path, tail = split(path)
- return ([path] if path == os.sep else path_parts(path) if path else []) \
- + ([tail] if tail else [])
- def take_from_environ(env, var, wrapper=(lambda x: x), default=None):
- if var in os.environ and os.environ[var]!='':
- env[var] = wrapper(os.environ[var])
- elif default is not None:
- env[var] = default
- def get_value_wo_error(dictionary, key, default = ''):
- if key in dictionary:
- return dictionary[key]
- else:
- return default
- def dict_index_list(dictionary, list_of_keys):
- return dict([ (k, get_value_wo_error(dictionary, k))
- for k in list_of_keys ])
- def to_test(env, variant_dir, source, args):
- """
- Run program with args to produce a check stamp. The name of the first source
- is used to generate the name of the stamp.
- """
- class tester:
- def __init__(self, args):
- self.args = args
- def __call__(self, target, source, env):
- print("-> running %s \n___________________" % str(self.args))
- r = os.spawnl(os.P_WAIT, self.args[0], self.args[0], *self.args[1:])
- print("^^^^^^^^^^^^^^^^^^^")
- print('r ', r)
- if not r:
- print("PASSED %s" % str(self.args))
- call(['touch', target[0].abspath])
- else:
- print("FAILED %s" % str(self.args))
- return r
- stamp = env.File(join(variant_dir, str(source[0])) + string.join(args[1:]) + '.check')
- return env.Command(stamp, source, tester(args))
- # def to_source(env, targets, source):
- # main = source[0]
- # for target in targets: env.Notangle(target, remove_ext(main)+'.nw')
- # env.Noweave(remove_ext(main)+'.tex', remove_ext(main)+'.nw')
- # env.PDF(remove_ext(main), remove_ext(main)+'.tex')
- def to_source_from_noweb(env, targets, source):
- main = source[0]
- env.Noweave(remove_ext(main) + '.tex', remove_ext(main) + '.nw')
- env.PDF(remove_ext(main), remove_ext(main) + '.tex')
- return [env.Notangle(target, remove_ext(main) + '.nw') for target in targets]
- def to_test_ra(env_, variant_dir):
- def f(source, target='', cxxflags=[], cppdefines=[]):
- if len(cxxflags)==0 or len(cppdefines)==0:
- env = env_
- else:
- env = env_.Clone()
- env.Append(CXXFLAGS=cxxflags + ['-U' + k for k in cppdefines.keys()], CPPDEFINES=cppdefines)
- if len(target)==0:
- target = source
- obj = env.Object(target, [source + '.C'])
- test = env.Program(target, obj)
- to_test(env, variant_dir, test, [test[0].abspath])
- return f
- def print_summary(GetBuildFailures, tag):
- test_item_tally = 0
- test_tally = 0
- build_tally = 0
- print('\n' + Style.BRIGHT + 'Summary for ' + tag + Style.RESET_ALL + '\n--------')
- for bf in GetBuildFailures():
- if str(bf.node).endswith('.check') and (bf.status > 0):
- print((Style.BRIGHT + Fore.RED + '%s ' + Style.RESET_ALL + Fore.RESET + ' failed (%d)') \
- % (bf.node, bf.status))
- test_item_tally += bf.status
- test_tally += 1
- else:
- print((Style.BRIGHT + Fore.YELLOW + '%s ' + Style.RESET_ALL + Fore.RESET + ' failed (%s)') \
- % (bf.node, bf.errstr))
- build_tally += 1
- print('%d targets failed to build.' % build_tally)
- print('%d tests failed with %d total failures.' % (test_tally, test_item_tally))
|