OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright (c) 2006-2009 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 |
| 6 """Platform-specific utility methods shared by several scripts.""" |
| 7 |
| 8 import os |
| 9 import subprocess |
| 10 |
| 11 import google.path_utils |
| 12 |
| 13 |
| 14 class PlatformUtility(object): |
| 15 def __init__(self, base_dir): |
| 16 """Args: |
| 17 base_dir: the base dir for running tests. |
| 18 """ |
| 19 self._base_dir = base_dir |
| 20 self._httpd_cmd_string = None # used for starting/stopping httpd |
| 21 self._bash = "/bin/bash" |
| 22 |
| 23 def _UnixRoot(self): |
| 24 """Returns the path to root.""" |
| 25 return "/" |
| 26 |
| 27 def GetFilesystemRoot(self): |
| 28 """Returns the root directory of the file system.""" |
| 29 return self._UnixRoot() |
| 30 |
| 31 def GetTempDirectory(self): |
| 32 """Returns the file system temp directory |
| 33 |
| 34 Note that this does not use a random subdirectory, so it's not |
| 35 intrinsically secure. If you need a secure subdir, use the tempfile |
| 36 package. |
| 37 """ |
| 38 return os.getenv("TMPDIR", "/tmp") |
| 39 |
| 40 def FilenameToUri(self, path, use_http=False, use_ssl=False, port=8000): |
| 41 """Convert a filesystem path to a URI. |
| 42 |
| 43 Args: |
| 44 path: For an http URI, the path relative to the httpd server's |
| 45 DocumentRoot; for a file URI, the full path to the file. |
| 46 use_http: if True, returns a URI of the form http://127.0.0.1:8000/. |
| 47 If False, returns a file:/// URI. |
| 48 use_ssl: if True, returns HTTPS URL (https://127.0.0.1:8000/). |
| 49 This parameter is ignored if use_http=False. |
| 50 port: The port number to append when returning an HTTP URI |
| 51 """ |
| 52 if use_http: |
| 53 protocol = 'http' |
| 54 if use_ssl: |
| 55 protocol = 'https' |
| 56 return "%s://127.0.0.1:%d/%s" % (protocol, port, path) |
| 57 return "file://" + path |
| 58 |
| 59 def GetStartHttpdCommand(self, output_dir, |
| 60 httpd_conf_path, mime_types_path, |
| 61 document_root=None, apache2=False): |
| 62 """Prepares the config file and output directory to start an httpd server. |
| 63 Returns a list of strings containing the server's command line+args. |
| 64 |
| 65 Args: |
| 66 output_dir: the path to the server's output directory, for log files. |
| 67 It will be created if necessary. |
| 68 httpd_conf_path: full path to the httpd.conf file to be used. |
| 69 mime_types_path: full path to the mime.types file to be used. |
| 70 document_root: full path to the DocumentRoot. If None, the DocumentRoot |
| 71 from the httpd.conf file will be used. Note that the httpd.conf |
| 72 file alongside this script does not specify any DocumentRoot, so if |
| 73 you're using that one, be sure to specify a document_root here. |
| 74 apache2: boolean if true will cause this function to return start |
| 75 command for Apache 2.x as opposed to Apache 1.3.x. This flag |
| 76 is ignored on Linux (but preserved here for compatibility in |
| 77 function signature with win), where apache2 is used always |
| 78 """ |
| 79 |
| 80 exe_name = "apache2" |
| 81 cert_file = google.path_utils.FindUpward(self._base_dir, 'tools', |
| 82 'python', 'google', |
| 83 'httpd_config', 'httpd2.pem') |
| 84 httpd_vars = { |
| 85 "httpd_executable_path": |
| 86 os.path.join(self._UnixRoot(), "usr", "sbin", exe_name), |
| 87 "httpd_conf_path": httpd_conf_path, |
| 88 "ssl_certificate_file": cert_file, |
| 89 "document_root" : document_root, |
| 90 "server_root": os.path.join(self._UnixRoot(), "usr"), |
| 91 "mime_types_path": mime_types_path, |
| 92 "output_dir": output_dir, |
| 93 "user": os.environ.get("USER", "#%d" % os.geteuid()), |
| 94 "lock_file": os.path.join(output_dir, "accept.lock"), |
| 95 } |
| 96 |
| 97 google.path_utils.MaybeMakeDirectory(output_dir) |
| 98 |
| 99 # We have to wrap the command in bash |
| 100 # -C: process directive before reading config files |
| 101 # -c: process directive after reading config files |
| 102 # Apache wouldn't run CGIs with permissions==700 unless we add |
| 103 # -c User "<username>" |
| 104 httpd_cmd_string = ( |
| 105 '%(httpd_executable_path)s' |
| 106 ' -f %(httpd_conf_path)s' |
| 107 ' -c \'TypesConfig "%(mime_types_path)s"\'' |
| 108 ' -c \'CustomLog "%(output_dir)s/access_log.txt" common\'' |
| 109 ' -c \'ErrorLog "%(output_dir)s/error_log.txt"\'' |
| 110 ' -c \'PidFile "%(output_dir)s/httpd.pid"\'' |
| 111 ' -C \'User "%(user)s"\'' |
| 112 ' -C \'ServerRoot "%(server_root)s"\'' |
| 113 ' -c \'LockFile "%(lock_file)s"\'' |
| 114 ' -c \'SSLCertificateFile "%(ssl_certificate_file)s"\'' |
| 115 ) |
| 116 |
| 117 if document_root: |
| 118 httpd_cmd_string += ' -C \'DocumentRoot "%(document_root)s"\'' |
| 119 # Save a copy of httpd_cmd_string to use for stopping httpd |
| 120 self._httpd_cmd_string = httpd_cmd_string % httpd_vars |
| 121 |
| 122 httpd_cmd = [self._bash, "-c", self._httpd_cmd_string] |
| 123 return httpd_cmd |
| 124 |
| 125 def GetStopHttpdCommand(self): |
| 126 """Returns a list of strings that contains the command line+args needed to |
| 127 stop the http server used in the http tests. |
| 128 |
| 129 This tries to fetch the pid of httpd (if available) and returns the |
| 130 command to kill it. If pid is not available, kill all httpd processes |
| 131 """ |
| 132 |
| 133 if not self._httpd_cmd_string: |
| 134 return ["true"] # Haven't been asked for the start cmd yet. Just pass. |
| 135 # Add a sleep after the shutdown because sometimes it takes some time for |
| 136 # the port to be available again. |
| 137 return [self._bash, "-c", self._httpd_cmd_string + ' -k stop && sleep 5'] |
OLD | NEW |