Skip to content

Instantly share code, notes, and snippets.

@herczy
Created December 2, 2012 10:37
Show Gist options
  • Select an option

  • Save herczy/4188118 to your computer and use it in GitHub Desktop.

Select an option

Save herczy/4188118 to your computer and use it in GitHub Desktop.
#!/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