def migrate_config(runner, quiet=False): with io.open(runner.config_file_path) as f: contents = f.read() # Find the first non-header line lines = contents.splitlines(True) i = 0 while _is_header_line(lines[i]): i += 1 header = ''.join(lines[:i]) rest = ''.join(lines[i:]) if isinstance(ordered_load(contents), list): # If they are using the "default" flow style of yaml, this operation # will yield a valid configuration try: trial_contents = header + 'repos:\n' + rest yaml.load(trial_contents) contents = trial_contents except yaml.YAMLError: contents = header + 'repos:\n' + _indent(rest) with io.open(runner.config_file_path, 'w') as f: f.write(contents) print('Configuration has been migrated.') elif not quiet: print('Configuration is already migrated.')
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) rev_line_indices_reversed = list(reversed([ i for i, line in enumerate(lines) if REV_LINE_RE.match(line) ])) for line in new_contents.splitlines(True): if REV_LINE_RE.match(line): # It's possible we didn't identify the rev lines in the original if not rev_line_indices_reversed: break line_index = rev_line_indices_reversed.pop() original_line = lines[line_index] orig_match = REV_LINE_RE.match(original_line) new_match = REV_LINE_RE.match(line) lines[line_index] = REV_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 rev 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): with open(path) as f: original_contents = f.read() output = remove_defaults(output, CONFIG_SCHEMA) new_contents = ordered_dump(output, **C.YAML_DUMP_KWARGS) lines = original_contents.splitlines(True) rev_line_indices_reversed = list( reversed([ i for i, line in enumerate(lines) if REV_LINE_RE.match(line) ]), ) for line in new_contents.splitlines(True): if REV_LINE_RE.match(line): # It's possible we didn't identify the rev lines in the original if not rev_line_indices_reversed: break line_index = rev_line_indices_reversed.pop() original_line = lines[line_index] orig_match = REV_LINE_RE.match(original_line) new_match = REV_LINE_RE.match(line) lines[line_index] = REV_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 rev 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 ordered_load_normalize_legacy_config(contents): data = ordered_load(contents) if isinstance(data, list): # TODO: Once happy, issue a deprecation warning and instructions return {'repos': data} else: return data
def modify_manifest(path): """Modify the manifest yielded by this context to write to hooks.yaml.""" manifest_path = os.path.join(path, C.MANIFEST_FILE) manifest = ordered_load(io.open(manifest_path).read()) yield manifest with io.open(manifest_path, 'w') as manifest_file: manifest_file.write(ordered_dump(manifest, **C.YAML_DUMP_KWARGS)) cmd_output('git', 'commit', '-am', 'update hooks.yaml', cwd=path)
def modify_config(path='.', commit=True): """Modify the config yielded by this context to write to .pre-commit-config.yaml """ config_path = os.path.join(path, C.CONFIG_FILE) config = ordered_load(io.open(config_path).read()) yield config with io.open(config_path, 'w', encoding='UTF-8') as config_file: config_file.write(ordered_dump(config, **C.YAML_DUMP_KWARGS)) if commit: cmd_output('git', 'commit', '-am', 'update config', cwd=path)
def modify_config(path='.', commit=True): """Modify the config yielded by this context to write to .pre-commit-config.yaml """ config_path = os.path.join(path, C.CONFIG_FILE) with io.open(config_path) as f: config = ordered_load(f.read()) yield config with io.open(config_path, 'w', encoding='UTF-8') as config_file: config_file.write(ordered_dump(config, **C.YAML_DUMP_KWARGS)) if commit: git_commit(msg=modify_config.__name__, cwd=path)
def _migrate_map(contents): # Find the first non-header line lines = contents.splitlines(True) i = 0 while _is_header_line(lines[i]): i += 1 header = ''.join(lines[:i]) rest = ''.join(lines[i:]) if isinstance(ordered_load(contents), list): # If they are using the "default" flow style of yaml, this operation # will yield a valid configuration try: trial_contents = header + 'repos:\n' + rest ordered_load(trial_contents) contents = trial_contents except yaml.YAMLError: contents = header + 'repos:\n' + _indent(rest) return contents
def modify_manifest(path, commit=True): """Modify the manifest yielded by this context to write to .pre-commit-hooks.yaml. """ manifest_path = os.path.join(path, C.MANIFEST_FILE) with io.open(manifest_path) as f: manifest = ordered_load(f.read()) yield manifest with io.open(manifest_path, 'w') as manifest_file: manifest_file.write(ordered_dump(manifest, **C.YAML_DUMP_KWARGS)) if commit: git_commit(msg=modify_manifest.__name__, cwd=path)
def _migrate_map(contents): # Find the first non-header line lines = contents.splitlines(True) i = 0 # Only loop on non empty configuration file while i < len(lines) and _is_header_line(lines[i]): i += 1 header = ''.join(lines[:i]) rest = ''.join(lines[i:]) if isinstance(ordered_load(contents), list): # If they are using the "default" flow style of yaml, this operation # will yield a valid configuration try: trial_contents = header + 'repos:\n' + rest ordered_load(trial_contents) contents = trial_contents except yaml.YAMLError: contents = header + 'repos:\n' + _indent(rest) return contents
def _migrate_map(contents: str) -> str: # Find the first non-header line lines = contents.splitlines(True) i = 0 # Only loop on non empty configuration file while i < len(lines) and _is_header_line(lines[i]): i += 1 header = ''.join(lines[:i]) rest = ''.join(lines[i:]) if isinstance(ordered_load(contents), list): # If they are using the "default" flow style of yaml, this operation # will yield a valid configuration try: trial_contents = f'{header}repos:\n{rest}' ordered_load(trial_contents) contents = trial_contents except yaml.YAMLError: contents = f'{header}repos:\n{_indent(rest)}' return contents
def _original_lines(path, rev_infos, retry=False): """detect `rev:` lines or reformat the file""" with open(path) as f: original = f.read() lines = original.splitlines(True) idxs = [i for i, line in enumerate(lines) if REV_LINE_RE.match(line)] if len(idxs) == len(rev_infos): return lines, idxs elif retry: raise AssertionError('could not find rev lines') else: with open(path, 'w') as f: f.write(ordered_dump(ordered_load(original), **C.YAML_DUMP_KWARGS)) return _original_lines(path, rev_infos, retry=True)
def modify_manifest(path): """Modify the manifest yielded by this context to write to .pre-commit-hooks.yaml. """ manifest_path = os.path.join(path, C.MANIFEST_FILE) with io.open(manifest_path) as f: manifest = ordered_load(f.read()) yield manifest with io.open(manifest_path, 'w') as manifest_file: manifest_file.write(ordered_dump(manifest, **C.YAML_DUMP_KWARGS)) cmd_output( 'git', 'commit', '--no-gpg-sign', '-am', 'update {}'.format(C.MANIFEST_FILE), cwd=path, )
def read_config(directory, config_file=C.CONFIG_FILE): config_path = os.path.join(directory, config_file) config = ordered_load(io.open(config_path).read()) return config