2025-02-09 21:33:11 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2025-02-09 21:10:22 +01:00
|
|
|
import re
|
2025-02-09 21:23:17 +01:00
|
|
|
import textwrap
|
2025-02-09 21:10:22 +01:00
|
|
|
|
2025-02-09 21:36:17 +01:00
|
|
|
import cfgv
|
2025-02-09 21:10:22 +01:00
|
|
|
import yaml
|
|
|
|
|
2025-02-09 21:36:17 +01:00
|
|
|
from pre_commit.clientlib import InvalidConfigError
|
|
|
|
from pre_commit.yaml import yaml_load
|
2025-02-09 21:10:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _is_header_line(line: str) -> bool:
|
|
|
|
return line.startswith(('#', '---')) or not line.strip()
|
|
|
|
|
|
|
|
|
|
|
|
def _migrate_map(contents: str) -> str:
|
2025-02-09 21:23:17 +01:00
|
|
|
if isinstance(yaml_load(contents), list):
|
|
|
|
# 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
|
2025-02-09 21:10:22 +01:00
|
|
|
|
2025-02-09 21:23:17 +01:00
|
|
|
header = ''.join(lines[:i])
|
|
|
|
rest = ''.join(lines[i:])
|
2025-02-09 21:10:22 +01:00
|
|
|
|
|
|
|
# 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}'
|
|
|
|
yaml_load(trial_contents)
|
|
|
|
contents = trial_contents
|
|
|
|
except yaml.YAMLError:
|
2025-02-09 21:23:17 +01:00
|
|
|
contents = f'{header}repos:\n{textwrap.indent(rest, " " * 4)}'
|
2025-02-09 21:10:22 +01:00
|
|
|
|
|
|
|
return contents
|
|
|
|
|
|
|
|
|
|
|
|
def _migrate_sha_to_rev(contents: str) -> str:
|
|
|
|
return re.sub(r'(\n\s+)sha:', r'\1rev:', contents)
|
|
|
|
|
|
|
|
|
2025-02-09 21:38:08 +01:00
|
|
|
def _migrate_python_venv(contents: str) -> str:
|
|
|
|
return re.sub(
|
|
|
|
r'(\n\s+)language: python_venv\b',
|
|
|
|
r'\1language: python',
|
|
|
|
contents,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2025-02-09 21:10:22 +01:00
|
|
|
def migrate_config(config_file: str, quiet: bool = False) -> int:
|
|
|
|
with open(config_file) as f:
|
|
|
|
orig_contents = contents = f.read()
|
|
|
|
|
2025-02-09 21:36:17 +01:00
|
|
|
with cfgv.reraise_as(InvalidConfigError):
|
|
|
|
with cfgv.validate_context(f'File {config_file}'):
|
|
|
|
try:
|
|
|
|
yaml_load(orig_contents)
|
|
|
|
except Exception as e:
|
|
|
|
raise cfgv.ValidationError(str(e))
|
|
|
|
|
2025-02-09 21:10:22 +01:00
|
|
|
contents = _migrate_map(contents)
|
|
|
|
contents = _migrate_sha_to_rev(contents)
|
2025-02-09 21:38:08 +01:00
|
|
|
contents = _migrate_python_venv(contents)
|
2025-02-09 21:10:22 +01:00
|
|
|
|
|
|
|
if contents != orig_contents:
|
|
|
|
with open(config_file, 'w') as f:
|
|
|
|
f.write(contents)
|
|
|
|
|
|
|
|
print('Configuration has been migrated.')
|
|
|
|
elif not quiet:
|
|
|
|
print('Configuration is already migrated.')
|
|
|
|
return 0
|