





import os
from argparse import ArgumentParser
from pathlib import Path
from shutil import copyfile
from typing import List

from datasets import config
from datasets.builder import DatasetBuilder
from datasets.commands import BaseDatasetsCLICommand
from datasets.download.download_config import DownloadConfig
from datasets.download.download_manager import DownloadMode
from datasets.load import dataset_module_factory, import_main_class
from datasets.utils.info_utils import VerificationMode







def    UpperCAmelCase				(  _lowerCamelCase   :	Optional[int]      , **_lowerCamelCase   :	Any  ):





						'''simple docstring'''


						return RunBeamCommand(
						    args.dataset      , args.name      , args.cache_dir      , args.beam_pipeline_options      , args.data_dir      , args.all_configs      , args.save_info or args.save_infos      , args.ignore_verifications      , args.force_redownload      , **_UpperCamelCase      , )



class  _a  (					lowercase_				):






							"""simple docstring"""

							@staticmethod
							def   A_	(   a					:  ArgumentParser     )						->str:
													SCREAMING_SNAKE_CASE__  :					Dict =       parser.add_parser("run_beam"				,						help="Run a Beam dataset processing pipeline"     )
													run_beam_parser.add_argument("dataset"				,						type=UpperCAmelCase_				,						help="Name of the dataset to download"     )
													run_beam_parser.add_argument("--name"				,						type=UpperCAmelCase_				,						default=UpperCAmelCase_				,						help="Dataset config name"     )
													run_beam_parser.add_argument(
													    "--cache_dir"				,						type=UpperCAmelCase_				,						default=UpperCAmelCase_				,						help="Cache directory where the datasets are stored"				,						)
													run_beam_parser.add_argument(
													    "--beam_pipeline_options"				,						type=UpperCAmelCase_				,						default=""				,						help="Beam pipeline options, separated by commas. Example:: `--beam_pipeline_options=job_name=my-job,project=my-project`"				,						)
													run_beam_parser.add_argument(
													    "--data_dir"				,						type=UpperCAmelCase_				,						default=UpperCAmelCase_				,						help="Can be used to specify a manual directory to get the files from"				,						)
													run_beam_parser.add_argument("--all_configs"				,						action="store_true"				,						help="Test all dataset configurations"     )
													run_beam_parser.add_argument("--save_info"				,						action="store_true"				,						help="Save the dataset infos file"     )
													run_beam_parser.add_argument(
													    "--ignore_verifications"				,						action="store_true"				,						help="Run the test without checksums and splits checks"     )
													run_beam_parser.add_argument("--force_redownload"				,						action="store_true"				,						help="Force dataset redownload"     )
													# aliases
													run_beam_parser.add_argument("--save_infos"				,						action="store_true"				,						help="alias for save_info"     )
													run_beam_parser.set_defaults(func=UpperCAmelCase_     )

							def __init__(   self					:  Any				,						a					:  str				,						a					:  str				,						a					:  str				,						a					:  str				,						a					:  str				,						a					:  bool				,						a					:  bool				,						a					:  bool				,						a					:  bool				,						**a					:  List[Any]				,						)						->Dict:
													SCREAMING_SNAKE_CASE__  :					Tuple =       dataset
													SCREAMING_SNAKE_CASE__  :					List[Any] =       name
													SCREAMING_SNAKE_CASE__  :					List[str] =       cache_dir
													SCREAMING_SNAKE_CASE__  :					Optional[int] =       beam_pipeline_options
													SCREAMING_SNAKE_CASE__  :					Optional[Any] =       data_dir
													SCREAMING_SNAKE_CASE__  :					Tuple =       all_configs
													SCREAMING_SNAKE_CASE__  :					List[Any] =       save_infos
													SCREAMING_SNAKE_CASE__  :					Optional[int] =       ignore_verifications
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       force_redownload
													SCREAMING_SNAKE_CASE__  :					Tuple =       config_kwargs






							def   A_	(   self					:  List[str]     )						->Dict:
													import apache_beam as beam

													if self._name is not None and self._all_configs:
																			print("Both parameters `name` and `all_configs` can\'t be used at once."     )
																			exit(1     )
													SCREAMING_SNAKE_CASE__  :					Any =       self._dataset, self._name
													SCREAMING_SNAKE_CASE__  :					Optional[int] =       dataset_module_factory(UpperCAmelCase_     )
													SCREAMING_SNAKE_CASE__  :					Any =       import_main_class(dataset_module.module_path     )
													SCREAMING_SNAKE_CASE__  :					List[DatasetBuilder] =       []
													if self._beam_pipeline_options:
																			SCREAMING_SNAKE_CASE__  :					Tuple =       beam.options.pipeline_options.PipelineOptions(
																			    flags=[f"""--{opt.strip()}""" for opt in self._beam_pipeline_options.split(","     ) if opt]     )
													else:
																			SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       None
													if self._all_configs and len(builder_cls.BUILDER_CONFIGS     ) > 0:
																			for builder_config in builder_cls.BUILDER_CONFIGS:
																									builders.append(
																									    builder_cls(
																									        config_name=builder_config.name				,						data_dir=self._data_dir				,						hash=dataset_module.hash				,						beam_options=UpperCAmelCase_				,						cache_dir=self._cache_dir				,						base_path=dataset_module.builder_kwargs.get("base_path"     )				,						)     )
													else:
																			builders.append(
																			    builder_cls(
																			        config_name=UpperCAmelCase_				,						data_dir=self._data_dir				,						beam_options=UpperCAmelCase_				,						cache_dir=self._cache_dir				,						base_path=dataset_module.builder_kwargs.get("base_path"     )				,						**self._config_kwargs				,						)     )

													for builder in builders:
																			builder.download_and_prepare(
																			    download_mode=DownloadMode.REUSE_CACHE_IF_EXISTS
																			    if not self._force_redownload
																			    else DownloadMode.FORCE_REDOWNLOAD				,						download_config=DownloadConfig(cache_dir=config.DOWNLOADED_DATASETS_PATH     )				,						verification_mode=VerificationMode.NO_CHECKS
																			    if self._ignore_verifications
																			    else VerificationMode.ALL_CHECKS				,						try_from_hf_gcs=UpperCAmelCase_				,						)
																			if self._save_infos:
																									builder._save_infos()

													print("Apache beam run successful."     )

													# If save_infos=True, the dataset infos file is created next to the loaded module file.
													# 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:
																			SCREAMING_SNAKE_CASE__  :					List[Any] =       os.path.join(builder_cls.get_imported_module_dir()				,						config.DATASETDICT_INFOS_FILENAME     )

																			SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       Path(UpperCAmelCase_     ).name + '''.py'''

																			SCREAMING_SNAKE_CASE__  :					Optional[int] =       os.path.join(UpperCAmelCase_				,						UpperCAmelCase_     )
																			if os.path.isfile(UpperCAmelCase_     ):
																									SCREAMING_SNAKE_CASE__  :					str =       os.path.dirname(UpperCAmelCase_     )
																			elif os.path.isfile(UpperCAmelCase_     ):
																									SCREAMING_SNAKE_CASE__  :					List[Any] =       path
																			else:  # in case of a remote dataset
																									print(f"""Dataset Infos file saved at {dataset_infos_path}"""     )
																									exit(1     )

																			# Move datasetinfo back to the user
																			SCREAMING_SNAKE_CASE__  :					Optional[Any] =       os.path.join(UpperCAmelCase_				,						config.DATASETDICT_INFOS_FILENAME     )
																			copyfile(UpperCAmelCase_				,						UpperCAmelCase_     )
																			print(f"""Dataset Infos file saved at {user_dataset_infos_path}"""     )




import importlib.metadata
import sys

from transformers.testing_utils import TestCasePlus
from transformers.utils.versions import require_version, require_version_core


UpperCAmelCase__			:   str      			=    importlib.metadata.version('numpy')
UpperCAmelCase__			:   Optional[int]      			=    '.'.join([str(x) for x in sys.version_info[:3]])





class 							lowerCAmelCase_		(		lowercase_     ):







		def 					a_  (    self  :   Tuple					)       ->		Tuple:


						'''simple docstring'''







						require_version_core('''numpy<1000.4.5'''					)
						require_version_core('''numpy<1000.4'''					)
						require_version_core('''numpy<1000'''					)

						# le
						require_version_core('''numpy<=1000.4.5'''					)
						require_version_core(F'''numpy<={numpy_ver}'''					)

						# eq
						require_version_core(F'''numpy=={numpy_ver}'''					)

						# ne
						require_version_core('''numpy!=1000.4.5'''					)

						# ge
						require_version_core('''numpy>=1.0'''					)
						require_version_core('''numpy>=1.0.0'''					)
						require_version_core(F'''numpy>={numpy_ver}'''					)

						# gt
						require_version_core('''numpy>1.0.0'''					)

						# mix
						require_version_core('''numpy>1.0.0,<1000'''					)

						# requirement w/o version
						require_version_core('''numpy'''					)

						# unmet requirements due to version conflict
						for req in ["numpy==1.0.0", "numpy>=1000.0.0", F'''numpy<{numpy_ver}''']:
										try:
														require_version_core(UpperCAmelCase_					)
										except ImportError as e:
														self.assertIn(F'''{req} is required'''       ,		str(UpperCAmelCase_					)					)
														self.assertIn('''but found'''       ,		str(UpperCAmelCase_					)					)

        # unmet requirements due to missing module
						for req in ["numpipypie>1", "numpipypie2"]:
										try:
														require_version_core(UpperCAmelCase_					)
										except importlib.metadata.PackageNotFoundError as e:
														self.assertIn(F'''The \'{req}\' distribution was not found and is required by this application'''       ,		str(UpperCAmelCase_					)					)
														self.assertIn('''Try: pip install transformers -U'''       ,		str(UpperCAmelCase_					)					)

        # bogus requirements formats:
        # 1. whole thing
						for req in ["numpy??1.0.0", "numpy1.0.0"]:
										try:
														require_version_core(UpperCAmelCase_					)
										except ValueError as e:
														self.assertIn('''requirement needs to be in the pip package format'''       ,		str(UpperCAmelCase_					)					)
        # 2. only operators
						for req in ["numpy=1.0.0", "numpy == 1.00", "numpy<>1.0.0", "numpy><1.00", "numpy>>1.0.0"]:
										try:
														require_version_core(UpperCAmelCase_					)
										except ValueError as e:
														self.assertIn('''need one of '''       ,		str(UpperCAmelCase_					)					)







		def 					a_  (    self  :   Tuple					)       ->		List[Any]:


						'''simple docstring'''







						require_version('''python>=3.6.0'''					)

						# not matching requirements
						for req in ["python>9.9.9", "python<3.0.0"]:
										try:
														require_version_core(UpperCAmelCase_					)
										except ImportError as e:
														self.assertIn(F'''{req} is required'''       ,		str(UpperCAmelCase_					)					)
														self.assertIn(F'''but found python=={python_ver}'''       ,		str(UpperCAmelCase_					)					)
