source: src/tests/pybin/tools.py @ d142ec5

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since d142ec5 was d142ec5, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Fixed handling of jobs parameter in test.py

  • Property mode set to 100644
File size: 6.2 KB
Line 
1from __future__ import print_function
2
3import __main__
4import argparse
5import multiprocessing
6import os
7import re
8import signal
9import stat
10import sys
11
12from pybin import settings
13from subprocess import Popen, PIPE, STDOUT
14
15################################################################################
16#               shell helpers
17################################################################################
18
19# helper functions to run terminal commands
20def sh(cmd, print2stdout = True, input = None):
21        # add input redirection if needed
22        if input and os.path.isfile(input):
23                cmd += " < %s" % input
24
25        # if this is a dry_run, only print the commands that would be ran
26        if settings.dry_run :
27                print("cmd: %s" % cmd)
28                return 0, None
29
30        # otherwise create a pipe and run the desired command
31        else :
32                proc = Popen(cmd, stdout=None if print2stdout else PIPE, stderr=STDOUT, shell=True)
33                out, err = proc.communicate()
34                return proc.returncode, out
35
36# Remove 1 or more files silently
37def rm( files ):
38        try:
39                for file in files:
40                        sh("rm -f %s > /dev/null 2>&1" % file )
41        except TypeError:
42                sh("rm -f %s > /dev/null 2>&1" % files )
43
44def chdir( dest = __main__.__file__ ):
45        abspath = os.path.abspath(dest)
46        dname = os.path.dirname(abspath)
47        os.chdir(dname)
48
49# diff two files
50def diff( lhs, rhs ):
51        # diff the output of the files
52        diff_cmd = ("diff --ignore-all-space "
53                                "--ignore-blank-lines "
54                                "--old-group-format='\t\tmissing lines :\n"
55                                "%%<' \\\n"
56                                "--new-group-format='\t\tnew lines :\n"
57                                "%%>' \\\n"
58                                "--unchanged-group-format='%%=' \\"
59                                "--changed-group-format='\t\texpected :\n"
60                                "%%<"
61                                "\t\tgot :\n"
62                                "%%>\n' \\\n"
63                                "--new-line-format='\t\t%%dn\t%%L' \\\n"
64                                "--old-line-format='\t\t%%dn\t%%L' \\\n"
65                                "--unchanged-line-format='' \\\n"
66                                "%s %s")
67
68        # fetch return code and error from the diff command
69        return sh(diff_cmd % (lhs, rhs), False)
70
71# call make
72def make(target, flags = '', redirects = '', error_file = None, silent = False):
73        test_param = """test="%s" """ % (error_file) if error_file else ''
74        cmd = ' '.join([
75                settings.make,
76                '-s' if silent else '',
77                test_param,
78                settings.debug.flags,
79                flags,
80                target,
81                redirects
82        ])
83        return sh(cmd)
84
85################################################################################
86#               file handling
87################################################################################
88
89# helper function to replace patterns in a file
90def 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)
104
105# helper function to check if a files contains only a specific string
106def fileContainsOnly(file, text) :
107        with open(file) as f:
108                ff = f.read().strip()
109                result = ff == text.strip()
110
111                return result;
112
113# check whether or not a file is executable
114def fileIsExecutable(file) :
115        try :
116                fileinfo = os.stat(file)
117                return bool(fileinfo.st_mode & stat.S_IXUSR)
118        except Exception as inst:
119                print(type(inst))    # the exception instance
120                print(inst.args)     # arguments stored in .args
121                print(inst)
122                return False
123
124# transform path to canonical form
125def canonicalPath(path):
126        return os.path.join('.', os.path.normpath(path) )
127
128# compare path even if form is different
129def pathCmp(lhs, rhs):
130        return canonicalPath( lhs ) == canonicalPath( rhs )
131
132# walk all files in a path
133def pathWalk( op ):
134        def step(_, dirname, names):
135                for name in names:
136                        path = os.path.join(dirname, name)
137
138                        op( path )
139
140        # Start the walk
141        os.path.walk('.', step, '')
142
143################################################################################
144#               system
145################################################################################
146
147# parses the Makefile to find the machine type (32-bit / 64-bit)
148def 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
167# count number of jobs to create
168def jobCount( options, tests ):
169        # check if the user already passed in a number of jobs for multi-threading
170        if not options.jobs:
171                make_flags = os.environ.get('MAKEFLAGS')
172                force = bool(make_flags)
173                make_jobs_fds = re.search("--jobserver-(auth|fds)=\s*([0-9]+),([0-9]+)", make_flags) if make_flags else None
174                if make_jobs_fds :
175                        tokens = os.read(int(make_jobs_fds.group(2)), 1024)
176                        options.jobs = len(tokens)
177                        os.write(int(make_jobs_fds.group(3)), tokens)
178                else :
179                        options.jobs = multiprocessing.cpu_count()
180        else :
181                force = True
182
183        # make sure we have a valid number of jobs that corresponds to user input
184        if options.jobs <= 0 :
185                print('ERROR: Invalid number of jobs', file=sys.stderr)
186                sys.exit(1)
187
188        return min( options.jobs, len(tests) ), force
189
190# setup a proper processor pool with correct signal handling
191def setupPool(jobs):
192        original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
193        pool = multiprocessing.Pool(jobs)
194        signal.signal(signal.SIGINT, original_sigint_handler)
195
196        return pool
197
198# handle signals in scope
199class SignalHandling():
200        def __enter__(self):
201                # enable signal handling
202                signal.signal(signal.SIGINT, signal.SIG_DFL)
203
204        def __exit__(self, type, value, traceback):
205                # disable signal handling
206                signal.signal(signal.SIGINT, signal.SIG_IGN)
207
208################################################################################
209#               misc
210################################################################################
211
212# check if arguments is yes or no
213def yes_no(string):
214        if string == "yes" :
215                return True
216        if string == "no" :
217                return False
218        raise argparse.ArgumentTypeError(msg)
219        return False
220
221
222settings.set_machine_default( getMachineType )
Note: See TracBrowser for help on using the repository browser.