~openerp-groupes/openobject-server/6.0-fix-setup-windows

« back to all changes in this revision

Viewing changes to bin/tools/config.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-3f10ee12cea3c4c75cef44ab04ad33ef47432907
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#
 
5
# $Id: config.py 996 2005-07-22 10:40:10Z nicoe $
 
6
#
 
7
# WARNING: This program as such is intended to be used by professional
 
8
# programmers who take the whole responsability of assessing all potential
 
9
# consequences resulting from its eventual inadequacies and bugs
 
10
# End users who are looking for a ready-to-use solution with commercial
 
11
# garantees and support are strongly adviced to contract a Free Software
 
12
# Service Company
 
13
#
 
14
# This program is Free Software; you can redistribute it and/or
 
15
# modify it under the terms of the GNU General Public License
 
16
# as published by the Free Software Foundation; either version 2
 
17
# of the License, or (at your option) any later version.
 
18
#
 
19
# This program is distributed in the hope that it will be useful,
 
20
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
22
# GNU General Public License for more details.
 
23
#
 
24
# You should have received a copy of the GNU General Public License
 
25
# along with this program; if not, write to the Free Software
 
26
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
27
#
 
28
##############################################################################
 
29
 
 
30
import ConfigParser,optparse,os,sys
 
31
 
 
32
 
 
33
class configmanager(object):
 
34
        def __init__(self, fname=None):
 
35
                self.options = {
 
36
                        'verbose': False,
 
37
                        'interface': '',        # this will bind the server to all interfaces
 
38
                        'port': '8069',
 
39
                        'db_host': False,
 
40
                        'db_port': False,
 
41
                        'db_name': 'terp',
 
42
                        'db_user': False,
 
43
                        'db_password': False,
 
44
                        'reportgz': False,
 
45
                        'netrpc': True,
 
46
                        'xmlrpc': True,
 
47
                        'soap': False,
 
48
                        'translate_in': None,
 
49
                        'translate_out': None,
 
50
                        'language': None,
 
51
                        'pg_path': None,
 
52
                        'admin_passwd': 'admin',
 
53
                        'addons_path': None,
 
54
                        'root_path': None,
 
55
                        'debug_mode': False,
 
56
                        'pidfile': None,
 
57
                        'logfile': None,
 
58
                        'secure': False,
 
59
                        'smtp_server': 'localhost',
 
60
                        'stop_after_init': False,   # this will stop the server after initialization
 
61
                }
 
62
 
 
63
                parser = optparse.OptionParser(version=tinyerp_version_string)
 
64
                
 
65
                parser.add_option("-c", "--config", dest="config", help="specify alternate config file")
 
66
                parser.add_option("-s", "--save", action="store_true", dest="save", default=False, help="save configuration to ~/.terp_serverrc")
 
67
                parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="enable debugging")
 
68
                parser.add_option("--pidfile", dest="pidfile", help="file where the server pid will be stored")
 
69
                parser.add_option("--logfile", dest="logfile", help="file where the server log will be stored")
 
70
                
 
71
                parser.add_option("-n", "--interface", dest="interface", help="specify the TCP IP address")
 
72
                parser.add_option("-p", "--port", dest="port", help="specify the TCP port")
 
73
                
 
74
                parser.add_option("-i", "--init", dest="init", help="init a module (use \"all\" for all modules)")
 
75
                parser.add_option("--without-demo", dest="without_demo", help="load demo data for a module (use \"all\" for all modules)", default=False)
 
76
                parser.add_option("-u", "--update", dest="update", help="update a module (use \"all\" for all modules)")
 
77
                # stops the server from launching after initialization
 
78
                parser.add_option("--stop-after-init", action="store_true", dest="stop_after_init", default=False, help="stop the server after it initializes")
 
79
                parser.add_option('--debug', dest='debug_mode', action='store_true', default=False, help='enable debug mode')
 
80
                parser.add_option("-S", "--secure", dest="secure", action="store_true", help="launch server over https instead of http", default=False)
 
81
                parser.add_option('--smtp', dest='smtp_server', default='', help='specify the SMTP server for sending mail')
 
82
                
 
83
                group = optparse.OptionGroup(parser, "Modules related options")
 
84
                group.add_option("-g", "--upgrade", action="store_true", dest="upgrade", default=False, help="Upgrade/install/uninstall modules")
 
85
 
 
86
                group = optparse.OptionGroup(parser, "Database related options")
 
87
                group.add_option("-d", "--database", dest="db_name", help="specify the database name")
 
88
                group.add_option("-r", "--db_user", dest="db_user", help="specify the database user name")
 
89
                group.add_option("-w", "--db_password", dest="db_password", help="specify the database password") 
 
90
                group.add_option("--pg_path", dest="pg_path", help="specify the pg executable path") 
 
91
                group.add_option("--db_host", dest="db_host", help="specify the database host") 
 
92
                group.add_option("--db_port", dest="db_port", help="specify the database port") 
 
93
                parser.add_option_group(group)
 
94
 
 
95
                group = optparse.OptionGroup(parser, "Internationalisation options",
 
96
                        "Use these options to translate Tiny ERP to another language."
 
97
                        "See i18n section of the user manual. Option '-l' is mandatory.")
 
98
 
 
99
                group.add_option('-l', "--language", dest="language", help="specify the language of the translation file. Use it with --i18n-export and --i18n-import")
 
100
                group.add_option("--i18n-export", dest="translate_out", help="export all sentences to be translated to a CSV file and exit")
 
101
                group.add_option("--i18n-import", dest="translate_in", help="import a CSV file with translations and exit")
 
102
                group.add_option("--modules", dest="translate_modules", help="specify modules to export. Use in combination with --i18n-export")
 
103
                parser.add_option_group(group)
 
104
 
 
105
                (opt, args) = parser.parse_args()
 
106
 
 
107
                if (opt.translate_in or opt.translate_out) and (not opt.language or not opt.db_name):
 
108
                        print "Error: the i18n-import and i18n-export options cannot be used without the language (-l) and database (-d) options"
 
109
                        sys.exit(2)
 
110
 
 
111
                # place/search the config file on Win32 near the server installation
 
112
                # (../etc from the server)
 
113
                # if the server is run by an unprivileged user, he has to specify location of a config file where he has the rights to write,
 
114
                # else he won't be able to save the configurations, or even to start the server...
 
115
                if os.name == 'nt':
 
116
                        rcfilepath = os.path.join(os.path.split(os.path.split(os.path.abspath(sys.argv[0]))[0])[0], 'etc', 'tinyerp-server.conf')
 
117
                else:
 
118
                        rcfilepath = os.path.expanduser('~/.terp_serverrc')
 
119
 
 
120
                self.rcfile = fname or opt.config or os.environ.get('TERP_SERVER') or rcfilepath
 
121
                self.load()
 
122
                
 
123
 
 
124
                # Verify that we want to log or not, if not the output will go to stdout
 
125
                if self.options['logfile'] in ('None', 'False'):
 
126
                    self.options['logfile'] = False
 
127
                # the same for the pidfile
 
128
                if self.options['pidfile'] in ('None', 'False'):
 
129
                    self.options['pidfile'] = False
 
130
                
 
131
                for arg in ('interface', 'port', 'db_name', 'db_user', 'db_password', 'db_host',
 
132
                                'db_port', 'logfile', 'pidfile', 'secure', 'smtp_server'):
 
133
                        if getattr(opt, arg):
 
134
                                self.options[arg] = getattr(opt, arg)
 
135
 
 
136
                for arg in ('language', 'translate_out', 'translate_in', 
 
137
                        'upgrade', 'verbose', 'debug_mode', 
 
138
                        'stop_after_init', 'without_demo'):
 
139
                        self.options[arg] = getattr(opt, arg)
 
140
                        
 
141
                if not self.options['root_path'] or self.options['root_path']=='None':
 
142
                        self.options['root_path'] = os.path.abspath(os.path.dirname(sys.argv[0]))
 
143
                if not self.options['addons_path'] or self.options['addons_path']=='None':
 
144
                        self.options['addons_path'] = os.path.join(self.options['root_path'], 'addons')
 
145
 
 
146
                init = {}
 
147
                if opt.init:
 
148
                        for i in opt.init.split(','):
 
149
                                init[i] = 1
 
150
                self.options['init'] = init
 
151
                self.options["demo"] = not opt.without_demo and self.options['init'] or {}
 
152
 
 
153
                update = {}
 
154
                if opt.update:
 
155
                        for i in opt.update.split(','):
 
156
                                update[i] = 1
 
157
                self.options['update'] = update
 
158
 
 
159
                self.options['translate_modules'] = opt.translate_modules and opt.translate_modules.split(',') or ['all']
 
160
                if opt.pg_path:
 
161
                        self.options['pg_path'] = opt.pg_path
 
162
 
 
163
                if self.options.get('language', False):
 
164
                        assert len(self.options['language'])<=5, 'ERROR: The Lang name must take max 5 chars, Eg: -lfr_BE'
 
165
                if opt.save:
 
166
                        self.save()
 
167
 
 
168
        def load(self):
 
169
                p = ConfigParser.ConfigParser()
 
170
                try:
 
171
                        p.read([self.rcfile])
 
172
                        for (name,value) in p.items('options'):
 
173
                                if value=='True' or value=='true':
 
174
                                        value = True
 
175
                                if value=='False' or value=='false':
 
176
                                        value = False
 
177
                                self.options[name] = value
 
178
                except IOError:
 
179
                        pass
 
180
                except ConfigParser.NoSectionError:
 
181
                        pass
 
182
 
 
183
        def save(self):
 
184
                p = ConfigParser.ConfigParser()
 
185
                p.add_section('options')
 
186
                for o in [opt for opt in self.options.keys() if opt not in ('version','language','translate_out','translate_in','init','update')]:
 
187
                        print o, self.options[o]
 
188
                        p.set('options', o, self.options[o])
 
189
 
 
190
                # try to create the directories and write the file
 
191
                try:
 
192
                        if not os.path.exists(os.path.dirname(self.rcfile)):
 
193
                                os.makedirs(os.path.dirname(self.rcfile))
 
194
                        try:
 
195
                                p.write(file(self.rcfile, 'w'))
 
196
                        except IOError:
 
197
                                sys.stderr.write("ERROR: couldn't write the config file\n")
 
198
 
 
199
                except OSError:
 
200
                        # what to do if impossible?
 
201
                        sys.stderr.write("ERROR: couldn't create the config directory\n")
 
202
 
 
203
                                
 
204
 
 
205
        def get(self, key, default=None):
 
206
                return self.options.get(key, default)
 
207
 
 
208
        def __setitem__(self, key, value):
 
209
                self.options[key] = value
 
210
 
 
211
        def __getitem__(self, key):
 
212
                return self.options[key]
 
213
 
 
214
config = configmanager()
 
215
 
 
216