Refactor duplicate code for calling into media controls
[WebKit-https.git] / Websites / perf.webkit.org / tools / process-maintenance-backlog.py
1 #!/usr/bin/python
2
3 import argparse
4 import json
5 import os
6 import sys
7 import urllib2
8
9 from util import load_server_config
10
11
12 def main():
13     parser = argparse.ArgumentParser()
14     parser.add_argument('--server-config-json', required=True, help='The path to a JSON file that specifies the perf dashboard.')
15     args = parser.parse_args()
16
17     maintenace_dir = determine_maintenance_dir()
18
19     server_config = load_server_config(args.server_config_json)
20
21     print 'Submitting results in "%s" to "%s"' % (maintenace_dir, server_config['server']['url'])
22
23     for filename in os.listdir(maintenace_dir):
24         path = os.path.join(maintenace_dir, filename)
25         if os.path.isfile(path) and filename.endswith('.json'):
26
27             with open(os.path.join(maintenace_dir, path)) as submitted_json_file:
28                 submitted_content = submitted_json_file.read()
29
30             print '%s...' % filename,
31             sys.stdout.flush()
32
33             suffix = '.done'
34             while True:
35                 if submit_report(server_config, submitted_content):
36                     break
37                 if ask_yes_no_question('Suffix the file with .error and continue?'):
38                     suffix = '.error'
39                     break
40                 else:
41                     sys.exit(0)
42
43             os.rename(path, path + suffix)
44
45             print 'Done'
46
47
48 def determine_maintenance_dir():
49     root_dir = os.path.join(os.path.dirname(__file__), '../')
50
51     config_json_path = os.path.abspath(os.path.join(root_dir, 'config.json'))
52     with open(config_json_path) as config_json_file:
53         config = json.load(config_json_file)
54
55     dirname = config.get('maintenanceDirectory')
56     if not dirname:
57         sys.exit('maintenanceDirectory is not specified in config.json')
58
59     return os.path.abspath(os.path.join(root_dir, dirname))
60
61
62 def ask_yes_no_question(question):
63     while True:
64         action = raw_input(question + ' (y/n): ')
65         if action == 'y' or action == 'yes':
66             return True
67         elif action == 'n' or action == 'no':
68             return False
69         else:
70             print 'Please answer by yes or no'
71
72
73 def submit_report(server_config, payload):
74     try:
75         request = urllib2.Request(server_config['server']['url'] + '/api/report')
76         request.add_header('Content-Type', 'application/json')
77         request.add_header('Content-Length', len(payload))
78
79         output = urllib2.urlopen(request, payload).read()
80         try:
81             result = json.loads(output)
82         except Exception, error:
83             raise Exception(error, output)
84
85         if result.get('status') != 'OK':
86             raise Exception(result)
87
88         return True
89     except Exception as error:
90         print >> sys.stderr, 'Failed to submit commits: %s' % str(error)
91         return False
92
93
94 if __name__ == "__main__":
95     main()