Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/pybin/tools.py

    rd65f92c r143e6f3  
    22import argparse
    33import contextlib
     4import datetime
    45import fileinput
    56import multiprocessing
     
    2223
    2324# helper functions to run terminal commands
    24 def sh(*cmd, timeout = False, output_file = None, input_file = None, input_text = None, error = subprocess.STDOUT):
     25def sh(*cmd, timeout = False, output = None, input = None, error = subprocess.STDOUT):
    2526        cmd = list(cmd)
    26 
    27         if input_file and input_text:
    28                 return 401, "Cannot use both text and file inputs"
    2927
    3028        # if this is a dry_run, only print the commands that would be ran
    3129        if settings.dry_run :
    3230                cmd = "{} cmd: {}".format(os.getcwd(), ' '.join(cmd))
    33                 if output_file and not isinstance(output_file, int):
     31                if output and not isinstance(output, int):
    3432                        cmd += " > "
    35                         cmd += output_file
     33                        cmd += output
    3634
    3735                if error and not isinstance(error, int):
     
    3937                        cmd += error
    4038
    41                 if input_file and not isinstance(input_file, int) and os.path.isfile(input_file):
     39                if input and not isinstance(input, int) and os.path.isfile(input):
    4240                        cmd += " < "
    43                         cmd += input_file
     41                        cmd += input
    4442
    4543                print(cmd)
     
    4846        with contextlib.ExitStack() as onexit:
    4947                # add input redirection if needed
    50                 input_file = openfd(input_file, 'r', onexit, True)
     48                input = openfd(input, 'r', onexit, True)
    5149
    5250                # add output redirection if needed
    53                 output_file = openfd(output_file, 'w', onexit, False)
     51                output = openfd(output, 'w', onexit, False)
    5452
    5553                # add error redirection if needed
     
    6058                        proc = subprocess.run(
    6159                                cmd,
    62                                 **({'input' : bytes(input_text, encoding='utf-8')} if input_text else {'stdin' : input_file}),
    63                                 stdout  = output_file,
    64                                 stderr  = error,
    65                                 timeout = settings.timeout.single if timeout else None
     60                                stdin =input,
     61                                stdout=output,
     62                                stderr=error,
     63                                timeout=settings.timeout.single if timeout else None
    6664                        )
    67 
    6865                        return proc.returncode, proc.stdout.decode("utf-8") if proc.stdout else None
    6966                except subprocess.TimeoutExpired:
     
    7875                return False
    7976
    80         code, out = sh("file %s" % fname, output_file=subprocess.PIPE)
     77        code, out = sh("file %s" % fname, output=subprocess.PIPE)
    8178        if code != 0:
    8279                return False
     
    110107        if isinstance(files, str ): files = [ files ]
    111108        for file in files:
    112                 sh( 'rm', '-f', file, output_file=subprocess.DEVNULL, error=subprocess.DEVNULL )
     109                sh( 'rm', '-f', file, output=subprocess.DEVNULL, error=subprocess.DEVNULL )
    113110
    114111# Create 1 or more directory
     
    118115                p = os.path.normpath( file )
    119116                d = os.path.dirname ( p )
    120                 sh( 'mkdir', '-p', d, output_file=subprocess.DEVNULL, error=subprocess.DEVNULL )
     117                sh( 'mkdir', '-p', d, output=subprocess.DEVNULL, error=subprocess.DEVNULL )
    121118
    122119
     
    141138                lhs,
    142139                rhs,
    143                 output_file=subprocess.PIPE
     140                output=subprocess.PIPE
    144141        )
    145142
    146143# call make
    147 def make(target, *, flags = '', output_file = None, error = None, error_file = None, silent = False):
     144def make(target, *, flags = '', output = None, error = None, error_file = None, silent = False):
    148145        test_param = """test="%s" """ % (error_file) if error_file else None
    149146        cmd = [
     
    154151                settings.debug.flags,
    155152                settings.install.flags,
    156                 settings.distcc if settings.distribute else None,
    157153                flags,
    158154                target
    159155        ]
    160156        cmd = [s for s in cmd if s]
    161         return sh(*cmd, output_file=output_file, error=error)
     157        return sh(*cmd, output=output, error=error)
    162158
    163159def which(program):
     
    205201# cat one file into the other
    206202def cat(source, dest):
    207         ret, _ = sh("cat", source, output_file=dest)
     203        ret, _ = sh("cat", source, output=dest)
    208204        return ret
    209205
     
    278274################################################################################
    279275
    280 # get hash for given configuration
    281 def config_hash():
    282         path = os.path.normpath(os.path.join(
    283                 settings.SRCDIR,
    284         ))
    285 
    286         distcc_hash = os.path.join(settings.SRCDIR, '../tools/build/distcc_hash')
    287         config = "%s-%s" % (settings.arch.target, settings.debug.path)
    288         _, out = sh(distcc_hash, config, output_file=subprocess.PIPE)
    289         return out.strip()
     276def pretty_now():
     277        ts = time.time()
     278        print(ts, file=sys.stderr)
     279        return datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H:%M:%S')
    290280
    291281# check if arguments is yes or no
     
    318308                return 1, "ERR No core dump"
    319309
    320         return sh('gdb', '-n', path, core, '-batch', '-x', cmd, output_file=subprocess.PIPE)
     310        return sh('gdb', '-n', path, core, '-batch', '-x', cmd, output=subprocess.PIPE)
     311
     312def core_archive(dst, name, exe):
     313        # Get the core dump
     314        core = os.path.join(os.getcwd(), "core" )
     315
     316        # update the path for this test
     317        dst  = os.path.join(dst, name)
     318
     319        # make a directory for this test
     320        # mkdir makes the parent directory only so add a dummy
     321        mkdir(os.path.join(dst, "dir"))
     322
     323        # moves the files
     324        mv( core, os.path.join(dst, "core" ) )
     325        mv( exe , os.path.join(dst, name   ) )
     326
     327        # return explanatory test
     328        return "Archiving %s (executable and core) to %s" % (os.path.relpath(exe, settings.BUILDDIR), os.path.relpath(dst, settings.original_path))
    321329
    322330class Timed:
Note: See TracChangeset for help on using the changeset viewer.