~openerp/openerp/openerp-server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/python

__all__ = ['update_openerp']

import os
import shutil
import glob
import bzrlib.builtins
from bzrlib.plugins import launchpad
from bzrlib.branch import Branch
from bzrlib.errors import NotBranchError
from bzrlib.revisionspec import RevisionSpec

def run_cmd(cmdname, *args, **kwargs):
    f = getattr(bzrlib.builtins, 'cmd_' + cmdname)()
    if hasattr(f, '_setup_outf'):
        # old versions of bzr does not have this function   
        # this function must be called to avoid a exception in bzr code
        f._setup_outf()
    return f.run(*args, **kwargs)

_VERSIONS = ('4.2', '5.0', 'trunk')
_DEFAULT_VERSION = '5.0'

def update_openerp(dest_dir, version=_DEFAULT_VERSION, lplogin=None, export=False, revision=None, verbose=False):
    """
        if lplogin == None -> make a branch instead of a checkout
        if export == True -> bzr export 
        if revision is provided, get the branches at this revision
            more information with:
                $> bzr help revisionspec
    """
    def log(msg):
        if verbose:
            print msg

    if version not in _VERSIONS:
        raise Exception('Unknown version')
    dest_dir = dest_dir or '.'

    branch = lplogin is None
    if branch:
        BASEURL = 'lp:'
    else:
        BASEURL = 'bzr+ssh://%s@bazaar.launchpad.net/' % (lplogin,)

    # for now, there is no 5.0-extra-addons -> use trunk instead
    webversion = version == '5.0' and '5.0.2' or version  

    bzr_repository = {
        'server': BASEURL + '~openerp/openobject-server/' + version,
        'addons': BASEURL + '~openerp/openobject-addons/' + version,
        'odms-addons': BASEURL + '~openerp/openobject-addons/' + version + '-odms',
        'web': BASEURL + '~openerp/openobject-client-web/' + webversion,
    }

    bzr_links = {
        'addons/*': 'server/bin/addons/',
        'odms-addons/*': 'server/bin/addons/',
    }

    if branch:
        cmd = {'new': lambda u, l, r: run_cmd('branch', u, l, revision=r),
               'update': lambda u, l, r: run_cmd('pull', u, directory=l, overwrite=True, revision=r),
        }
    else:
        cmd = {'new': lambda u, l, r: run_cmd('checkout', u, l, lightweight=True, revision=r),
               'update': lambda u, l, r: run_cmd('update', l), # no revision option :(
        }
    cmd['export'] = lambda u, l, r: run_cmd('export', l, u, revision=r)

    msg = "%(status)s %(type)s of %(from)s into %(to)s"

    if not os.path.exists(dest_dir):
        os.makedirs(dest_dir)

    for local,bzrdir in bzr_repository.items():
        local = os.path.join(dest_dir, local)
        typ = ['checkout', 'branch'][branch]
        if export:
            if os.path.exists(local):
                shutil.rmtree(local)
            status = 'export'
            typ = 'sources'
        else:
            try:
                b = Branch.open(local)
                # FIXME check that the current workingDirectory is a branch or a checkout
                status = 'update'
            except NotBranchError:
                status = 'new'
        
        frm = bzrdir
        rev = None
        if revision:
            frm = '%s (%s)' % (bzrdir, revision)
            rev = RevisionSpec.from_string(revision)

        log(msg % {'status': status, 'type': typ, 'to': local, 'from': frm})
                
        cmd[status](bzrdir, local, rev and [rev] or None)

    # Doing symlinks
    log('(Re)Computing Symbolic links...')
    for src2,dest2 in bzr_links.items():
        src2 = os.path.join(dest_dir, src2)
        dest2 = os.path.join(dest_dir, dest2)
        for src in glob.glob(src2):
            dest = os.path.join(dest2, os.path.basename(src))
            if not os.path.isdir(dest):
                os.symlink(os.path.realpath(src), dest)

    log('='*79)
    log('Sources of OpenERP have been installed. If you develop new features,')
    log('you can get more information on how to contribute to the project here:')
    log('\thttp://openerp.com/community-process.html')
    log('='*79)


if __name__ == '__main__':
    import optparse

    parser = optparse.OptionParser(description="Tool that allow you to get the last sources of openerp on launchpad", 
                                   usage="%prog [options] [directory]")
    parser.add_option('--checkout', dest='lplogin', help="Specify the launchpad login to make a checkout instead of a branch")
    parser.add_option('--export', dest='export', help='Make an export of the sources', action='store_true', default=False)
    parser.add_option('-v', dest="version", default=_DEFAULT_VERSION, type="choice", choices=_VERSIONS, help="Specify the version to take")
    parser.add_option('-r', dest="revision", default=None, help="Specify the revision to take. (usefull to take a specific TAG or to specify a DATE)")
    parser.add_option('-q', '--quiet', dest='quiet', help='Suppress the output', action='store_true', default=False)

    opt, args = parser.parse_args()
    dest_dir = args and args[0] or '.'

    update_openerp(dest_dir, opt.version, opt.lplogin, opt.export, opt.revision, not opt.quiet)