def try_repo(args: argparse.Namespace) -> int: with tmpdir() as tempdir: repo, ref = _repo_ref(tempdir, args.repo, args.ref) store = Store(tempdir) if args.hook: hooks = [{'id': args.hook}] else: repo_path = store.clone(repo, ref) manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE)) manifest = sorted(manifest, key=lambda hook: hook['id']) hooks = [{'id': hook['id']} for hook in manifest] config = {'repos': [{'repo': repo, 'rev': ref, 'hooks': hooks}]} config_s = yaml_dump(config) config_filename = os.path.join(tempdir, C.CONFIG_FILE) with open(config_filename, 'w') as cfg: cfg.write(config_s) output.write_line('=' * 79) output.write_line('Using config:') output.write_line('=' * 79) output.write(config_s) output.write_line('=' * 79) return run(config_filename, store, args)
def try_repo(args: argparse.Namespace) -> int: with tmpdir() as tempdir: repo, ref = _repo_ref(tempdir, args.repo, args.ref) store = Store(tempdir) if args.hook: hooks = [{"id": args.hook}] else: repo_path = store.clone(repo, ref) manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE)) manifest = sorted(manifest, key=lambda hook: hook["id"]) hooks = [{"id": hook["id"]} for hook in manifest] config = {"repos": [{"repo": repo, "rev": ref, "hooks": hooks}]} config_s = yaml_dump(config) config_filename = os.path.join(tempdir, C.CONFIG_FILE) with open(config_filename, "w") as cfg: cfg.write(config_s) output.write_line("=" * 79) output.write_line("Using config:") output.write_line("=" * 79) output.write(config_s) output.write_line("=" * 79) return run(config_filename, store, args)
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 open(config_path) as f: config = yaml_load(f.read()) yield config with open(config_path, 'w', encoding='UTF-8') as config_file: config_file.write(yaml_dump(config)) if commit: git_commit(msg=modify_config.__name__, cwd=path)
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 open(manifest_path) as f: manifest = yaml_load(f.read()) yield manifest with open(manifest_path, 'w') as manifest_file: manifest_file.write(yaml_dump(manifest)) if commit: git_commit(msg=modify_manifest.__name__, cwd=path)
def install_environment( prefix: Prefix, version: str, additional_dependencies: Sequence[str], ) -> None: helpers.assert_version_default('conda', version) directory = helpers.environment_dir(ENVIRONMENT_DIR, version) env_dir = prefix.path(directory) env_yaml_path = prefix.path('environment.yml') with clean_path_on_failure(env_dir): with open(env_yaml_path) as env_file: env_yaml = yaml_load(env_file) env_yaml['dependencies'] += additional_dependencies tmp_env_file = None try: with NamedTemporaryFile( suffix='.yml', mode='w', delete=False, ) as tmp_env_file: yaml_dump(env_yaml, stream=tmp_env_file) cmd_output_b( 'conda', 'env', 'create', '-p', env_dir, '--file', tmp_env_file.name, cwd=prefix.prefix_dir, ) finally: if tmp_env_file and os.path.exists(tmp_env_file.name): os.remove(tmp_env_file.name)
def _original_lines( path: str, rev_infos: List[Optional[RevInfo]], retry: bool = False, ) -> Tuple[List[str], List[int]]: """detect `rev:` lines or reformat the file""" with open(path, newline='') 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(yaml_dump(yaml_load(original))) return _original_lines(path, rev_infos, retry=True)
def _write_new_config(path: str, rev_infos: List[Optional[RevInfo]]) -> None: lines, idxs = _original_lines(path, rev_infos) for idx, rev_info in zip(idxs, rev_infos): if rev_info is None: continue match = REV_LINE_RE.match(lines[idx]) assert match is not None new_rev_s = yaml_dump({'rev': rev_info.rev}, default_style=match[3]) new_rev = new_rev_s.split(':', 1)[1].strip() if rev_info.frozen is not None: comment = f' # frozen: {rev_info.frozen}' elif match[5].strip().startswith('# frozen:'): comment = '' else: comment = match[5] lines[idx] = f'{match[1]}rev:{match[2]}{new_rev}{comment}{match[6]}' with open(path, 'w', newline='') as f: f.write(''.join(lines))
def _write_new_config(path: str, rev_infos: List[Optional[RevInfo]]) -> None: lines, idxs = _original_lines(path, rev_infos) for idx, rev_info in zip(idxs, rev_infos): if rev_info is None: continue match = REV_LINE_RE.match(lines[idx]) assert match is not None new_rev_s = yaml_dump({"rev": rev_info.rev}, default_style=match[3]) new_rev = new_rev_s.split(":", 1)[1].strip() if rev_info.frozen is not None: comment = f" # frozen: {rev_info.frozen}" elif match[5].strip().startswith("# frozen:"): comment = "" else: comment = match[5] lines[idx] = f"{match[1]}rev:{match[2]}{new_rev}{comment}{match[6]}" with open(path, "w", newline="") as f: f.write("".join(lines))
def write_config(directory, config, config_file=C.CONFIG_FILE): if type(config) is not list and 'repos' not in config: assert isinstance(config, dict), config config = {'repos': [config]} with open(os.path.join(directory, config_file), 'w') as outfile: outfile.write(yaml_dump(config))