





import argparse
import os
import re

import packaging.version


__lowerCamelCase	:					Tuple			  =					"examples/"
__lowerCamelCase	:					Dict			  =					{
    "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"),
}
__lowerCamelCase	:					Dict			  =					{
    "init": "src/transformers/__init__.py",
    "setup": "setup.py",
}
__lowerCamelCase	:					List[Any]			  =					"README.md"



def 		A_    (					_lowerCAmelCase							,       _lowerCAmelCase							,       _lowerCAmelCase					)						->			Optional[Any]:



 with open(lowerCAmelCase__							,       "r"							,       encoding="utf-8"							,       newline="\n"					) as f:
  UpperCamelCase     : List[str]	     =       f.read()
 UpperCamelCase     ,   UpperCamelCase     : List[Any]	     =       REPLACE_PATTERNS[pattern]
 UpperCamelCase     : List[Any]	     =       replace.replace("VERSION"							,       lowerCAmelCase__					)
 UpperCamelCase     : Union[str, Any]	     =       re_pattern.sub(lowerCAmelCase__							,       lowerCAmelCase__					)
 with open(lowerCAmelCase__							,       "w"							,       encoding="utf-8"							,       newline="\n"					) as f:
  f.write(lowerCAmelCase__					)



def 		A_    (					_lowerCAmelCase					)						->			Optional[Any]:



 for folder, directories, fnames in os.walk(lowerCAmelCase__					):
  # 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(lowerCAmelCase__							,       lowerCAmelCase__					)							,       lowerCAmelCase__							,       pattern="examples"					)



def 		A_    (					_lowerCAmelCase							,       _lowerCAmelCase=False					)						->			List[Any]:



 for pattern, fname in REPLACE_FILES.items():
  update_version_in_file(lowerCAmelCase__							,       lowerCAmelCase__							,       lowerCAmelCase__					)
 if not patch:
  update_version_in_examples(lowerCAmelCase__					)



def 		A_    (					)						->			str:
 UpperCamelCase     : str	     =       "🤗 Transformers currently provides the following architectures"
 UpperCamelCase     : List[str]	     =       "1. Want to contribute a new model?"
 with open(lowerCAmelCase__							,       "r"							,       encoding="utf-8"							,       newline="\n"					) as f:
  UpperCamelCase     : str	     =       f.readlines()

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

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

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



def 		A_    (					)						->			Optional[int]:



 with open(REPLACE_FILES["init"]							,       "r"					) as f:
  UpperCamelCase     : Dict	     =       f.read()
 UpperCamelCase     : str	     =       REPLACE_PATTERNS["init"][0].search(lowerCAmelCase__					).groups()[0]
 return packaging.version.parse(lowerCAmelCase__					)



def 		A_    (					_lowerCAmelCase=False					)						->			Any:
 UpperCamelCase     : Optional[int]	     =       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:
  UpperCamelCase     : Dict	     =       default_version.base_version
 elif patch:
  UpperCamelCase     : List[Any]	     =       F"""{default_version.major}.{default_version.minor}.{default_version.micro + 1}"""
 else:
  UpperCamelCase     : Any	     =       F"""{default_version.major}.{default_version.minor + 1}.0"""

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

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



def 		A_    (					)						->			Optional[Any]:
 UpperCamelCase     : int	     =       get_version()
 UpperCamelCase     : int	     =       F"""{current_version.major}.{current_version.minor + 1}.0.dev0"""
 UpperCamelCase     : str	     =       current_version.base_version

 # Check with the user we got that right.
 UpperCamelCase     : Any	     =       input(F"""Which version are we developing now? [{dev_version}]"""					)
 if len(lowerCAmelCase__					) == 0:
  UpperCamelCase     : Optional[Any]	     =       dev_version

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


if __name__ == "__main__":
    __lowerCamelCase	:					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.""")
    __lowerCamelCase	:					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()







__lowerCamelCase	:					Tuple			  =					"""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"""



def 		A_    (					_lowerCAmelCase					)						->			bytes:
 # Make sure the supplied data is a bytes-like object
 if not isinstance(_lowerCAmelCase							,       _lowerCAmelCase					):
  UpperCamelCase     : str	     =       F"""a bytes-like object is required, not '{data.__class__.__name__}'"""
  raise TypeError(_lowerCAmelCase					)

 UpperCamelCase     : Optional[int]	     =       "".join(bin(_lowerCAmelCase					)[2:].zfill(8					) for byte in data					)

 UpperCamelCase     : str	     =       len(_lowerCAmelCase					) % 6 != 0

 if padding_needed:
  # The padding that will be added later
  UpperCamelCase     : Optional[Any]	     =       b"=" * ((6 - len(_lowerCAmelCase					) % 6) // 2)

  # Append binary_stream with arbitrary binary digits (0's by default) to make its
  # length a multiple of 6.
  binary_stream += "0" * (6 - len(_lowerCAmelCase					) % 6)
 else:
  UpperCamelCase     : List[Any]	     =       b""

 # Encode every 6 binary digits to their corresponding Base64 character
 return (
     "".join(
         B64_CHARSET[int(binary_stream[index : index + 6]							,       2					)]
         for index in range(0							,       len(_lowerCAmelCase					)							,       6					)					).encode()
     + padding
 )



def 		A_    (					_lowerCAmelCase					)						->			bytes:
 # Make sure encoded_data is either a string or a bytes-like object
 if not isinstance(_lowerCAmelCase							,       _lowerCAmelCase					) and not isinstance(_lowerCAmelCase							,       _lowerCAmelCase					):
  UpperCamelCase     : List[Any]	     =       (
      "argument should be a bytes-like object or ASCII string, "
      F"""not '{encoded_data.__class__.__name__}'"""
  )
  raise TypeError(_lowerCAmelCase					)

 # In case encoded_data is a bytes-like object, make sure it contains only
 # ASCII characters so we convert it to a string object
 if isinstance(_lowerCAmelCase							,       _lowerCAmelCase					):
  try:
   UpperCamelCase     : Any	     =       encoded_data.decode("utf-8"					)
  except UnicodeDecodeError:
   raise ValueError("base64 encoded data should only contain ASCII characters"					)

 UpperCamelCase     : Union[str, Any]	     =       encoded_data.count("="					)

 # Check if the encoded string contains non base64 characters
 if padding:
  assert all(
      char in B64_CHARSET for char in encoded_data[:-padding]					), "Invalid base64 character(s) found."
 else:
  assert all(
      char in B64_CHARSET for char in encoded_data					), "Invalid base64 character(s) found."

 # Check the padding
 assert len(_lowerCAmelCase					) % 4 == 0 and padding < 3, "Incorrect padding"

 if padding:
  # Remove padding if there is one
  UpperCamelCase     : List[str]	     =       encoded_data[:-padding]

  UpperCamelCase     : List[Any]	     =       "".join(
      bin(B64_CHARSET.index(_lowerCAmelCase					)					)[2:].zfill(6					) for char in encoded_data					)[: -padding * 2]
 else:
  UpperCamelCase     : List[Any]	     =       "".join(
      bin(B64_CHARSET.index(_lowerCAmelCase					)					)[2:].zfill(6					) for char in encoded_data					)

 UpperCamelCase     : Any	     =       [
     int(binary_stream[index : index + 8]							,       2					)
     for index in range(0							,       len(_lowerCAmelCase					)							,       8					)
 ]

 return bytes(_lowerCAmelCase					)


if __name__ == "__main__":
    import doctest

    doctest.testmod()
