The HR team at Initrode were a happy bunch, casting their nets into the perpetual stream of eager undergrads from nearby WTF U. It was a summer tradition at Initrode to invite a school of juniors to get a taste of their future by spending the long, sun-drenched afternoons of their dwindling youth hunched in cubicles.

Chris was on the Dev Tools Team at Initrode, building widgets and gizmos to help his fellow developers be more productive. Since few of his colleagues were willing to unleash students on production code, the duds among the summer-student pool tended to end up on Chris's team. And that's why the intern at the center of this SOD bears the pseudonym Dudley.

The Dev Tools summer project was to replace their build scripts, a kludge of bash and Perl, with an orderly Python script. Rewriting these tools in Python would let Chris put a nice web front-end on top so his fellow developers could run the tools from the Initrode intranet. He was therefore eager to see the results of Dudley's work on the bit of the code-analysis suite responsible for measuring cyclomatic complexity.

Unfortunately, when asked to implement this in Python:

find $1 -name '*.[c]' | xargs pmccabe | awk '{ print $1, $4, $5, $6, $7}'

Dudley did exactly that:

from subprocess import Popen, check_call, CalledProcessError 
DEVNULL = open(os.devnull, 'w')

def check_for_error(stderr): 
    if stderr:
        sys.stderr.write(stderr) 
        sys.exit(1)

p = Popen(['find', directory, '-name', '*.[c]'], 
    stdout=subprocess.PIPE, stderr=DEVNULL)
stdout, stderr = p.communicate() 
check_for_error(stderr)
p = Popen(['xargs', 'pmccabe'], stdin=subprocess.PIPE,
    stdout=subprocess.PIPE, stderr=DEVNULL)
stdout, stderr = p.communicate(stdout)
check_for_error(stderr)
p = Popen(['awk', ('$1 > %d' % max_limit)], 
    stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=DEVNULL) 
stdout, stderr = p.communicate(stdout) 
check_for_error(stderr)
p = Popen(['awk', ('{ print $1, $4, $5, $6, $7}'],
    stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=DEVNULL) 
results, stderr = p.communicate(stdout)
check_for_error(stderr)

Chris had hoped Dudley would use Python's extensive function library to provide the inputs to pmccabe and parse its output in a more compact, expressive way than they'd done on the command line. But he had to admit Dudley had earned an E for effort.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!