blob: a526f712f3618e09a64fc3ec6ead3d5bf52463dd [file] [log] [blame]
Joe Gregorio80bc9312011-03-13 00:49:18 -05001#!/usr/bin/env python
2"""Execute all sample applications.
3
4Runs over all the sample applications, determines their type (App Engine,
5Django, or a command-line application), and then runs them checking for a good
6return status in the case of command-line applications and a 200 OK response in
7the case of the App Engine and Django samples.
8"""
9import gflags
10import httplib2
11import logging
12import os
13import signal
14import subprocess
15import sys
16import time
17
18FLAGS = gflags.FLAGS
19
20gflags.DEFINE_list('samples_to_skip', ['latitude'],
21 'A comma separated list of project directory names to be skipped.')
22
23gflags.DEFINE_enum('logging_level', 'INFO',
24 ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
25 'Set the level of logging detail.')
26
27gflags.DEFINE_string('app_engine_dir', '../google_appengine/',
28 'Directory where Google App Engine is installed.')
29
30gflags.DEFINE_string('sample_root', 'samples/oauth2',
31 'The root directory for all the samples.')
32
33
34def 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):
Joe Gregorio1ef83e12011-04-05 10:53:49 -040044 fulldirname = os.path.join(FLAGS.sample_root, dirname)
Joe Gregorio80bc9312011-03-13 00:49:18 -050045 if dirname in FLAGS.samples_to_skip:
46 logging.debug('Skipping ' + fulldirname + ' (blacklist)')
47 continue
Joe Gregorio80bc9312011-03-13 00:49:18 -050048 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
100if __name__ == '__main__':
101 main(sys.argv)