Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/pybin/tools.py

    r34e1494 r143e6f3  
    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):
     25def sh(*cmd, timeout = False, output = None, input = None, error = subprocess.STDOUT):
    2626        cmd = list(cmd)
    2727
    28         if input_file and input_text:
    29                 return 401, "Cannot use both text and file inputs"
    30 
    3128        # if this is a dry_run, only print the commands that would be ran
    32         if settings.dry_run and not ignore_dry_run:
     29        if settings.dry_run :
    3330                cmd = "{} cmd: {}".format(os.getcwd(), ' '.join(cmd))
    34                 if output_file and not isinstance(output_file, int):
     31                if output and not isinstance(output, int):
    3532                        cmd += " > "
    36                         cmd += output_file
     33                        cmd += output
    3734
    3835                if error and not isinstance(error, int):
     
    4037                        cmd += error
    4138
    42                 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):
    4340                        cmd += " < "
    44                         cmd += input_file
     41                        cmd += input
    4542
    4643                print(cmd)
     
    4946        with contextlib.ExitStack() as onexit:
    5047                # add input redirection if needed
    51                 input_file = openfd(input_file, 'r', onexit, True)
     48                input = openfd(input, 'r', onexit, True)
    5249
    5350                # add output redirection if needed
    54                 output_file = openfd(output_file, 'w', onexit, False)
     51                output = openfd(output, 'w', onexit, False)
    5552
    5653                # add error redirection if needed
     
    6158                        proc = subprocess.run(
    6259                                cmd,
    63                                 **({'input' : bytes(input_text, encoding='utf-8')} if input_text else {'stdin' : input_file}),
    64                                 stdout  = output_file,
    65                                 stderr  = error,
    66                                 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
    6764                        )
    68 
    6965                        return proc.returncode, proc.stdout.decode("utf-8") if proc.stdout else None
    7066                except subprocess.TimeoutExpired:
     
    7975                return False
    8076
    81         code, out = sh("file %s" % fname, output_file=subprocess.PIPE)
     77        code, out = sh("file %s" % fname, output=subprocess.PIPE)
    8278        if code != 0:
    8379                return False
     
    111107        if isinstance(files, str ): files = [ files ]
    112108        for file in files:
    113                 sh( 'rm', '-f', file, output_file=subprocess.DEVNULL, error=subprocess.DEVNULL )
     109                sh( 'rm', '-f', file, output=subprocess.DEVNULL, error=subprocess.DEVNULL )
    114110
    115111# Create 1 or more directory
     
    119115                p = os.path.normpath( file )
    120116                d = os.path.dirname ( p )
    121                 sh( 'mkdir', '-p', d, output_file=subprocess.DEVNULL, error=subprocess.DEVNULL )
     117                sh( 'mkdir', '-p', d, output=subprocess.DEVNULL, error=subprocess.DEVNULL )
    122118
    123119
     
    142138                lhs,
    143139                rhs,
    144                 output_file=subprocess.PIPE
     140                output=subprocess.PIPE
    145141        )
    146142
    147143# call make
    148 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):
    149145        test_param = """test="%s" """ % (error_file) if error_file else None
    150146        cmd = [
     
    155151                settings.debug.flags,
    156152                settings.install.flags,
    157                 settings.distcc if settings.distribute else None,
    158153                flags,
    159154                target
    160155        ]
    161156        cmd = [s for s in cmd if s]
    162         return sh(*cmd, output_file=output_file, error=error)
     157        return sh(*cmd, output=output, error=error)
    163158
    164159def which(program):
     
    206201# cat one file into the other
    207202def cat(source, dest):
    208         ret, _ = sh("cat", source, output_file=dest)
     203        ret, _ = sh("cat", source, output=dest)
    209204        return ret
    210205
     
    261256                        os.write(int(make_jobs_fds.group(3)), tokens)
    262257                else :
    263                         if settings.distribute:
    264                                 ret, jstr = sh("distcc", "-j", output_file=subprocess.PIPE, ignore_dry_run=True)
    265                                 if ret == 0:
    266                                         options.jobs = int(jstr.strip())
    267                                 else :
    268                                         options.jobs = multiprocessing.cpu_count()
    269                         else:
    270                                 options.jobs = multiprocessing.cpu_count()
     258                        options.jobs = multiprocessing.cpu_count()
    271259        else :
    272260                force = True
     
    286274################################################################################
    287275
    288 # get hash for given configuration
    289 def config_hash():
    290         path = os.path.normpath(os.path.join(
    291                 settings.SRCDIR,
    292         ))
    293 
    294         distcc_hash = os.path.join(settings.SRCDIR, '../tools/build/distcc_hash')
    295         config = "%s-%s" % (settings.arch.target, settings.debug.path)
    296         _, out = sh(distcc_hash, config, output_file=subprocess.PIPE, ignore_dry_run=True)
    297         return out.strip()
    298 
    299 # get pretty string for time of day
    300276def pretty_now():
    301277        ts = time.time()
     
    332308                return 1, "ERR No core dump"
    333309
    334         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)
    335311
    336312def core_archive(dst, name, exe):
Note: See TracChangeset for help on using the changeset viewer.