1#!/usr/bin/env python
2# Copyright (c) 2014 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import argparse
7import json
8import os
9import sys
10
11tracing_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
12                                            '..', '..'))
13if tracing_path not in sys.path:
14  sys.path.append(tracing_path)
15
16from tracing import tracing_project
17
18from build import dev_server
19
20
21def getFilesIn(basedir):
22  data_files = []
23  for dirpath, dirnames, filenames in os.walk(basedir, followlinks=True):
24    new_dirnames = [d for d in dirnames if not d.startswith('.')]
25    del dirnames[:]
26    dirnames += new_dirnames
27
28    for f in filenames:
29      if f.startswith('.'):
30        continue
31      if f == 'README.md':
32        continue
33      full_f = os.path.join(dirpath, f)
34      rel_f = os.path.relpath(full_f, basedir)
35      data_files.append(rel_f)
36
37  data_files.sort()
38  return data_files
39
40
41def do_GET_json_examples(request):
42  data_files = getFilesIn(request.server.data_dir)
43  files_as_json = json.dumps(data_files)
44
45  request.send_response(200)
46  request.send_header('Content-Type', 'application/json')
47  request.send_header('Content-Length', len(files_as_json))
48  request.end_headers()
49  request.wfile.write(files_as_json)
50
51
52def do_GET_json_examples_skp(request):
53  data_files = getFilesIn(request.server.skp_data_dir)
54  files_as_json = json.dumps(data_files)
55
56  request.send_response(200)
57  request.send_header('Content-Type', 'application/json')
58  request.send_header('Content-Length', len(files_as_json))
59  request.end_headers()
60  request.wfile.write(files_as_json)
61
62
63def do_GET_json_tests(self):
64  test_module_resources = self.server.project.FindAllTestModuleResources()
65
66  test_relpaths = [x.unix_style_relative_path
67                   for x in test_module_resources]
68
69  tests = {'test_relpaths': test_relpaths}
70  tests_as_json = json.dumps(tests)
71
72  self.send_response(200)
73  self.send_header('Content-Type', 'application/json')
74  self.send_header('Content-Length', len(tests_as_json))
75  self.end_headers()
76  self.wfile.write(tests_as_json)
77
78
79def do_POST_report_test_results(request):
80  request.send_response(200)
81  request.send_header('Content-Length', '0')
82  request.end_headers()
83  msg = request.rfile.read()
84  ostream = sys.stdout if 'PASSED' in msg else sys.stderr
85  ostream.write(msg + '\n')
86
87
88def do_POST_report_test_completion(request):
89  request.send_response(200)
90  request.send_header('Content-Length', '0')
91  request.end_headers()
92  msg = request.rfile.read()
93  sys.stdout.write(msg + '\n')
94  request.server.RequestShutdown(exit_code=(0 if 'ALL_PASSED' in msg else 1))
95
96
97def Main(args):
98  project = tracing_project.TracingProject()
99
100  parser = argparse.ArgumentParser(description='Run tracing development server')
101  parser.add_argument(
102      '-d', '--data-dir',
103      default=os.path.abspath(os.path.join(project.test_data_path)))
104  parser.add_argument(
105      '-s', '--skp-data-dir',
106      default=os.path.abspath(os.path.join(project.skp_data_path)))
107  parser.add_argument('-p', '--port', default=8003, type=int)
108  args = parser.parse_args(args=args)
109
110  server = dev_server.DevServer(port=args.port, project=project)
111  server.data_dir = os.path.abspath(args.data_dir)
112  server.skp_data_dir = os.path.abspath(args.skp_data_dir)
113  project.source_paths.append(server.data_dir)
114
115  server.AddPathHandler('/json/examples', do_GET_json_examples)
116  server.AddPathHandler('/tr/json/tests', do_GET_json_tests)
117  server.AddPathHandler('/json/examples/skp', do_GET_json_examples_skp)
118
119  server.AddSourcePathMapping(project.tracing_root_path)
120
121  server.AddPathHandler('/test_automation/notify_test_result',
122                        do_POST_report_test_results, supports_post=True)
123  server.AddPathHandler('/test_automation/notify_completion',
124                        do_POST_report_test_completion, supports_post=True)
125
126  server.serve_forever()
127