Refactor duplicate code for calling into media controls
[WebKit-https.git] / Websites / perf.webkit.org / tools / remote-cache-server.py
1 #!/usr/bin/python
2
3 import json
4 import os
5 import shutil
6 import subprocess
7 import sys
8 import tempfile
9
10
11 def main():
12     if len(sys.argv) <= 1:
13         sys.exit('Specify the command: start, stop, reset')
14
15     command = sys.argv[1]
16
17     with open(abspath_from_root('config.json')) as config_file:
18         config = json.load(config_file)
19
20     cache_dir = abspath_from_root(config['cacheDirectory'])
21     if command == 'start':
22         if not os.path.isdir(cache_dir):
23             os.makedirs(cache_dir)
24         os.chmod(cache_dir, 0755)
25
26         start_httpd(config['remoteServer'])
27     elif command == 'stop':
28         stop_httpd(config['remoteServer'])
29     elif command == 'reset':
30         shutil.rmtree(cache_dir)
31     else:
32         sys.exit('Unknown command: ' + command)
33
34
35 def start_httpd(remote_server_config):
36     httpd_config_file = abspath_from_root(remote_server_config['httpdConfig'])
37     httpd_pid_file = abspath_from_root(remote_server_config['httpdPID'])
38     httpd_error_log_file = abspath_from_root(remote_server_config['httpdErrorLog'])
39     httpd_mutex_dir = abspath_from_root(remote_server_config['httpdMutexDir'])
40     version_info = subprocess.check_output(['php', '-v'], stderr=subprocess.PIPE)
41     php_version = 'PHP5' if 'PHP 5' in version_info else 'PHP7'
42
43     if not os.path.isdir(httpd_mutex_dir):
44         os.makedirs(httpd_mutex_dir)
45     os.chmod(httpd_mutex_dir, 0755)
46
47     doc_root = abspath_from_root('public')
48
49     # don't use -X since http://svn.apache.org/viewvc?view=revision&revision=1711479
50     subprocess.call(['httpd',
51         '-f', httpd_config_file,
52         '-c', 'PidFile ' + httpd_pid_file,
53         '-c', 'Mutex file:' + httpd_mutex_dir,
54         '-c', 'DocumentRoot ' + doc_root,
55         '-c', 'ErrorLog ' + httpd_error_log_file,
56         '-D', php_version])
57
58 def stop_httpd(remote_server_config):
59     httpd_pid_file = abspath_from_root(remote_server_config['httpdPID'])
60     if not os.path.isfile(httpd_pid_file):
61         sys.exit("PID file doesn't exist at %s" % httpd_pid_file)
62
63     with open(httpd_pid_file) as pid_file:
64         pid = pid_file.read().strip()
65         print "Stopping", pid
66         subprocess.call(['kill', '-TERM', pid])
67
68
69 def abspath_from_root(relpath):
70     return os.path.abspath(os.path.join(os.path.dirname(__file__), '../', relpath))
71
72
73 if __name__ == "__main__":
74     main()