def ordered_load_normalize_legacy_config(contents: str) -> Dict[str, Any]: data = yaml_load(contents) if isinstance(data, list): # TODO: Once happy, issue a deprecation warning and instructions return {'repos': data} else: return data
def ordered_load_normalize_legacy_config(contents: str) -> Dict[str, Any]: data = yaml_load(contents) if isinstance(data, list): logger.warning( 'normalizing pre-commit configuration to a top-level map. ' 'support for top level list will be removed in a future version. ' 'run: `pre-commit migrate-config` to automatically fix this.', ) return {'repos': data} else: return data
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 _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(yaml_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}' yaml_load(trial_contents) contents = trial_contents except yaml.YAMLError: contents = f'{header}repos:\n{_indent(rest)}' return contents
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 _install_dir(prefix_p: Prefix, pub_cache: str) -> None: dart_env = {**os.environ, 'PUB_CACHE': pub_cache} with open(prefix_p.path('pubspec.yaml')) as f: pubspec_contents = yaml_load(f) helpers.run_setup_cmd(prefix_p, ('dart', 'pub', 'get'), env=dart_env) for executable in pubspec_contents['executables']: helpers.run_setup_cmd( prefix_p, ( 'dart', 'compile', 'exe', '--output', os.path.join(bin_dir, win_exe(executable)), prefix_p.path('bin', f'{executable}.dart'), ), env=dart_env, )
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 read_config(directory, config_file=C.CONFIG_FILE): config_path = os.path.join(directory, config_file) with open(config_path) as f: config = yaml_load(f.read()) return config