~camptocamp/openobject-server/trunk-fix_1314680-afe

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
Define a few common arguments for server-side command-line tools.
"""
import argparse
import os
try:
    from setproctitle import setproctitle
except ImportError:
    setproctitle = lambda x: None
import sys

def add_addons_argument(parser):
    """
    Add a common --addons argument to a parser.
    """
    parser.add_argument('--addons', metavar='ADDONS',
        **required_or_default('ADDONS',
                              'colon-separated list of paths to addons'))
def set_addons(args):
    """
    Turn args.addons into a list instead of a column-separated strings.
    Set openerp.toools.config accordingly.
    """
    import openerp.tools.config
    config = openerp.tools.config

    assert hasattr(args, 'addons')
    if args.addons:
        args.addons = args.addons.split(':')
    else:
        args.addons = []

    config['addons_path'] = ','.join(args.addons)

def get_addons_from_paths(paths, exclude):
    """
    Build a list of available modules from a list of addons paths.
    """
    exclude = exclude or []
    module_names = []
    for p in paths:
        if os.path.exists(p):
            names = [n for n in os.listdir(p) if os.path.isfile(os.path.join(p, n, '__openerp__.py')) and not n.startswith('.') and n not in exclude]
            names = filter(lambda a: os.path.isdir(os.path.join(p, a)), names)
            names = filter(lambda a: os.path.exists(os.path.join(p, a, '__openerp__.py')), names)
            module_names.extend(names)
        else:
            print "The addons path `%s` doesn't exist." % p
            sys.exit(1)
    return module_names

def required_or_default(name, h):
    """
    Helper to define `argparse` arguments. If the name is the environment,
    the argument is optional and draw its value from the environment if not
    supplied on the command-line. If it is not in the environment, make it
    a mandatory argument.
    """
    if os.environ.get('OPENERP_' + name.upper()):
        d = {'default': os.environ['OPENERP_' + name.upper()]}
    else:
        d = {'required': True}
    d['help'] = h + '. The environment variable OPENERP_' + \
                name.upper() + ' can be used instead.'
    return d

class Command(object):
    """
    Base class to create command-line tools. It must be inherited and the
    run() method overriden.
    """

    command_name = 'stand-alone'

    def __init__(self, subparsers=None):
        if subparsers:
            self.parser = parser = subparsers.add_parser(self.command_name,
	        description=self.__class__.__doc__)
        else:
            self.parser = parser = argparse.ArgumentParser(
                description=self.__class__.__doc__)

        parser.add_argument('-d', '--database', metavar='DATABASE',
            **required_or_default('DATABASE', 'the database to connect to'))
        parser.add_argument('-u', '--user', metavar='USER',
            **required_or_default('USER', 'the user login or ID. When using '
            'RPC, providing an ID avoid the login() step'))
        parser.add_argument('-p', '--password', metavar='PASSWORD',
            **required_or_default('PASSWORD', 'the user password')) # TODO read it from the command line or from file.

        parser.set_defaults(run=self.run_with_args)

    def run_with_args(self, args):
        self.args = args
        self.run()

    def run(self):
        print 'Stub Command.run().'

    @classmethod
    def stand_alone(cls):
        """
        A single Command object is a complete command-line program. See
        `openerp-command/stand-alone` for an example.
        """
        command = cls()
        args = command.parser.parse_args()
        args.run(args)