Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/pybin/tools.py

    r143e6f3 rd65f92c  
    22import argparse
    33import contextlib
    4 import datetime
    54import fileinput
    65import multiprocessing
     
    2322
    2423# helper functions to run terminal commands
    25 def sh(*cmd, timeout = False, output = None, input = None, error = subprocess.STDOUT):
     24def sh(*cmd, timeout = False, output_file = None, input_file = None, input_text = None, error = subprocess.STDOUT):
    2625        cmd = list(cmd)
     26
     27        if input_file and input_text:
     28                return 401, "Cannot use both text and file inputs"
    2729
    2830        # if this is a dry_run, only print the commands that would be ran
    2931        if settings.dry_run :
    3032                cmd = "{} cmd: {}".format(os.getcwd(), ' '.join(cmd))
    31                 if output and not isinstance(output, int):
     33                if output_file and not isinstance(output_file, int):
    3234                        cmd += " > "
    33                         cmd += output
     35                        cmd += output_file
    3436
    3537                if error and not isinstance(error, int):
     
    3739                        cmd += error
    3840
    39                 if input and not isinstance(input, int) and os.path.isfile(input):
     41                if input_file and not isinstance(input_file, int) and os.path.isfile(input_file):
    4042                        cmd += " < "
    41                         cmd += input
     43                        cmd += input_file
    4244
    4345                print(cmd)
     
    4648        with contextlib.ExitStack() as onexit:
    4749                # add input redirection if needed
    48                 input = openfd(input, 'r', onexit, True)
     50                input_file = openfd(input_file, 'r', onexit, True)
    4951
    5052                # add output redirection if needed
    51                 output = openfd(output, 'w', onexit, False)
     53                output_file = openfd(output_file, 'w', onexit, False)
    5254
    5355                # add error redirection if needed
     
    5860                        proc = subprocess.run(
    5961                                cmd,
    60                                 stdin =input,
    61                                 stdout=output,
    62                                 stderr=error,
    63                                 timeout=settings.timeout.single if timeout else None
     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
    6466                        )
     67
    6568                        return proc.returncode, proc.stdout.decode("utf-8") if proc.stdout else None
    6669                except subprocess.TimeoutExpired:
     
    7578                return False
    7679
    77         code, out = sh("file %s" % fname, output=subprocess.PIPE)
     80        code, out = sh("file %s" % fname, output_file=subprocess.PIPE)
    7881        if code != 0:
    7982                return False
     
    107110        if isinstance(files, str ): files = [ files ]
    108111        for file in files:
    109                 sh( 'rm', '-f', file, output=subprocess.DEVNULL, error=subprocess.DEVNULL )
     112                sh( 'rm', '-f', file, output_file=subprocess.DEVNULL, error=subprocess.DEVNULL )
    110113
    111114# Create 1 or more directory
     
    115118                p = os.path.normpath( file )
    116119                d = os.path.dirname ( p )
    117                 sh( 'mkdir', '-p', d, output=subprocess.DEVNULL, error=subprocess.DEVNULL )
     120                sh( 'mkdir', '-p', d, output_file=subprocess.DEVNULL, error=subprocess.DEVNULL )
    118121
    119122
     
    138141                lhs,
    139142                rhs,
    140                 output=subprocess.PIPE
     143                output_file=subprocess.PIPE
    141144        )
    142145
    143146# call make
    144 def make(target, *, flags = '', output = None, error = None, error_file = None, silent = False):
     147def make(target, *, flags = '', output_file = None, error = None, error_file = None, silent = False):
    145148        test_param = """test="%s" """ % (error_file) if error_file else None
    146149        cmd = [
     
    151154                settings.debug.flags,
    152155                settings.install.flags,
     156                settings.distcc if settings.distribute else None,
    153157                flags,
    154158                target
    155159        ]
    156160        cmd = [s for s in cmd if s]
    157         return sh(*cmd, output=output, error=error)
     161        return sh(*cmd, output_file=output_file, error=error)
    158162
    159163def which(program):
     
    201205# cat one file into the other
    202206def cat(source, dest):
    203         ret, _ = sh("cat", source, output=dest)
     207        ret, _ = sh("cat", source, output_file=dest)
    204208        return ret
    205209
     
    274278################################################################################
    275279
    276 def 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')
     280# get hash for given configuration
     281def 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()
    280290
    281291# check if arguments is yes or no
     
    308318                return 1, "ERR No core dump"
    309319
    310         return sh('gdb', '-n', path, core, '-batch', '-x', cmd, output=subprocess.PIPE)
    311 
    312 def 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))
     320        return sh('gdb', '-n', path, core, '-batch', '-x', cmd, output_file=subprocess.PIPE)
    329321
    330322class Timed:
Note: See TracChangeset for help on using the changeset viewer.