cubicweb/cwctl.py
changeset 12754 22ece66dcd47
parent 12749 ff63319a1730
equal deleted inserted replaced
12753:38d24fb2ae48 12754:22ece66dcd47
    19 provide a pluggable commands system.
    19 provide a pluggable commands system.
    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 os
    24 import sys
    25 import sys
    25 import traceback
    26 import traceback
    26 from warnings import filterwarnings
    27 from warnings import filterwarnings
    27 from os import listdir
    28 from os import listdir
    28 from os.path import exists, join, isdir
    29 from os.path import exists, join, isdir
   105 class InstanceCommand(Command):
   106 class InstanceCommand(Command):
   106     """base class for command taking one instance id as arguments"""
   107     """base class for command taking one instance id as arguments"""
   107     arguments = '<instance>'
   108     arguments = '<instance>'
   108 
   109 
   109     # enforce having one instance
   110     # enforce having one instance
   110     min_args = max_args = 1
   111     min_args = 0
       
   112     max_args = 1
   111 
   113 
   112     options = (
   114     options = (
   113         ("force",
   115         ("force",
   114          {'short': 'f', 'action': 'store_true',
   116          {'short': 'f', 'action': 'store_true',
   115           'default': False,
   117           'default': False,
   140 
   142 
   141     def run(self, args):
   143     def run(self, args):
   142         """run the <command>_method on each argument (a list of instance
   144         """run the <command>_method on each argument (a list of instance
   143         identifiers)
   145         identifiers)
   144         """
   146         """
   145         appid = args[0]
   147         if not args:
       
   148             if "CW_INSTANCE" in os.environ:
       
   149                 appid = os.environ["CW_INSTANCE"]
       
   150             else:
       
   151                 raise BadCommandUsage("Error: instance id is missing")
       
   152         else:
       
   153             appid = args[0]
       
   154 
   146         cmdmeth = getattr(self, '%s_instance' % self.name)
   155         cmdmeth = getattr(self, '%s_instance' % self.name)
   147 
   156 
   148         traceback_ = None
   157         traceback_ = None
   149 
   158 
   150         # debugmode=True is to force to have a StreamHandler used instead of
   159         # debugmode=True is to force to have a StreamHandler used instead of