





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,
)


UpperCAmelCase_           =	"\\n    Text data.\n    Second line of data."

UpperCAmelCase_           =	"file"
@pytest.fixture(scope='''session'''						)
def      A__       (    SCREAMING_SNAKE_CASE_   :      Optional[int]						)  ->  Dict:






   """simple docstring"""







   _UpperCAmelCase           =   tmp_path_factory.mktemp('''data'''						) / (FILE_PATH + '''.zstd''')
   _UpperCAmelCase           =   bytes(SCREAMING_SNAKE_CASE_				,	'''utf-8'''						)
   with zstd.open(SCREAMING_SNAKE_CASE_				,	'''wb'''						) as f:
      f.write(SCREAMING_SNAKE_CASE_						)
   return path
@pytest.fixture
def      A__       (    SCREAMING_SNAKE_CASE_   :      Tuple						)  ->  Optional[Any]:






   """simple docstring"""







   with open(os.path.join(tmpfs.local_root_dir				,	SCREAMING_SNAKE_CASE_						)				,	'''w'''						) as f:
      f.write(SCREAMING_SNAKE_CASE_						)
   return FILE_PATH
@pytest.mark.parametrize('''compression_format'''				,	['''gzip''', '''xz''', '''zstd''']						)
def      A__       (    SCREAMING_SNAKE_CASE_   :      List[str]				,	SCREAMING_SNAKE_CASE_   :      int				,	SCREAMING_SNAKE_CASE_   :      Union[str, Any]				,	SCREAMING_SNAKE_CASE_   :      Any				,	SCREAMING_SNAKE_CASE_   :      Any				,	SCREAMING_SNAKE_CASE_   :      Any						)  ->  str:






   """simple docstring"""







   _UpperCAmelCase           =   {'''gzip''': gz_file, '''xz''': xz_file, '''zstd''': zstd_path}
   _UpperCAmelCase           =   input_paths[compression_format]
   _UpperCAmelCase           =   tmp_path / '''cache'''
   _UpperCAmelCase           =   DownloadConfig(cache_dir=SCREAMING_SNAKE_CASE_				,	extract_compressed_file=SCREAMING_SNAKE_CASE_						)
   _UpperCAmelCase           =   cached_path(SCREAMING_SNAKE_CASE_				,	download_config=SCREAMING_SNAKE_CASE_						)
   with open(SCREAMING_SNAKE_CASE_						) as f:
      _UpperCAmelCase           =   f.read()
   with open(SCREAMING_SNAKE_CASE_						) as f:
      _UpperCAmelCase           =   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__       (    SCREAMING_SNAKE_CASE_   :      Any				,	SCREAMING_SNAKE_CASE_   :      List[Any]				,	SCREAMING_SNAKE_CASE_   :      Dict				,	SCREAMING_SNAKE_CASE_   :      List[str]				,	SCREAMING_SNAKE_CASE_   :      Union[str, Any]						)  ->  List[str]:






   """simple docstring"""







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

   _UpperCAmelCase           =   xz_file
   _UpperCAmelCase           =   (
       DownloadConfig(extract_compressed_file=SCREAMING_SNAKE_CASE_						)
       if default_cache_dir
       else DownloadConfig(cache_dir=tmp_path / custom_cache_dir				,	extract_compressed_file=SCREAMING_SNAKE_CASE_						)
   )
   _UpperCAmelCase           =   cached_path(SCREAMING_SNAKE_CASE_				,	download_config=SCREAMING_SNAKE_CASE_						)
   assert Path(SCREAMING_SNAKE_CASE_						).parent.parts[-2:] == expected
def      A__       (    SCREAMING_SNAKE_CASE_   :      Any						)  ->  Optional[int]:






   """simple docstring"""







   _UpperCAmelCase           =   str(Path(SCREAMING_SNAKE_CASE_						).resolve()						)
   assert cached_path(SCREAMING_SNAKE_CASE_						) == text_file
   # relative path
   _UpperCAmelCase           =   str(Path(SCREAMING_SNAKE_CASE_						).resolve().relative_to(Path(os.getcwd()						)						)						)
   assert cached_path(SCREAMING_SNAKE_CASE_						) == text_file
def      A__       (    SCREAMING_SNAKE_CASE_   :      List[Any]						)  ->  List[Any]:






   """simple docstring"""







   _UpperCAmelCase           =   str(tmp_path.resolve() / '''__missing_file__.txt'''						)
   with pytest.raises(SCREAMING_SNAKE_CASE_						):
      cached_path(SCREAMING_SNAKE_CASE_						)
   # relative path
   _UpperCAmelCase           =   '''./__missing_file__.txt'''
   with pytest.raises(SCREAMING_SNAKE_CASE_						):
      cached_path(SCREAMING_SNAKE_CASE_						)
def      A__       (    SCREAMING_SNAKE_CASE_   :      List[Any]						)  ->  Any:






   """simple docstring"""







   _UpperCAmelCase           =   get_from_cache(F'''tmp://{tmpfs_file}'''						)
   with open(SCREAMING_SNAKE_CASE_						) as f:
      _UpperCAmelCase           =   f.read()
   assert output_file_content == FILE_CONTENT
@patch('''datasets.config.HF_DATASETS_OFFLINE'''				,	SCREAMING_SNAKE_CASE_						)
def      A__       (    )  ->  Any:






   """simple docstring"""







   with pytest.raises(SCREAMING_SNAKE_CASE_						):
      cached_path('''https://huggingface.co'''						)
@patch('''datasets.config.HF_DATASETS_OFFLINE'''				,	SCREAMING_SNAKE_CASE_						)
def      A__       (    SCREAMING_SNAKE_CASE_   :      Union[str, Any]						)  ->  str:






   """simple docstring"""







   _UpperCAmelCase           =   tmp_path_factory.mktemp('''data'''						) / '''file.html'''
   with pytest.raises(SCREAMING_SNAKE_CASE_						):
      http_get('''https://huggingface.co'''				,	temp_file=SCREAMING_SNAKE_CASE_						)
   with pytest.raises(SCREAMING_SNAKE_CASE_						):
      http_head('''https://huggingface.co'''						)
@patch('''datasets.config.HF_DATASETS_OFFLINE'''				,	SCREAMING_SNAKE_CASE_						)
def      A__       (    SCREAMING_SNAKE_CASE_   :      Optional[int]						)  ->  Union[str, Any]:






   """simple docstring"""







   _UpperCAmelCase           =   tmp_path_factory.mktemp('''data'''						) / '''file.html'''
   with pytest.raises(SCREAMING_SNAKE_CASE_						):
      ftp_get('''ftp://huggingface.co'''				,	temp_file=SCREAMING_SNAKE_CASE_						)
   with pytest.raises(SCREAMING_SNAKE_CASE_						):
      ftp_head('''ftp://huggingface.co'''						)
@patch('''datasets.config.HF_DATASETS_OFFLINE'''				,	SCREAMING_SNAKE_CASE_						)
def      A__       (    SCREAMING_SNAKE_CASE_   :      Tuple						)  ->  List[Any]:






   """simple docstring"""







   _UpperCAmelCase           =   tmp_path_factory.mktemp('''data'''						) / '''file.html'''
   with pytest.raises(SCREAMING_SNAKE_CASE_						):
      fsspec_get('''s3://huggingface.co'''				,	temp_file=SCREAMING_SNAKE_CASE_						)
   with pytest.raises(SCREAMING_SNAKE_CASE_						):
      fsspec_head('''s3://huggingface.co'''						)






"""simple docstring"""
import argparse
import os
import re

import packaging.version


__UpperCamelCase     :						Union[str, Any]						       =  '''examples/'''
__UpperCamelCase     :						str						       =  {
    '''examples''': (re.compile(R'''^check_min_version\("[^"]+"\)\s*$''', re.MULTILINE), '''check_min_version("VERSION")\n'''),
    '''init''': (re.compile(R'''^__version__\s+=\s+"([^"]+)"\s*$''', re.MULTILINE), '''__version__ = "VERSION"\n'''),
    '''setup''': (re.compile(R'''^(\s*)version\s*=\s*"[^"]+",''', re.MULTILINE), R'''\1version="VERSION",'''),
    '''doc''': (re.compile(R'''^(\s*)release\s*=\s*"[^"]+"$''', re.MULTILINE), '''release = "VERSION"\n'''),
}
__UpperCamelCase     :						List[str]						       =  {
    '''init''': '''src/transformers/__init__.py''',
    '''setup''': '''setup.py''',
}
__UpperCamelCase     :						Optional[int]						       =  '''README.md'''

def 		_SCREAMING_SNAKE_CASE			(_UpperCAmelCase					:    Any  ,     _UpperCAmelCase					:    Dict  ,     _UpperCAmelCase					:    Any			):
 with open(_UpperCAmelCase  ,     'r'  ,     encoding='utf-8'  ,     newline='\n'			) as f:
  lowerCAmelCase    				= f.read()
 lowerCAmelCase				,lowerCAmelCase    				= REPLACE_PATTERNS[pattern]
 lowerCAmelCase    				= replace.replace('VERSION'  ,     _UpperCAmelCase			)
 lowerCAmelCase    				= re_pattern.sub(_UpperCAmelCase  ,     _UpperCAmelCase			)
 with open(_UpperCAmelCase  ,     'w'  ,     encoding='utf-8'  ,     newline='\n'			) as f:
  f.write(_UpperCAmelCase			)

def 		_SCREAMING_SNAKE_CASE			(_UpperCAmelCase					:    Union[str, Any]			):
 for folder, directories, fnames in os.walk(_UpperCAmelCase			):
  # Removing some of the folders with non-actively maintained examples from the walk
  if "research_projects" in directories:
   directories.remove('research_projects'			)
  if "legacy" in directories:
   directories.remove('legacy'			)
  for fname in fnames:
   if fname.endswith('.py'			):
    update_version_in_file(os.path.join(_UpperCAmelCase  ,     _UpperCAmelCase			)  ,     _UpperCAmelCase  ,     pattern='examples'			)

def 		_SCREAMING_SNAKE_CASE			(_UpperCAmelCase					:    List[str]  ,     _UpperCAmelCase					:    Dict=False			):
 for pattern, fname in REPLACE_FILES.items():
  update_version_in_file(_UpperCAmelCase  ,     _UpperCAmelCase  ,     _UpperCAmelCase			)
 if not patch:
  update_version_in_examples(_UpperCAmelCase			)

def 		_SCREAMING_SNAKE_CASE			():
 lowerCAmelCase    				= '🤗 Transformers currently provides the following architectures'
 lowerCAmelCase    				= '1. Want to contribute a new model?'
 with open(_UpperCAmelCase  ,     'r'  ,     encoding='utf-8'  ,     newline='\n'			) as f:
  lowerCAmelCase    				= f.readlines()

 # Find the start of the list.
 lowerCAmelCase    				= 0
 while not lines[start_index].startswith(_start_prompt			):
  start_index += 1
 start_index += 1

 lowerCAmelCase    				= start_index
 # Update the lines in the model list.
 while not lines[index].startswith(_end_prompt			):
  if lines[index].startswith('1.'			):
   lowerCAmelCase    				= lines[index].replace(
       'https://huggingface.co/docs/transformers/main/model_doc'  ,     'https://huggingface.co/docs/transformers/model_doc'  ,     )
  index += 1

 with open(_UpperCAmelCase  ,     'w'  ,     encoding='utf-8'  ,     newline='\n'			) as f:
  f.writelines(_UpperCAmelCase			)

def 		_SCREAMING_SNAKE_CASE			():
 with open(REPLACE_FILES['init']  ,     'r'			) as f:
  lowerCAmelCase    				= f.read()
 lowerCAmelCase    				= REPLACE_PATTERNS['init'][0].search(_UpperCAmelCase			).groups()[0]
 return packaging.version.parse(_UpperCAmelCase			)

def 		_SCREAMING_SNAKE_CASE			(_UpperCAmelCase					:    Tuple=False			):
 lowerCAmelCase    				= get_version()
 if patch and default_version.is_devrelease:
  raise ValueError('Can\'t create a patch version from the dev branch, checkout a released version!'			)
 if default_version.is_devrelease:
  lowerCAmelCase    				= default_version.base_version
 elif patch:
  lowerCAmelCase    				= F'{default_version.major}.{default_version.minor}.{default_version.micro + 1}'
 else:
  lowerCAmelCase    				= F'{default_version.major}.{default_version.minor + 1}.0'

 # Now let's ask nicely if that's the right one.
 lowerCAmelCase    				= input(F'Which version are you releasing? [{default_version}]'			)
 if len(_UpperCAmelCase			) == 0:
  lowerCAmelCase    				= default_version

 print(F'Updating version to {version}.'			)
 global_version_update(_UpperCAmelCase  ,     patch=_UpperCAmelCase			)
 if not patch:
  print('Cleaning main README, don\'t forget to run `make fix-copies`.'			)
  clean_main_ref_in_model_list()

def 		_SCREAMING_SNAKE_CASE			():
 lowerCAmelCase    				= get_version()
 lowerCAmelCase    				= F'{current_version.major}.{current_version.minor + 1}.0.dev0'
 lowerCAmelCase    				= current_version.base_version

 # Check with the user we got that right.
 lowerCAmelCase    				= input(F'Which version are we developing now? [{dev_version}]'			)
 if len(_UpperCAmelCase			) == 0:
  lowerCAmelCase    				= dev_version

 print(F'Updating version to {version}.'			)
 global_version_update(_UpperCAmelCase			)
 print('Cleaning main README, don\'t forget to run `make fix-copies`.'			)
 clean_main_ref_in_model_list()


if __name__ == "__main__":
       __UpperCamelCase     :						Optional[int]						       =  argparse.ArgumentParser()
       parser.add_argument('''--post_release''', action='''store_true''', help='''Whether this is pre or post release.''')
       parser.add_argument('''--patch''', action='''store_true''', help='''Whether or not this is a patch release.''')
       __UpperCamelCase     :						Optional[int]						       =  parser.parse_args()
       if not args.post_release:
              pre_release_work(patch=args.patch)
       elif args.patch:
              print('''Nothing to do after a patch :-)''')
       else:
              post_release_work()





