Changeset 51969 in webkit


Ignore:
Timestamp:
Dec 10, 2009 3:47:36 PM (14 years ago)
Author:
eric@webkit.org
Message:

2009-12-10 Eric Seidel <eric@webkit.org>

Reviewed by Adam Barth.

Move run_command onto Executive to make code which uses run_command testable
https://bugs.webkit.org/show_bug.cgi?id=32396

  • Scripts/modules/executive.py:
    • Move run_command and error handlers onto Executive.
  • Scripts/modules/scm.py:
  • Scripts/modules/scm_unittest.py:
Location:
trunk/WebKitTools
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebKitTools/ChangeLog

    r51959 r51969  
     12009-12-10  Eric Seidel  <eric@webkit.org>
     2
     3        Reviewed by Adam Barth.
     4
     5        Move run_command onto Executive to make code which uses run_command testable
     6        https://bugs.webkit.org/show_bug.cgi?id=32396
     7
     8        * Scripts/modules/executive.py:
     9         - Move run_command and error handlers onto Executive.
     10        * Scripts/modules/scm.py:
     11        * Scripts/modules/scm_unittest.py:
     12
    1132009-12-09  Eric Seidel  <eric@webkit.org>
    214
  • trunk/WebKitTools/Scripts/modules/executive.py

    r51891 r51969  
    5858        return str(self)
    5959
    60 def default_error_handler(error):
    61     raise error
    62 
    63 def ignore_error(error):
    64     pass
    6560
    6661# FIXME: This should not be a global static.
    67 def run_command(args, cwd=None, input=None, error_handler=default_error_handler, return_exit_code=False, return_stderr=True):
    68     if hasattr(input, 'read'): # Check if the input is a file.
    69         stdin = input
    70         string_to_communicate = None
    71     else:
    72         stdin = subprocess.PIPE if input else None
    73         string_to_communicate = input
    74     if return_stderr:
    75         stderr = subprocess.STDOUT
    76     else:
    77         stderr = None
    78     process = subprocess.Popen(args, stdin=stdin, stdout=subprocess.PIPE, stderr=stderr, cwd=cwd)
    79     output = process.communicate(string_to_communicate)[0]
    80     exit_code = process.wait()
    81     if exit_code:
    82         script_error = ScriptError(script_args=args, exit_code=exit_code, output=output, cwd=cwd)
    83         error_handler(script_error)
    84     if return_exit_code:
    85         return exit_code
    86     return output
     62# New code should use Executive.run_command directly instead
     63def run_command(*args, **kwargs):
     64    return Executive().run_command(*args, **kwargs)
    8765
    8866
     
    11492        if exit_code:
    11593            raise ScriptError(script_args=args, exit_code=exit_code, output=child_output)
     94
     95    # Error handlers do not need to be static methods once all callers are updated to use an Executive object.
     96    @staticmethod
     97    def default_error_handler(error):
     98        raise error
     99
     100    @staticmethod
     101    def ignore_error(error):
     102        pass
     103
     104    # FIXME: This should be merged with run_and_throw_if_fail
     105    def run_command(self, args, cwd=None, input=None, error_handler=None, return_exit_code=False, return_stderr=True):
     106        if hasattr(input, 'read'): # Check if the input is a file.
     107            stdin = input
     108            string_to_communicate = None
     109        else:
     110            stdin = subprocess.PIPE if input else None
     111            string_to_communicate = input
     112        if return_stderr:
     113            stderr = subprocess.STDOUT
     114        else:
     115            stderr = None
     116        process = subprocess.Popen(args, stdin=stdin, stdout=subprocess.PIPE, stderr=stderr, cwd=cwd)
     117        output = process.communicate(string_to_communicate)[0]
     118        exit_code = process.wait()
     119        if exit_code:
     120            script_error = ScriptError(script_args=args, exit_code=exit_code, output=output, cwd=cwd)
     121            (error_handler or self.default_error_handler)(script_error)
     122        if return_exit_code:
     123            return exit_code
     124        return output
  • trunk/WebKitTools/Scripts/modules/scm.py

    r51889 r51969  
    3636# Import WebKit-specific modules.
    3737from modules.changelogs import ChangeLog
    38 from modules.executive import run_command, ScriptError, default_error_handler, ignore_error
     38from modules.executive import Executive, run_command, ScriptError
    3939from modules.logging import error, log
    4040
     
    8888    if re.search("resource out of date", error.output):
    8989        raise CheckoutNeedsUpdate(script_args=error.script_args, exit_code=error.exit_code, output=error.output, cwd=error.cwd)
    90     default_error_handler(error)
     90    Executive.default_error_handler(error)
    9191
    9292
     
    105105    def ensure_clean_working_directory(self, force_clean):
    106106        if not force_clean and not self.working_directory_is_clean():
    107             print run_command(self.status_command(), error_handler=ignore_error)
     107            print run_command(self.status_command(), error_handler=Executive.ignore_error)
    108108            raise ScriptError(message="Working directory has modifications, pass --force-clean or --no-clean to continue.")
    109109       
     
    369369    @classmethod
    370370    def in_working_directory(cls, path):
    371         return run_command(['git', 'rev-parse', '--is-inside-work-tree'], cwd=path, error_handler=ignore_error).rstrip() == "true"
     371        return run_command(['git', 'rev-parse', '--is-inside-work-tree'], cwd=path, error_handler=Executive.ignore_error).rstrip() == "true"
    372372
    373373    @classmethod
     
    439439        # I think this will always fail due to ChangeLogs.
    440440        # FIXME: We need to detec specific failure conditions and handle them.
    441         run_command(['git', 'revert', '--no-commit', git_commit], error_handler=ignore_error)
     441        run_command(['git', 'revert', '--no-commit', git_commit], error_handler=Executive.ignore_error)
    442442
    443443        # Fix any ChangeLogs if necessary.
  • trunk/WebKitTools/Scripts/modules/scm_unittest.py

    r51889 r51969  
    3939
    4040from datetime import date
    41 from modules.executive import run_command, ignore_error, ScriptError
     41from modules.executive import Executive, run_command, ScriptError
    4242from modules.scm import detect_scm_system, SCM, CheckoutNeedsUpdate, commit_error_handler
    4343
     
    148148        command_does_not_exist = ['does_not_exist', 'invalid_option']
    149149        self.assertRaises(OSError, run_command, command_does_not_exist)
    150         self.assertRaises(OSError, run_command, command_does_not_exist, error_handler=ignore_error)
     150        self.assertRaises(OSError, run_command, command_does_not_exist, error_handler=Executive.ignore_error)
    151151
    152152        command_returns_non_zero = ['/bin/sh', '--invalid-option']
    153153        self.assertRaises(ScriptError, run_command, command_returns_non_zero)
    154154        # Check if returns error text:
    155         self.assertTrue(run_command(command_returns_non_zero, error_handler=ignore_error))
     155        self.assertTrue(run_command(command_returns_non_zero, error_handler=Executive.ignore_error))
    156156
    157157        self.assertRaises(CheckoutNeedsUpdate, commit_error_handler, ScriptError(output=git_failure_message))
Note: See TracChangeset for help on using the changeset viewer.