[bacc36c] | 1 | from __future__ import print_function |
---|
| 2 | |
---|
[c07d724] | 3 | import __main__ |
---|
| 4 | import argparse |
---|
[bacc36c] | 5 | import multiprocessing |
---|
[c07d724] | 6 | import os |
---|
| 7 | import re |
---|
[bacc36c] | 8 | import signal |
---|
[c07d724] | 9 | import stat |
---|
[bacc36c] | 10 | import sys |
---|
[c07d724] | 11 | |
---|
[bacc36c] | 12 | from pybin import settings |
---|
[c07d724] | 13 | from subprocess import Popen, PIPE, STDOUT |
---|
| 14 | |
---|
[bacc36c] | 15 | ################################################################################ |
---|
| 16 | # shell helpers |
---|
| 17 | ################################################################################ |
---|
| 18 | |
---|
[c07d724] | 19 | # helper functions to run terminal commands |
---|
[bacc36c] | 20 | def 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 : |
---|
[c07d724] | 27 | print("cmd: %s" % cmd) |
---|
| 28 | return 0, None |
---|
[bacc36c] | 29 | |
---|
| 30 | # otherwise create a pipe and run the desired command |
---|
| 31 | else : |
---|
[c07d724] | 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 |
---|
[bacc36c] | 37 | def rm( files ): |
---|
[c07d724] | 38 | try: |
---|
| 39 | for file in files: |
---|
[bacc36c] | 40 | sh("rm -f %s > /dev/null 2>&1" % file ) |
---|
[c07d724] | 41 | except TypeError: |
---|
[bacc36c] | 42 | sh("rm -f %s > /dev/null 2>&1" % files ) |
---|
[c07d724] | 43 | |
---|
| 44 | def chdir( dest = __main__.__file__ ): |
---|
| 45 | abspath = os.path.abspath(dest) |
---|
| 46 | dname = os.path.dirname(abspath) |
---|
| 47 | os.chdir(dname) |
---|
| 48 | |
---|
[bacc36c] | 49 | # diff two files |
---|
| 50 | def 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 |
---|
| 72 | def 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, |
---|
[f3b9efc] | 78 | settings.debug.flags, |
---|
[bacc36c] | 79 | flags, |
---|
| 80 | target, |
---|
| 81 | redirects |
---|
| 82 | ]) |
---|
| 83 | return sh(cmd) |
---|
| 84 | |
---|
| 85 | ################################################################################ |
---|
| 86 | # file handling |
---|
| 87 | ################################################################################ |
---|
| 88 | |
---|
[c07d724] | 89 | # helper function to replace patterns in a file |
---|
| 90 | def 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 | |
---|
[0ad0c55] | 105 | # helper function to check if a files contains only a specific string |
---|
[c07d724] | 106 | def 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 |
---|
| 114 | def 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 | |
---|
[bacc36c] | 124 | # transform path to canonical form |
---|
| 125 | def canonicalPath(path): |
---|
| 126 | return os.path.join('.', os.path.normpath(path) ) |
---|
[c07d724] | 127 | |
---|
[bacc36c] | 128 | # compare path even if form is different |
---|
| 129 | def pathCmp(lhs, rhs): |
---|
| 130 | return canonicalPath( lhs ) == canonicalPath( rhs ) |
---|
[c07d724] | 131 | |
---|
[bacc36c] | 132 | # walk all files in a path |
---|
| 133 | def 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 | ################################################################################ |
---|
[0ad0c55] | 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') |
---|
[bacc36c] | 150 | ret, out = make('.dummy', silent = True) |
---|
[0ad0c55] | 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 | |
---|
[f3b9efc] | 162 | if settings.dry_run : |
---|
| 163 | return 'x64' |
---|
| 164 | |
---|
| 165 | return re.search(r"[^,]+,([^,]+),", out).group(1).strip() |
---|
[bacc36c] | 166 | |
---|
| 167 | # count number of jobs to create |
---|
| 168 | def jobCount( options, tests ): |
---|
| 169 | # 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) |
---|
| 176 | else : |
---|
| 177 | options.jobs = multiprocessing.cpu_count() |
---|
| 178 | |
---|
| 179 | # make sure we have a valid number of jobs that corresponds to user input |
---|
| 180 | if options.jobs <= 0 : |
---|
| 181 | print('ERROR: Invalid number of jobs', file=sys.stderr) |
---|
| 182 | sys.exit(1) |
---|
| 183 | |
---|
| 184 | return min( options.jobs, len(tests) ), True if make_flags else False |
---|
| 185 | |
---|
| 186 | # setup a proper processor pool with correct signal handling |
---|
| 187 | def setupPool(jobs): |
---|
| 188 | original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN) |
---|
| 189 | pool = multiprocessing.Pool(jobs) |
---|
| 190 | signal.signal(signal.SIGINT, original_sigint_handler) |
---|
| 191 | |
---|
| 192 | return pool |
---|
| 193 | |
---|
| 194 | # handle signals in scope |
---|
| 195 | class SignalHandling(): |
---|
| 196 | def __enter__(self): |
---|
| 197 | # enable signal handling |
---|
| 198 | signal.signal(signal.SIGINT, signal.SIG_DFL) |
---|
| 199 | |
---|
| 200 | def __exit__(self, type, value, traceback): |
---|
| 201 | # disable signal handling |
---|
| 202 | signal.signal(signal.SIGINT, signal.SIG_IGN) |
---|
| 203 | |
---|
| 204 | ################################################################################ |
---|
| 205 | # misc |
---|
| 206 | ################################################################################ |
---|
| 207 | |
---|
| 208 | # check if arguments is yes or no |
---|
| 209 | def yes_no(string): |
---|
| 210 | if string == "yes" : |
---|
| 211 | return True |
---|
| 212 | if string == "no" : |
---|
| 213 | return False |
---|
| 214 | raise argparse.ArgumentTypeError(msg) |
---|
[f3b9efc] | 215 | return False |
---|
| 216 | |
---|
| 217 | |
---|
| 218 | settings.set_machine_default( getMachineType ) |
---|