2 Define a few common arguments for server-side command-line tools.
7 from setproctitle import setproctitle
9 setproctitle = lambda x: None
12 def add_addons_argument(parser):
14 Add a common --addons argument to a parser.
16 parser.add_argument('--addons', metavar='ADDONS',
17 **required_or_default('ADDONS',
18 'colon-separated list of paths to addons'))
21 Turn args.addons into a list instead of a column-separated strings.
22 Set openerp.toools.config accordingly.
24 import openerp.tools.config
25 config = openerp.tools.config
27 assert hasattr(args, 'addons')
29 args.addons = args.addons.split(':')
33 config['addons_path'] = ','.join(args.addons)
35 def get_addons_from_paths(paths, exclude):
37 Build a list of available modules from a list of addons paths.
39 exclude = exclude or []
43 names = list(set(os.listdir(p)))
44 names = filter(lambda a: not (a.startswith('.') or a in exclude), names)
45 names = filter(lambda a: os.path.isdir(os.path.join(p, a)), names)
46 names = filter(lambda a: os.path.exists(os.path.join(p, a, '__openerp__.py')), names)
47 module_names.extend(names)
49 print "The addons path `%s` doesn't exist." % p
53 def required_or_default(name, h):
55 Helper to define `argparse` arguments. If the name is the environment,
56 the argument is optional and draw its value from the environment if not
57 supplied on the command-line. If it is not in the environment, make it
60 if os.environ.get('OPENERP_' + name.upper()):
61 d = {'default': os.environ['OPENERP_' + name.upper()]}
63 d = {'required': True}
64 d['help'] = h + '. The environment variable OPENERP_' + \
65 name.upper() + ' can be used instead.'
68 class Command(object):
70 Base class to create command-line tools. It must be inherited and the
71 run() method overriden.
74 command_name = 'stand-alone'
76 def __init__(self, subparsers=None):
78 self.parser = parser = subparsers.add_parser(self.command_name,
79 description=self.__class__.__doc__)
81 self.parser = parser = argparse.ArgumentParser(
82 description=self.__class__.__doc__)
84 parser.add_argument('-d', '--database', metavar='DATABASE',
85 **required_or_default('DATABASE', 'the database to connect to'))
86 parser.add_argument('-u', '--user', metavar='USER',
87 **required_or_default('USER', 'the user login or ID. When using '
88 'RPC, providing an ID avoid the login() step'))
89 parser.add_argument('-p', '--password', metavar='PASSWORD',
90 **required_or_default('PASSWORD', 'the user password')) # TODO read it from the command line or from file.
92 parser.set_defaults(run=self.run_with_args)
94 def run_with_args(self, args):
99 print 'Stub Command.run().'
102 def stand_alone(cls):
104 A single Command object is a complete command-line program. See
105 `openerp-command/stand-alone` for an example.
108 args = command.parser.parse_args()