






import os
from pathlib import Path
from unittest.mock import patch

import pytest
import zstandard as zstd

from datasets.download.download_config import DownloadConfig
from datasets.utils.file_utils import (
    OfflineModeIsEnabled,
    cached_path,
    fsspec_get,
    fsspec_head,
    ftp_get,
    ftp_head,
    get_from_cache,
    http_get,
    http_head,
)


a_							=    '''\
    Text data.
    Second line of data.'''

a_							=    '''file'''


@pytest.fixture(scope="session"   )
def        _a  (     UpperCamelCase_					:				Union[str, Any]   ) ->   Tuple:
						"""simple docstring"""







						lowerCAmelCase__       =						tmp_path_factory.mktemp("data"   ) / (FILE_PATH + ".zstd")
						lowerCAmelCase__       =						bytes(UpperCamelCase_    ,   "utf-8"   )
						with zstd.open(UpperCamelCase_    ,   "wb"   ) as f:
												f.write(UpperCamelCase_   )
						return path


@pytest.fixture
def        _a  (     UpperCamelCase_					:				List[str]   ) ->   int:
						"""simple docstring"""







						with open(os.path.join(tmpfs.local_root_dir    ,   UpperCamelCase_   )    ,   "w"   ) as f:
												f.write(UpperCamelCase_   )
						return FILE_PATH


@pytest.mark.parametrize("compression_format"    ,   ["gzip", "xz", "zstd"]   )
def        _a  (     UpperCamelCase_					:				List[str]    ,   UpperCamelCase_					:				Union[str, Any]    ,   UpperCamelCase_					:				Dict    ,   UpperCamelCase_					:				List[Any]    ,   UpperCamelCase_					:				Any    ,   UpperCamelCase_					:				Dict   ) ->   Union[str, Any]:
						"""simple docstring"""







						lowerCAmelCase__       =						{"gzip": gz_file, "xz": xz_file, "zstd": zstd_path}
						lowerCAmelCase__       =						input_paths[compression_format]
						lowerCAmelCase__       =						tmp_path / "cache"
						lowerCAmelCase__       =						DownloadConfig(cache_dir=UpperCamelCase_    ,   extract_compressed_file=UpperCamelCase_   )
						lowerCAmelCase__       =						cached_path(UpperCamelCase_    ,   download_config=UpperCamelCase_   )
						with open(UpperCamelCase_   ) as f:
												lowerCAmelCase__       =						f.read()
						with open(UpperCamelCase_   ) as f:
												lowerCAmelCase__       =						f.read()
						assert extracted_file_content == expected_file_content


@pytest.mark.parametrize("default_extracted"    ,   [True, False]   )
@pytest.mark.parametrize("default_cache_dir"    ,   [True, False]   )
def        _a  (     UpperCamelCase_					:				List[str]    ,   UpperCamelCase_					:				Dict    ,   UpperCamelCase_					:				Dict    ,   UpperCamelCase_					:				Optional[Any]    ,   UpperCamelCase_					:				str   ) ->   List[str]:
						"""simple docstring"""







						lowerCAmelCase__       =						"custom_cache"
						lowerCAmelCase__       =						"custom_extracted_dir"
						lowerCAmelCase__       =						tmp_path / "custom_extracted_path"
						if default_extracted:
												lowerCAmelCase__       =						("downloads" if default_cache_dir else custom_cache_dir, "extracted")
						else:
												monkeypatch.setattr("datasets.config.EXTRACTED_DATASETS_DIR"    ,   UpperCamelCase_   )
												monkeypatch.setattr("datasets.config.EXTRACTED_DATASETS_PATH"    ,   str(UpperCamelCase_   )   )
												lowerCAmelCase__       =						custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

						lowerCAmelCase__       =						xz_file
						lowerCAmelCase__       =						(
						    DownloadConfig(extract_compressed_file=UpperCamelCase_   )
						    if default_cache_dir
						    else DownloadConfig(cache_dir=tmp_path / custom_cache_dir    ,   extract_compressed_file=UpperCamelCase_   )
						)
						lowerCAmelCase__       =						cached_path(UpperCamelCase_    ,   download_config=UpperCamelCase_   )
						assert Path(UpperCamelCase_   ).parent.parts[-2:] == expected


def        _a  (     UpperCamelCase_					:				int   ) ->   Any:
						"""simple docstring"""







						lowerCAmelCase__       =						str(Path(UpperCamelCase_   ).resolve()   )
						assert cached_path(UpperCamelCase_   ) == text_file
						# relative path
						lowerCAmelCase__       =						str(Path(UpperCamelCase_   ).resolve().relative_to(Path(os.getcwd()   )   )   )
						assert cached_path(UpperCamelCase_   ) == text_file


def        _a  (     UpperCamelCase_					:				Optional[Any]   ) ->   str:
						"""simple docstring"""







						lowerCAmelCase__       =						str(tmp_path.resolve() / "__missing_file__.txt"   )
						with pytest.raises(UpperCamelCase_   ):
												cached_path(UpperCamelCase_   )
						# relative path
						lowerCAmelCase__       =						"./__missing_file__.txt"
						with pytest.raises(UpperCamelCase_   ):
												cached_path(UpperCamelCase_   )


def        _a  (     UpperCamelCase_					:				List[str]   ) ->   List[Any]:
						"""simple docstring"""







						lowerCAmelCase__       =						get_from_cache(F"tmp://{tmpfs_file}"   )
						with open(UpperCamelCase_   ) as f:
												lowerCAmelCase__       =						f.read()
						assert output_file_content == FILE_CONTENT


@patch("datasets.config.HF_DATASETS_OFFLINE"    ,   UpperCamelCase_   )
def        _a  (     ) ->   int:
						"""simple docstring"""







						with pytest.raises(UpperCamelCase_   ):
												cached_path("https://huggingface.co"   )


@patch("datasets.config.HF_DATASETS_OFFLINE"    ,   UpperCamelCase_   )
def        _a  (     UpperCamelCase_					:				List[Any]   ) ->   Any:
						"""simple docstring"""







						lowerCAmelCase__       =						tmp_path_factory.mktemp("data"   ) / "file.html"
						with pytest.raises(UpperCamelCase_   ):
												http_get("https://huggingface.co"    ,   temp_file=UpperCamelCase_   )
						with pytest.raises(UpperCamelCase_   ):
												http_head("https://huggingface.co"   )


@patch("datasets.config.HF_DATASETS_OFFLINE"    ,   UpperCamelCase_   )
def        _a  (     UpperCamelCase_					:				Tuple   ) ->   str:
						"""simple docstring"""







						lowerCAmelCase__       =						tmp_path_factory.mktemp("data"   ) / "file.html"
						with pytest.raises(UpperCamelCase_   ):
												ftp_get("ftp://huggingface.co"    ,   temp_file=UpperCamelCase_   )
						with pytest.raises(UpperCamelCase_   ):
												ftp_head("ftp://huggingface.co"   )





@patch("datasets.config.HF_DATASETS_OFFLINE"    ,   UpperCamelCase_   )
def        _a  (     UpperCamelCase_					:				str   ) ->   List[Any]:
						"""simple docstring"""







						lowerCAmelCase__       =						tmp_path_factory.mktemp("data"   ) / "file.html"
						with pytest.raises(UpperCamelCase_   ):
												fsspec_get("s3://huggingface.co"    ,   temp_file=UpperCamelCase_   )
						with pytest.raises(UpperCamelCase_   ):
												fsspec_head("s3://huggingface.co"   )














import json
import logging
import os
import sys
from time import time
from unittest.mock import patch

from transformers.testing_utils import TestCasePlus, require_torch_tpu


logging.basicConfig(level=logging.DEBUG)

a_							=    logging.getLogger()


def        _a  (     UpperCamelCase_					:				Union[str, Any]   ) ->   Dict:
						"""simple docstring"""







						lowerCAmelCase__       =						{}
						lowerCAmelCase__       =						os.path.join(UpperCamelCase_    ,   "all_results.json"   )
						if os.path.exists(UpperCamelCase_   ):
												with open(UpperCamelCase_    ,   "r"   ) as f:
																		lowerCAmelCase__       =						json.load(UpperCamelCase_   )
						else:
												raise ValueError(F"can't find {path}"   )
						return results


a_							=    logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)





@require_torch_tpu
class      lowercase__     (			_UpperCAmelCase	):

	def 		UpperCAmelCase  (							self       )->					Optional[Any]:







							'''simple docstring'''





							import xla_spawn

							lowerCAmelCase__       =						self.get_auto_remove_tmp_dir()
							lowerCAmelCase__       =						F"\n            ./examples/pytorch/text-classification/run_glue.py\n            --num_cores=8\n            ./examples/pytorch/text-classification/run_glue.py\n            --model_name_or_path distilbert-base-uncased\n            --output_dir {tmp_dir}\n            --overwrite_output_dir\n            --train_file ./tests/fixtures/tests_samples/MRPC/train.csv\n            --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv\n            --do_train\n            --do_eval\n            --debug tpu_metrics_debug\n            --per_device_train_batch_size=2\n            --per_device_eval_batch_size=1\n            --learning_rate=1e-4\n            --max_steps=10\n            --warmup_steps=2\n            --seed=42\n            --max_seq_length=128\n            ".split()

							with patch.object(__UpperCAmelCase		,    "argv"		,    __UpperCAmelCase       ):
													lowerCAmelCase__       =						time()
													xla_spawn.main()
													lowerCAmelCase__       =						time()

													lowerCAmelCase__       =						get_results(__UpperCAmelCase       )
													self.assertGreaterEqual(result["eval_accuracy"]		,    0.75       )

													# Assert that the script takes less than 500 seconds to make sure it doesn't hang.
													self.assertLess(end - start		,    500       )


	def 		UpperCAmelCase  (							self       )->					Dict:







							'''simple docstring'''





							import xla_spawn

							lowerCAmelCase__       =						"\n            ./tests/test_trainer_tpu.py\n            --num_cores=8\n            ./tests/test_trainer_tpu.py\n            ".split()
							with patch.object(__UpperCAmelCase		,    "argv"		,    __UpperCAmelCase       ):
													xla_spawn.main()






