def _write_new_config_file(path, output): original_contents = open(path).read() output = remove_defaults(output, CONFIG_SCHEMA) new_contents = ordered_dump(output, **C.YAML_DUMP_KWARGS) lines = original_contents.splitlines(True) sha_line_indices_rev = list(reversed([ i for i, line in enumerate(lines) if SHA_LINE_RE.match(line) ])) for line in new_contents.splitlines(True): if SHA_LINE_RE.match(line): # It's possible we didn't identify the sha lines in the original if not sha_line_indices_rev: break line_index = sha_line_indices_rev.pop() original_line = lines[line_index] orig_match = SHA_LINE_RE.match(original_line) new_match = SHA_LINE_RE.match(line) lines[line_index] = SHA_LINE_FMT.format( orig_match.group(1), orig_match.group(2), new_match.group(3), orig_match.group(4), ) # If we failed to intelligently rewrite the sha lines, fall back to the # pretty-formatted yaml output to_write = ''.join(lines) if remove_defaults(ordered_load(to_write), CONFIG_SCHEMA) != output: to_write = new_contents with open(path, 'w') as f: f.write(to_write)
def _write_new_config_file(path, output): original_contents = open(path).read() output = remove_defaults(output, CONFIG_SCHEMA) new_contents = ordered_dump(output, **C.YAML_DUMP_KWARGS) lines = original_contents.splitlines(True) sha_line_indices_rev = list( reversed( [i for i, line in enumerate(lines) if SHA_LINE_RE.match(line)])) for line in new_contents.splitlines(True): if SHA_LINE_RE.match(line): # It's possible we didn't identify the sha lines in the original if not sha_line_indices_rev: break line_index = sha_line_indices_rev.pop() original_line = lines[line_index] orig_match = SHA_LINE_RE.match(original_line) new_match = SHA_LINE_RE.match(line) lines[line_index] = SHA_LINE_FMT.format( orig_match.group(1), orig_match.group(2), new_match.group(3), orig_match.group(4), ) # If we failed to intelligently rewrite the sha lines, fall back to the # pretty-formatted yaml output to_write = ''.join(lines) if remove_defaults(ordered_load(to_write), CONFIG_SCHEMA) != output: to_write = new_contents with open(path, 'w') as f: f.write(to_write)
def autoupdate(runner, tags_only): """Auto-update the pre-commit config to the latest versions of repos.""" retv = 0 output_configs = [] changed = False input_configs = load_config( runner.config_file_path, load_strategy=ordered_load, ) for repo_config in input_configs: if is_local_repo(repo_config): output_configs.append(repo_config) continue output.write('Updating {}...'.format(repo_config['repo'])) try: new_repo_config = _update_repo(repo_config, runner, tags_only) except RepositoryCannotBeUpdatedError as error: output.write_line(error.args[0]) output_configs.append(repo_config) retv = 1 continue if new_repo_config['sha'] != repo_config['sha']: changed = True output.write_line('updating {} -> {}.'.format( repo_config['sha'], new_repo_config['sha'], )) output_configs.append(new_repo_config) else: output.write_line('already up to date.') output_configs.append(repo_config) if changed: with open(runner.config_file_path, 'w') as config_file: config_file.write( ordered_dump(remove_defaults(output_configs, CONFIG_SCHEMA), **C.YAML_DUMP_KWARGS)) return retv
def autoupdate(runner, tags_only): """Auto-update the pre-commit config to the latest versions of repos.""" retv = 0 output_configs = [] changed = False input_configs = load_config( runner.config_file_path, load_strategy=ordered_load, ) for repo_config in input_configs: if is_local_repo(repo_config): output_configs.append(repo_config) continue output.write('Updating {}...'.format(repo_config['repo'])) try: new_repo_config = _update_repo(repo_config, runner, tags_only) except RepositoryCannotBeUpdatedError as error: output.write_line(error.args[0]) output_configs.append(repo_config) retv = 1 continue if new_repo_config['sha'] != repo_config['sha']: changed = True output.write_line('updating {} -> {}.'.format( repo_config['sha'], new_repo_config['sha'], )) output_configs.append(new_repo_config) else: output.write_line('already up to date.') output_configs.append(repo_config) if changed: with open(runner.config_file_path, 'w') as config_file: config_file.write(ordered_dump( remove_defaults(output_configs, CONFIG_SCHEMA), **C.YAML_DUMP_KWARGS )) return retv
def test_remove_defaults_nested(): val = {'repo': 'repo1', 'hooks': [{'key': False}]} ret = remove_defaults(val, nested_schema_optional) assert ret == {'repo': 'repo1', 'hooks': [{}]}
def test_remove_defaults_does_nothing_on_non_optional(): ret = remove_defaults({'key': True}, map_required) assert ret == {'key': True}
def test_remove_defaults_map_in_list(): ret = remove_defaults([{'key': False}], Array(map_optional)) assert ret == [{}]
def test_remove_defaults_does_not_change_non_default(): ret = remove_defaults({'key': True}, map_optional) assert ret == {'key': True}
def test_remove_defaults_nothing_to_remove(): ret = remove_defaults({}, map_optional) assert ret == {}
def test_remove_defaults_removes_defaults(): ret = remove_defaults({'key': False}, map_optional) assert ret == {}
def test_remove_defaults_copies_object(): val = {'key': False} ret = remove_defaults(val, map_optional) assert ret is not val