diff options
Diffstat (limited to 'Tools/Scripts/webkitpy/common/system')
6 files changed, 107 insertions, 10 deletions
diff --git a/Tools/Scripts/webkitpy/common/system/executive.py b/Tools/Scripts/webkitpy/common/system/executive.py index 02619db..7d198dd 100644 --- a/Tools/Scripts/webkitpy/common/system/executive.py +++ b/Tools/Scripts/webkitpy/common/system/executive.py @@ -45,6 +45,7 @@ import sys import time from webkitpy.common.system.deprecated_logging import tee +from webkitpy.common.system.filesystem import FileSystem from webkitpy.python24 import versioning @@ -179,6 +180,22 @@ class Executive(object): # machines. return 2 + @staticmethod + def interpreter_for_script(script_path, fs=FileSystem()): + lines = fs.read_text_file(script_path).splitlines() + if not len(lines): + return None + first_line = lines[0] + if not first_line.startswith('#!'): + return None + if first_line.find('python') > -1: + return sys.executable + if first_line.find('perl') > -1: + return 'perl' + if first_line.find('ruby') > -1: + return 'ruby' + return None + def kill_process(self, pid): """Attempts to kill the given pid. Will fail silently if pid does not exist or insufficient permisssions.""" diff --git a/Tools/Scripts/webkitpy/common/system/executive_unittest.py b/Tools/Scripts/webkitpy/common/system/executive_unittest.py index 1dadc36..9a14d6b 100644 --- a/Tools/Scripts/webkitpy/common/system/executive_unittest.py +++ b/Tools/Scripts/webkitpy/common/system/executive_unittest.py @@ -34,6 +34,7 @@ import sys import unittest from webkitpy.common.system.executive import Executive, run_command, ScriptError +from webkitpy.common.system.filesystem_mock import MockFileSystem from webkitpy.test import cat, echo @@ -65,6 +66,33 @@ def never_ending_command(): class ExecutiveTest(unittest.TestCase): + def assert_interpreter_for_content(self, intepreter, content): + fs = MockFileSystem() + file_path = None + file_interpreter = None + + tempfile, temp_name = fs.open_binary_tempfile('') + tempfile.write(content) + tempfile.close() + file_interpreter = Executive.interpreter_for_script(temp_name, fs) + + self.assertEqual(file_interpreter, intepreter) + + def test_interpreter_for_script(self): + self.assert_interpreter_for_content(None, '') + self.assert_interpreter_for_content(None, 'abcd\nefgh\nijklm') + self.assert_interpreter_for_content(None, '##/usr/bin/perl') + self.assert_interpreter_for_content('perl', '#!/usr/bin/env perl') + self.assert_interpreter_for_content('perl', '#!/usr/bin/env perl\nfirst\nsecond') + self.assert_interpreter_for_content('perl', '#!/usr/bin/perl') + self.assert_interpreter_for_content('perl', '#!/usr/bin/perl -w') + self.assert_interpreter_for_content(sys.executable, '#!/usr/bin/env python') + self.assert_interpreter_for_content(sys.executable, '#!/usr/bin/env python\nfirst\nsecond') + self.assert_interpreter_for_content(sys.executable, '#!/usr/bin/python') + self.assert_interpreter_for_content('ruby', '#!/usr/bin/env ruby') + self.assert_interpreter_for_content('ruby', '#!/usr/bin/env ruby\nfirst\nsecond') + self.assert_interpreter_for_content('ruby', '#!/usr/bin/ruby') + def test_run_command_with_bad_command(self): def run_bad_command(): run_command(["foo_bar_command_blah"], error_handler=Executive.ignore_error, return_exit_code=True) diff --git a/Tools/Scripts/webkitpy/common/system/filesystem.py b/Tools/Scripts/webkitpy/common/system/filesystem.py index 1988546..58be03a 100644 --- a/Tools/Scripts/webkitpy/common/system/filesystem.py +++ b/Tools/Scripts/webkitpy/common/system/filesystem.py @@ -61,6 +61,10 @@ class FileSystem(object): """Wraps os.path.basename().""" return os.path.basename(path) + def chdir(self, path): + """Wraps os.chdir().""" + return os.chdir(path) + def copyfile(self, source, destination): """Copies the contents of the file at the given path to the destination path.""" @@ -108,6 +112,10 @@ class FileSystem(object): files.append(self.join(dirpath, filename)) return files + def getcwd(self): + """Wraps os.getcwd().""" + return os.getcwd() + def glob(self, path): """Wraps glob.glob().""" return glob.glob(path) diff --git a/Tools/Scripts/webkitpy/common/system/filesystem_mock.py b/Tools/Scripts/webkitpy/common/system/filesystem_mock.py index a6d158a..3be5854 100644 --- a/Tools/Scripts/webkitpy/common/system/filesystem_mock.py +++ b/Tools/Scripts/webkitpy/common/system/filesystem_mock.py @@ -35,7 +35,7 @@ from webkitpy.common.system import ospath class MockFileSystem(object): - def __init__(self, files=None): + def __init__(self, files=None, cwd='/'): """Initializes a "mock" filesystem that can be used to completely stub out a filesystem. @@ -48,6 +48,8 @@ class MockFileSystem(object): self.written_files = {} self._sep = '/' self.current_tmpno = 0 + self.cwd = cwd + self.dirs = {} def _get_sep(self): return self._sep @@ -61,13 +63,19 @@ class MockFileSystem(object): return path.rsplit(self.sep, 1) def abspath(self, path): - if path.endswith(self.sep): - return path[:-1] - return path + if os.path.isabs(path): + return self.normpath(path) + return self.abspath(self.join(self.cwd, path)) def basename(self, path): return self._split(path)[1] + def chdir(self, path): + path = self.normpath(path) + if not self.isdir(path): + raise OSError(errno.ENOENT, path, os.strerror(errno.ENOENT)) + self.cwd = path + def copyfile(self, source, destination): if not self.exists(source): self._raise_not_found(source) @@ -117,6 +125,9 @@ class MockFileSystem(object): return files + def getcwd(self, path): + return self.cwd + def glob(self, path): # FIXME: This only handles a wildcard '*' at the end of the path. # Maybe it should handle more? @@ -134,14 +145,18 @@ class MockFileSystem(object): def isdir(self, path): if path in self.files: return False - if not path.endswith(self.sep): - path += self.sep + path = self.normpath(path) + if path in self.dirs: + return True # 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()[:] - return any(f.startswith(path) for f in files) + result = any(f.startswith(path) for f in files) + if result: + self.dirs[path] = True + return result def join(self, *comps): # FIXME: might want tests for this and/or a better comment about how @@ -204,8 +219,9 @@ class MockFileSystem(object): return TemporaryDirectory(fs=self, **kwargs) def maybe_make_directory(self, *path): - # FIXME: Implement such that subsequent calls to isdir() work? - pass + norm_path = self.normpath(self.join(*path)) + if not self.isdir(norm_path): + self.dirs[norm_path] = True def move(self, source, destination): if self.files[source] is None: @@ -216,7 +232,9 @@ class MockFileSystem(object): self.written_files[source] = None def normpath(self, path): - return path + # Like join(), relies on os.path functionality but normalizes the + # path separator to the mock one. + return re.sub(re.escape(os.path.sep), self.sep, os.path.normpath(path)) def open_binary_tempfile(self, suffix=''): path = self._mktemp(suffix) diff --git a/Tools/Scripts/webkitpy/common/system/filesystem_unittest.py b/Tools/Scripts/webkitpy/common/system/filesystem_unittest.py index 8455d72..8d4f0cb 100644 --- a/Tools/Scripts/webkitpy/common/system/filesystem_unittest.py +++ b/Tools/Scripts/webkitpy/common/system/filesystem_unittest.py @@ -48,6 +48,23 @@ class FileSystemTest(unittest.TestCase): self._missing_file = os.path.join(self._this_dir, 'missing_file.py') self._this_file = os.path.join(self._this_dir, 'filesystem_unittest.py') + def test_chdir(self): + fs = FileSystem() + cwd = fs.getcwd() + newdir = '/' + if sys.platform == 'win32': + newdir = 'c:\\' + fs.chdir(newdir) + self.assertEquals(fs.getcwd(), newdir) + fs.chdir(cwd) + + def test_chdir__notexists(self): + fs = FileSystem() + newdir = '/dirdoesnotexist' + if sys.platform == 'win32': + newdir = 'c:\\dirdoesnotexist' + self.assertRaises(OSError, fs.chdir, newdir) + def test_exists__true(self): fs = FileSystem() self.assertTrue(fs.exists(self._this_file)) @@ -56,6 +73,10 @@ class FileSystemTest(unittest.TestCase): fs = FileSystem() self.assertFalse(fs.exists(self._missing_file)) + def test_getcwd(self): + fs = FileSystem() + self.assertTrue(fs.exists(fs.getcwd())) + def test_isdir__true(self): fs = FileSystem() self.assertTrue(fs.isdir(self._this_dir)) diff --git a/Tools/Scripts/webkitpy/common/system/user.py b/Tools/Scripts/webkitpy/common/system/user.py index b79536c..aecb6ec 100644 --- a/Tools/Scripts/webkitpy/common/system/user.py +++ b/Tools/Scripts/webkitpy/common/system/user.py @@ -26,6 +26,7 @@ # (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 getpass import logging import os import re @@ -65,6 +66,10 @@ class User(object): return response @classmethod + def prompt_password(cls, message, repeat=1): + return cls.prompt(message, repeat=repeat, raw_input=getpass.getpass) + + @classmethod def prompt_with_list(cls, list_title, list_items, can_choose_multiple=False, raw_input=raw_input): print list_title i = 0 |