


import json
import os
import shutil
import warnings
from argparse import ArgumentParser, Namespace
from pathlib import Path
from typing import List

from ..utils import logging
from . import BaseTransformersCLICommand


try:
       from cookiecutter.main import cookiecutter

       __UpperCamelCase       :       Any						       =				True
except ImportError:
       __UpperCamelCase       :       Optional[Any]						       =				False

__UpperCamelCase       :       List[str]						       =				logging.get_logger(__name__)  # pylint: disable=invalid-name

def 			_a  (				SCREAMING_SNAKE_CASE	:				Namespace     ):







 """simple docstring"""



 return AddNewModelCommand(args.testing    , args.testing_file    , path=args.path     )






class       __magic_name__	(     __lowerCAmelCase):


     @staticmethod
     def      UpperCAmelCase__	(      lowerCamelCase__							:     ArgumentParser			)  ->      List[str]:





      '''simple docstring'''






      UpperCamelCase__					:    Dict          =   parser.add_parser('''add-new-model'''			)
      add_new_model_parser.add_argument('''--testing'''							,	action='''store_true'''							,	help='''If in testing mode.'''			)
      add_new_model_parser.add_argument('''--testing_file'''							,	type=lowerCamelCase__							,	help='''Configuration file on which to run.'''			)
      add_new_model_parser.add_argument(
          '''--path'''							,	type=lowerCamelCase__							,	help='''Path to cookiecutter. Should only be used for testing purposes.'''			)
      add_new_model_parser.set_defaults(func=lowerCamelCase__			)


     def __init__(      self							:     List[str]							,	lowerCamelCase__							:     bool							,	lowerCamelCase__							:     str							,	lowerCamelCase__							:     List[Any]=None							,	*lowerCamelCase__							:     Optional[Any]			)  ->      Tuple:





      '''simple docstring'''






      UpperCamelCase__					:    int          =   testing
      UpperCamelCase__					:    int          =   testing_file
      UpperCamelCase__					:    List[str]          =   path


     def      UpperCAmelCase__	(      self							:     Tuple			)  ->      Optional[int]:





      '''simple docstring'''






      warnings.warn(
          '''The command `transformers-cli add-new-model` is deprecated and will be removed in v5 of Transformers. '''
          '''It is not actively maintained anymore, so might give a result that won\'t pass all tests and quality '''
          '''checks, you should use `transformers-cli add-new-model-like` instead.'''			)
      if not _has_cookiecutter:
       raise ImportError(
           '''Model creation dependencies are required to use the `add_new_model` command. Install them by running '''
           '''the following at the root of your `transformers` clone:\n\n\t$ pip install -e .[modelcreation]\n'''			)
      # Ensure that there is no other `cookiecutter-template-xxx` directory in the current working directory
      UpperCamelCase__					:    Dict          =   [directory for directory in os.listdir() if '''cookiecutter-template-''' == directory[:22]]
      if len(lowerCamelCase__			) > 0:
       raise ValueError(
           '''Several directories starting with `cookiecutter-template-` in current working directory. '''
           '''Please clean your directory by removing all folders starting with `cookiecutter-template-` or '''
           '''change your working directory.'''			)

      UpperCamelCase__					:    Optional[Any]          =   (
          Path(lowerCamelCase__			).parent.parent.parent.parent if self._path is None else Path(self._path			).parent.parent
      )
      UpperCamelCase__					:    Tuple          =   path_to_transformer_root / '''templates''' / '''adding_a_new_model'''

      # Execute cookiecutter
      if not self._testing:
       cookiecutter(str(lowerCamelCase__			)			)
      else:
       with open(self._testing_file							,	'''r'''			) as configuration_file:
        UpperCamelCase__					:    str          =   json.load(lowerCamelCase__			)

       cookiecutter(
           str(path_to_cookiecutter if self._path is None else self._path			)							,	no_input=lowerCamelCase__							,	extra_context=lowerCamelCase__							,	)

      UpperCamelCase__					:    Union[str, Any]          =   [directory for directory in os.listdir() if '''cookiecutter-template-''' in directory[:22]][0]

      # Retrieve configuration
      with open(directory + '''/configuration.json'''							,	'''r'''			) as configuration_file:
       UpperCamelCase__					:    Any          =   json.load(lowerCamelCase__			)

      UpperCamelCase__					:    List[Any]          =   configuration['''lowercase_modelname''']
      UpperCamelCase__					:    int          =   configuration['''generate_tensorflow_pytorch_and_flax''']
      os.remove(F"{directory}/configuration.json"			)

      UpperCamelCase__					:    str          =   '''PyTorch''' in generate_tensorflow_pytorch_and_flax
      UpperCamelCase__					:    Dict          =   '''TensorFlow''' in generate_tensorflow_pytorch_and_flax
      UpperCamelCase__					:    Dict          =   '''Flax''' in generate_tensorflow_pytorch_and_flax

      UpperCamelCase__					:    Union[str, Any]          =   F"{path_to_transformer_root}/src/transformers/models/{lowercase_model_name}"
      os.makedirs(lowerCamelCase__							,	exist_ok=lowerCamelCase__			)
      os.makedirs(F"{path_to_transformer_root}/tests/models/{lowercase_model_name}"							,	exist_ok=lowerCamelCase__			)

      # Tests require submodules as they have parent imports
      with open(F"{path_to_transformer_root}/tests/models/{lowercase_model_name}/__init__.py"							,	'''w'''			):
       pass

      shutil.move(
          F"{directory}/__init__.py"							,	F"{model_dir}/__init__.py"							,	)
      shutil.move(
          F"{directory}/configuration_{lowercase_model_name}.py"							,	F"{model_dir}/configuration_{lowercase_model_name}.py"							,	)

      def remove_copy_lines(lowerCamelCase__							:     Union[str, Any]			):
       with open(lowerCamelCase__							,	'''r'''			) as f:
        UpperCamelCase__					:    Tuple          =   f.readlines()
       with open(lowerCamelCase__							,	'''w'''			) as f:
        for line in lines:
         if "# Copied from transformers." not in line:
          f.write(lowerCamelCase__			)

      if output_pytorch:
       if not self._testing:
        remove_copy_lines(F"{directory}/modeling_{lowercase_model_name}.py"			)

       shutil.move(
           F"{directory}/modeling_{lowercase_model_name}.py"							,	F"{model_dir}/modeling_{lowercase_model_name}.py"							,	)

       shutil.move(
           F"{directory}/test_modeling_{lowercase_model_name}.py"							,	F"{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_{lowercase_model_name}.py"							,	)
      else:
       os.remove(F"{directory}/modeling_{lowercase_model_name}.py"			)
       os.remove(F"{directory}/test_modeling_{lowercase_model_name}.py"			)

      if output_tensorflow:
       if not self._testing:
        remove_copy_lines(F"{directory}/modeling_tf_{lowercase_model_name}.py"			)

       shutil.move(
           F"{directory}/modeling_tf_{lowercase_model_name}.py"							,	F"{model_dir}/modeling_tf_{lowercase_model_name}.py"							,	)

       shutil.move(
           F"{directory}/test_modeling_tf_{lowercase_model_name}.py"							,	F"{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_tf_{lowercase_model_name}.py"							,	)
      else:
       os.remove(F"{directory}/modeling_tf_{lowercase_model_name}.py"			)
       os.remove(F"{directory}/test_modeling_tf_{lowercase_model_name}.py"			)

      if output_flax:
       if not self._testing:
        remove_copy_lines(F"{directory}/modeling_flax_{lowercase_model_name}.py"			)

       shutil.move(
           F"{directory}/modeling_flax_{lowercase_model_name}.py"							,	F"{model_dir}/modeling_flax_{lowercase_model_name}.py"							,	)

       shutil.move(
           F"{directory}/test_modeling_flax_{lowercase_model_name}.py"							,	F"{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_flax_{lowercase_model_name}.py"							,	)
      else:
       os.remove(F"{directory}/modeling_flax_{lowercase_model_name}.py"			)
       os.remove(F"{directory}/test_modeling_flax_{lowercase_model_name}.py"			)

      shutil.move(
          F"{directory}/{lowercase_model_name}.md"							,	F"{path_to_transformer_root}/docs/source/en/model_doc/{lowercase_model_name}.md"							,	)

      shutil.move(
          F"{directory}/tokenization_{lowercase_model_name}.py"							,	F"{model_dir}/tokenization_{lowercase_model_name}.py"							,	)

      shutil.move(
          F"{directory}/tokenization_fast_{lowercase_model_name}.py"							,	F"{model_dir}/tokenization_{lowercase_model_name}_fast.py"							,	)

      from os import fdopen, remove
      from shutil import copymode, move
      from tempfile import mkstemp

      def replace(lowerCamelCase__							:     str							,	lowerCamelCase__							:     str							,	lowerCamelCase__							:     List[str]			):
       # Create temp file
       UpperCamelCase__       ,				UpperCamelCase__					:    int          =   mkstemp()
       UpperCamelCase__					:    Optional[int]          =   False
       with fdopen(lowerCamelCase__							,	'''w'''			) as new_file:
        with open(lowerCamelCase__			) as old_file:
         for line in old_file:
          new_file.write(lowerCamelCase__			)
          if line_to_copy_below in line:
           UpperCamelCase__					:    int          =   True
           for line_to_copy in lines_to_copy:
            new_file.write(lowerCamelCase__			)

       if not line_found:
        raise ValueError(F"Line {line_to_copy_below} was not found in file."			)

       # Copy the file permissions from the old file to the new file
       copymode(lowerCamelCase__							,	lowerCamelCase__			)
       # Remove original file
       remove(lowerCamelCase__			)
       # Move new file
       move(lowerCamelCase__							,	lowerCamelCase__			)

      def skip_units(lowerCamelCase__							:     int			):
       return (
           ("generating PyTorch" in line and not output_pytorch)
           or ("generating TensorFlow" in line and not output_tensorflow)
           or ("generating Flax" in line and not output_flax)
       )

      def replace_in_files(lowerCamelCase__							:     Tuple			):
       with open(lowerCamelCase__			) as datafile:
        UpperCamelCase__					:    Optional[Any]          =   []
        UpperCamelCase__					:    str          =   False
        UpperCamelCase__					:    Tuple          =   False
        for line in datafile:
         if "# To replace in: " in line and "##" not in line:
          UpperCamelCase__					:    Optional[int]          =   line.split('''"'''			)[1]
          UpperCamelCase__					:    Tuple          =   skip_units(lowerCamelCase__			)
         elif "# Below: " in line and "##" not in line:
          UpperCamelCase__					:    int          =   line.split('''"'''			)[1]
          UpperCamelCase__					:    int          =   skip_units(lowerCamelCase__			)
         elif "# End." in line and "##" not in line:
          if not skip_file and not skip_snippet:
           replace(lowerCamelCase__							,	lowerCamelCase__							,	lowerCamelCase__			)

          UpperCamelCase__					:    Dict          =   []
         elif "# Replace with" in line and "##" not in line:
          UpperCamelCase__					:    Dict          =   []
         elif "##" not in line:
          lines_to_copy.append(lowerCamelCase__			)

       remove(lowerCamelCase__			)

      replace_in_files(F"{directory}/to_replace_{lowercase_model_name}.py"			)
      os.rmdir(lowerCamelCase__			)






import contextlib
import importlib
import io
import unittest

import transformers

# Try to import everything from transformers to ensure every object can be loaded.
from transformers import *  # noqa F406
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch
from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available


if is_torch_available():
       from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification

if is_tf_available():
       from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification

if is_flax_available():
       from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification


__UpperCamelCase       :       Dict						       =				DUMMY_UNKNOWN_IDENTIFIER
# An actual model hosted on huggingface.co

__UpperCamelCase       :       Optional[int]						       =				"main"
# Default branch name
__UpperCamelCase       :       Optional[Any]						       =				"f2c752cfc5c0ab6f4bdec59acea69eefbee381c2"
# One particular commit (not the top of `main`)
__UpperCamelCase       :       str						       =				"aaaaaaa"
# This commit does not exist, so we should 404.

__UpperCamelCase       :       Optional[Any]						       =				"d9e9f15bc825e4b2c9249e9578f884bbcb5e3684"
# Sha-1 of config.json on the top of `main`, for checking purposes
__UpperCamelCase       :       Tuple						       =				"4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3"

@contextlib.contextmanager
def 			_a  (				):







 """simple docstring"""



 print('''Welcome!'''     )
 yield
 print('''Bye!'''     )

@contextlib.contextmanager
def 			_a  (				):







 """simple docstring"""



 print('''Bonjour!'''     )
 yield
 print('''Au revoir!'''     )

class       __magic_name__	(     unittest.TestCase):


     def      UpperCAmelCase__	(      self							:     Union[str, Any]			)  ->      Any:





      '''simple docstring'''






      assert transformers.__spec__ is not None
      assert importlib.util.find_spec('''transformers'''			) is not None



class       __magic_name__	(     unittest.TestCase):


     @unittest.mock.patch('''sys.stdout'''							,	new_callable=io.StringIO			)
     def      UpperCAmelCase__	(      self							:     Union[str, Any]							,	lowerCamelCase__							:     Union[str, Any]			)  ->      Optional[Any]:





      '''simple docstring'''






      with ContextManagers([]			):
       print('''Transformers are awesome!'''			)
      # The print statement adds a new line at the end of the output
      self.assertEqual(mock_stdout.getvalue()							,	'''Transformers are awesome!\n'''			)


     @unittest.mock.patch('''sys.stdout'''							,	new_callable=io.StringIO			)
     def      UpperCAmelCase__	(      self							:     Any							,	lowerCamelCase__							:     Union[str, Any]			)  ->      Dict:





      '''simple docstring'''






      with ContextManagers([context_en()]			):
       print('''Transformers are awesome!'''			)
      # The output should be wrapped with an English welcome and goodbye
      self.assertEqual(mock_stdout.getvalue()							,	'''Welcome!\nTransformers are awesome!\nBye!\n'''			)


     @unittest.mock.patch('''sys.stdout'''							,	new_callable=io.StringIO			)
     def      UpperCAmelCase__	(      self							:     List[Any]							,	lowerCamelCase__							:     List[str]			)  ->      Tuple:





      '''simple docstring'''






      with ContextManagers([context_fr(), context_en()]			):
       print('''Transformers are awesome!'''			)
      # The output should be wrapped with an English and French welcome and goodbye
      self.assertEqual(mock_stdout.getvalue()							,	'''Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n'''			)


     @require_torch
     def      UpperCAmelCase__	(      self							:     Tuple			)  ->      Optional[int]:





      '''simple docstring'''






      self.assertEqual(find_labels(lowerCamelCase__			)							,	['''labels''']			)
      self.assertEqual(find_labels(lowerCamelCase__			)							,	['''labels''', '''next_sentence_label''']			)
      self.assertEqual(find_labels(lowerCamelCase__			)							,	['''start_positions''', '''end_positions''']			)

      class       __magic_name__	(     __lowerCAmelCase):
           pass



      self.assertEqual(find_labels(lowerCamelCase__			)							,	['''labels''']			)


     @require_tf
     def      UpperCAmelCase__	(      self							:     Union[str, Any]			)  ->      Tuple:





      '''simple docstring'''






      self.assertEqual(find_labels(lowerCamelCase__			)							,	['''labels''']			)
      self.assertEqual(find_labels(lowerCamelCase__			)							,	['''labels''', '''next_sentence_label''']			)
      self.assertEqual(find_labels(lowerCamelCase__			)							,	['''start_positions''', '''end_positions''']			)

      class       __magic_name__	(     __lowerCAmelCase):
           pass



      self.assertEqual(find_labels(lowerCamelCase__			)							,	['''labels''']			)


     @require_flax
     def      UpperCAmelCase__	(      self							:     str			)  ->      Optional[Any]:





      '''simple docstring'''






      self.assertEqual(find_labels(lowerCamelCase__			)							,	[]			)
      self.assertEqual(find_labels(lowerCamelCase__			)							,	[]			)
      self.assertEqual(find_labels(lowerCamelCase__			)							,	[]			)

      class       __magic_name__	(     __lowerCAmelCase):
           pass



      self.assertEqual(find_labels(lowerCamelCase__			)							,	[]			)


