github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/consensus/poet/cli/sawtooth_poet_cli/main.py (about)

     1  # Copyright 2017 Intel Corporation
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  # ------------------------------------------------------------------------------
    15  import argparse
    16  import logging
    17  import os
    18  import sys
    19  import traceback
    20  import pkg_resources
    21  
    22  from colorlog import ColoredFormatter
    23  
    24  from sawtooth_poet_cli.exceptions import CliException
    25  from sawtooth_poet_cli.registration import add_registration_parser
    26  from sawtooth_poet_cli.registration import do_registration
    27  from sawtooth_poet_cli.enclave import add_enclave_parser
    28  from sawtooth_poet_cli.enclave import do_enclave
    29  
    30  
    31  DISTRIBUTION_NAME = 'sawtooth-poet-cli'
    32  
    33  
    34  def create_console_handler(verbose_level):
    35      clog = logging.StreamHandler()
    36      formatter = ColoredFormatter(
    37          "%(log_color)s[%(asctime)s %(levelname)-8s%(module)s]%(reset)s "
    38          "%(white)s%(message)s",
    39          datefmt="%H:%M:%S",
    40          reset=True,
    41          log_colors={
    42              'DEBUG': 'cyan',
    43              'INFO': 'green',
    44              'WARNING': 'yellow',
    45              'ERROR': 'red',
    46              'CRITICAL': 'red',
    47          })
    48  
    49      clog.setFormatter(formatter)
    50  
    51      if verbose_level == 0:
    52          clog.setLevel(logging.WARN)
    53      elif verbose_level == 1:
    54          clog.setLevel(logging.INFO)
    55      else:
    56          clog.setLevel(logging.DEBUG)
    57  
    58      return clog
    59  
    60  
    61  def setup_loggers(verbose_level):
    62      logger = logging.getLogger()
    63      logger.setLevel(logging.DEBUG)
    64      logger.addHandler(create_console_handler(verbose_level))
    65  
    66  
    67  def create_parent_parser(prog_name):
    68      parent_parser = argparse.ArgumentParser(prog=prog_name, add_help=False)
    69      parent_parser.add_argument(
    70          '-v', '--verbose',
    71          action='count',
    72          help='enable more verbose output')
    73  
    74      try:
    75          version = pkg_resources.get_distribution(DISTRIBUTION_NAME).version
    76      except pkg_resources.DistributionNotFound:
    77          version = 'UNKNOWN'
    78  
    79      parent_parser.add_argument(
    80          '-V', '--version',
    81          action='version',
    82          version=(DISTRIBUTION_NAME + ' (Hyperledger Sawtooth) version {}')
    83          .format(version),
    84          help='display version information')
    85  
    86      return parent_parser
    87  
    88  
    89  def create_parser(prog_name):
    90      parent_parser = create_parent_parser(
    91          prog_name)
    92  
    93      parser = argparse.ArgumentParser(
    94          description='Provides subcommands for creating PoET registration.',
    95          parents=[parent_parser],
    96          formatter_class=argparse.RawDescriptionHelpFormatter)
    97  
    98      subparsers = parser.add_subparsers(title='subcommands', dest='command')
    99      subparsers.required = True
   100  
   101      add_registration_parser(subparsers, parent_parser)
   102      add_enclave_parser(subparsers)
   103  
   104      return parser
   105  
   106  
   107  def main(prog_name=os.path.basename(sys.argv[0]), args=None,
   108           with_loggers=True):
   109  
   110      if args is None:
   111          args = sys.argv[1:]
   112      parser = create_parser(prog_name)
   113      args = parser.parse_args(args)
   114  
   115      if with_loggers is True:
   116          if args.verbose is None:
   117              verbose_level = 0
   118          else:
   119              verbose_level = args.verbose
   120          setup_loggers(verbose_level=verbose_level)
   121  
   122      if args.command == 'registration':
   123          do_registration(args)
   124      elif args.command == 'enclave':
   125          do_enclave(args)
   126      else:
   127          raise AssertionError('invalid command: {}'.format(args.command))
   128  
   129  
   130  def main_wrapper():
   131      # pylint: disable=bare-except
   132      try:
   133          main()
   134      except CliException as e:
   135          print("Error: {}".format(e), file=sys.stderr)
   136          sys.exit(1)
   137      except KeyboardInterrupt:
   138          pass
   139      except SystemExit as e:
   140          raise e
   141      except:
   142          traceback.print_exc(file=sys.stderr)
   143          sys.exit(1)