





'''simple docstring'''
import os
from argparse import ArgumentParser
from pathlib import Path
from shutil import copyfile, rmtree
from typing import Generator

import datasets.config
from datasets.builder import DatasetBuilder
from datasets.commands import BaseDatasetsCLICommand
from datasets.download.download_manager import DownloadMode
from datasets.load import dataset_module_factory, import_main_class
from datasets.utils.filelock import logger as fl_logger
from datasets.utils.info_utils import VerificationMode
from datasets.utils.logging import ERROR, get_logger


_UpperCamelCase											=			get_logger(__name__)


def   lowercase_	(   lowerCAmelCase__	:  Optional[int] ):

			"""simple docstring"""





			return TestCommand(
			    args.dataset			,							args.name			,							args.cache_dir			,							args.data_dir			,							args.all_configs			,							args.save_info or args.save_infos			,							args.ignore_verifications			,							args.force_redownload			,							args.clear_cache			,							)






class _A  (    lowerCAmelCase_    ):
		_SCREAMING_SNAKE_CASE					:    Optional[Any]						      =			False  # to tell pytest it's not a test class





		@staticmethod
		def 							__A       (     __UpperCAmelCase				)       ->     Union[str, Any]:

					'''simple docstring'''
					__UpperCAmelCase   :      Any										=     parser.add_parser("""test""" ,       help="""Test dataset implementation."""				)
					test_parser.add_argument("""--name""" ,       type=lowerCamelCase_ ,       default=lowerCamelCase_ ,       help="""Dataset processing name"""				)
					test_parser.add_argument(
					    """--cache_dir""" ,       type=lowerCamelCase_ ,       default=lowerCamelCase_ ,       help="""Cache directory where the datasets are stored.""" ,       )
					test_parser.add_argument(
					    """--data_dir""" ,       type=lowerCamelCase_ ,       default=lowerCamelCase_ ,       help="""Can be used to specify a manual directory to get the files from.""" ,       )
					test_parser.add_argument("""--all_configs""" ,       action="""store_true""" ,       help="""Test all dataset configurations"""				)
					test_parser.add_argument(
					    """--save_info""" ,       action="""store_true""" ,       help="""Save the dataset infos in the dataset card (README.md)"""				)
					test_parser.add_argument(
					    """--ignore_verifications""" ,       action="""store_true""" ,       help="""Run the test without checksums and splits checks.""" ,       )
					test_parser.add_argument("""--force_redownload""" ,       action="""store_true""" ,       help="""Force dataset redownload"""				)
					test_parser.add_argument(
					    """--clear_cache""" ,       action="""store_true""" ,       help="""Remove downloaded files and cached datasets after each config test""" ,       )
					# aliases
					test_parser.add_argument("""--save_infos""" ,       action="""store_true""" ,       help="""alias to save_info"""				)
					test_parser.add_argument("""dataset""" ,       type=lowerCamelCase_ ,       help="""Name of the dataset to download"""				)
					test_parser.set_defaults(func=_test_command_factory				)





		def __init__(     self ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       )       ->     List[str]:

					'''simple docstring'''
					__UpperCAmelCase   :      Optional[int]										=     dataset
					__UpperCAmelCase   :      List[Any]										=     name
					__UpperCAmelCase   :      Tuple										=     cache_dir
					__UpperCAmelCase   :      Any										=     data_dir
					__UpperCAmelCase   :      Union[str, Any]										=     all_configs
					__UpperCAmelCase   :      int										=     save_infos
					__UpperCAmelCase   :      Tuple										=     ignore_verifications
					__UpperCAmelCase   :      List[Any]										=     force_redownload
					__UpperCAmelCase   :      Optional[int]										=     clear_cache
					if clear_cache and not cache_dir:
								print(
								    """When --clear_cache is used, specifying a cache directory is mandatory.\n"""
								    """The 'download' folder of the cache directory and the dataset builder cache will be deleted after each configuration test.\n"""
								    """Please provide a --cache_dir that will be used to test the dataset script."""				)
								exit(1				)
					if save_infos:
								__UpperCAmelCase   :      Optional[Any]										=     True





		def 							__A       (     self				)       ->     Dict:

					'''simple docstring'''
					fl_logger().setLevel(lowerCamelCase_				)
					if self._name is not None and self._all_configs:
								print("""Both parameters `config` and `all_configs` can't be used at once."""				)
								exit(1				)
					__UpperCAmelCase				,  __UpperCAmelCase   :      Union[str, Any]										=     self._dataset, self._name
					__UpperCAmelCase   :      str										=     dataset_module_factory(lowerCamelCase_				)
					__UpperCAmelCase   :      int										=     import_main_class(module.module_path				)
					__UpperCAmelCase   :      Any										=     len(builder_cls.BUILDER_CONFIGS				) if self._all_configs and builder_cls.BUILDER_CONFIGS else 1

					def get_builders() -> Generator[DatasetBuilder, None, None]:
								if self._all_configs and builder_cls.BUILDER_CONFIGS:
											for i, config in enumerate(builder_cls.BUILDER_CONFIGS				):
														if "config_name" in module.builder_kwargs:
																	yield builder_cls(
																	    cache_dir=self._cache_dir ,       data_dir=self._data_dir ,       **module.builder_kwargs ,       )
														else:
																	yield builder_cls(
																	    config_name=config.name ,       cache_dir=self._cache_dir ,       data_dir=self._data_dir ,       **module.builder_kwargs ,       )
								else:
											if "config_name" in module.builder_kwargs:
														yield builder_cls(cache_dir=self._cache_dir ,       data_dir=self._data_dir ,       **module.builder_kwargs				)
											else:
														yield builder_cls(
														    config_name=lowerCamelCase_ ,       cache_dir=self._cache_dir ,       data_dir=self._data_dir ,       **module.builder_kwargs ,       )

					for j, builder in enumerate(get_builders()				):
								print(f'Testing builder \'{builder.config.name}\' ({j + 1}/{n_builders})'				)
								__UpperCAmelCase   :      int										=     os.path.exists(
								    os.path.join(builder.get_imported_module_dir() ,       datasets.config.DATASETDICT_INFOS_FILENAME				)				)  # record checksums only if we need to update a (deprecated) dataset_infos.json
								builder.download_and_prepare(
								    download_mode=DownloadMode.REUSE_CACHE_IF_EXISTS
								    if not self._force_redownload
								    else DownloadMode.FORCE_REDOWNLOAD ,       verification_mode=VerificationMode.NO_CHECKS
								    if self._ignore_verifications
								    else VerificationMode.ALL_CHECKS ,       try_from_hf_gcs=lowerCamelCase_ ,       )
								builder.as_dataset()
								if self._save_infos:
											builder._save_infos()

								# If save_infos=True, the dataset card (README.md) is created next to the loaded module file.
								# The dataset_infos are saved in the YAML part of the README.md

								# Let's move it to the original directory of the dataset script, to allow the user to
								# upload them on S3 at the same time afterwards.
								if self._save_infos:
											__UpperCAmelCase   :      Optional[int]										=     os.path.join(builder_cls.get_imported_module_dir() ,       """README.md"""				)
											__UpperCAmelCase   :      Tuple										=     Path(lowerCamelCase_				).name + """.py"""
											__UpperCAmelCase   :      Optional[int]										=     os.path.join(lowerCamelCase_ ,       lowerCamelCase_				)
											if os.path.isfile(lowerCamelCase_				):
														__UpperCAmelCase   :      Tuple										=     os.path.dirname(lowerCamelCase_				)
											elif os.path.isfile(lowerCamelCase_				):
														__UpperCAmelCase   :      List[str]										=     path
											elif os.path.isdir(lowerCamelCase_				):  # for local directories containing only data files
														__UpperCAmelCase   :      List[Any]										=     path
											else:  # in case of a remote dataset
														__UpperCAmelCase   :      Union[str, Any]										=     None
														print(f'Dataset card saved at {dataset_readme_path}'				)

											# Move dataset_info back to the user
											if dataset_dir is not None:
														__UpperCAmelCase   :      List[str]										=     os.path.join(lowerCamelCase_ ,       """README.md"""				)
														copyfile(lowerCamelCase_ ,       lowerCamelCase_				)
														print(f'Dataset card saved at {user_dataset_readme_path}'				)

            # If clear_cache=True, the download folder and the dataset builder cache directory are deleted
								if self._clear_cache:
											if os.path.isdir(builder._cache_dir				):
														logger.warning(f'Clearing cache at {builder._cache_dir}'				)
														rmtree(builder._cache_dir				)
											__UpperCAmelCase   :      Union[str, Any]										=     os.path.join(self._cache_dir ,       datasets.config.DOWNLOADED_DATASETS_DIR				)
											if os.path.isdir(lowerCamelCase_				):
														logger.warning(f'Clearing cache at {download_dir}'				)
														rmtree(lowerCamelCase_				)

					print("""Test successful."""				)









'''simple docstring'''
import argparse
import glob
import inspect
import os
import re
import unittest

from get_test_info import get_test_classes

from tests.test_pipeline_mixin import pipeline_test_mapping


_UpperCamelCase											=			{}
for task, _ in pipeline_test_mapping.items():
					_UpperCamelCase											=			{'''pt''': None, '''tf''': None}


# DO **NOT** add item to this set (unless the reason is approved)
_UpperCamelCase											=			{
    '''tests/models/esm/test_modeling_esmfold.py''',  # The pipeline test mapping is added to `test_modeling_esm.py`
}


def   lowercase_	(   lowerCAmelCase__	:  Optional[Any] ):

			"""simple docstring"""





			if "ModelTesterMixin" in [x.__name__ for x in test_class.__bases__]:
						return "pt"
			elif "TFModelTesterMixin" in [x.__name__ for x in test_class.__bases__]:
						return "tf"
			elif "FlaxModelTesterMixin" in [x.__name__ for x in test_class.__bases__]:
						return "flax"
			else:
						return None


def   lowercase_	(   lowerCAmelCase__	:  List[Any]			,							lowerCAmelCase__	:  Any ):

			"""simple docstring"""





			if PIPELINE_TEST_MAPPING[task].get(lowerCAmelCase__			,							lowerCAmelCase__ ) is not None:
						return PIPELINE_TEST_MAPPING[task][framework]

			__UpperCAmelCase   :      Optional[int]										=     pipeline_test_mapping[task]["""test"""]
			__UpperCAmelCase   :      Optional[Any]										=     None

			if framework == "pt":
						__UpperCAmelCase   :      List[str]										=     getattr(lowerCAmelCase__			,							"""model_mapping"""			,							lowerCAmelCase__ )
			elif framework == "tf":
						__UpperCAmelCase   :      List[Any]										=     getattr(lowerCAmelCase__			,							"""tf_model_mapping"""			,							lowerCAmelCase__ )

			if mapping is not None:
						__UpperCAmelCase   :      Union[str, Any]										=     dict(mapping.items() )

			# cache the results
			__UpperCAmelCase   :      Union[str, Any]										=     mapping
			return mapping


def   lowercase_	(   lowerCAmelCase__	:  Tuple			,							lowerCAmelCase__	:  Tuple ):

			"""simple docstring"""





			__UpperCAmelCase   :      int										=     get_framework(lowerCAmelCase__ )
			if framework is None:
						return None
			__UpperCAmelCase   :      Tuple										=     get_mapping_for_task(lowerCAmelCase__			,							lowerCAmelCase__ )
			if mapping is None:
						return None

			__UpperCAmelCase   :      Union[str, Any]										=     list({model_class.config_class for model_class in test_class.all_model_classes} )
			if len(lowerCAmelCase__ ) != 1:
						raise ValueError("""There should be exactly one configuration class from `test_class.all_model_classes`.""" )

			# This could be a list/tuple of model classes, but it's rare.
			__UpperCAmelCase   :      Union[str, Any]										=     mapping.get(config_classes[0]			,							lowerCAmelCase__ )
			if isinstance(lowerCAmelCase__			,							(tuple, list) ):
						__UpperCAmelCase   :      int										=     sorted(lowerCAmelCase__			,							key=lambda lowerCAmelCase__ : x.__name__ )

			return model_class


def   lowercase_	(   lowerCAmelCase__	:  int ):

			"""simple docstring"""





			__UpperCAmelCase   :      Any										=     [(task, get_model_for_pipeline_test(lowerCAmelCase__			,							lowerCAmelCase__ )) for task in pipeline_test_mapping]
			__UpperCAmelCase   :      int										=     sorted([(task, model) for task, model in mapping if model is not None]			,							key=lambda lowerCAmelCase__ : x[0] )

			return dict(lowerCAmelCase__ )


def   lowercase_	(   lowerCAmelCase__	:  Tuple ):

			"""simple docstring"""





			__UpperCAmelCase   :      List[str]										=     get_framework(lowerCAmelCase__ )
			if framework == "pt":
						__UpperCAmelCase   :      str										=     """torch"""
			__UpperCAmelCase   :      str										=     """{}"""

			__UpperCAmelCase   :      Optional[int]										=     get_pipeline_model_mapping(lowerCAmelCase__ )
			if len(lowerCAmelCase__ ) == 0:
						return ""

			__UpperCAmelCase   :      Optional[int]										=     []
			for task, model_classes in mapping.items():
						if isinstance(lowerCAmelCase__			,							(tuple, list) ):
									# A list/tuple of model classes
									__UpperCAmelCase   :      Dict										=     """(""" + """, """.join([x.__name__ for x in model_classes] ) + """)"""
						else:
									# A single model class
									__UpperCAmelCase   :      List[str]										=     model_classes.__name__
						texts.append(f'"{task}": {value}' )
			__UpperCAmelCase   :      Dict										=     """{""" + """, """.join(lowerCAmelCase__ ) + """}"""
			__UpperCAmelCase   :      Dict										=     f'pipeline_model_mapping = {text} if is_{framework}_available() else {default_value}'

			return text


def   lowercase_	(   lowerCAmelCase__	:  Tuple ):

			"""simple docstring"""





			__UpperCAmelCase   :      Dict										=     {"""ModelTesterMixin""", """TFModelTesterMixin""", """FlaxModelTesterMixin"""}
			if not issubclass(lowerCAmelCase__			,							unittest.TestCase ):
						return False
			return len(base_class_names.intersection([x.__name__ for x in test_class.__bases__] ) ) > 0


def   lowercase_	(   lowerCAmelCase__	:  Optional[Any] ):

			"""simple docstring"""





			__UpperCAmelCase   :      Optional[int]										=     [x for x in get_test_classes(lowerCAmelCase__ ) if is_valid_test_class(lowerCAmelCase__ )]

			__UpperCAmelCase   :      str										=     None
			for test_class in test_classes:
						# If a test class has defined `pipeline_model_mapping`, let's take it
						if getattr(lowerCAmelCase__			,							"""pipeline_model_mapping"""			,							lowerCAmelCase__ ) is not None:
									__UpperCAmelCase   :      Optional[int]										=     test_class
									break
    # Take the test class with the shortest name (just a heuristic)
			if target_test_class is None and len(lowerCAmelCase__ ) > 0:
						__UpperCAmelCase   :      List[str]										=     sorted(lowerCAmelCase__			,							key=lambda lowerCAmelCase__ : (len(x.__name__ ), x.__name__) )[0]

			return target_test_class


def   lowercase_	(   lowerCAmelCase__	:  Dict			,							lowerCAmelCase__	:  Optional[int]			,							lowerCAmelCase__	:  List[Any] ):

			"""simple docstring"""





			__UpperCAmelCase   :      Tuple										=     start_idx
			for idx, line in enumerate(lines[start_idx:] ):
						__UpperCAmelCase   :      Optional[int]										=     len(lowerCAmelCase__ ) - len(line.lstrip() )
						if idx == 0 or indent > indent_level or (indent == indent_level and line.strip() == ")"):
									__UpperCAmelCase   :      List[str]										=     start_idx + idx
						elif idx > 0 and indent <= indent_level:
									# Outside the definition block of `pipeline_model_mapping`
									break

			return end_idx


def   lowercase_	(   lowerCAmelCase__	:  str			,							lowerCAmelCase__	:  Optional[Any]=False ):

			"""simple docstring"""





			if getattr(lowerCAmelCase__			,							"""pipeline_model_mapping"""			,							lowerCAmelCase__ ) is not None:
						if not overwrite:
									return "", -1

			__UpperCAmelCase   :      Dict										=     get_pipeline_model_mapping_string(lowerCAmelCase__ )
			if len(lowerCAmelCase__ ) == 0:
						return "", -1
			__UpperCAmelCase   :      str										=     line_to_add + """\n"""

			# The code defined the class `test_class`
			__UpperCAmelCase				,  __UpperCAmelCase   :      Optional[Any]										=     inspect.getsourcelines(lowerCAmelCase__ )
			# `inspect` gives the code for an object, including decorator(s) if any.
			# We (only) need the exact line of the class definition.
			for idx, line in enumerate(lowerCAmelCase__ ):
						if line.lstrip().startswith("""class """ ):
									__UpperCAmelCase   :      List[Any]										=     class_lines[idx:]
									class_start_line_no += idx
									break
			__UpperCAmelCase   :      List[str]										=     class_start_line_no + len(lowerCAmelCase__ ) - 1

			# The index in `class_lines` that starts the definition of `all_model_classes`, `all_generative_model_classes` or
			# `pipeline_model_mapping`. This assumes they are defined in such order, and we take the start index of the last
			# block that appears in a `test_class`.
			__UpperCAmelCase   :      str										=     None
			# The indent level of the line at `class_lines[start_idx]` (if defined)
			__UpperCAmelCase   :      List[Any]										=     0
			# To record if `pipeline_model_mapping` is found in `test_class`.
			__UpperCAmelCase   :      Any										=     None
			for idx, line in enumerate(lowerCAmelCase__ ):
						if line.strip().startswith("""all_model_classes = """ ):
									__UpperCAmelCase   :      Optional[Any]										=     len(lowerCAmelCase__ ) - len(line.lstrip() )
									__UpperCAmelCase   :      List[str]										=     idx
						elif line.strip().startswith("""all_generative_model_classes = """ ):
									__UpperCAmelCase   :      Union[str, Any]										=     len(lowerCAmelCase__ ) - len(line.lstrip() )
									__UpperCAmelCase   :      Optional[int]										=     idx
						elif line.strip().startswith("""pipeline_model_mapping = """ ):
									__UpperCAmelCase   :      List[str]										=     len(lowerCAmelCase__ ) - len(line.lstrip() )
									__UpperCAmelCase   :      Optional[int]										=     idx
									__UpperCAmelCase   :      Dict										=     line
									break

			if start_idx is None:
						return "", -1
			# Find the ending index (inclusive) of the above found block.
			__UpperCAmelCase   :      Optional[int]										=     find_block_ending(lowerCAmelCase__			,							lowerCAmelCase__			,							lowerCAmelCase__ )

			# Extract `is_xxx_available()` from existing blocks: some models require specific libraries like `timm` and use
			# `is_timm_available()` instead of `is_torch_available()`.
			# Keep leading and trailing whitespaces
			__UpperCAmelCase   :      str										=     re.compile(r"""\s(is_\S+?_available\(\))\s""" )
			for line in class_lines[start_idx : end_idx + 1]:
						__UpperCAmelCase   :      List[Any]										=     r.search(lowerCAmelCase__ )
						if backend_condition is not None:
									# replace the leading and trailing whitespaces to the space character " ".
									__UpperCAmelCase   :      int										=     """ """ + backend_condition[0][1:-1] + """ """
									__UpperCAmelCase   :      Tuple										=     r.sub(lowerCAmelCase__			,							lowerCAmelCase__ )
									break

			if def_line is None:
						# `pipeline_model_mapping` is not defined. The target index is set to the ending index (inclusive) of
						# `all_model_classes` or `all_generative_model_classes`.
						__UpperCAmelCase   :      int										=     end_idx
			else:
						# `pipeline_model_mapping` is defined. The target index is set to be one **BEFORE** its start index.
						__UpperCAmelCase   :      Optional[int]										=     start_idx - 1
						# mark the lines of the currently existing `pipeline_model_mapping` to be removed.
						for idx in range(lowerCAmelCase__			,							end_idx + 1 ):
									# These lines are going to be removed before writing to the test file.
									__UpperCAmelCase   :      int										=     None  # noqa

    # Make sure the test class is a subclass of `PipelineTesterMixin`.
			__UpperCAmelCase   :      Any										=     [x.__name__ for x in test_class.__bases__]
			if "PipelineTesterMixin" not in parent_classes:
						# Put `PipelineTesterMixin` just before `unittest.TestCase`
						__UpperCAmelCase   :      Optional[Any]										=     [x for x in parent_classes if x != """TestCase"""] + ["""PipelineTesterMixin"""]
						if "TestCase" in parent_classes:
									# Here we **assume** the original string is always with `unittest.TestCase`.
									_parent_classes.append("""unittest.TestCase""" )
						__UpperCAmelCase   :      List[Any]										=     """, """.join(_parent_classes )
						for idx, line in enumerate(lowerCAmelCase__ ):
									# Find the ending of the declaration of `test_class`
									if line.strip().endswith("""):""" ):
												# mark the lines of the declaration of `test_class` to be removed
												for _idx in range(idx + 1 ):
															__UpperCAmelCase   :      int										=     None  # noqa
												break
        # Add the new, one-line, class declaration for `test_class`
						__UpperCAmelCase   :      List[Any]										=     f'class {test_class.__name__}({parent_classes}):\n'

			# Add indentation
			__UpperCAmelCase   :      Optional[int]										=     """ """ * indent_level + line_to_add
			# Insert `pipeline_model_mapping` to `class_lines`.
			# (The line at `target_idx` should be kept by definition!)
			__UpperCAmelCase   :      Union[str, Any]										=     class_lines[: target_idx + 1] + [line_to_add] + class_lines[target_idx + 1 :]
			# Remove the lines that are marked to be removed
			__UpperCAmelCase   :      Union[str, Any]										=     [x for x in class_lines if x is not None]

			# Move from test class to module (in order to write to the test file)
			__UpperCAmelCase   :      Optional[Any]										=     inspect.getsourcelines(inspect.getmodule(lowerCAmelCase__ ) )[0]
			# Be careful with the 1-off between line numbers and array indices
			__UpperCAmelCase   :      List[str]										=     module_lines[: class_start_line_no - 1] + class_lines + module_lines[class_end_line_no:]
			__UpperCAmelCase   :      Optional[int]										=     """""".join(lowerCAmelCase__ )

			__UpperCAmelCase   :      str										=     inspect.getsourcefile(lowerCAmelCase__ )
			with open(lowerCAmelCase__			,							"""w"""			,							encoding="""UTF-8"""			,							newline="""\n""" ) as fp:
						fp.write(lowerCAmelCase__ )

			return line_to_add


def   lowercase_	(   lowerCAmelCase__	:  Union[str, Any]			,							lowerCAmelCase__	:  Optional[int]=False ):

			"""simple docstring"""





			__UpperCAmelCase   :      int										=     find_test_class(lowerCAmelCase__ )
			if test_class:
						add_pipeline_model_mapping(lowerCAmelCase__			,							overwrite=lowerCAmelCase__ )


if __name__ == "__main__":
					_UpperCamelCase											=			argparse.ArgumentParser()
					parser.add_argument(
					    '''--test_file''', type=str, help='''A path to the test file, starting with the repository\'s `tests` directory.'''
					)
					parser.add_argument(
					    '''--all''',
					    action='''store_true''',
					    help='''If to check and modify all test files.''',
					)
					parser.add_argument(
					    '''--overwrite''',
					    action='''store_true''',
					    help='''If to overwrite a test class if it has already defined `pipeline_model_mapping`.''',
					)
					_UpperCamelCase											=			parser.parse_args()

					if not args.all and not args.test_file:
										raise ValueError('''Please specify either `test_file` or pass `--all` to check/modify all test files.''')
					elif args.all and args.test_file:
										raise ValueError('''Only one of `--test_file` and `--all` could be specified.''')

					_UpperCamelCase											=			[]
					if args.test_file:
										_UpperCamelCase											=			[args.test_file]
					else:
										_UpperCamelCase											=			os.path.join('''tests''', '''models''', '''**''', '''test_modeling_*.py''')
										for test_file in glob.glob(pattern):
															# `Flax` is not concerned at this moment
															if not test_file.startswith('''test_modeling_flax_'''):
																				test_files.append(test_file)

					for test_file in test_files:
										if test_file in TEST_FILE_TO_IGNORE:
															print(F'[SKIPPED] {test_file} is skipped as it is in `TEST_FILE_TO_IGNORE` in the file {__file__}.')
															continue
										add_pipeline_model_mapping_to_test_file(test_file, overwrite=args.overwrite)




