示例#1
0
def migration(OPTIONS):
    BASECIBLE = OPTIONS.dbc
    BASESOURCE = OPTIONS.dbs



    if OPTIONS.userc == 'terp':
        OPTIONS.userc = "admin"
        OPTIONS.passwdc = "admin"

    if OPTIONS.dbc == 'terp':
        OPTIONS.dbc = OPTIONS.dbs
    print
    print "-" * 80
    print
    X = 1
    START = datetime.now()
    if OPTIONS.createdb:
        try:
            os.remove("/media/mint/migration-%s.sqlite" % BASESOURCE)
        except:
            pass
        fname = "/tmp/%s_model.sql" % BASECIBLE
        if os.path.exists(fname):

            os.system('dropdb %s ' % BASECIBLE)

            os.system("createdb --encoding='unicode' %s " % BASECIBLE)
            load_cmd = 'psql -f %s %s >/tmp/load.log' % (fname, BASECIBLE)
            retval = os.system(load_cmd)
            if retval != 0:
                sys.exit(1)

            CONNECTION_CIBLE = openerp_connectionv7.openerp(OPTIONS.protocolec + '://',
                                                            OPTIONS.hostc, OPTIONS.portc, BASECIBLE, 'admin',
                                                            OPTIONS.passwdadmin)
            connectionsource = openerp_connection.openerp(OPTIONS.protocoles +
                                                          '://', OPTIONS.hosts, OPTIONS.ports,
                                                          OPTIONS.dbs, OPTIONS.users,
                                                          OPTIONS.passwds)
            migration = MigrationLib(connectionsource, CONNECTION_CIBLE, X, OPTIONS)
            migration.connect_pg()
            migration.passwordsource = OPTIONS.passwds
            migration.controle_data()
            migration.clean_source_db()
        else:
            recree_db(OPTIONS)
            CONNECTION_CIBLE = openerp_connectionv7.openerp(OPTIONS.protocolec + '://',
                                                            OPTIONS.hostc, OPTIONS.portc, BASECIBLE, 'admin',
                                                            OPTIONS.passwdadmin)
            connectionsource = openerp_connection.openerp(OPTIONS.protocoles +
                                                          '://', OPTIONS.hosts, OPTIONS.ports, OPTIONS.dbs,
                                                          OPTIONS.users,
                                                          OPTIONS.passwds)

            migration = MigrationLib(connectionsource, CONNECTION_CIBLE,
                                     X, OPTIONS)
            migration.passwordsource = OPTIONS.passwds
            migration.connect_pg()
            migration.controle_data()
            migration.clean_source_db()
            print u"base créé ", datetime.now() - START
            CONNECTION_CIBLE.object.execute(CONNECTION_CIBLE.dbname, CONNECTION_CIBLE.uid,
                                            CONNECTION_CIBLE.pwd, 'base.module.update', 'create', {})
            CONNECTION_CIBLE.object.execute(CONNECTION_CIBLE.dbname, CONNECTION_CIBLE.uid,
                                            CONNECTION_CIBLE.pwd, 'base.module.update', 'update_module', [1])
            DB_MODULE = openerp_connectionv7.module(CONNECTION_CIBLE)
            DB_MODULE.install('multi_company')
            DB_MODULE.install('auth_crypt')
            DB_MODULE.install('uni_otp')
            DB_MODULE.install('uni_account')
            DB_MODULE.install('base_vat')
            DB_MODULE.install('uni_features')
            source_module_ids = connectionsource.search('ir.module.module',
                                                        [('state', '=', 'installed')])
            cible_module_ids = CONNECTION_CIBLE.search('ir.module.module', [])
            cible_modules = {}
            for cible_module_id in cible_module_ids:
                cible_module = CONNECTION_CIBLE.read('ir.module.module',
                                                     cible_module_id)
                cible_modules[cible_module['name']] = cible_module_id
            source_modules = {}
            for source_module_id in source_module_ids:
                source_module = connectionsource.read('ir.module.module',
                                                      source_module_id)
                if "hr_timesheet_invoice" in source_module['name']:
                    migration.hr = True
                    print "HR OK"
                if source_module['name'] == 'school':
                    source_module['name'] = 'uni_school'
                source_modules[source_module['name']] = source_module_id
            for source_module in source_modules:
                if source_module in cible_modules:
                    cible_module_id = cible_modules[source_module]
                    cible_module = CONNECTION_CIBLE.read('ir.module.module',
                                                         cible_module_id)
                    if cible_module['state'] != 'installed':
                        DB_MODULE.install(module_id=[cible_module_id])
            print "Fin installation modules"
            actserver_ids = CONNECTION_CIBLE.search('ir.actions.server', [], 0, 1000)
            CONNECTION_CIBLE.unlink('ir.actions.server', actserver_ids)
    else:
        CONNECTION_CIBLE = openerp_connectionv7.openerp(OPTIONS.protocolec + '://',
                                                        OPTIONS.hostc, OPTIONS.portc, BASECIBLE, 'admin',
                                                        OPTIONS.passwdadmin)
        connectionsource = openerp_connection.openerp(OPTIONS.protocoles +
                                                      '://', OPTIONS.hosts, OPTIONS.ports, OPTIONS.dbs, OPTIONS.users,
                                                      OPTIONS.passwds)
        migration = MigrationLib(connectionsource, CONNECTION_CIBLE, X, OPTIONS)
        migration.connect_pg()

        migration.controle_data()
        migration.clean_source_db()
    BASESOURCE = OPTIONS.dbs

    try:
        print
        print "-" * 80
        print
        print "Migration %s" % BASESOURCE
        print
        try:
            migration.pass_admin_new_base = 'uniforme'
            migration.load_fields()
            #if OPTIONS.createdb == 'true':
            vals = migration.get_values(1, 'res.company', ['name'])
            if 'account_id' in vals:
                vals['account_id'] = 1
            vals['name'] = BASESOURCE
            if X == 1:
                if 'partner_id' in vals:
                    vals['partner_id'] = 1
                CONNECTION_CIBLE.write('res.company', 1, vals)
                company_id = 1
            elif not CONNECTION_CIBLE.search('res.company', [('name', '=', BASESOURCE)]):
                vals['partner_id'] = CONNECTION_CIBLE.create('res.partner', {'name': BASESOURCE})
                company_id = CONNECTION_CIBLE.create('res.company', vals)

        except BaseException, e:
            EXC_TYPE, EXC_VALUE, EXC_TRACEBACK = sys.exc_info()
            print "*** print_exception:"
            traceback.print_exception(EXC_TYPE, EXC_VALUE, EXC_TRACEBACK,
                                      limit=2, file=sys.stdout)
            sys.exit()

        if OPTIONS.module == 'all' or OPTIONS.module == 'base':
            migration.migre_base_data()
        if OPTIONS.module == 'all' or OPTIONS.module == 'partner':
            migration.migre_partner()
        if OPTIONS.module == 'all' or OPTIONS.module == 'product':
            migration.migre_product()
        if OPTIONS.module == 'all' or OPTIONS.module == 'compta':
            migration.init()
            migration.migre_compta()
        migration.controle_tables()
        migration.deconnect_pg()
        END = datetime.now()
        print "Fin Migration %s en %s" % (BASESOURCE, END - START)
        mailmsg = "Fin Migration %s en %s" % (BASESOURCE, END - START)
        mail_envoye = MIMEText(str(mailmsg))
        mail_envoye['From'] = "*****@*****.**"
        mail_envoye['Subject'] = "Migration %s " % BASESOURCE
        mail_envoye['To'] = '*****@*****.**'
        #envoi = SMTP('192.168.12.15')
        envoi = SMTP('smtp.free.fr')
        envoi.sendmail(mail_envoye['From'], ['*****@*****.**'], mail_envoye.as_string())
        X += 1
parser.add_option("-U", "--user", dest="user", default='terp', help="User Openerp")
parser.add_option("-W", "--passwd", dest="passwd", default='terp', help="mot de passe Openerp ")
parser.add_option("-H", "--host", dest="host", default='127.0.0.1', help="Adresse  Serveur")
parser.add_option("-p", "--port", dest="port", default='8069', help="port du serveur")
parser.add_option("-P", "--protocole", dest="protocole", default='https', help="protocole http/https")
(options, args) = parser.parse_args()

try:
    connect_db = openerp_db(options.protocole + '://', options.host, options.port)
    connection = openerp(options.protocole + '://', options.host, options.port, options.db, options.user,
                         options.passwd)
except Exception, e:
    print "Erreur de connection ", e
    sys.exit(1)

db_module = module(connection)

#Mise à jour de la liste des modules
if options.action == 'update_list':
    db_module.update_list()
elif options.action == "update_all":
    db_module.update_all()
elif options.action == "clean_all":
    db_module.clean_all()
elif options.action == 'update':
    if options.module == "none":
        print "Vous devez donner un nom de module pour l'action update"
        sys.exit(1)
    else:
        db_module.update(options.module, force=options.force)
elif options.action == 'install':