~kaspar90/openerp/openerp

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
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/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', '6.0', 'trunk')
_DEFAULT_VERSION = '6.0'
_EXTRA_ADDONS_MAP = {
    '4.2': '4.2-extra-addons',
    '5.0': 'stable_5.0-extra-addons',
    '6.0': 'extra-6.0',
    'trunk': 'trunk-extra-addons',
}

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,)

    # map branch URLs according to version
    extraversion = _EXTRA_ADDONS_MAP[version]
    communityversion = 'trunk'
    webversion = version

    bzr_repository = {
        'server': (BASEURL + '~openerp/openobject-server/' + version, True),
        'client': (BASEURL + '~openerp/openobject-client/' + version, True),
        'addons': (BASEURL + '~openerp/openobject-addons/' + version, True),
        'addons-extra': (BASEURL + '~openerp-commiter/openobject-addons/' + extraversion, False),
        'addons-community': (BASEURL + '~openerp-community/openobject-addons/' + communityversion + '-addons-community', False),
        'web': (BASEURL + '~openerp/openobject-client-web/' + webversion, True),
    }

    bzr_links = {
        '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, has_tags) 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 and (not revision.startswith('tag:') or has_tags):
            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://test.openobject.com')
    log('='*79)

#
# Testing bzr send
#

if __name__ == '__main__':
    import optparse
    description = """Tool that allows you to get the last sources of openerp on launchpad.
It downloads all branches, and create symlinks for addons in the server. By
default, it loads the latest stable version.
"""
    parser = optparse.OptionParser(description=description, 
                                   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, instead of branches', action='store_true', default=False)
    parser.add_option('-v', dest="version", default=_DEFAULT_VERSION, type="choice", choices=_VERSIONS, help="Specify the version to take (trunk, 4.2, 5.0, 6.0)")
    parser.add_option('-r', dest="revision", default=None, help="Specify the revision to take. (useful 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)