Ignore:
Timestamp:
Aug 27, 2018, 4:40:34 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
b7c89aa
Parents:
f9feab8 (diff), 305581d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into cleanup-dtors

File:
1 moved

Legend:

Unmodified
Added
Removed
  • tests/pybin/tools.py

    rf9feab8 r90152a4  
    99import stat
    1010import sys
     11import fileinput
    1112
    1213from pybin import settings
     
    3435                return proc.returncode, out
    3536
     37def is_ascii(fname):
     38        if settings.dry_run:
     39                print("is_ascii: %s" % fname)
     40                return True
     41
     42        if not os.path.isfile(fname):
     43                return False
     44
     45        code, out = sh("file %s" % fname, print2stdout = False)
     46        if code != 0:
     47                return False
     48
     49        match = re.search(".*: (.*)", out)
     50
     51        if not match:
     52                return False
     53
     54        return match.group(1).startswith("ASCII text")
     55
    3656# Remove 1 or more files silently
    3757def rm( files ):
    38         try:
     58        if isinstance( files, basestring ):
     59                sh("rm -f %s > /dev/null 2>&1" % files )
     60        else:
    3961                for file in files:
    4062                        sh("rm -f %s > /dev/null 2>&1" % file )
    41         except TypeError:
    42                 sh("rm -f %s > /dev/null 2>&1" % files )
     63
     64# Create 1 or more directory
     65def mkdir( files ):
     66        if isinstance( files, basestring ):
     67                sh("mkdir -p %s" % os.path.dirname(files) )
     68        else:
     69                for file in files:
     70                        sh("mkdir -p %s" % os.path.dirname(file) )
     71
    4372
    4473def chdir( dest = __main__.__file__ ):
     
    5079def diff( lhs, rhs ):
    5180        # diff the output of the files
    52         diff_cmd = ("diff --ignore-all-space "
     81        diff_cmd = ("diff --ignore-all-space --text "
    5382                                "--ignore-blank-lines "
    5483                                "--old-group-format='\t\tmissing lines :\n"
     
    76105                '-s' if silent else '',
    77106                test_param,
     107                settings.arch.flags,
    78108                settings.debug.flags,
     109                settings.install.flags,
    79110                flags,
    80111                target,
     
    83114        return sh(cmd)
    84115
     116def which(program):
     117    import os
     118    def is_exe(fpath):
     119        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
     120
     121    fpath, fname = os.path.split(program)
     122    if fpath:
     123        if is_exe(program):
     124            return program
     125    else:
     126        for path in os.environ["PATH"].split(os.pathsep):
     127            exe_file = os.path.join(path, program)
     128            if is_exe(exe_file):
     129                return exe_file
     130
     131    return None
    85132################################################################################
    86133#               file handling
     
    89136# helper function to replace patterns in a file
    90137def file_replace(fname, pat, s_after):
    91     # first, see if the pattern is even in the file.
    92     with open(fname) as f:
    93         if not any(re.search(pat, line) for line in f):
    94             return # pattern does not occur in file so we are done.
    95 
    96     # pattern is in the file, so perform replace operation.
    97     with open(fname) as f:
    98         out_fname = fname + ".tmp"
    99         out = open(out_fname, "w")
    100         for line in f:
    101             out.write(re.sub(pat, s_after, line))
    102         out.close()
    103         os.rename(out_fname, fname)
     138        if settings.dry_run:
     139                print("replacing '%s' with '%s' in %s" % (pat, s_after, fname))
     140                return
     141
     142        file = fileinput.FileInput(fname, inplace=True, backup='.bak')
     143        for line in file:
     144                print(line.replace(pat, s_after), end='')
     145        file.close()
    104146
    105147# helper function to check if a files contains only a specific string
     
    124166# transform path to canonical form
    125167def canonicalPath(path):
    126         return os.path.join('.', os.path.normpath(path) )
     168        abspath = os.path.abspath(__main__.__file__)
     169        dname = os.path.dirname(abspath)
     170        return os.path.join(dname, os.path.normpath(path) )
    127171
    128172# compare path even if form is different
     
    135179                for name in names:
    136180                        path = os.path.join(dirname, name)
    137 
    138181                        op( path )
    139182
    140183        # Start the walk
    141         os.path.walk('.', step, '')
     184        dname = settings.SRCDIR
     185        os.path.walk(dname, step, '')
    142186
    143187################################################################################
    144188#               system
    145189################################################################################
    146 
    147 # parses the Makefile to find the machine type (32-bit / 64-bit)
    148 def getMachineType():
    149         sh('echo "void ?{}(int&a,int b){}int main(){return 0;}" > .dummy.c')
    150         ret, out = make('.dummy', silent = True)
    151 
    152         if ret != 0:
    153                 print("Failed to identify architecture:")
    154                 print(out)
    155                 print("Stopping")
    156                 rm( (".dummy.c",".dummy") )
    157                 sys.exit(1)
    158 
    159         _, out = sh("file .dummy", print2stdout=False)
    160         rm( (".dummy.c",".dummy") )
    161 
    162         if settings.dry_run :
    163                 return 'x64'
    164 
    165         return re.search(r"[^,]+,([^,]+),", out).group(1).strip()
    166 
    167190# count number of jobs to create
    168191def jobCount( options, tests ):
    169192        # check if the user already passed in a number of jobs for multi-threading
    170         make_flags = os.environ.get('MAKEFLAGS')
    171         make_jobs_fds = re.search("--jobserver-(auth|fds)=\s*([0-9]+),([0-9]+)", make_flags) if make_flags else None
    172         if make_jobs_fds :
    173                 tokens = os.read(int(make_jobs_fds.group(2)), 1024)
    174                 options.jobs = len(tokens)
    175                 os.write(int(make_jobs_fds.group(3)), tokens)
     193        if not options.jobs:
     194                make_flags = os.environ.get('MAKEFLAGS')
     195                force = bool(make_flags)
     196                make_jobs_fds = re.search("--jobserver-(auth|fds)=\s*([0-9]+),([0-9]+)", make_flags) if make_flags else None
     197                if make_jobs_fds :
     198                        tokens = os.read(int(make_jobs_fds.group(2)), 1024)
     199                        options.jobs = len(tokens)
     200                        os.write(int(make_jobs_fds.group(3)), tokens)
     201                else :
     202                        options.jobs = multiprocessing.cpu_count()
    176203        else :
    177                 options.jobs = multiprocessing.cpu_count()
     204                force = True
    178205
    179206        # make sure we have a valid number of jobs that corresponds to user input
     
    182209                sys.exit(1)
    183210
    184         return min( options.jobs, len(tests) ), True if make_flags else False
     211        return min( options.jobs, len(tests) ), force
    185212
    186213# setup a proper processor pool with correct signal handling
     
    215242        return False
    216243
    217 
    218 settings.set_machine_default( getMachineType )
     244def fancy_print(text):
     245        column = which('column')
     246        if column:
     247                cmd = "%s 2> /dev/null" % column
     248                print(cmd)
     249                proc = Popen(cmd, stdin=PIPE, stderr=None, shell=True)
     250                proc.communicate(input=text)
     251        else:
     252                print(text)
Note: See TracChangeset for help on using the changeset viewer.