From feb394ce5bf8c2ab9d84df8a4d9a95795f0c5846 Mon Sep 17 00:00:00 2001 From: Nicholas Wiles Date: Mon, 9 Jul 2018 21:31:12 -0700 Subject: [PATCH 1/3] Updated stylers to use pipelined approach (feature/150_cascade_stylers) --- zazu/style.py | 70 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/zazu/style.py b/zazu/style.py index d741796..01fa775 100644 --- a/zazu/style.py +++ b/zazu/style.py @@ -63,7 +63,7 @@ def stage_patch(path, input_string, styled_string): zazu.util.check_popen(args=['git', 'apply', '--cached', '--verbose', '-'], stdin_str=patch_string) -def style_file(styler, path, read_fn, write_fn): +def style_file(stylers, path, read_fn, write_fn): """Style a file. Args: @@ -74,11 +74,17 @@ def style_file(styler, path, read_fn, write_fn): """ input_string = read_fn(path) - styled_string = styler.style_string(input_string, path) + styled_string = input_string + for styler in stylers: + styled_string = styler.style_string(styled_string, path) violation = styled_string != input_string if violation and callable(write_fn): write_fn(path, input_string, styled_string) - return path, violation + return path, stylers, violation + + +def styler_list(file, sets, keys): + return [s for s in keys if file in sets[s]] @click.command() @@ -92,37 +98,49 @@ def style(ctx, verbose, check, cached): file_count = 0 violation_count = 0 stylers = ctx.obj.stylers() - FIXED_OK = [click.style('FIXED', fg='red', bold=True), click.style(' OK ', fg='green', bold=True)] - tags = zazu.util.FAIL_OK if check else FIXED_OK + fixed_ok_tags = [click.style('FIXED', fg='red', bold=True), click.style(' OK ', fg='green', bold=True)] + tags = zazu.util.FAIL_OK if check else fixed_ok_tags with zazu.util.cd(ctx.obj.repo_root): if stylers: if cached: staged_files = zazu.git_helper.get_touched_files(ctx.obj.repo) - # Run each Styler + read_fn = zazu.git_helper.read_staged + write_fn = stage_patch + else: + read_fn = read_file + write_fn = write_file + if check: + write_fn = None + # Determine files for each styler. + file_sets = {} + styler_file_sets = {} + all_files = set() for s in stylers: - files = zazu.util.scantree(ctx.obj.repo_root, - s.includes, - s.excludes, - exclude_hidden=True) - if cached: - files = set(files).intersection(staged_files) - read_fn = zazu.git_helper.read_staged - write_fn = stage_patch + includes = tuple(s.includes) + excludes = tuple(s.excludes) + if (includes, excludes) not in file_sets: + files = set(zazu.util.scantree(ctx.obj.repo_root, + includes, + excludes, + exclude_hidden=True)) + if cached: + files = files.intersection(staged_files) + file_sets[(includes, excludes)] = files else: - read_fn = read_file - write_fn = write_file - if check: - write_fn = None - file_count += len(files) - work = [functools.partial(style_file, s, f, read_fn, write_fn) for f in files] - checked_files = zazu.util.dispatch(work) - for f, violation in checked_files: - if verbose: - click.echo(zazu.util.format_checklist_item(not violation, - text='({}) {}'.format(s.name(), f), - tag_formats=tags)) + files = file_sets[(includes, excludes)] + styler_file_sets[s] = files + all_files |= files + + work = [functools.partial(style_file, styler_list(f, styler_file_sets, stylers), f, read_fn, write_fn) for f in all_files] + checked_files = zazu.util.dispatch(work) + for f, stylers, violation in checked_files: + if verbose: + click.echo(zazu.util.format_checklist_item(not violation, + text='({}) {}'.format(', '.join([s.name() for s in stylers]), f), + tag_formats=tags)) violation_count += violation if verbose: + file_count = len(all_files) if check: click.echo('{} files with violations in {} files'.format(violation_count, file_count)) else: From df00c4363bd0164462301bd2fbeedf86c8ee313d Mon Sep 17 00:00:00 2001 From: Nicholas Wiles Date: Thu, 12 Jul 2018 21:27:31 -0700 Subject: [PATCH 2/3] Fix up coverage (feature/150_cascade_stylers) --- tests/conftest.py | 1 + tests/test_style.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index 024e5bb..8547cbf 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -43,6 +43,7 @@ def repo_with_style(git_repo): {'exclude': ['dependency'], 'stylers':[ {'type': 'autopep8'}, + {'type': 'docformatter'}, {'type': 'clang-format'} ] } diff --git a/tests/test_style.py b/tests/test_style.py index 99bb878..54daa94 100644 --- a/tests/test_style.py +++ b/tests/test_style.py @@ -6,6 +6,10 @@ import subprocess import zazu.cli import zazu.plugins.clang_format_styler +import zazu.plugins.eslint_styler +import zazu.plugins.esformatter_styler +import zazu.plugins.generic_styler +import zazu.plugins.goimports_styler import zazu.plugins.astyle_styler import zazu.plugins.autopep8_styler import zazu.plugins.docformatter_styler From dfd1e19cfd2d267b79446f1631b1d3cb704c2add Mon Sep 17 00:00:00 2001 From: Nicholas Wiles Date: Fri, 13 Jul 2018 08:28:49 -0700 Subject: [PATCH 3/3] Fix up some doc strings (feature/150_cascade_stylers) --- zazu/dev/commands.py | 1 + zazu/plugins/eslint_styler.py | 1 - zazu/style.py | 2 ++ 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/zazu/dev/commands.py b/zazu/dev/commands.py index cfa4954..072b901 100755 --- a/zazu/dev/commands.py +++ b/zazu/dev/commands.py @@ -155,6 +155,7 @@ def find_branch_with_id(repo, id): def branch_is_current(repo, branch): + """Return True if branch is up to date with its tracking branch or if it doesn't have a tracking branch.""" repo.remotes.origin.fetch() if repo.heads[branch].tracking_branch() is None: return True diff --git a/zazu/plugins/eslint_styler.py b/zazu/plugins/eslint_styler.py index 9423cc3..342dd95 100644 --- a/zazu/plugins/eslint_styler.py +++ b/zazu/plugins/eslint_styler.py @@ -26,7 +26,6 @@ def style_string(self, string, filepath): Styled string. """ - eslint = 'eslint' cwd = os.path.normpath(os.getcwd()) dirname = os.path.normpath(os.path.dirname(filepath)) diff --git a/zazu/style.py b/zazu/style.py index 01fa775..c08175a 100644 --- a/zazu/style.py +++ b/zazu/style.py @@ -32,6 +32,7 @@ def write_file(path, _, styled_string): return f.write(styled_string) +"""The git binary doesn't allow concurrent access, so serailize calls to it using a lock.""" git_lock = threading.Lock() @@ -84,6 +85,7 @@ def style_file(stylers, path, read_fn, write_fn): def styler_list(file, sets, keys): + """Get the list of stylers to apply to a file based on the file set of each styler.""" return [s for s in keys if file in sets[s]]