~openupgrade-committers/openupgrade-server/5.0

« back to all changes in this revision

Viewing changes to bin/addons/openupgrade_records/model/analysis_wizard.py

  • Committer: Stefan Rijnhart
  • Date: 2012-05-27 12:24:43 UTC
  • mfrom: (2175.1.3 openupgrade-server)
  • Revision ID: stefan@therp.nl-20120527122443-7lh1cvrm26wt8u6e
[MRG] Add openupgrade development module for easy analysis file generation

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    This module Copyright (C) 2012 OpenUpgrade community
 
6
#    https://launchpad.net/~openupgrade-committers
 
7
#
 
8
#    Contributors:
 
9
#    Therp BV <http://therp.nl>
 
10
#
 
11
#    This program is free software: you can redistribute it and/or modify
 
12
#    it under the terms of the GNU Affero General Public License as
 
13
#    published by the Free Software Foundation, either version 3 of the
 
14
#    License, or (at your option) any later version.
 
15
#
 
16
#    This program is distributed in the hope that it will be useful,
 
17
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
#    GNU Affero General Public License for more details.
 
20
#
 
21
#    You should have received a copy of the GNU Affero General Public License
 
22
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
23
#
 
24
##############################################################################
 
25
 
 
26
import os
 
27
from osv import osv, fields
 
28
 
 
29
try:
 
30
    from openerp.addons.openupgrade_records.lib import compare
 
31
    from openerp.openupgrade_records.lib import apriori
 
32
    from openerp.addons import get_module_path
 
33
except ImportError:
 
34
    from openupgrade_records.lib import compare
 
35
    from openupgrade_records.lib import apriori
 
36
    from addons import get_module_path
 
37
 
 
38
class openupgrade_analysis_wizard(osv.osv_memory):
 
39
    _name = 'openupgrade.analysis.wizard'
 
40
    _description = 'OpenUpgrade Analysis Wizard'
 
41
    _columns = {
 
42
        'server_config': fields.many2one(
 
43
            'openupgrade.comparison.config',
 
44
            'Configuration', required=True),
 
45
        'state': fields.selection(
 
46
            [('init', 'Init'), ('ready', 'Ready')], 'State',
 
47
            readonly=True),
 
48
        'log': fields.text('Log'),
 
49
        'write': fields.boolean(
 
50
            'Write files',
 
51
            help='Write analysis files to the module directories'
 
52
            ),
 
53
        }
 
54
    _defaults = {
 
55
        'state': lambda *a: 'init',
 
56
        'write': lambda *a: True,
 
57
        }
 
58
 
 
59
    def get_communication(self, cr, uid, ids, context=None):
 
60
        """ 
 
61
        Retrieve both sets of database representations,
 
62
        perform the comparison and register the resulting
 
63
        change set
 
64
        """
 
65
        def write_file(
 
66
            module, version, contents, filename='openupgrade_analysis.txt'):
 
67
            module_path = get_module_path(module)
 
68
            if not module_path:
 
69
                return "ERROR: could not find module path:\n"
 
70
            full_path = os.path.join(
 
71
                module_path, 'migrations', version)
 
72
            if not os.path.exists(full_path):
 
73
                try:
 
74
                    os.makedirs(full_path)
 
75
                except os.error:
 
76
                    return "ERROR: could not create migrations directory:\n"
 
77
            logfile = os.path.join(full_path, filename)
 
78
            try:
 
79
                f = open(logfile, 'w')
 
80
            except Exception:
 
81
                return "ERROR: could not open file %s for writing:\n" % logfile
 
82
            f.write(contents)
 
83
            f.close()
 
84
            return None
 
85
 
 
86
        wizard = self.browse(cr, uid, ids[0], context=context)
 
87
        # Retrieve connection and access methods
 
88
        conf_obj = self.pool.get('openupgrade.comparison.config')
 
89
        connection = conf_obj.get_connection(
 
90
            cr, uid, [wizard.server_config.id], context=context)
 
91
        remote_record_obj = connection.get_model('openupgrade.record')
 
92
        local_record_obj = self.pool.get('openupgrade.record')
 
93
        
 
94
        # Retrieve field representations and compare
 
95
        remote_records = remote_record_obj.field_dump(context)
 
96
        local_records = local_record_obj.field_dump(cr, uid, context)
 
97
        res = compare.compare_sets(remote_records, local_records)
 
98
 
 
99
        # Retrieve xml id representations and compare
 
100
        fields = ['module', 'model', 'name']
 
101
        local_xml_record_ids = local_record_obj.search(
 
102
            cr, uid, [('type', '=', 'xmlid')])
 
103
        remote_xml_record_ids = remote_record_obj.search(
 
104
            [('type', '=', 'xmlid')])
 
105
        local_xml_records = [
 
106
            dict([(field, x[field]) for field in fields])
 
107
            for x in local_record_obj.read(
 
108
                cr, uid, local_xml_record_ids, fields)
 
109
            ]
 
110
        remote_xml_records = [
 
111
            dict([(field, x[field]) for field in fields])
 
112
            for x in remote_record_obj.read(
 
113
                remote_xml_record_ids, fields)
 
114
            ]
 
115
        res_xml = compare.compare_xml_sets(
 
116
            remote_xml_records, local_xml_records)
 
117
 
 
118
        # reorder and output the result
 
119
        keys = list(set(res.keys() + res_xml.keys()))
 
120
        keys.remove('general')
 
121
        keys = ['general'] + keys
 
122
        module_obj = self.pool.get('ir.module.module')
 
123
        module_ids = module_obj.search(
 
124
            cr, uid, [('state', '=', 'installed')])
 
125
        modules = dict([(x['name'], x) for x in module_obj.read(cr, uid, module_ids)])
 
126
        general = ''
 
127
        for key in keys:
 
128
            contents = "---%s---\n" % key
 
129
            if key in res:
 
130
                contents += '\n'.join([unicode(line) for line in sorted(res[key])])
 
131
                if res[key]:
 
132
                    contents += '\n'
 
133
            if key in res_xml:
 
134
                contents += '\n'.join([unicode(line) for line in sorted(res_xml[key])])
 
135
                if res_xml[key]:
 
136
                    contents += '\n'
 
137
            if key == 'general':
 
138
                general += contents
 
139
                continue
 
140
            if key not in modules:
 
141
                general += (
 
142
                    "ERROR: module not in list of installed modules:\n"
 
143
                    + contents)
 
144
                continue
 
145
            if wizard.write:
 
146
                error = write_file(
 
147
                    key, modules[key]['installed_version'], contents)
 
148
                if error:
 
149
                    general += error
 
150
                    general += contents
 
151
            else:
 
152
                general += contents
 
153
        
 
154
        # Store the general log in as many places as possible ;-)
 
155
        if wizard.write and 'base' in modules:
 
156
            write_file(
 
157
                'base', modules['base']['installed_version'], general,
 
158
                'openupgrade_general_log.txt')
 
159
        self.pool.get('openupgrade.comparison.config').write(
 
160
            cr, uid, wizard.server_config.id,
 
161
            {'last_log': general})
 
162
        self.write(cr, uid, ids, {'state': 'ready', 'log': general})
 
163
 
 
164
        result = {
 
165
            'name': self._description,
 
166
            'view_type': 'form',
 
167
            'view_mode': 'form',
 
168
            'res_model': 'openupgrade.analysis.wizard',
 
169
            'domain': [],
 
170
            'context': context,
 
171
            'type': 'ir.actions.act_window',
 
172
            #'target': 'new',
 
173
            'res_id': ids[0],
 
174
            }
 
175
        return result
 
176
 
 
177
openupgrade_analysis_wizard()
 
178