Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/pybin/tools.py

    r0f5da65 rdcfedca  
    2323
    2424# helper functions to run terminal commands
    25 def sh(*cmd, timeout = False, output_file = None, input_file = None, input_text = None, error = subprocess.STDOUT, ignore_dry_run = False):
    26         try:
    27                 cmd = list(cmd)
    28 
    29                 if input_file and input_text:
    30                         return 401, "Cannot use both text and file inputs"
    31 
    32                 # if this is a dry_run, only print the commands that would be ran
    33                 if settings.dry_run and not ignore_dry_run:
    34                         cmd = "{} cmd: {}".format(os.getcwd(), ' '.join(cmd))
    35                         if output_file and not isinstance(output_file, int):
    36                                 cmd += " > "
    37                                 cmd += output_file
    38 
    39                         if error and not isinstance(error, int):
    40                                 cmd += " 2> "
    41                                 cmd += error
    42 
    43                         if input_file and not isinstance(input_file, int) and os.path.isfile(input_file):
    44                                 cmd += " < "
    45                                 cmd += input_file
    46 
    47                         print(cmd)
    48                         return 0, None
    49 
    50                 with contextlib.ExitStack() as onexit:
    51                         # add input redirection if needed
    52                         input_file = openfd(input_file, 'r', onexit, True)
    53 
    54                         # add output redirection if needed
    55                         output_file = openfd(output_file, 'w', onexit, False)
    56 
    57                         # add error redirection if needed
    58                         error = openfd(error, 'w', onexit, False)
    59 
    60                         # run the desired command
    61                         # use with statement to make sure proc is cleaned
    62                         # don't use subprocess.run because we want to send SIGABRT on exit
    63                         with subprocess.Popen(
     25def sh(*cmd, timeout = False, output = None, input = None, error = subprocess.STDOUT):
     26        cmd = list(cmd)
     27
     28        # if this is a dry_run, only print the commands that would be ran
     29        if settings.dry_run :
     30                cmd = "{} cmd: {}".format(os.getcwd(), ' '.join(cmd))
     31                if output and not isinstance(output, int):
     32                        cmd += " > "
     33                        cmd += output
     34
     35                if error and not isinstance(error, int):
     36                        cmd += " 2> "
     37                        cmd += error
     38
     39                if input and not isinstance(input, int) and os.path.isfile(input):
     40                        cmd += " < "
     41                        cmd += input
     42
     43                print(cmd)
     44                return 0, None
     45
     46        with contextlib.ExitStack() as onexit:
     47                # add input redirection if needed
     48                input = openfd(input, 'r', onexit, True)
     49
     50                # add output redirection if needed
     51                output = openfd(output, 'w', onexit, False)
     52
     53                # add error redirection if needed
     54                error = openfd(error, 'w', onexit, False)
     55
     56                # run the desired command
     57                try:
     58                        proc = subprocess.run(
    6459                                cmd,
    65                                 **({'input' : bytes(input_text, encoding='utf-8')} if input_text else {'stdin' : input_file}),
    66                                 stdout  = output_file,
    67                                 stderr  = error
    68                         ) as proc:
    69 
    70                                 try:
    71                                         out, _ = proc.communicate(
    72                                                 timeout = settings.timeout.single if timeout else None
    73                                         )
    74 
    75                                         return proc.returncode, out.decode("utf-8") if out else None
    76                                 except subprocess.TimeoutExpired:
    77                                         proc.send_signal(signal.SIGABRT)
    78                                         proc.communicate()
    79                                         return 124, str(None)
    80 
    81         except Exception as ex:
    82                 print ("Unexpected error: %s" % ex)
    83                 raise
     60                                stdin =input,
     61                                stdout=output,
     62                                stderr=error,
     63                                timeout=settings.timeout.single if timeout else None
     64                        )
     65                        return proc.returncode, proc.stdout.decode("utf-8") if proc.stdout else None
     66                except subprocess.TimeoutExpired:
     67                        return 124, str(None)
    8468
    8569def is_ascii(fname):
     
    9175                return False
    9276
    93         code, out = sh("file %s" % fname, output_file=subprocess.PIPE)
     77        code, out = sh("file %s" % fname, output=subprocess.PIPE)
    9478        if code != 0:
    9579                return False
     
    123107        if isinstance(files, str ): files = [ files ]
    124108        for file in files:
    125                 sh( 'rm', '-f', file, output_file=subprocess.DEVNULL, error=subprocess.DEVNULL )
     109                sh( 'rm', '-f', file, output=subprocess.DEVNULL, error=subprocess.DEVNULL )
    126110
    127111# Create 1 or more directory
     
    131115                p = os.path.normpath( file )
    132116                d = os.path.dirname ( p )
    133                 sh( 'mkdir', '-p', d, output_file=subprocess.DEVNULL, error=subprocess.DEVNULL )
     117                sh( 'mkdir', '-p', d, output=subprocess.DEVNULL, error=subprocess.DEVNULL )
    134118
    135119
     
    154138                lhs,
    155139                rhs,
    156                 output_file=subprocess.PIPE
     140                output=subprocess.PIPE
    157141        )
    158142
    159143# call make
    160 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):
    161145        test_param = """test="%s" """ % (error_file) if error_file else None
    162146        cmd = [
     
    167151                settings.debug.flags,
    168152                settings.install.flags,
    169                 settings.distcc if settings.distribute else None,
    170153                flags,
    171154                target
    172155        ]
    173156        cmd = [s for s in cmd if s]
    174         return sh(*cmd, output_file=output_file, error=error)
     157        return sh(*cmd, output=output, error=error)
    175158
    176159def which(program):
    177         fpath, fname = os.path.split(program)
    178         if fpath:
    179                 if is_exe(program):
    180                         return program
    181         else:
    182                 for path in os.environ["PATH"].split(os.pathsep):
    183                         exe_file = os.path.join(path, program)
    184                         if is_exe(exe_file):
    185                                 return exe_file
    186         return None
     160    fpath, fname = os.path.split(program)
     161    if fpath:
     162        if is_exe(program):
     163            return program
     164    else:
     165        for path in os.environ["PATH"].split(os.pathsep):
     166            exe_file = os.path.join(path, program)
     167            if is_exe(exe_file):
     168                return exe_file
     169
     170    return None
    187171
    188172@contextlib.contextmanager
     
    217201# cat one file into the other
    218202def cat(source, dest):
    219         ret, _ = sh("cat", source, output_file=dest)
     203        ret, _ = sh("cat", source, output=dest)
    220204        return ret
    221205
     
    272256                        os.write(int(make_jobs_fds.group(3)), tokens)
    273257                else :
    274                         if settings.distribute:
    275                                 ret, jstr = sh("distcc", "-j", output_file=subprocess.PIPE, ignore_dry_run=True)
    276                                 if ret == 0:
    277                                         options.jobs = int(jstr.strip())
    278                                 else :
    279                                         options.jobs = multiprocessing.cpu_count()
    280                         else:
    281                                 options.jobs = multiprocessing.cpu_count()
     258                        options.jobs = multiprocessing.cpu_count()
    282259        else :
    283260                force = True
     
    297274################################################################################
    298275
    299 # get hash for given configuration
    300 def config_hash():
    301         path = os.path.normpath(os.path.join(
    302                 settings.SRCDIR,
    303         ))
    304 
    305         distcc_hash = os.path.join(settings.SRCDIR, '../tools/build/distcc_hash')
    306         config = "%s-%s" % (settings.arch.target, settings.debug.path)
    307         _, out = sh(distcc_hash, config, output_file=subprocess.PIPE, ignore_dry_run=True)
    308         return out.strip()
    309 
    310 # get pretty string for time of day
    311276def pretty_now():
    312277        ts = time.time()
     
    343308                return 1, "ERR No core dump"
    344309
    345         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)
    346311
    347312def core_archive(dst, name, exe):
    348         # Get the core dump
     313        # Get the files to copy
    349314        core = os.path.join(os.getcwd(), "core" )
    350315
    351         # update the path for this test
    352         dst  = os.path.join(dst, name)
     316        # Uncomment if we want timestamps on coredumps
     317        # dst  = os.path.join(dst, "%s_%s" % (name, pretty_now()))
    353318
    354319        # make a directory for this test
    355         # mkdir makes the parent directory only so add a dummy
    356         mkdir(os.path.join(dst, name ))
     320        mkdir(os.path.join(dst, "dir"))
    357321
    358322        # moves the files
     
    364328
    365329class Timed:
    366         def __enter__(self):
    367                 self.start = time.time()
    368                 return self
    369 
    370         def __exit__(self, *args):
    371                 self.end = time.time()
    372                 self.duration = self.end - self.start
     330    def __enter__(self):
     331        self.start = time.time()
     332        return self
     333
     334    def __exit__(self, *args):
     335        self.end = time.time()
     336        self.duration = self.end - self.start
    373337
    374338def timed(src, timeout):
    375339        expire = time.time() + timeout
    376340        i = iter(src)
    377         with contextlib.suppress(StopIteration):
    378                 while True:
    379                         yield i.next(max(expire - time.time(), 0))
     341        while True:
     342                yield i.next(max(expire - time.time(), 0))
Note: See TracChangeset for help on using the changeset viewer.