diff --git a/build-support/bin/ci.py b/build-support/bin/ci.py index cd8c72d0798..6929e0adaf2 100755 --- a/build-support/bin/ci.py +++ b/build-support/bin/ci.py @@ -262,6 +262,7 @@ def get_listed_targets(filename: str) -> TargetSet: f"--tag={'-' if test_type == TestType.unit else '+'}integration", "filter", "--type=python_tests", + "build-support::", "src/python::", "tests/python::", "contrib::", diff --git a/build-support/migration-support/fix_deprecated_globs_usage.py b/build-support/migration-support/fix_deprecated_globs_usage.py index 52451476fd0..61c1dfe6458 100755 --- a/build-support/migration-support/fix_deprecated_globs_usage.py +++ b/build-support/migration-support/fix_deprecated_globs_usage.py @@ -45,7 +45,7 @@ def create_parser() -> argparse.ArgumentParser: description='Modernize BUILD files to no longer use globs, rglobs, and zglobs.', ) parser.add_argument( - 'folders', type=Path, nargs='*', + 'folders', type=Path, nargs='+', help="Folders to recursively search for `BUILD` files", ) parser.add_argument( diff --git a/build-support/migration-support/migrate_to_toml_config.py b/build-support/migration-support/migrate_to_toml_config.py new file mode 100755 index 00000000000..e74d5c0135d --- /dev/null +++ b/build-support/migration-support/migrate_to_toml_config.py @@ -0,0 +1,127 @@ +#!/usr/bin/env python3 +# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md). +# Licensed under the Apache License, Version 2.0 (see LICENSE). + +"""A script to automatically convert an INI Pants config file to TOML. There will still likely be +some issues remaining which require manual fixes, but this script will automate most of the tedium. + +Run `python3 migrate_to_toml_config.py --help`. +""" + +import argparse +import logging +import re +from pathlib import Path +from typing import Dict, List + + +def main() -> None: + args = create_parser().parse_args() + updates: Dict[Path, List[str]] = {} + for config in args.files: + if config.suffix not in [".ini", ".cfg"]: + logging.warning(f"This script may only be run on INI files. Skipping {config}.") + continue + new_path = Path(config.parent, f"{config.stem}.toml") + if new_path.exists(): + logging.warning(f"{new_path} already exists. Skipping conversion of {config}.") + continue + new_config_content = generate_new_config(config) + updates[new_path] = new_config_content + for new_path, new_content in updates.items(): + joined_new_content = '\n'.join(new_content) + "\n" + if args.preview: + print(f"Would create {new_path} with the following content:\n\n{joined_new_content}") + else: + logging.info( + f"Created {new_path}. There are likely some remaining issues that need manual " + "attention. Please copy the file into https://www.toml-lint.com or open with your editor " + "to fix any remaining issues." + ) + new_path.write_text(joined_new_content) + + +def create_parser() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser( + description='Convert INI config files to TOML config files.', + ) + parser.add_argument( + 'files', type=Path, nargs='*', + help="Config files to convert.", + ) + parser.add_argument( + '-p', '--preview', action='store_true', + help="Output to stdout rather than creating the new TOML config file(s).", + ) + return parser + + +def update_primitive_value(original: str) -> str: + if original in ["true", "True"]: + return "true" + if original in ["false", "False"]: + return "false" + + try: + return str(int(original)) + except ValueError: + pass + + try: + return str(float(original)) + except ValueError: + pass + + return f'"{original}"' + + +def generate_new_config(config: Path) -> List[str]: + original_text = config.read_text() + original_text_lines = original_text.splitlines() + updated_text_lines = original_text_lines.copy() + + for i, line in enumerate(original_text_lines): + option_regex = r"(?P