




"""simple docstring"""
import importlib
import shutil
import threading
import warnings
from typing import List

import fsspec
import fsspec.asyn

from . import compression
from .hffilesystem import HfFileSystem


a_				:    List[str]										=     importlib.util.find_spec('''s3fs''') is not None

if _has_safs:
					from .safilesystem import SaFileSystem  # noqa: F401

a_				:    List[compression.BaseCompressedFileFileSystem]										=     [
    compression.BzaFileSystem,
    compression.GzipFileSystem,
    compression.LzaFileSystem,
    compression.XzFileSystem,
    compression.ZstdFileSystem,
]

# Register custom filesystems
for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]:
					if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class:
										warnings.warn(F"""A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.""")
					fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True)





def 						UpperCAmelCase				(			A__:    str     )  ->     str:
			if "://" in dataset_path:
						__lowerCamelCase					:	List[Any]	       =				dataset_path.split('://'     )[1]
			return dataset_path





def 						UpperCAmelCase				(			A__:    fsspec.AbstractFileSystem     )  ->     bool:
			if fs is not None and fs.protocol != "file":
						return True
			else:
						return False





def 						UpperCAmelCase				(			A__:    fsspec.AbstractFileSystem							,				A__:    str							,				A__:    str     )  ->     Any:
			__lowerCamelCase					:	Dict	       =				not is_remote_filesystem(A__     )
			if is_local:
						# LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory
						shutil.move(fs._strip_protocol(A__     )							,				fs._strip_protocol(A__     )     )
			else:
						fs.mv(A__							,				A__							,				recursive=A__     )






def 						UpperCAmelCase				(			)  ->     None:
			if hasattr(fsspec.asyn							,				'reset_lock'     ):
						# for future fsspec>2022.05.0
						fsspec.asyn.reset_lock()
			else:
						__lowerCamelCase					:	Tuple	       =				None
						__lowerCamelCase					:	List[Any]	       =				None
						__lowerCamelCase					:	Optional[int]	       =				threading.Lock()









"""simple docstring"""
import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


a_				:    Tuple										=     '''hf-internal-testing/tiny-random-bert'''
a_				:    List[Any]										=     os.path.join(TRANSFORMERS_CACHE, '''models--hf-internal-testing--tiny-random-bert''')
a_				:    Dict										=     '''9b8c223d42b2188cb49d29af482996f9d0f3e5a6'''

class __lowercase(      unittest.TestCase				):




					'''simple docstring'''


					def     snake_case_				(    self     ):
								__lowerCamelCase					:	List[Any]	       =				cached_file(__a						,	__a     )
								# Should have downloaded the file in here
								self.assertTrue(os.path.isdir(__a     )     )
								# Cache should contain at least those three subfolders:
								for subfolder in ["blobs", "refs", "snapshots"]:
											self.assertTrue(os.path.isdir(os.path.join(__a						,	__a     )     )     )
								with open(os.path.join(__a						,	'refs'						,	'main'     )     ) as f:
											__lowerCamelCase					:	Optional[int]	       =				f.read()
								self.assertEqual(__a						,	os.path.join(__a						,	'snapshots'						,	__a						,	__a     )     )
								self.assertTrue(os.path.isfile(__a     )     )

								# File is cached at the same place the second time.
								__lowerCamelCase					:	Optional[Any]	       =				cached_file(__a						,	__a     )
								self.assertEqual(__a						,	__a     )

								# Using a specific revision to test the full commit hash.
								__lowerCamelCase					:	Optional[int]	       =				cached_file(__a						,	__a						,	revision='9b8c223'     )
								self.assertEqual(__a						,	os.path.join(__a						,	'snapshots'						,	__a						,	__a     )     )
					def     snake_case_				(    self     ):
								with self.assertRaisesRegex(__a						,	'is not a valid model identifier'     ):
											__lowerCamelCase					:	List[str]	       =				cached_file('tiny-random-bert'						,	__a     )

								with self.assertRaisesRegex(__a						,	'is not a valid git identifier'     ):
											__lowerCamelCase					:	int	       =				cached_file(__a						,	__a						,	revision='aaaa'     )

								with self.assertRaisesRegex(__a						,	'does not appear to have a file named'     ):
											__lowerCamelCase					:	List[Any]	       =				cached_file(__a						,	'conf'     )
					def     snake_case_				(    self     ):
								with self.assertRaisesRegex(__a						,	'does not appear to have a file named'     ):
											__lowerCamelCase					:	Dict	       =				cached_file(__a						,	'conf'     )

								with open(os.path.join(__a						,	'refs'						,	'main'     )     ) as f:
											__lowerCamelCase					:	Any	       =				f.read()
								self.assertTrue(os.path.isfile(os.path.join(__a						,	'.no_exist'						,	__a						,	'conf'     )     )     )

								__lowerCamelCase					:	str	       =				cached_file(__a						,	'conf'						,	_raise_exceptions_for_missing_entries=__a     )
								self.assertIsNone(__a     )

								__lowerCamelCase					:	int	       =				cached_file(__a						,	'conf'						,	local_files_only=__a						,	_raise_exceptions_for_missing_entries=__a     )
								self.assertIsNone(__a     )

								__lowerCamelCase					:	List[str]	       =				mock.Mock()
								__lowerCamelCase					:	str	       =				500
								__lowerCamelCase					:	Union[str, Any]	       =				{}
								__lowerCamelCase					:	Dict	       =				HTTPError
								__lowerCamelCase					:	Any	       =				{}

								# Under the mock environment we get a 500 error when trying to reach the tokenizer.
								with mock.patch('requests.Session.request'						,	return_value=__a     ) as mock_head:
											__lowerCamelCase					:	Any	       =				cached_file(__a						,	'conf'						,	_raise_exceptions_for_connection_errors=__a     )
											self.assertIsNone(__a     )
											# This check we did call the fake head request
											mock_head.assert_called()
					def     snake_case_				(    self     ):
								self.assertTrue(has_file('hf-internal-testing/tiny-bert-pt-only'						,	__a     )     )
								self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only'						,	__a     )     )
								self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only'						,	__a     )     )
					def     snake_case_				(    self     ):
								# `get_file_from_repo` returns None if the file does not exist
								self.assertIsNone(get_file_from_repo('bert-base-cased'						,	'ahah.txt'     )     )

								# The function raises if the repository does not exist.
								with self.assertRaisesRegex(__a						,	'is not a valid model identifier'     ):
											get_file_from_repo('bert-base-case'						,	__a     )

								# The function raises if the revision does not exist.
								with self.assertRaisesRegex(__a						,	'is not a valid git identifier'     ):
											get_file_from_repo('bert-base-cased'						,	__a						,	revision='ahaha'     )

								__lowerCamelCase					:	Optional[int]	       =				get_file_from_repo('bert-base-cased'						,	__a     )
								# The name is the cached name which is not very easy to test, so instead we load the content.
								__lowerCamelCase					:	int	       =				json.loads(open(__a						,	'r'     ).read()     )
								self.assertEqual(config['hidden_size']						,	768     )





					def     snake_case_				(    self     ):
								with tempfile.TemporaryDirectory() as tmp_dir:
											__lowerCamelCase					:	int	       =				Path(__a     ) / 'a.txt'
											filename.touch()
											self.assertEqual(get_file_from_repo(__a						,	'a.txt'     )						,	str(__a     )     )

											self.assertIsNone(get_file_from_repo(__a						,	'b.txt'     )     )



