Merge lp:~syleam/openobject-server/trunk-interactive_mode into lp:openobject-server

Proposed by Sylvain Garancher
Status: Rejected
Rejected by: Vo Minh Thu
Proposed branch: lp:~syleam/openobject-server/trunk-interactive_mode
Merge into: lp:openobject-server
Diff against target: 75 lines (+22/-4)
2 files modified
openerp-server (+18/-2)
openerp/tools/config.py (+4/-2)
To merge this branch: bzr merge lp:~syleam/openobject-server/trunk-interactive_mode
Reviewer Review Type Date Requested Status
OpenERP Core Team Pending
Review via email: mp+92148@code.launchpad.net

Description of the change

Add two command line arguments for openerp-server :
"--execute=path/script.py" : Executes a custom python script.
"--interactive" : Turns on python interactive console just after server initialization.

These two arguments can be used together. The interactive console is activated after script execution, so we can use a script to initialize some variables for interactive use.

Using one or both of them implies --stop-after-init : Services are not initialized, to allows using these modes without stopping a running instance, and the process stops when we quit the interactive console or when the script ends if interactive console was not started.

If the "--database" argument is supplied, a database is loaded (only the first is used for comma separated list of databases names) and some variables are initialized :
- db : Connection to the database.
- pool : Pooler on the database.
- cr : Cursor on the database. Automatically closed at end of execution, but no commit is done. You have to commit manually if needed.
- uid : integer 1 (admin user's id).

To post a comment you must log in.
Revision history for this message
Vo Minh Thu (thu) wrote :

I don't want to add feature the `openerp-server` anymore. The startup script is already difficult to read and future-proof.

Instead I prefer you create your own startup script. If you want to share it, you can propose a similar feature in the openerp-command project: https://launchpad.net/openerp-command. It is not yet officially supported but was done to accept things like this one.

That being said, I think I would prefer to make your 'execute' option the other way around: instead of passing a script to be executed, the script is importing the `openerp` library and do whatever it wants (and thus be no longer an option).

Unmerged revisions

4020. By Sylvain Garancher

[IMP] Add '--execute' and '--interactive' command line arguments

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'openerp-server'
2--- openerp-server 2012-02-06 20:31:51 +0000
3+++ openerp-server 2012-02-08 22:02:18 +0000
4@@ -38,6 +38,7 @@
5 import threading
6 import traceback
7 import time
8+import code
9
10 import openerp
11 __author__ = openerp.release.author
12@@ -234,7 +235,7 @@
13 import_translation()
14 sys.exit(0)
15
16- if not config["stop_after_init"]:
17+ if not (config["stop_after_init"] or config['execute'] or config['interactive_mode']):
18 # Some module register themselves when they are loaded so we need the
19 # services to be running before loading any registry.
20 openerp.service.start_services()
21@@ -258,7 +259,22 @@
22 for dbname in config['db_name'].split(','):
23 preload_registry(dbname)
24
25- if config["stop_after_init"]:
26+ if config['execute'] or config['interactive_mode']:
27+ if config['db_name']:
28+ db, pool = openerp.pooler.get_db_and_pool(config['db_name'].split(',')[0])
29+ cr = db.cursor()
30+ uid = 1
31+
32+ if config['execute']:
33+ execfile(config['execute'], globals(), locals())
34+ if config['interactive_mode']:
35+ code.interact(local=locals())
36+
37+ if config['db_name']:
38+ cr.close()
39+
40+ sys.exit(0)
41+ elif config["stop_after_init"]:
42 sys.exit(0)
43
44 setup_pid_file()
45
46=== modified file 'openerp/tools/config.py'
47--- openerp/tools/config.py 2012-02-06 23:02:15 +0000
48+++ openerp/tools/config.py 2012-02-08 22:02:18 +0000
49@@ -73,7 +73,7 @@
50 # Not exposed in the configuration file.
51 self.blacklist_for_save = set(
52 ['publisher_warranty_url', 'load_language', 'root_path',
53- 'init', 'save', 'config', 'update', 'stop_after_init'])
54+ 'init', 'save', 'config', 'update', 'execute', 'interactive_mode', 'stop_after_init'])
55
56 # dictionary mapping option destination (keys in self.options) to MyOptions.
57 self.casts = {}
58@@ -258,6 +258,8 @@
59 group.add_option('--debug', dest='debug_mode', action='store_true', my_default=False, help='enable debug mode')
60 group.add_option("--stop-after-init", action="store_true", dest="stop_after_init", my_default=False,
61 help="stop the server after its initialization")
62+ group.add_option('--execute', dest='execute', help='allows to execute a script on the selected database')
63+ group.add_option('--interactive', dest='interactive_mode', action='store_true', default=False, help='enable interactive mode')
64 group.add_option("-t", "--timezone", dest="timezone", my_default=False,
65 help="specify reference timezone for the server (e.g. Europe/Brussels")
66 group.add_option("--osv-memory-count-limit", dest="osv_memory_count_limit", my_default=False,
67@@ -389,7 +391,7 @@
68 # if defined but None take the configfile value
69 keys = [
70 'language', 'translate_out', 'translate_in', 'overwrite_existing_translations',
71- 'debug_mode', 'smtp_ssl', 'load_language',
72+ 'debug_mode', 'execute', 'interactive_mode', 'smtp_ssl', 'load_language',
73 'stop_after_init', 'logrotate', 'without_demo', 'netrpc', 'xmlrpc', 'syslog',
74 'list_db', 'xmlrpcs',
75 'test_file', 'test_disable', 'test_commit', 'test_report_directory',