~jfb-tempo-consulting/unifield-wm/sync-env-py3

« back to all changes in this revision

Viewing changes to scripts/common.py

  • Committer: Samus CTO
  • Date: 2012-08-28 12:38:31 UTC
  • Revision ID: cto@openerp.com-20120828123831-9dhavvjktnrl2p8d
[INIT]

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from sys import stdout, stderr, exit
 
2
 
 
3
from xmlrpclib import Fault
 
4
import openerplib
 
5
 
 
6
from tests.openerplib import db
 
7
 
 
8
import config
 
9
 
 
10
server, client = None, None
 
11
 
 
12
stdout.write("Establishing connections to the server... ")
 
13
stdout.flush();
 
14
try:
 
15
    server = openerplib.get_connection(hostname=config.server_host, port=config.server_port)
 
16
    server.get_service('db').list()
 
17
except:
 
18
    stdout.write("failed!\n")
 
19
    stderr.write("Unable to connect to OpenERP Sync Server at %s:%s\n" % (config.server_host, config.server_port,))
 
20
    exit(1)
 
21
try:
 
22
    client = openerplib.get_connection(hostname=config.client_host, port=config.client_port)
 
23
    client.get_service('db').list()
 
24
except:
 
25
    stdout.write("failed!\n")
 
26
    stderr.write("Unable to connect to OpenERP Sync Client at %s:%s\n" % (config.client_host, config.client_port,))
 
27
    exit(1)
 
28
stdout.write("done.\n")
 
29
 
 
30
def sync(test, db):
 
31
    if not db.get('sync.client.entity').sync():
 
32
        test.failed('Synchronization process of database "%s" failed!' % (db.db_name,))
 
33
        monitor = db.get('sync.monitor')
 
34
        ids = monitor.search([], 0, 1, '"end" desc')
 
35
        test.traceback = monitor.read(ids, ['error'])[0]['error']
 
36
        return False
 
37
    else:
 
38
        return True
 
39
 
 
40
class db_instance(type):
 
41
    instance = None
 
42
 
 
43
    def connect(cls, login=None, password=None, reconnect=False):
 
44
        if not cls.instance or reconnect:
 
45
            #stderr.write("\n!! Initialization required "+cls.__name__+" !!\n")
 
46
            u = login if login else config.user_login
 
47
            p = password if password else (config.admin_password if login == 'admin' else config.user_password)
 
48
            cls.instance = db(cls.server, cls.__name__, user=u, password=p)
 
49
            try:
 
50
                if hasattr(cls, 'synchro') and cls.synchro:
 
51
                    synchro_serv = cls.instance.get('sync.client.sync_server_connection')
 
52
                    ## Remove all previous connection
 
53
                    ids = synchro_serv.search([])
 
54
                    ## Create our owns
 
55
                    if ids: synchro_serv.write(ids, cls.synchro)
 
56
                    else: ids = [synchro_serv.create(cls.synchro)]
 
57
                    synchro_serv.connect(ids)
 
58
            except:
 
59
                pass
 
60
        return cls
 
61
 
 
62
    def __getattr__(cls, attr):
 
63
        if not cls.instance: raise AttributeError("Class %s is not connected!" % (cls.__name__,))
 
64
        real_attr = getattr(cls.instance, attr)
 
65
        return real_attr
 
66
 
 
67
class Synchro:
 
68
    __metaclass__ = db_instance
 
69
 
 
70
    server = server
 
71
 
 
72
    def __init__(self):
 
73
        raise Exception, 'This class must not be instanced'
 
74
 
 
75
class HQ:
 
76
    __metaclass__ = db_instance
 
77
 
 
78
    server = client
 
79
    synchro = {
 
80
        'protocol' : 'netrpc',
 
81
        'host' : config.server_host,
 
82
        #'port' : config.server_port, ## XMLRPC port
 
83
        'port' : 8070,
 
84
        'database' : 'Synchro',
 
85
        'login' : 'HQ',
 
86
        'password' : 'HQ',
 
87
    }
 
88
 
 
89
    def __init__(self):
 
90
        raise Exception, 'This class must not be instanced'
 
91
 
 
92
class Coordo:
 
93
    __metaclass__ = db_instance
 
94
 
 
95
    server = client
 
96
    synchro = {
 
97
        'protocol' : 'netrpc',
 
98
        'host' : config.server_host,
 
99
        #'port' : config.server_port, ## XMLRPC port
 
100
        'port' : 8070,
 
101
        'database' : 'Synchro',
 
102
        'login' : 'Coordo',
 
103
        'password' : 'Coordo',
 
104
    }
 
105
 
 
106
    server = client
 
107
    def __init__(self):
 
108
        raise Exception, 'This class must not be instanced'
 
109
 
 
110
class Project:
 
111
    __metaclass__ = db_instance
 
112
 
 
113
    server = client
 
114
    synchro = {
 
115
        'protocol' : 'netrpc',
 
116
        'host' : config.server_host,
 
117
        #'port' : config.server_port, ## XMLRPC port
 
118
        'port' : 8070,
 
119
        'database' : 'Synchro',
 
120
        'login' : 'Project',
 
121
        'password' : 'Project',
 
122
    }
 
123
 
 
124
    server = client
 
125
    def __init__(self):
 
126
        raise Exception, 'This class must not be instanced'
 
127
 
 
128
class Project2:
 
129
    __metaclass__ = db_instance
 
130
 
 
131
    server = client
 
132
    synchro = {
 
133
        'protocol' : 'netrpc',
 
134
        'host' : config.server_host,
 
135
        #'port' : config.server_port, ## XMLRPC port
 
136
        'port' : 8070,
 
137
        'database' : 'Synchro',
 
138
        'login' : 'Project2',
 
139
        'password' : 'Project2',
 
140
    }
 
141
 
 
142
    server = client
 
143
    def __init__(self):
 
144
        raise Exception, 'This class must not be instanced'
 
145