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')
42 def GetAbsolutePathOfUserPath(user_path
):
43 """Expand the given |user_path| (like "~/file") and return its absolute path.
47 return os
.path
.abspath(os
.path
.expanduser(user_path
))
51 """Deletes a directory recursively, which must exist."""
52 # Don't use shutil.rmtree because it can't delete read-only files on Win.
53 for root
, dirs
, files
in os
.walk(path
, topdown
=False):
55 filename
= os
.path
.join(root
, name
)
56 os
.chmod(filename
, stat
.S_IWRITE
)
59 os
.rmdir(os
.path
.join(root
, name
))
64 """Deletes the given file or directory (recursively), which must exist."""
65 if os
.path
.isdir(path
):
71 def MaybeDelete(path
):
72 """Deletes the given file or directory (recurisvely), if it exists."""
73 if os
.path
.exists(path
):
77 def MakeTempDir(parent_dir
=None):
78 """Creates a temporary directory and returns an absolute path to it.
80 The temporary directory is automatically deleted when the python interpreter
84 parent_dir: the directory to create the temp dir in. If None, the system
88 The absolute path to the temporary directory.
90 path
= tempfile
.mkdtemp(dir=parent_dir
)
91 atexit
.register(MaybeDelete
, path
)
96 """Zips the given path and returns the zipped file."""
97 zip_path
= os
.path
.join(MakeTempDir(), 'build.zip')
98 f
= zipfile
.ZipFile(zip_path
, 'w', zipfile
.ZIP_DEFLATED
)
99 f
.write(path
, os
.path
.basename(path
))
104 def Unzip(zip_path
, output_dir
):
105 """Unzips the given zip file using a system installed unzip tool.
108 zip_path: zip file to unzip.
109 output_dir: directory to unzip the contents of the zip file. The directory
113 RuntimeError if the unzip operation fails.
116 unzip_cmd
= ['C:\\Program Files\\7-Zip\\7z.exe', 'x', '-y']
118 unzip_cmd
= ['unzip', '-o']
119 unzip_cmd
+= [zip_path
]
120 if RunCommand(unzip_cmd
, output_dir
) != 0:
121 raise RuntimeError('Unable to unzip %s to %s' % (zip_path
, output_dir
))
125 """Terminate the given pid."""
127 subprocess
.call(['taskkill.exe', '/T', '/F', '/PID', str(pid
)])
129 os
.kill(pid
, signal
.SIGTERM
)
132 def RunCommand(cmd
, cwd
=None):
133 """Runs the given command and returns the exit code.
136 cmd: list of command arguments.
137 cwd: working directory to execute the command, or None if the current
138 working directory should be used.
141 The exit code of the command.
144 process
= subprocess
.Popen(cmd
, cwd
=cwd
)
147 return process
.returncode
150 def DoesUrlExist(url
):
151 """Determines whether a resource exists at the given URL.
154 url: URL to be verified.
157 True if url exists, otherwise False.
159 parsed
= urlparse
.urlparse(url
)
161 conn
= httplib
.HTTPConnection(parsed
.netloc
)
162 conn
.request('HEAD', parsed
.path
)
163 response
= conn
.getresponse()
164 except (socket
.gaierror
, socket
.error
):
168 # Follow both permanent (301) and temporary (302) redirects.
169 if response
.status
== 302 or response
.status
== 301:
170 return DoesUrlExist(response
.getheader('location'))
171 return response
.status
== 200
174 def MarkBuildStepStart(name
):
175 print '@@@BUILD_STEP %s@@@' % name
179 def MarkBuildStepError():
180 print '@@@STEP_FAILURE@@@'
184 def AddBuildStepText(text
):
185 print '@@@STEP_TEXT@%s@@@' % text
189 def PrintAndFlush(text
):
194 def AddLink(label
, url
):
195 """Adds a link with name |label| linking to |url| to current buildbot step.
198 label: A string with the name of the label.
199 url: A string of the URL.
201 print '@@@STEP_LINK@%s@%s@@@' % (label
, url
)