"""simple docstring"""







import importlib.metadata
import subprocess
import sys



def       A__    (	lowercase_     :	bool = False						)  ->							Tuple:
   __lowerCamelCase    		=					False
   if "IPython" in sys.modules:
      __lowerCamelCase    		=					'google.colab' in str(sys.modules['IPython'].get_ipython()						)

   if in_colab:
      if upgrade:
         __lowerCamelCase    		=					['pip', 'install', '-U', 'torch']
         subprocess.run(lowercase_      , check=lowercase_						)
      # get the current version of torch
      __lowerCamelCase    		=					importlib.metadata.version('torch'						)
      __lowerCamelCase    		=					torch_version[: torch_version.rindex('.'						)]
      __lowerCamelCase    		=					F'''https://storage.googleapis.com/tpu-pytorch/wheels/colab/torch_xla-{torch_version_trunc}-cp37-cp37m-linux_x86_64.whl'''
      __lowerCamelCase    		=					['pip', 'install', xla_wheel]
      subprocess.run(lowercase_      , check=lowercase_						)
   else:
      raise RuntimeError('`install_xla` utility works only on google colab.'						)










import subprocess
from argparse import ArgumentParser
from typing import List, Union

from huggingface_hub.hf_api import HfFolder, create_repo, whoami
from requests.exceptions import HTTPError

from . import BaseTransformersCLICommand




class  A_ (			a_  ):







 """simple docstring"""





 @staticmethod
 def 	_UpperCamelCase     (      _a       :			ArgumentParser					)     ->      str:
      UpperCAmelCase    :					str			=      parser.add_parser("login"							,     help="Log in using the same credentials as on huggingface.co"					)
      login_parser.set_defaults(func=lambda _a					: LoginCommand(_a					)					)
      UpperCAmelCase    :					str			=      parser.add_parser("whoami"							,     help="Find out which huggingface.co account you are logged in as."					)
      whoami_parser.set_defaults(func=lambda _a					: WhoamiCommand(_a					)					)
      UpperCAmelCase    :					List[Any]			=      parser.add_parser("logout"							,     help="Log out"					)
      logout_parser.set_defaults(func=lambda _a					: LogoutCommand(_a					)					)

      # new system: git-based repo system
      UpperCAmelCase    :					List[str]			=      parser.add_parser(
          "repo"							,     help="Deprecated: use `huggingface-cli` instead. Commands to interact with your huggingface.co repos."							,     )
      UpperCAmelCase    :					List[str]			=      repo_parser.add_subparsers(
          help="Deprecated: use `huggingface-cli` instead. huggingface.co repos related commands"					)
      UpperCAmelCase    :					Optional[Any]			=      repo_subparsers.add_parser(
          "create"							,     help="Deprecated: use `huggingface-cli` instead. Create a new repo on huggingface.co"					)
      repo_create_parser.add_argument(
          "name"							,     type=_a							,     help="Name for your model's repo. Will be namespaced under your username to build the model id."							,     )
      repo_create_parser.add_argument("--organization"							,     type=_a							,     help="Optional: organization namespace."					)
      repo_create_parser.add_argument("-y"							,     "--yes"							,     action="store_true"							,     help="Optional: answer Yes to the prompt"					)
      repo_create_parser.set_defaults(func=lambda _a					: RepoCreateCommand(_a					)					)




class  A_ :







 """simple docstring"""





 _UpperCamelCase					:			str     	=       '''\u001b[1m'''
 _UpperCamelCase					:			Optional[Any]     	=       '''\u001b[31m'''
 _UpperCamelCase					:			List[Any]     	=       '''\u001b[90m'''
 _UpperCamelCase					:			Tuple     	=       '''\u001b[0m'''
 @classmethod
 def 	_UpperCamelCase     (      cls       :			Any							,     _a       :			Tuple					)     ->      Any:
      return f'{cls._bold}{s}{cls._reset}'
 @classmethod
 def 	_UpperCamelCase     (      cls       :			Optional[Any]							,     _a       :			List[Any]					)     ->      str:
      return f'{cls._bold}{cls._red}{s}{cls._reset}'

 @classmethod
 def 	_UpperCamelCase     (      cls       :			str							,     _a       :			Optional[Any]					)     ->      Tuple:
      return f'{cls._gray}{s}{cls._reset}'







def 	A (		A__		,		A__			):
     UpperCAmelCase    :					str			=      [max(len(str(A__			)			) for x in col			) for col in zip(*A__		,		A__			)]
     UpperCAmelCase    :					str			=      ("{{:{}}} " * len(A__			)).format(*A__			)
     UpperCAmelCase    :					Dict			=      []
     lines.append(row_format.format(*A__			)			)
     lines.append(row_format.format(*["-" * w for w in col_widths]			)			)
     for row in rows:
          lines.append(row_format.format(*A__			)			)
     return "\n".join(A__			)




class  A_ :







 """simple docstring"""





 def __init__(      self       :			List[str]							,     _a       :			str					)     ->      str:
      UpperCAmelCase    :					str			=      args




class  A_ (			a_  ):







 """simple docstring"""





 def 	_UpperCamelCase     (      self       :			Optional[int]					)     ->      Any:
      print(
          ANSI.red(
              "ERROR! `huggingface-cli login` uses an outdated login mechanism "
              "that is not compatible with the Hugging Face Hub backend anymore. "
              "Please use `huggingface-cli login instead."					)					)




class  A_ (			a_  ):







 """simple docstring"""





 def 	_UpperCamelCase     (      self       :			Tuple					)     ->      Tuple:
      print(
          ANSI.red(
              "WARNING! `transformers-cli whoami` is deprecated and will be removed in v5. Please use "
              "`huggingface-cli whoami` instead."					)					)
      UpperCAmelCase    :					Dict			=      HfFolder.get_token()
      if token is None:
           print("Not logged in"					)
           exit()
      try:
           UpperCAmelCase	,			UpperCAmelCase    :					Optional[int]			=      whoami(_a					)
           print(_a					)
           if orgs:
                print(ANSI.bold("orgs: "					)							,     ",".join(_a					)					)
      except HTTPError as e:
           print(_a					)
           print(ANSI.red(e.response.text					)					)
           exit(1					)




class  A_ (			a_  ):







 """simple docstring"""





 def 	_UpperCamelCase     (      self       :			List[Any]					)     ->      int:
      print(
          ANSI.red(
              "ERROR! `transformers-cli logout` uses an outdated logout mechanism "
              "that is not compatible with the Hugging Face Hub backend anymore. "
              "Please use `huggingface-cli logout instead."					)					)




class  A_ (			a_  ):







 """simple docstring"""





 def 	_UpperCamelCase     (      self       :			List[str]					)     ->      Union[str, Any]:
      print(
          ANSI.red(
              "WARNING! Managing repositories through transformers-cli is deprecated. "
              "Please use `huggingface-cli` instead."					)					)
      UpperCAmelCase    :					List[Any]			=      HfFolder.get_token()
      if token is None:
           print("Not logged in"					)
           exit(1					)
      try:
           UpperCAmelCase    :					List[str]			=      subprocess.check_output(["git", "--version"]					).decode("utf-8"					)
           print(ANSI.gray(stdout.strip()					)					)
      except FileNotFoundError:
           print("Looks like you do not have git installed, please install."					)

      try:
           UpperCAmelCase    :					Tuple			=      subprocess.check_output(["git-lfs", "--version"]					).decode("utf-8"					)
           print(ANSI.gray(stdout.strip()					)					)
      except FileNotFoundError:
           print(
               ANSI.red(
                   "Looks like you do not have git-lfs installed, please install."
                   " You can install from https://git-lfs.github.com/."
                   " Then run `git lfs install` (you only have to do this once)."					)					)
      print(""					)

      UpperCAmelCase	,			UpperCAmelCase    :					Tuple			=      whoami(_a					)
      UpperCAmelCase    :					Union[str, Any]			=      self.args.organization if self.args.organization is not None else user
      UpperCAmelCase    :					Dict			=      f'{namespace}/{self.args.name}'
      print(f'You are about to create {ANSI.bold(_a					)}'					)

      if not self.args.yes:
           UpperCAmelCase    :					Any			=      input("Proceed? [Y/n] "					).lower()
           if not (choice == "" or choice == "y" or choice == "yes"):
                print("Abort"					)
                exit()
      try:
           UpperCAmelCase    :					List[Any]			=      create_repo(_a							,     name=self.args.name							,     organization=self.args.organization					)
      except HTTPError as e:
           print(_a					)
           print(ANSI.red(e.response.text					)					)
           exit(1					)
      print("\nYour repo now lives at:"					)
      print(f'  {ANSI.bold(_a					)}'					)
      print("\nYou can clone it locally with the command below, and commit/push as usual."					)
      print(f'\n  git clone {url}'					)
      print(""					)



