diff options
| author | Simon Hausmann <simon.hausmann@nokia.com> | 2012-02-24 16:36:50 +0100 |
|---|---|---|
| committer | Simon Hausmann <simon.hausmann@nokia.com> | 2012-02-24 16:36:50 +0100 |
| commit | ad0d549d4cc13433f77c1ac8f0ab379c83d93f28 (patch) | |
| tree | b34b0daceb7c8e7fdde4b4ec43650ab7caadb0a9 /Tools/Scripts/webkitpy/common/system | |
| parent | 03e12282df9aa1e1fb05a8b90f1cfc2e08764cec (diff) | |
| download | qtwebkit-ad0d549d4cc13433f77c1ac8f0ab379c83d93f28.tar.gz | |
Imported WebKit commit bb52bf3c0119e8a128cd93afe5572413a8617de9 (http://svn.webkit.org/repository/webkit/trunk@108790)
Diffstat (limited to 'Tools/Scripts/webkitpy/common/system')
7 files changed, 148 insertions, 29 deletions
diff --git a/Tools/Scripts/webkitpy/common/system/autoinstall.py b/Tools/Scripts/webkitpy/common/system/autoinstall.py index a928db63a..e5bc0b2cb 100755 --- a/Tools/Scripts/webkitpy/common/system/autoinstall.py +++ b/Tools/Scripts/webkitpy/common/system/autoinstall.py @@ -259,6 +259,44 @@ class AutoInstaller(object): return target_path + # This is a replacement for ZipFile.extractall(), which is + # available in Python 2.6 but not in earlier versions. + # NOTE: The version in 2.6.1 (which shipped on Snow Leopard) is broken! + def _extract_all(self, zip_file, target_dir): + self._log_transfer("Extracting zip file...", zip_file, target_dir) + + # This is helpful for debugging purposes. + _log.debug("Listing zip file contents...") + for name in zip_file.namelist(): + _log.debug(' * "%s"' % name) + + for name in zip_file.namelist(): + path = os.path.join(target_dir, name) + self._log_transfer("Extracting...", name, path) + + if not os.path.basename(path): + # Then the path ends in a slash, so it is a directory. + self._create_directory(path) + continue + # Otherwise, it is a file. + + try: + # We open this file w/o encoding, as we're reading/writing + # the raw byte-stream from the zip file. + outfile = open(path, 'wb') + except IOError, err: + # Not all zip files seem to list the directories explicitly, + # so try again after creating the containing directory. + _log.debug("Got IOError: retrying after creating directory...") + dir = os.path.dirname(path) + self._create_directory(dir) + outfile = open(path, 'wb') + + try: + outfile.write(zip_file.read(name)) + finally: + outfile.close() + def _unzip(self, path, scratch_dir): # zipfile.extractall() extracts to a path without the # trailing ".zip". @@ -276,7 +314,7 @@ class AutoInstaller(object): raise Exception(message) try: - zip_file.extractall(scratch_dir) + self._extract_all(zip_file, scratch_dir) finally: zip_file.close() diff --git a/Tools/Scripts/webkitpy/common/system/executive_mock.py b/Tools/Scripts/webkitpy/common/system/executive_mock.py index f698235e9..a76268129 100644 --- a/Tools/Scripts/webkitpy/common/system/executive_mock.py +++ b/Tools/Scripts/webkitpy/common/system/executive_mock.py @@ -32,8 +32,16 @@ from webkitpy.common.system.deprecated_logging import log from webkitpy.common.system.executive import ScriptError +class MockProcess(object): + def __init__(self): + self.pid = 42 + + # FIXME: This should be unified with MockExecutive2 class MockExecutive(object): + PIPE = "MOCK PIPE" + STDOUT = "MOCK STDOUT" + @staticmethod def ignore_error(error): pass @@ -55,7 +63,7 @@ class MockExecutive(object): env_string = ", env=%s" % env log("MOCK run_and_throw_if_fail: %s, cwd=%s%s" % (args, cwd, env_string)) if self._should_throw_when_run.intersection(args): - raise ScriptError("Exception for %s" % args) + raise ScriptError("Exception for %s" % args, output="MOCK command output") return "MOCK output of child process" def run_command(self, @@ -81,6 +89,10 @@ class MockExecutive(object): def cpu_count(self): return 2 + def popen(self, *args, **kwargs): + # FIXME: Implement logging when self._should_log is set. + return MockProcess() + class MockExecutive2(object): @staticmethod diff --git a/Tools/Scripts/webkitpy/common/system/file_lock.py b/Tools/Scripts/webkitpy/common/system/file_lock.py index fb853c8fb..b4bfffc40 100644 --- a/Tools/Scripts/webkitpy/common/system/file_lock.py +++ b/Tools/Scripts/webkitpy/common/system/file_lock.py @@ -71,6 +71,8 @@ class FileLock(object): os.close(self._lock_file_descriptor) self._lock_file_descriptor = None return False + # There's no compelling reason to spin hard here, so sleep for a bit. + time.sleep(0.01) def release_lock(self): try: diff --git a/Tools/Scripts/webkitpy/common/system/file_lock_integrationtest.py b/Tools/Scripts/webkitpy/common/system/file_lock_integrationtest.py index c5c1db3de..5cd27d11d 100644 --- a/Tools/Scripts/webkitpy/common/system/file_lock_integrationtest.py +++ b/Tools/Scripts/webkitpy/common/system/file_lock_integrationtest.py @@ -36,8 +36,8 @@ class FileLockTest(unittest.TestCase): def setUp(self): self._lock_name = "TestWebKit" + str(os.getpid()) + ".lock" self._lock_path = os.path.join(tempfile.gettempdir(), self._lock_name) - self._file_lock1 = FileLock(self._lock_path, 1) - self._file_lock2 = FileLock(self._lock_path, 1) + self._file_lock1 = FileLock(self._lock_path, 0.1) + self._file_lock2 = FileLock(self._lock_path, 0.1) def tearDown(self): self._file_lock1.release_lock() diff --git a/Tools/Scripts/webkitpy/common/system/filesystem_mock.py b/Tools/Scripts/webkitpy/common/system/filesystem_mock.py index 6e106dd83..6fb1dcaee 100644 --- a/Tools/Scripts/webkitpy/common/system/filesystem_mock.py +++ b/Tools/Scripts/webkitpy/common/system/filesystem_mock.py @@ -71,7 +71,11 @@ class MockFileSystem(object): raise IOError(errno.ENOENT, path, os.strerror(errno.ENOENT)) def _split(self, path): - return path.rsplit(self.sep, 1) + # This is not quite a full implementation of os.path.split + # http://docs.python.org/library/os.path.html#os.path.split + if self.sep in path: + return path.rsplit(self.sep, 1) + return ('', path) def abspath(self, path): if os.path.isabs(path): @@ -169,40 +173,45 @@ class MockFileSystem(object): return path in self.files and self.files[path] is not None def isdir(self, path): - if path in self.files: - return False - path = self.normpath(path) - if path in self.dirs: - return True + return self.normpath(path) in self.dirs - # We need to use a copy of the keys here in order to avoid switching - # to a different thread and potentially modifying the dict in - # mid-iteration. - files = self.files.keys()[:] - result = any(f.startswith(path) and len(self.split(f)[0]) >= len(path) for f in files) - if result: - self.dirs.add(path) - return result + def _slow_but_correct_join(self, *comps): + return re.sub(re.escape(os.path.sep), self.sep, os.path.join(*comps)) def join(self, *comps): - # FIXME: might want tests for this and/or a better comment about how - # it works. - return re.sub(re.escape(os.path.sep), self.sep, os.path.join(*comps)) + # This function is called a lot, so we optimize it; there are + # unittests to check that we match _slow_but_correct_join(), above. + path = '' + sep = self.sep + for comp in comps: + if not comp: + continue + if comp[0] == sep: + path = comp + continue + if path: + path += sep + path += comp + if comps[-1] == '' and path: + path += '/' + path = path.replace(sep + sep, sep) + return path def listdir(self, path): + sep = self.sep if not self.isdir(path): raise OSError("%s is not a directory" % path) - if not path.endswith(self.sep): - path += self.sep + if not path.endswith(sep): + path += sep dirs = [] files = [] for f in self.files: if self.exists(f) and f.startswith(path): remaining = f[len(path):] - if self.sep in remaining: - dir = remaining[:remaining.index(self.sep)] + if sep in remaining: + dir = remaining[:remaining.index(sep)] if not dir in dirs: dirs.append(dir) else: @@ -258,11 +267,27 @@ class MockFileSystem(object): self.files[source] = None self.written_files[source] = None - def normpath(self, path): - # Like join(), relies on os.path functionality but normalizes the - # path separator to the mock one. + def _slow_but_correct_normpath(self, path): return re.sub(re.escape(os.path.sep), self.sep, os.path.normpath(path)) + def normpath(self, path): + # This function is called a lot, so we try to optimize the common cases + # instead of always calling _slow_but_correct_normpath(), above. + if '..' in path: + # This doesn't happen very often; don't bother trying to optimize it. + return self._slow_but_correct_normpath(path) + if not path: + return '.' + if path == '/': + return path + if path == '/.': + return '/' + if path.endswith('/.'): + return path[:-2] + if path.endswith('/'): + return path[:-1] + return path + def open_binary_tempfile(self, suffix=''): path = self._mktemp(suffix) return (WritableBinaryFileObject(self, path), path) @@ -280,6 +305,7 @@ class MockFileSystem(object): def write_binary_file(self, path, contents): # FIXME: should this assert if dirname(path) doesn't exist? + self.maybe_make_directory(self.dirname(path)) self.files[path] = contents self.written_files[path] = contents diff --git a/Tools/Scripts/webkitpy/common/system/filesystem_mock_unittest.py b/Tools/Scripts/webkitpy/common/system/filesystem_mock_unittest.py index 5169fc598..2a6ccbf4b 100644 --- a/Tools/Scripts/webkitpy/common/system/filesystem_mock_unittest.py +++ b/Tools/Scripts/webkitpy/common/system/filesystem_mock_unittest.py @@ -26,8 +26,11 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +import os +import re import unittest + from webkitpy.common.system import filesystem_mock from webkitpy.common.system import filesystem_unittest @@ -41,6 +44,45 @@ class MockFileSystemTest(unittest.TestCase, filesystem_unittest.GenericFileSyste self.teardown_generic_test_dir() self.fs = None + def quick_check(self, test_fn, good_fn, *tests): + for test in tests: + if hasattr(test, '__iter__'): + expected = good_fn(*test) + actual = test_fn(*test) + else: + expected = good_fn(test) + actual = test_fn(test) + self.assertEquals(expected, actual, 'given %s, expected %s, got %s' % (repr(test), repr(expected), repr(actual))) + + def test_join(self): + self.quick_check(self.fs.join, + self.fs._slow_but_correct_join, + ('',), + ('', 'bar'), + ('foo',), + ('foo/',), + ('foo', ''), + ('foo/', ''), + ('foo', 'bar'), + ('foo', '/bar'), + ) + + def test_normpath(self): + self.quick_check(self.fs.normpath, + self.fs._slow_but_correct_normpath, + '', + '/', + '.', + '/.', + 'foo', + 'foo/', + 'foo/.', + 'foo/bar', + '/foo', + 'foo/../bar', + 'foo/../bar/baz', + '../foo') + if __name__ == '__main__': unittest.main() diff --git a/Tools/Scripts/webkitpy/common/system/zipfileset_unittest.py b/Tools/Scripts/webkitpy/common/system/zipfileset_unittest.py index 680140680..16a74cbd7 100644 --- a/Tools/Scripts/webkitpy/common/system/zipfileset_unittest.py +++ b/Tools/Scripts/webkitpy/common/system/zipfileset_unittest.py @@ -21,7 +21,6 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -import os import shutil import tempfile import unittest |
