in tools/upgrade/commands/targets_to_configuration.py [0:0]
def convert_directory(self, directory: Path) -> None:
all_targets = find_targets(directory, pyre_only=self._pyre_only)
if not all_targets:
LOG.warning("No configuration created because no targets found.")
return
# Set strict default to true if any binary or unittest targets are strict.
apply_strict = self._strict and any(
target.strict
for target in [
target for target_list in all_targets.values() for target in target_list
]
)
# Collect targets.
new_targets = self.collect_full_targets(all_targets)
targets_files = [Path(path) for path in all_targets.keys()]
configuration = self.find_or_create_configuration(directory, new_targets)
# Try setting a glob target.
glob_threshold = self._glob_threshold
all_errors = None
if glob_threshold is not None:
original_targets = configuration.targets
configuration.targets = ["//" + str(directory) + "/..."]
configuration.write()
all_errors = configuration.get_errors()
if any(
len(errors) > glob_threshold
for errors in all_errors.paths_to_errors.values()
):
# Fall back to non-glob codemod.
LOG.info(
"Exceeding error threshold of %d; falling back to listing "
"individual targets.",
glob_threshold,
)
configuration.targets = original_targets
configuration.write()
all_errors = configuration.get_errors()
else:
targets_files = [
directory / path
for path in get_filesystem().list(
str(directory), patterns=[r"**/TARGETS"]
)
]
if not all_errors:
all_errors = configuration.get_errors()
# Remove all type-related target settings.
self.remove_target_typing_fields(targets_files)
if not self._pyre_only:
remove_non_pyre_ignores(directory)
# Suppress errors in individual files where fixme threshold is not exceeded.
error_threshold = self._fixme_threshold
for path, errors in all_errors.paths_to_errors.items():
errors = list(errors)
error_count = len(errors)
if error_threshold and error_count > error_threshold:
LOG.info(
"%d errors found in `%s`. Adding file-level ignore.",
error_count,
path,
)
add_local_mode(path, LocalMode.IGNORE)
else:
self._apply_suppressions(Errors(errors))
# Spin up strict codemod if applicable, otherwise skip to final clean and lint.
if apply_strict:
LOG.info(
"Some targets were running strict type checking. "
"Adding strict setting to configuration."
)
strict_codemod = StrictDefault(
command_arguments=CommandArguments(
comment=self._comment,
max_line_length=self._max_line_length,
truncate=self._truncate,
unsafe=self._unsafe,
force_format_unsuppressed=self._force_format_unsuppressed,
lint=self._lint,
no_commit=True,
should_clean=True,
),
repository=self._repository,
local_configuration=directory,
remove_strict_headers=True,
fixme_threshold=0,
)
strict_codemod.run()
else:
self._get_and_suppress_errors(configuration)