1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Generic utilities for all python scripts."""
18 def GetPlatformName():
19 """Return a string to be used in paths for the platform."""
26 raise NotImplementedError('Unknown platform "%s".' % sys.platform)
30 return sys.platform == 'cygwin' or sys.platform.startswith('win')
34 return sys.platform.startswith('linux')
38 return sys.platform.startswith('darwin')
42 """Deletes a directory recursively, which must exist."""
43 # Don't use shutil.rmtree because it can't delete read-only files on Win.
44 for root, dirs, files in os.walk(path, topdown=False):
46 filename = os.path.join(root, name)
47 os.chmod(filename, stat.S_IWRITE)
50 os.rmdir(os.path.join(root, name))
55 """Deletes the given file or directory (recursively), which must exist."""
56 if os.path.isdir(path):
62 def MaybeDelete(path):
63 """Deletes the given file or directory (recurisvely), if it exists."""
64 if os.path.exists(path):
68 def MakeTempDir(parent_dir=None):
69 """Creates a temporary directory and returns an absolute path to it.
71 The temporary directory is automatically deleted when the python interpreter
75 parent_dir: the directory to create the temp dir in. If None, the system
79 The absolute path to the temporary directory.
81 path = tempfile.mkdtemp(dir=parent_dir)
82 atexit.register(MaybeDelete, path)
86 def Unzip(zip_path, output_dir):
87 """Unzips the given zip file using a system installed unzip tool.
90 zip_path: zip file to unzip.
91 output_dir: directory to unzip the contents of the zip file. The directory
95 RuntimeError if the unzip operation fails.
98 unzip_cmd = ['C:\\Program Files\\7-Zip\\7z.exe', 'x', '-y']
100 unzip_cmd = ['unzip', '-o']
101 unzip_cmd += [zip_path]
102 if RunCommand(unzip_cmd, output_dir) != 0:
103 raise RuntimeError('Unable to unzip %s to %s' % (zip_path, output_dir))
107 """Terminate the given pid."""
109 subprocess.call(['taskkill.exe', '/T', '/F', '/PID', str(pid)])
111 os.kill(pid, signal.SIGTERM)
114 def RunCommand(cmd, cwd=None):
115 """Runs the given command and returns the exit code.
118 cmd: list of command arguments.
119 cwd: working directory to execute the command, or None if the current
120 working directory should be used.
123 The exit code of the command.
125 process = subprocess.Popen(cmd, cwd=cwd)
127 return process.returncode
130 def DoesUrlExist(url):
131 """Determines whether a resource exists at the given URL.
134 url: URL to be verified.
137 True if url exists, otherwise False.
139 parsed = urlparse.urlparse(url)
141 conn = httplib.HTTPConnection(parsed.netloc)
142 conn.request('HEAD', parsed.path)
143 response = conn.getresponse()
144 except (socket.gaierror, socket.error):
148 # Follow both permanent (301) and temporary (302) redirects.
149 if response.status == 302 or response.status == 301:
150 return DoesUrlExist(response.getheader('location'))
151 return response.status == 200