|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Execute all sample applications. |
| 3 | +
|
| 4 | +Runs over all the sample applications, determines their type (App Engine, |
| 5 | +Django, or a command-line application), and then runs them checking for a good |
| 6 | +return status in the case of command-line applications and a 200 OK response in |
| 7 | +the case of the App Engine and Django samples. |
| 8 | +""" |
| 9 | +import gflags |
| 10 | +import httplib2 |
| 11 | +import logging |
| 12 | +import os |
| 13 | +import signal |
| 14 | +import subprocess |
| 15 | +import sys |
| 16 | +import time |
| 17 | + |
| 18 | +FLAGS = gflags.FLAGS |
| 19 | + |
| 20 | +gflags.DEFINE_list('samples_to_skip', ['latitude'], |
| 21 | + 'A comma separated list of project directory names to be skipped.') |
| 22 | + |
| 23 | +gflags.DEFINE_enum('logging_level', 'INFO', |
| 24 | + ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
| 25 | + 'Set the level of logging detail.') |
| 26 | + |
| 27 | +gflags.DEFINE_string('app_engine_dir', '../google_appengine/', |
| 28 | + 'Directory where Google App Engine is installed.') |
| 29 | + |
| 30 | +gflags.DEFINE_string('sample_root', 'samples/oauth2', |
| 31 | + 'The root directory for all the samples.') |
| 32 | + |
| 33 | + |
| 34 | +def main(argv): |
| 35 | + try: |
| 36 | + argv = FLAGS(argv) |
| 37 | + except gflags.FlagsError, e: |
| 38 | + print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS) |
| 39 | + sys.exit(1) |
| 40 | + |
| 41 | + logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level)) |
| 42 | + |
| 43 | + for dirname in os.listdir(FLAGS.sample_root): |
| 44 | + if dirname in FLAGS.samples_to_skip: |
| 45 | + logging.debug('Skipping ' + fulldirname + ' (blacklist)') |
| 46 | + continue |
| 47 | + fulldirname = os.path.join(FLAGS.sample_root, dirname) |
| 48 | + filelist = os.listdir(fulldirname) |
| 49 | + if 'settings.py' in filelist and 'manage.py' in filelist: |
| 50 | + logging.info(fulldirname + ' [Django]') |
| 51 | + proc = subprocess.Popen( |
| 52 | + [os.path.join(fulldirname, 'manage.py'), |
| 53 | + 'runserver']) |
| 54 | + # Now just wait, because Django actually spawns a sub-process that does |
| 55 | + # the I/O and does something funky with stdout so we can't read it and |
| 56 | + # figure out when it is started. |
| 57 | + time.sleep(3) |
| 58 | + h = httplib2.Http() |
| 59 | + resp, content = h.request('http://localhost:8000/') |
| 60 | + assert(200 == resp.status) |
| 61 | + time.sleep(1) |
| 62 | + logging.debug('Django ppid: %d', proc.pid) |
| 63 | + # Find and kill the sub-process manage.py forked. |
| 64 | + findpids = subprocess.Popen(['ps', '--ppid', str(proc.pid), 'o', 'pid',], |
| 65 | + stdout=subprocess.PIPE) |
| 66 | + for p in findpids.stdout.readlines(): |
| 67 | + if 'PID' not in p: |
| 68 | + os.kill(int(p), signal.SIGINT) |
| 69 | + os.kill(proc.pid, signal.SIGINT) |
| 70 | + proc.wait() |
| 71 | + elif 'app.yaml' in filelist: |
| 72 | + logging.info(fulldirname + ' [App Engine]') |
| 73 | + proc = subprocess.Popen( |
| 74 | + [os.path.join(FLAGS.app_engine_dir, 'dev_appserver.py'), |
| 75 | + fulldirname], |
| 76 | + stdout=subprocess.PIPE, |
| 77 | + stderr=subprocess.STDOUT) |
| 78 | + line = proc.stdout.readline() |
| 79 | + logging.debug('READ: ' + line) |
| 80 | + while '] Running application' not in line: |
| 81 | + line = proc.stdout.readline() |
| 82 | + logging.debug('READ: ' + line) |
| 83 | + h = httplib2.Http() |
| 84 | + resp, content = h.request('http://localhost:8080/') |
| 85 | + assert(200 == resp.status) |
| 86 | + time.sleep(1) |
| 87 | + os.kill(proc.pid, signal.SIGINT) |
| 88 | + proc.wait() |
| 89 | + else: |
| 90 | + logging.info(fulldirname + ' [Command-line]') |
| 91 | + for filename in os.listdir(fulldirname): |
| 92 | + if filename.endswith('.py'): |
| 93 | + logging.info('Running: ' + filename) |
| 94 | + proc = subprocess.Popen(['python', |
| 95 | + os.path.join(fulldirname, filename)]) |
| 96 | + returncode = proc.wait() |
| 97 | + assert(returncode == 0) |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + main(sys.argv) |
| 102 | + |
0 commit comments