Autoupdate roundtrips. Resolves #210

This commit is contained in:
Anthony Sottile 2015-03-18 10:06:23 -07:00
parent da7e85c851
commit de33fad483
11 changed files with 78 additions and 98 deletions

View file

@ -9,9 +9,9 @@ import sys
import jsonschema
import jsonschema.exceptions
import pkg_resources
import yaml
from pre_commit.jsonschema_extensions import apply_defaults
from pre_commit.util import yaml_load
def is_regex_valid(regex):
@ -36,14 +36,14 @@ def get_validator(
the object read from the file. The function should either raise
exception_type on failure.
"""
def validate(filename, load_strategy=yaml.load):
def validate(filename):
if not os.path.exists(filename):
raise exception_type('File {0} does not exist'.format(filename))
file_contents = open(filename, 'r').read()
try:
obj = load_strategy(file_contents)
obj = yaml_load(file_contents)
except Exception as e:
raise exception_type(
'Invalid yaml: {0}\n{1}'.format(os.path.relpath(filename), e),

View file

@ -3,18 +3,14 @@ from __future__ import unicode_literals
import sys
from aspy.yaml import ordered_dump
from aspy.yaml import ordered_load
import pre_commit.constants as C
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
from pre_commit.clientlib.validate_config import is_local_hooks
from pre_commit.clientlib.validate_config import load_config
from pre_commit.jsonschema_extensions import remove_defaults
from pre_commit.ordereddict import OrderedDict
from pre_commit.repository import Repository
from pre_commit.util import cmd_output
from pre_commit.util import cwd
from pre_commit.util import yaml_dump
class RepositoryCannotBeUpdatedError(RuntimeError):
@ -37,10 +33,10 @@ def _update_repository(repo_config, runner):
# Don't bother trying to update if our sha is the same
if head_sha == repo_config['sha']:
return repo_config
return head_sha
# Construct a new config with the head sha
new_config = OrderedDict(repo_config)
# Modify the sha to be the head sha
new_config = dict(repo_config)
new_config['sha'] = head_sha
new_repo = Repository.create(new_config, runner.store)
@ -53,53 +49,40 @@ def _update_repository(repo_config, runner):
'{0}'.format(', '.join(sorted(hooks_missing)))
)
return new_config
return head_sha
def autoupdate(runner):
"""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,
)
configs = load_config(runner.config_file_path)
for repo_config in input_configs:
for repo_config in configs:
if is_local_hooks(repo_config):
output_configs.append(repo_config)
continue
sys.stdout.write('Updating {0}...'.format(repo_config['repo']))
sys.stdout.flush()
original_sha = repo_config['sha']
try:
new_repo_config = _update_repository(repo_config, runner)
new_sha = _update_repository(repo_config, runner)
except RepositoryCannotBeUpdatedError as error:
print(error.args[0])
output_configs.append(repo_config)
retv = 1
continue
if new_repo_config['sha'] != repo_config['sha']:
if new_sha != original_sha:
changed = True
print(
'updating {0} -> {1}.'.format(
repo_config['sha'], new_repo_config['sha'],
)
)
output_configs.append(new_repo_config)
print('updating {0} -> {1}.'.format(original_sha, new_sha))
repo_config['sha'] = new_sha
else:
print('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_JSON_SCHEMA),
**C.YAML_DUMP_KWARGS
)
yaml_dump(remove_defaults(configs, CONFIG_JSON_SCHEMA))
)
return retv

View file

@ -4,10 +4,3 @@ from __future__ import unicode_literals
CONFIG_FILE = '.pre-commit-config.yaml'
MANIFEST_FILE = 'hooks.yaml'
YAML_DUMP_KWARGS = {
'default_flow_style': False,
# Use unicode
'encoding': None,
'indent': 4,
}

View file

@ -1,7 +0,0 @@
from __future__ import absolute_import
from __future__ import unicode_literals
try:
from collections import OrderedDict # noqa
except ImportError: # pragma: no cover (PY26)
from ordereddict import OrderedDict # noqa

View file

@ -12,6 +12,7 @@ import tarfile
import tempfile
import pkg_resources
import ruamel.yaml
from pre_commit import five
from pre_commit import parse_shebang
@ -213,3 +214,11 @@ def rmtree(path):
else:
raise
shutil.rmtree(path, ignore_errors=False, onerror=handle_remove_readonly)
yaml_load = functools.partial(
ruamel.yaml.load, Loader=ruamel.yaml.RoundTripLoader,
)
yaml_dump = functools.partial(
ruamel.yaml.dump, Dumper=ruamel.yaml.RoundTripDumper, encoding=None,
)