Source

pythonwise / joborder.py

#!/usr/bin/env python
'''Show dependencies of Azkaban jobs.

Depenencies are in *.job files with a line starting with 'dependencies='.

You'll need graphviz "dot" utility in your path.
'''

from glob import glob
import re
from os.path import isdir, join, basename
from tempfile import NamedTemporaryFile
from subprocess import call
from sys import platform


def iter_dependencies(job_files, keep=False):
    '''Return a generator of (job, dep) from job_files.'''
    # Dependency line: dependencies=cooljob
    find_dep = re.compile('dependencies=(\w+)').search
    for job_file in job_files:
        name = basename(job_file)[:-4]
        with open(job_file) as fo:
            match = find_dep(fo.read())

            if not match:
                continue

            yield name, match.group(1)


def gen_image(dependencies, image):
    '''Generate an image from dependencies using "dot".'''
    dotfile = NamedTemporaryFile(suffix='.dot')
    dotfile.write('digraph G {\n')
    for job, dep in dependencies:
        dotfile.write('    {} -> {};\n'.format(job, dep))
    dotfile.write('}\n')
    dotfile.flush()

    return call(['dot', '-Tpng', '-o', image, dotfile.name]) == 0


def show_image(path):
    '''Show image using OS default viewer.'''
    cmd = {
        'darwin': 'open',
        'linux2': 'xdg-open',
        'win32': 'start',
    }[platform]

    return call([cmd, path]) == 0


def main(argv=None):
    import sys
    from argparse import ArgumentParser

    argv = argv or sys.argv

    parser = ArgumentParser(description='Show job order in directory')
    parser.add_argument('directory', help='jobs directory [.]', default='.',
                       nargs='?')
    args = parser.parse_args(argv[1:])

    if not isdir(args.directory):
        raise SystemExit('error: {} is not a directory'.format(args.directory))

    jobfiles = glob(join(args.directory, '*.job'))
    if not jobfiles:
        raise SystemExit('error: no *.job files in {}'.format(args.directory))

    image = '/tmp/job-deps.png'
    dependencies = iter_dependencies(jobfiles)
    if not gen_image(dependencies, image):
        raise SystemExit('error: cannot run dot (PATH problem?)')

    if not show_image(image):
        raise SystemExit('error: cannot show {}'.format(image))

if __name__ == '__main__':
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.