cubicweb/cwctl.py
changeset 12735 17d1b1f4eddd
parent 12696 eb83daa69495
child 12736 5add82b08a6d
equal deleted inserted replaced
12734:d177d8ab4fd3 12735:17d1b1f4eddd
    20 """
    20 """
    21 # *ctl module should limit the number of import to be imported as quickly as
    21 # *ctl module should limit the number of import to be imported as quickly as
    22 # possible (for cubicweb-ctl reactivity, necessary for instance for usable bash
    22 # possible (for cubicweb-ctl reactivity, necessary for instance for usable bash
    23 # completion). So import locally in command helpers.
    23 # completion). So import locally in command helpers.
    24 import sys
    24 import sys
       
    25 import traceback
    25 from warnings import filterwarnings
    26 from warnings import filterwarnings
    26 from os import listdir
    27 from os import listdir
    27 from os.path import exists, join, isdir
    28 from os.path import exists, join, isdir
    28 
    29 
    29 try:
    30 try:
   134         identifiers)
   135         identifiers)
   135         """
   136         """
   136         appid = args[0]
   137         appid = args[0]
   137         cmdmeth = getattr(self, '%s_instance' % self.name)
   138         cmdmeth = getattr(self, '%s_instance' % self.name)
   138 
   139 
       
   140         traceback_ = None
       
   141 
   139         try:
   142         try:
   140             status = cmdmeth(appid) or 0
   143             status = cmdmeth(appid) or 0
   141         except (ExecutionError, ConfigurationError) as ex:
   144         except (ExecutionError, ConfigurationError) as ex:
       
   145             # we need to do extract this information here for pdb since it is
       
   146             # now lost in python 3 once we exit the try/catch statement
       
   147             exception_type, exception, traceback_ = sys.exc_info()
       
   148 
   142             sys.stderr.write('instance %s not %s: %s\n' % (
   149             sys.stderr.write('instance %s not %s: %s\n' % (
   143                 appid, self.actionverb, ex))
   150                 appid, self.actionverb, ex))
   144             status = 4
   151             status = 4
   145         except Exception as ex:
   152         except Exception as ex:
   146             import traceback
   153             # idem
       
   154             exception_type, exception, traceback_ = sys.exc_info()
       
   155 
   147             traceback.print_exc()
   156             traceback.print_exc()
   148 
   157 
   149             sys.stderr.write('instance %s not %s: %s\n' % (
   158             sys.stderr.write('instance %s not %s: %s\n' % (
   150                 appid, self.actionverb, ex))
   159                 appid, self.actionverb, ex))
   151             status = 8
   160             status = 8
   152 
   161 
   153         except (KeyboardInterrupt, SystemExit) as ex:
   162         except (KeyboardInterrupt, SystemExit) as ex:
       
   163             # idem
       
   164             exception_type, exception, traceback_ = sys.exc_info()
       
   165 
   154             sys.stderr.write('%s aborted\n' % self.name)
   166             sys.stderr.write('%s aborted\n' % self.name)
   155             if isinstance(ex, KeyboardInterrupt):
   167             if isinstance(ex, KeyboardInterrupt):
   156                 status = 2  # specific error code
   168                 status = 2  # specific error code
   157             else:
   169             else:
   158                 status = ex.code
   170                 status = ex.code
   159 
   171 
   160         if status != 0 and self.config.pdb:
   172         if status != 0 and self.config.pdb:
   161             exception_type, exception, traceback_ = sys.exc_info()
       
   162             pdb = get_pdb()
   173             pdb = get_pdb()
   163             pdb.post_mortem(traceback_)
   174 
       
   175             if traceback_ is not None:
       
   176                 pdb.post_mortem(traceback_)
       
   177             else:
       
   178                 print("WARNING: Could not access to the traceback because the command return "
       
   179                       "code is different than 0 but the command didn't raised an exception.")
       
   180                 # we can't use "header=" of set_trace because ipdb doesn't supports it
       
   181                 pdb.set_trace()
   164 
   182 
   165         sys.exit(status)
   183         sys.exit(status)
   166 
   184 
   167 
   185 
   168 # base commands ###############################################################
   186 # base commands ###############################################################