1 # Copyright (c) 2012 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."""
19 def GetPlatformName():
20 """Return a string to be used in paths for the platform."""
27 raise NotImplementedError('Unknown platform "%s".' % sys
.platform
)
31 return sys
.platform
== 'cygwin' or sys
.platform
.startswith('win')
35 return sys
.platform
.startswith('linux')
39 return sys
.platform
.startswith('darwin')
43 """Deletes a directory recursively, which must exist."""
44 # Don't use shutil.rmtree because it can't delete read-only files on Win.
45 for root
, dirs
, files
in os
.walk(path
, topdown
=False):
47 filename
= os
.path
.join(root
, name
)
48 os
.chmod(filename
, stat
.S_IWRITE
)
51 os
.rmdir(os
.path
.join(root
, name
))
56 """Deletes the given file or directory (recursively), which must exist."""
57 if os
.path
.isdir(path
):
63 def MaybeDelete(path
):
64 """Deletes the given file or directory (recurisvely), if it exists."""
65 if os
.path
.exists(path
):
69 def MakeTempDir(parent_dir
=None):
70 """Creates a temporary directory and returns an absolute path to it.
72 The temporary directory is automatically deleted when the python interpreter
76 parent_dir: the directory to create the temp dir in. If None, the system
80 The absolute path to the temporary directory.
82 path
= tempfile
.mkdtemp(dir=parent_dir
)
83 atexit
.register(MaybeDelete
, path
)
88 """Zips the given path and returns the zipped file."""
89 zip_path
= os
.path
.join(MakeTempDir(), 'build.zip')
90 f
= zipfile
.ZipFile(zip_path
, 'w', zipfile
.ZIP_DEFLATED
)
91 f
.write(path
, os
.path
.basename(path
))
96 def Unzip(zip_path
, output_dir
):
97 """Unzips the given zip file using a system installed unzip tool.
100 zip_path: zip file to unzip.
101 output_dir: directory to unzip the contents of the zip file. The directory
105 RuntimeError if the unzip operation fails.
108 unzip_cmd
= ['C:\\Program Files\\7-Zip\\7z.exe', 'x', '-y']
110 unzip_cmd
= ['unzip', '-o']
111 unzip_cmd
+= [zip_path
]
112 if RunCommand(unzip_cmd
, output_dir
) != 0:
113 raise RuntimeError('Unable to unzip %s to %s' % (zip_path
, output_dir
))
117 """Terminate the given pid."""
119 subprocess
.call(['taskkill.exe', '/T', '/F', '/PID', str(pid
)])
121 os
.kill(pid
, signal
.SIGTERM
)
124 def RunCommand(cmd
, cwd
=None):
125 """Runs the given command and returns the exit code.
128 cmd: list of command arguments.
129 cwd: working directory to execute the command, or None if the current
130 working directory should be used.
133 The exit code of the command.
136 process
= subprocess
.Popen(cmd
, cwd
=cwd
)
139 return process
.returncode
142 def DoesUrlExist(url
):
143 """Determines whether a resource exists at the given URL.
146 url: URL to be verified.
149 True if url exists, otherwise False.
151 parsed
= urlparse
.urlparse(url
)
153 conn
= httplib
.HTTPConnection(parsed
.netloc
)
154 conn
.request('HEAD', parsed
.path
)
155 response
= conn
.getresponse()
156 except (socket
.gaierror
, socket
.error
):
160 # Follow both permanent (301) and temporary (302) redirects.
161 if response
.status
== 302 or response
.status
== 301:
162 return DoesUrlExist(response
.getheader('location'))
163 return response
.status
== 200
166 def MarkBuildStepStart(name
):
167 print '@@@BUILD_STEP %s@@@' % name
171 def MarkBuildStepError():
172 print '@@@STEP_FAILURE@@@'
176 def AddBuildStepText(text
):
177 print '@@@STEP_TEXT@%s@@@' % text
181 def PrintAndFlush(text
):
186 def AddLink(label
, url
):
187 """Adds a link with name |label| linking to |url| to current buildbot step.
190 label: A string with the name of the label.
191 url: A string of the URL.
193 print '@@@STEP_LINK@%s@%s@@@' % (label
, url
)