Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/pybin/tools.py

    ra468e1e9 r8364209  
    7373                                        )
    7474
    75                                         return proc.returncode, out.decode("latin-1") if out else None
     75                                        return proc.returncode, out.decode("utf-8") if out else None
    7676                                except subprocess.TimeoutExpired:
    77                                         if settings.timeout2gdb:
    78                                                 print("Process {} timeout".format(proc.pid))
    79                                                 proc.communicate()
    80                                                 return 124, str(None)
    81                                         else:
    82                                                 proc.send_signal(signal.SIGABRT)
    83                                                 proc.communicate()
    84                                                 return 124, str(None)
     77                                        proc.send_signal(signal.SIGABRT)
     78                                        proc.communicate()
     79                                        return 124, str(None)
    8580
    8681        except Exception as ex:
     
    8883                raise
    8984
    90 def is_empty(fname):
    91         if not os.path.isfile(fname):
    92                 return True
    93 
    94         if os.stat(fname).st_size == 0:
    95                 return True
    96 
    97         return False
    98 
    9985def is_ascii(fname):
    10086        if settings.dry_run:
    10187                print("is_ascii: %s" % fname)
    102                 return (True, "")
     88                return True
    10389
    10490        if not os.path.isfile(fname):
    105                 return (False, "No file")
    106 
    107         code, out = sh("file", fname, output_file=subprocess.PIPE)
     91                return False
     92
     93        code, out = sh("file %s" % fname, output_file=subprocess.PIPE)
    10894        if code != 0:
    109                 return (False, "'file EXPECT' failed with code {}".format(code))
     95                return False
    11096
    11197        match = re.search(".*: (.*)", out)
    11298
    11399        if not match:
    114                 return (False, "Unreadable file type: '{}'".format(out))
    115 
    116         if "ASCII text" in match.group(1):
    117                 return (True, "")
    118 
    119         return (False, "File type should be 'ASCII text', was '{}'".format(match.group(1)))
     100                return False
     101
     102        return match.group(1).startswith("ASCII text")
    120103
    121104def is_exe(fname):
     
    132115                return None
    133116
    134         file = open(file, mode, encoding="latin-1") # use latin-1 so all chars mean something.
     117        file = open(file, mode)
    135118        exitstack.push(file)
    136119        return file
     
    181164                '-s' if silent else None,
    182165                test_param,
    183                 settings.ast.flags,
    184166                settings.arch.flags,
    185167                settings.debug.flags,
     
    192174        return sh(*cmd, output_file=output_file, error=error)
    193175
    194 def make_recon(target):
    195         cmd = [
    196                 *settings.make,
    197                 '-W',
    198                 os.path.abspath(os.path.join(settings.BUILDDIR, '../driver/cfa')),
    199                 '--recon',
    200                 target
    201         ]
    202         cmd = [s for s in cmd if s]
    203         return sh(*cmd, output_file=subprocess.PIPE)
    204 
    205176def which(program):
    206         fpath, fname = os.path.split(program)
    207         if fpath:
    208                 if is_exe(program):
    209                         return program
    210         else:
    211                 for path in os.environ["PATH"].split(os.pathsep):
    212                         exe_file = os.path.join(path, program)
    213                         if is_exe(exe_file):
    214                                 return exe_file
    215         return None
     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
     187    return None
    216188
    217189@contextlib.contextmanager
     
    262234# helper function to check if a files contains only a specific string
    263235def file_contains_only(file, text) :
    264         with open(file, encoding="latin-1") as f: # use latin-1 so all chars mean something.
     236        with open(file) as f:
    265237                ff = f.read().strip()
    266238                result = ff == text.strip()
     
    270242# transform path to canonical form
    271243def canonical_path(path):
    272         abspath = os.path.abspath(os.path.realpath(__main__.__file__))
     244        abspath = os.path.abspath(__main__.__file__)
    273245        dname = os.path.dirname(abspath)
    274246        return os.path.join(dname, os.path.normpath(path) )
     
    351323        raise argparse.ArgumentTypeError(msg)
    352324
    353 # Convert a function that converts a string to one that converts comma separated string.
    354 def comma_separated(elements):
    355     return lambda string: [elements(part) for part in string.split(',')]
    356 
    357325def fancy_print(text):
    358326        column = which('column')
     
    397365
    398366class Timed:
    399         def __enter__(self):
    400                 self.start = time.time()
    401                 return self
    402 
    403         def __exit__(self, *args):
    404                 self.end = time.time()
    405                 self.duration = self.end - self.start
     367    def __enter__(self):
     368        self.start = time.time()
     369        return self
     370
     371    def __exit__(self, *args):
     372        self.end = time.time()
     373        self.duration = self.end - self.start
    406374
    407375def timed(src, timeout):
    408376        expire = time.time() + timeout
    409377        i = iter(src)
    410         with contextlib.suppress(StopIteration):
    411                 while True:
    412                         yield i.next(max(expire - time.time(), 0))
    413 
    414 def fmtDur( duration ):
    415         if duration :
    416                 hours, rem = divmod(duration, 3600)
    417                 minutes, rem = divmod(rem, 60)
    418                 seconds, millis = divmod(rem, 1)
    419                 return "%2d:%02d.%03d" % (minutes, seconds, millis * 1000)
    420         return " n/a"
     378        while True:
     379                yield i.next(max(expire - time.time(), 0))
Note: See TracChangeset for help on using the changeset viewer.