Changeset 90152a4 for tests/pybin/tools.py
- Timestamp:
- Aug 27, 2018, 4:40:34 PM (7 years ago)
- 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. - File:
-
- 1 moved
-
tests/pybin/tools.py (moved) (moved from src/tests/pybin/tools.py ) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
tests/pybin/tools.py
rf9feab8 r90152a4 9 9 import stat 10 10 import sys 11 import fileinput 11 12 12 13 from pybin import settings … … 34 35 return proc.returncode, out 35 36 37 def 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 36 56 # Remove 1 or more files silently 37 57 def rm( files ): 38 try: 58 if isinstance( files, basestring ): 59 sh("rm -f %s > /dev/null 2>&1" % files ) 60 else: 39 61 for file in files: 40 62 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 65 def 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 43 72 44 73 def chdir( dest = __main__.__file__ ): … … 50 79 def diff( lhs, rhs ): 51 80 # diff the output of the files 52 diff_cmd = ("diff --ignore-all-space "81 diff_cmd = ("diff --ignore-all-space --text " 53 82 "--ignore-blank-lines " 54 83 "--old-group-format='\t\tmissing lines :\n" … … 76 105 '-s' if silent else '', 77 106 test_param, 107 settings.arch.flags, 78 108 settings.debug.flags, 109 settings.install.flags, 79 110 flags, 80 111 target, … … 83 114 return sh(cmd) 84 115 116 def 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 85 132 ################################################################################ 86 133 # file handling … … 89 136 # helper function to replace patterns in a file 90 137 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) 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() 104 146 105 147 # helper function to check if a files contains only a specific string … … 124 166 # transform path to canonical form 125 167 def 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) ) 127 171 128 172 # compare path even if form is different … … 135 179 for name in names: 136 180 path = os.path.join(dirname, name) 137 138 181 op( path ) 139 182 140 183 # Start the walk 141 os.path.walk('.', step, '') 184 dname = settings.SRCDIR 185 os.path.walk(dname, step, '') 142 186 143 187 ################################################################################ 144 188 # system 145 189 ################################################################################ 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 167 190 # count number of jobs to create 168 191 def jobCount( options, tests ): 169 192 # 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() 176 203 else : 177 options.jobs = multiprocessing.cpu_count()204 force = True 178 205 179 206 # make sure we have a valid number of jobs that corresponds to user input … … 182 209 sys.exit(1) 183 210 184 return min( options.jobs, len(tests) ), True if make_flags else False211 return min( options.jobs, len(tests) ), force 185 212 186 213 # setup a proper processor pool with correct signal handling … … 215 242 return False 216 243 217 218 settings.set_machine_default( getMachineType ) 244 def 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.