diff options
author | borenet <borenet@google.com> | 2014-06-25 08:40:58 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-06-25 08:40:58 -0700 |
commit | 3da21d2b62270c993167e98b28a3b6a0967f847f (patch) | |
tree | 8c28c324a9a2e09fe5d4a9f272a6ce4c82772114 /tools | |
parent | ec368cf4d6efec8354c2c316cabfb3f5d59643f8 (diff) |
Whitespace fixes for Python tools
BUG=skia:
R=rmistry@google.com
Author: borenet@google.com
Review URL: https://codereview.chromium.org/353853003
Diffstat (limited to 'tools')
-rwxr-xr-x | tools/add_codereview_message.py | 48 | ||||
-rw-r--r-- | tools/misc_utils.py | 91 | ||||
-rwxr-xr-x | tools/roll_deps.py | 447 |
3 files changed, 293 insertions, 293 deletions
diff --git a/tools/add_codereview_message.py b/tools/add_codereview_message.py index a7c53a9a5e..296a894062 100755 --- a/tools/add_codereview_message.py +++ b/tools/add_codereview_message.py @@ -32,38 +32,38 @@ RIETVELD_URL = 'https://codereview.chromium.org' def add_codereview_message(issue, message): - """Add a message to a given codereview. + """Add a message to a given codereview. - Args: - codereview_url: (string) we will extract the issue number from - this url, or this could simply be the issue number. - message: (string) message to add. - """ - # Passing None for the email and password will result in a prompt or - # reuse of existing cached credentials. - my_rietveld = rietveld.Rietveld(RIETVELD_URL, email=None, password=None) - - my_rietveld.add_comment(issue, message) + Args: + codereview_url: (string) we will extract the issue number from + this url, or this could simply be the issue number. + message: (string) message to add. + """ + # Passing None for the email and password will result in a prompt or + # reuse of existing cached credentials. + my_rietveld = rietveld.Rietveld(RIETVELD_URL, email=None, password=None) + + my_rietveld.add_comment(issue, message) def main(argv): - """main function; see module-level docstring and GetOptionParser help. + """main function; see module-level docstring and GetOptionParser help. - Args: - argv: sys.argv[1:]-type argument list. - """ - option_parser = optparse.OptionParser(usage=__doc__) - _, arguments = option_parser.parse_args(argv) + Args: + argv: sys.argv[1:]-type argument list. + """ + option_parser = optparse.OptionParser(usage=__doc__) + _, arguments = option_parser.parse_args(argv) - if len(arguments) > 1: - option_parser.error('Extra arguments.') - if len(arguments) != 1: - option_parser.error('Missing issue number.') + if len(arguments) > 1: + option_parser.error('Extra arguments.') + if len(arguments) != 1: + option_parser.error('Missing issue number.') - message = sys.stdin.read() - add_codereview_message(int(arguments[0]), message) + message = sys.stdin.read() + add_codereview_message(int(arguments[0]), message) if __name__ == '__main__': - main(sys.argv[1:]) + main(sys.argv[1:]) diff --git a/tools/misc_utils.py b/tools/misc_utils.py index c3e09da40f..89a2f924b1 100644 --- a/tools/misc_utils.py +++ b/tools/misc_utils.py @@ -11,48 +11,49 @@ import re class ReSearch(object): - """A collection of static methods for regexing things.""" - - @staticmethod - def search_within_stream(input_stream, pattern, default=None): - """Search for regular expression in a file-like object. - - Opens a file for reading and searches line by line for a match to - the regex and returns the parenthesized group named return for the - first match. Does not search across newlines. - - For example: - pattern = '^root(:[^:]*){4}:(?P<return>[^:]*)' - with open('/etc/passwd', 'r') as stream: - return search_within_file(stream, pattern) - should return root's home directory (/root on my system). - - Args: - input_stream: file-like object to be read - pattern: (string) to be passed to re.compile - default: what to return if no match - - Returns: - A string or whatever default is - """ - pattern_object = re.compile(pattern) - for line in input_stream: - match = pattern_object.search(line) - if match: - return match.group('return') - return default - - @staticmethod - def search_within_string(input_string, pattern, default=None): - """Search for regular expression in a string. - - Args: - input_string: (string) to be searched - pattern: (string) to be passed to re.compile - default: what to return if no match - - Returns: - A string or whatever default is - """ - match = re.search(pattern, input_string) - return match.group('return') if match else default + """A collection of static methods for regexing things.""" + + @staticmethod + def search_within_stream(input_stream, pattern, default=None): + """Search for regular expression in a file-like object. + + Opens a file for reading and searches line by line for a match to + the regex and returns the parenthesized group named return for the + first match. Does not search across newlines. + + For example: + pattern = '^root(:[^:]*){4}:(?P<return>[^:]*)' + with open('/etc/passwd', 'r') as stream: + return search_within_file(stream, pattern) + should return root's home directory (/root on my system). + + Args: + input_stream: file-like object to be read + pattern: (string) to be passed to re.compile + default: what to return if no match + + Returns: + A string or whatever default is + """ + pattern_object = re.compile(pattern) + for line in input_stream: + match = pattern_object.search(line) + if match: + return match.group('return') + return default + + @staticmethod + def search_within_string(input_string, pattern, default=None): + """Search for regular expression in a string. + + Args: + input_string: (string) to be searched + pattern: (string) to be passed to re.compile + default: what to return if no match + + Returns: + A string or whatever default is + """ + match = re.search(pattern, input_string) + return match.group('return') if match else default + diff --git a/tools/roll_deps.py b/tools/roll_deps.py index 18f4157bae..e255099ec0 100755 --- a/tools/roll_deps.py +++ b/tools/roll_deps.py @@ -38,27 +38,27 @@ from common.py.utils import shell_utils DEFAULT_BOTS_LIST = [ - 'android_clang_dbg', - 'android_dbg', - 'android_rel', - 'cros_daisy', - 'linux', - 'linux_asan', - 'linux_chromeos', - 'linux_chromeos_asan', - 'linux_chromium_gn_dbg', - 'linux_gpu', - 'linux_layout', - 'linux_layout_rel', - 'mac', - 'mac_asan', - 'mac_gpu', - 'mac_layout', - 'mac_layout_rel', - 'win', - 'win_gpu', - 'win_layout', - 'win_layout_rel', + 'android_clang_dbg', + 'android_dbg', + 'android_rel', + 'cros_daisy', + 'linux', + 'linux_asan', + 'linux_chromeos', + 'linux_chromeos_asan', + 'linux_chromium_gn_dbg', + 'linux_gpu', + 'linux_layout', + 'linux_layout_rel', + 'mac', + 'mac_asan', + 'mac_gpu', + 'mac_layout', + 'mac_layout_rel', + 'win', + 'win_gpu', + 'win_layout', + 'win_layout_rel', ] REGEXP_SKIA_REVISION = ( @@ -66,234 +66,233 @@ REGEXP_SKIA_REVISION = ( class DepsRollConfig(object): - """Contains configuration options for this module. - - Attributes: - chromium_path: (string) path to a local chromium git repository. - save_branches: (boolean) iff false, delete temporary branches. - verbose: (boolean) iff false, suppress the output from git-cl. - skip_cl_upload: (boolean) - cl_bot_list: (list of strings) - """ + """Contains configuration options for this module. + + Attributes: + chromium_path: (string) path to a local chromium git repository. + save_branches: (boolean) iff false, delete temporary branches. + verbose: (boolean) iff false, suppress the output from git-cl. + skip_cl_upload: (boolean) + cl_bot_list: (list of strings) + """ + + # pylint: disable=I0011,R0903,R0902 + def __init__(self, options=None): + if not options: + options = DepsRollConfig.GetOptionParser() + # pylint: disable=I0011,E1103 + self.verbose = options.verbose + self.save_branches = not options.delete_branches + self.chromium_path = options.chromium_path + self.skip_cl_upload = options.skip_cl_upload + # Split and remove empty strigns from the bot list. + self.cl_bot_list = [bot for bot in options.bots.split(',') if bot] + self.default_branch_name = 'autogenerated_deps_roll_branch' + self.reviewers_list = ','.join([ + # 'rmistry@google.com', + # 'reed@google.com', + # 'bsalomon@google.com', + # 'robertphillips@google.com', + ]) + self.cc_list = ','.join([ + # 'skia-team@google.com', + ]) + + @staticmethod + def GetOptionParser(): + # pylint: disable=I0011,C0103 + """Returns an optparse.OptionParser object. + + Returns: + An optparse.OptionParser object. - # pylint: disable=I0011,R0903,R0902 - def __init__(self, options=None): - if not options: - options = DepsRollConfig.GetOptionParser() - # pylint: disable=I0011,E1103 - self.verbose = options.verbose - self.save_branches = not options.delete_branches - self.chromium_path = options.chromium_path - self.skip_cl_upload = options.skip_cl_upload - # Split and remove empty strigns from the bot list. - self.cl_bot_list = [bot for bot in options.bots.split(',') if bot] - self.default_branch_name = 'autogenerated_deps_roll_branch' - self.reviewers_list = ','.join([ - # 'rmistry@google.com', - # 'reed@google.com', - # 'bsalomon@google.com', - # 'robertphillips@google.com', - ]) - self.cc_list = ','.join([ - # 'skia-team@google.com', - ]) - - @staticmethod - def GetOptionParser(): - # pylint: disable=I0011,C0103 - """Returns an optparse.OptionParser object. - - Returns: - An optparse.OptionParser object. - - Called by the main() function. - """ - option_parser = optparse.OptionParser(usage=__doc__) - # Anyone using this script on a regular basis should set the - # CHROMIUM_CHECKOUT_PATH environment variable. - option_parser.add_option( - '-c', '--chromium_path', help='Path to local Chromium Git' - ' repository checkout, defaults to CHROMIUM_CHECKOUT_PATH' - ' if that environment variable is set.', - default=os.environ.get('CHROMIUM_CHECKOUT_PATH')) - option_parser.add_option( - '-r', '--revision', default=None, - help='The Skia Git commit hash.') - - option_parser.add_option( - '', '--delete_branches', help='Delete the temporary branches', - action='store_true', dest='delete_branches', default=False) - option_parser.add_option( - '', '--verbose', help='Do not suppress the output from `git cl`.', - action='store_true', dest='verbose', default=False) - option_parser.add_option( - '', '--skip_cl_upload', help='Skip the cl upload step; useful' - ' for testing.', - action='store_true', default=False) - - default_bots_help = ( - 'Comma-separated list of bots, defaults to a list of %d bots.' - ' To skip `git cl try`, set this to an empty string.' - % len(DEFAULT_BOTS_LIST)) - default_bots = ','.join(DEFAULT_BOTS_LIST) - option_parser.add_option( - '', '--bots', help=default_bots_help, default=default_bots) - - return option_parser + Called by the main() function. + """ + option_parser = optparse.OptionParser(usage=__doc__) + # Anyone using this script on a regular basis should set the + # CHROMIUM_CHECKOUT_PATH environment variable. + option_parser.add_option( + '-c', '--chromium_path', help='Path to local Chromium Git' + ' repository checkout, defaults to CHROMIUM_CHECKOUT_PATH' + ' if that environment variable is set.', + default=os.environ.get('CHROMIUM_CHECKOUT_PATH')) + option_parser.add_option( + '-r', '--revision', default=None, + help='The Skia Git commit hash.') + + option_parser.add_option( + '', '--delete_branches', help='Delete the temporary branches', + action='store_true', dest='delete_branches', default=False) + option_parser.add_option( + '', '--verbose', help='Do not suppress the output from `git cl`.', + action='store_true', dest='verbose', default=False) + option_parser.add_option( + '', '--skip_cl_upload', help='Skip the cl upload step; useful' + ' for testing.', + action='store_true', default=False) + + default_bots_help = ( + 'Comma-separated list of bots, defaults to a list of %d bots.' + ' To skip `git cl try`, set this to an empty string.' + % len(DEFAULT_BOTS_LIST)) + default_bots = ','.join(DEFAULT_BOTS_LIST) + option_parser.add_option( + '', '--bots', help=default_bots_help, default=default_bots) + + return option_parser class DepsRollError(Exception): - """Exceptions specific to this module.""" - pass + """Exceptions specific to this module.""" + pass def change_skia_deps(revision, depspath): - """Update the DEPS file. + """Update the DEPS file. - Modify the skia_revision entry in the given DEPS file. + Modify the skia_revision entry in the given DEPS file. - Args: - revision: (string) Skia commit hash. - depspath: (string) path to DEPS file. - """ - temp_file = tempfile.NamedTemporaryFile(delete=False, - prefix='skia_DEPS_ROLL_tmp_') - try: - deps_regex_rev = re.compile(REGEXP_SKIA_REVISION) - deps_regex_rev_repl = ' "skia_revision": "%s",' % revision + Args: + revision: (string) Skia commit hash. + depspath: (string) path to DEPS file. + """ + temp_file = tempfile.NamedTemporaryFile(delete=False, + prefix='skia_DEPS_ROLL_tmp_') + try: + deps_regex_rev = re.compile(REGEXP_SKIA_REVISION) + deps_regex_rev_repl = ' "skia_revision": "%s",' % revision - with open(depspath, 'r') as input_stream: - for line in input_stream: - line = deps_regex_rev.sub(deps_regex_rev_repl, line) - temp_file.write(line) - finally: - temp_file.close() - shutil.move(temp_file.name, depspath) + with open(depspath, 'r') as input_stream: + for line in input_stream: + line = deps_regex_rev.sub(deps_regex_rev_repl, line) + temp_file.write(line) + finally: + temp_file.close() + shutil.move(temp_file.name, depspath) def submit_tries(bots_to_run, dry_run=False): - """Submit try requests for the current branch on the given bots. + """Submit try requests for the current branch on the given bots. - Args: - bots_to_run: (list of strings) bots to request. - dry_run: (bool) whether to actually submit the try request. - """ - git_try = [ - git_utils.GIT, 'cl', 'try', '-m', 'tryserver.chromium'] - git_try.extend([arg for bot in bots_to_run for arg in ('-b', bot)]) + Args: + bots_to_run: (list of strings) bots to request. + dry_run: (bool) whether to actually submit the try request. + """ + git_try = [ + git_utils.GIT, 'cl', 'try', '-m', 'tryserver.chromium'] + git_try.extend([arg for bot in bots_to_run for arg in ('-b', bot)]) - if dry_run: - space = ' ' - print 'You should call:' - print space, git_try - print - else: - shell_utils.run(git_try) + if dry_run: + space = ' ' + print 'You should call:' + print space, git_try + print + else: + shell_utils.run(git_try) def roll_deps(config, revision): - """Upload changed DEPS and a whitespace change. - - Given the correct git_hash, create two Reitveld issues. - - Args: - config: (roll_deps.DepsRollConfig) object containing options. - revision: (string) Skia Git hash. - - Returns: - a tuple containing textual description of the two issues. - - Raises: - OSError: failed to execute git or git-cl. - subprocess.CalledProcessError: git returned unexpected status. - """ - - with misc.ChDir(config.chromium_path, verbose=config.verbose): - git_utils.Fetch() - output = shell_utils.run([git_utils.GIT, 'show', 'origin/master:DEPS'], - log_in_real_time=False).rstrip() - match = re.search(REGEXP_SKIA_REVISION, output, flags=re.MULTILINE) - old_revision = None - if match: - old_revision = match.group('revision') - assert old_revision - - master_hash = git_utils.FullHash('origin/master').rstrip() - - # master_hash[8] gives each whitespace CL a unique name. - branch = 'control_%s' % master_hash[:8] - message = ('whitespace change %s\n\n' - 'Chromium base revision: %s\n\n' - 'This CL was created by Skia\'s roll_deps.py script.\n' - ) % (master_hash[:8], master_hash[:8]) - with git_utils.GitBranch(branch, message, - delete_when_finished=not config.save_branches, - upload=not config.skip_cl_upload - ) as whitespace_branch: - branch = git_utils.GetCurrentBranch() - with open(os.path.join('build', 'whitespace_file.txt'), 'a') as f: - f.write('\nCONTROL\n') - - control_url = whitespace_branch.commit_and_upload() - if config.cl_bot_list: - submit_tries(config.cl_bot_list, dry_run=config.skip_cl_upload) - whitespace_cl = control_url - if config.save_branches: - whitespace_cl += '\n branch: %s' % branch - - branch = 'roll_%s_%s' % (revision, master_hash[:8]) - message = ( - 'roll skia DEPS to %s\n\n' - 'Chromium base revision: %s\n' - 'Old Skia revision: %s\n' - 'New Skia revision: %s\n' - 'Control CL: %s\n\n' - 'This CL was created by Skia\'s roll_deps.py script.\n\n' - 'Bypassing commit queue trybots:\n' - 'NOTRY=true\n' - % (revision, master_hash[:8], - old_revision[:8], revision[:8], control_url)) - with git_utils.GitBranch(branch, message, - delete_when_finished=not config.save_branches, - upload=not config.skip_cl_upload - ) as roll_branch: - change_skia_deps(revision, 'DEPS') - deps_url = roll_branch.commit_and_upload() - if config.cl_bot_list: - submit_tries(config.cl_bot_list, dry_run=config.skip_cl_upload) - deps_cl = deps_url - if config.save_branches: - deps_cl += '\n branch: %s' % branch - - return deps_cl, whitespace_cl + """Upload changed DEPS and a whitespace change. + + Given the correct git_hash, create two Reitveld issues. + + Args: + config: (roll_deps.DepsRollConfig) object containing options. + revision: (string) Skia Git hash. + + Returns: + a tuple containing textual description of the two issues. + + Raises: + OSError: failed to execute git or git-cl. + subprocess.CalledProcessError: git returned unexpected status. + """ + with misc.ChDir(config.chromium_path, verbose=config.verbose): + git_utils.Fetch() + output = shell_utils.run([git_utils.GIT, 'show', 'origin/master:DEPS'], + log_in_real_time=False).rstrip() + match = re.search(REGEXP_SKIA_REVISION, output, flags=re.MULTILINE) + old_revision = None + if match: + old_revision = match.group('revision') + assert old_revision + + master_hash = git_utils.FullHash('origin/master').rstrip() + + # master_hash[8] gives each whitespace CL a unique name. + branch = 'control_%s' % master_hash[:8] + message = ('whitespace change %s\n\n' + 'Chromium base revision: %s\n\n' + 'This CL was created by Skia\'s roll_deps.py script.\n' + ) % (master_hash[:8], master_hash[:8]) + with git_utils.GitBranch(branch, message, + delete_when_finished=not config.save_branches, + upload=not config.skip_cl_upload + ) as whitespace_branch: + branch = git_utils.GetCurrentBranch() + with open(os.path.join('build', 'whitespace_file.txt'), 'a') as f: + f.write('\nCONTROL\n') + + control_url = whitespace_branch.commit_and_upload() + if config.cl_bot_list: + submit_tries(config.cl_bot_list, dry_run=config.skip_cl_upload) + whitespace_cl = control_url + if config.save_branches: + whitespace_cl += '\n branch: %s' % branch + + branch = 'roll_%s_%s' % (revision, master_hash[:8]) + message = ( + 'roll skia DEPS to %s\n\n' + 'Chromium base revision: %s\n' + 'Old Skia revision: %s\n' + 'New Skia revision: %s\n' + 'Control CL: %s\n\n' + 'This CL was created by Skia\'s roll_deps.py script.\n\n' + 'Bypassing commit queue trybots:\n' + 'NOTRY=true\n' + % (revision, master_hash[:8], + old_revision[:8], revision[:8], control_url)) + with git_utils.GitBranch(branch, message, + delete_when_finished=not config.save_branches, + upload=not config.skip_cl_upload + ) as roll_branch: + change_skia_deps(revision, 'DEPS') + deps_url = roll_branch.commit_and_upload() + if config.cl_bot_list: + submit_tries(config.cl_bot_list, dry_run=config.skip_cl_upload) + deps_cl = deps_url + if config.save_branches: + deps_cl += '\n branch: %s' % branch + + return deps_cl, whitespace_cl def main(args): - """main function; see module-level docstring and GetOptionParser help. + """main function; see module-level docstring and GetOptionParser help. - Args: - args: sys.argv[1:]-type argument list. - """ - option_parser = DepsRollConfig.GetOptionParser() - options = option_parser.parse_args(args)[0] + Args: + args: sys.argv[1:]-type argument list. + """ + option_parser = DepsRollConfig.GetOptionParser() + options = option_parser.parse_args(args)[0] - if not options.revision: - option_parser.error('Must specify a revision.') - if not options.chromium_path: - option_parser.error('Must specify chromium_path.') - if not os.path.isdir(options.chromium_path): - option_parser.error('chromium_path must be a directory.') + if not options.revision: + option_parser.error('Must specify a revision.') + if not options.chromium_path: + option_parser.error('Must specify chromium_path.') + if not os.path.isdir(options.chromium_path): + option_parser.error('chromium_path must be a directory.') - config = DepsRollConfig(options) - shell_utils.VERBOSE = options.verbose - deps_issue, whitespace_issue = roll_deps(config, options.revision) + config = DepsRollConfig(options) + shell_utils.VERBOSE = options.verbose + deps_issue, whitespace_issue = roll_deps(config, options.revision) - if deps_issue and whitespace_issue: - print 'DEPS roll:\n %s\n' % deps_issue - print 'Whitespace change:\n %s\n' % whitespace_issue - else: - print >> sys.stderr, 'No issues created.' + if deps_issue and whitespace_issue: + print 'DEPS roll:\n %s\n' % deps_issue + print 'Whitespace change:\n %s\n' % whitespace_issue + else: + print >> sys.stderr, 'No issues created.' if __name__ == '__main__': - main(sys.argv[1:]) + main(sys.argv[1:]) |