Created
December 2, 2012 10:37
-
-
Save herczy/4188118 to your computer and use it in GitHub Desktop.
BOFH gcc compiler - see http://bofh.ntk.net/BOFH/0000/bastard11.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import os | |
| import sys | |
| import random | |
| import hashlib | |
| import tempfile | |
| import subprocess | |
| import atexit | |
| PROBABILITY = 1 | |
| cexts = set(('c', 'cc', 'cpp', 'cxx')) | |
| def check_drop(): | |
| return random.randint(0, 99) < PROBABILITY | |
| def make_problems(ofn): | |
| digest = hashlib.new('md5') | |
| digest.update(open(ofn).read()) | |
| random.seed(digest.hexdigest()) | |
| ext = ofn.rsplit('.', 1)[-1] | |
| fd, name = tempfile.mkstemp(suffix='.%s' % ext, dir='.') | |
| atexit.register(lambda fn: os.remove(fn), name) | |
| with os.fdopen(fd, 'w') as outf: | |
| with open(ofn) as inf: | |
| for line in open(ofn): | |
| if not check_drop(): | |
| outf.write(line) | |
| return name | |
| # Parse arguments for C/C++ files | |
| args = list(sys.argv) | |
| c_files = [(i, x) for i, x in enumerate(sys.argv) if x.rsplit('.', 1)[-1] in cexts] | |
| # Copy files to temp and introduce problems | |
| replacements = {} | |
| for index, filename in c_files: | |
| replacements[filename] = make_problems(filename) | |
| args[index] = replacements[filename] | |
| pipe = subprocess.Popen(['/usr/bin/%s' % os.path.basename(sys.argv[0])] + args[1:], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=os.getcwd(), env=os.environ) | |
| rc = pipe.poll() | |
| out = pipe.stdout.read() | |
| err = pipe.stderr.read() | |
| for inf, outf in replacements.items(): | |
| out = out.replace(outf, inf) | |
| err = err.replace(outf, inf) | |
| sys.stdout.write(out) | |
| sys.stderr.write(err) | |
| exit(rc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment