summaryrefslogtreecommitdiffstats
path: root/tryton-module2apkbuild.py
blob: 51572d38323601010dd8768770111a85b7a224d4 (plain)
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
#!/usr/bin/env python
#
# Licensed under GPLv3+
# 
# Copyright (c) 2011 Fabian Affolter <fabian at affolter-engineering.ch>
# 
# Generate APKBUILD files for Tryton modules, fetching the list of
# modules from the repository.
#
# Based the work of Hartmut Goebel <h.goebel@goebel-consult.de>
#

SERVER='http://downloads.tryton.org/'
CONTRIBUTOR = 'Fabian Affolter <fabian@affolter-engineering.ch>'
MAINTAINER = 'Fabian Affolter <fabian@affolter-engineering.ch>'
TEMPLATE_FILE = 'tryton-module2apkbuild.template'
APKBUILDNAME_PREFIX = 'trytond-'
APKBUILDNAME_TEMPLATE = APKBUILDNAME_PREFIX + '%s'

import optparse
import urllib2
import re
import os.path
import time
import tarfile

p_KERNEL_MODULES = re.compile('(ir|res|workflow|webdav)(\W|$)')

def download(url, download_dir):
    filename = os.path.basename(url) # todo: use urlparse here
    filename = os.path.join(download_dir, filename)
    if os.path.exists(filename):
        print 'Already downloaded:', filename, '(skipping)'
        return

    print 'Downloading', url
    infh = urllib2.urlopen(url)
    outfh = open(filename, 'wb')
    while 1:
        data = infh.read()
        if not data: break
        outfh.write(data)
    outfh.close()
    infh.close()

def get_requirements(download_dir, package):
    filename = os.path.join(download_dir, package)
    print filename
    tar = tarfile.open(filename)
    tryton_py = {}
    for fn in tar:
        if os.path.basename(fn.path) == '__tryton__.py':
            if tryton_py:
                raise tarfile.TarError('more than one __tryton__.py in archive')
            tryton_py = tar.extractfile(fn).read()
            tryton_py = eval(tryton_py)
    # the next lines are taken from a tryton module's setup.py
    requires = []
    for dep in tryton_py.get('depends', []):
        match = p_KERNEL_MODULES.match(dep)
        if match:
            continue
        else:
            requires.append(dep.replace('_', '-'))
    return requires

parser = optparse.OptionParser('%prog [options] MAJOR SOURCES_DIR')
parser.add_option('-D', '--no-download', action='store_true',
                  help='Do not download modules '
                       '(default is to download modules from %s)' % SERVER)

opts, args = parser.parse_args()
if len(args) < 2:
    parser.error('required argument missing')
elif len(args) > 2:
    parser.error('too many arguments')
major, download_dir = args

baseurl = SERVER + major
basepath = os.getcwd()
print 'Fetching module list from', baseurl
dirlist = urllib2.urlopen(baseurl).read()
packages = [href[6:-1]
          for href in re.findall(r'href="trytond_.*?\.tar.*?"', dirlist)]
template = open(TEMPLATE_FILE).read()

VALUES = {
    'contributor': CONTRIBUTOR,
    'maintainer': MAINTAINER,
    'tryton-version': major,
    'major': major,
    'version': None,
    'timestamp': time.strftime("%a %b %d %Y"),
    #'kernel': '2.2',
    }

for package in packages:
    if not opts.no_download:
        download(baseurl + '/' + package, download_dir)
    pkgname, version = package.split('-', 1)
    modname = pkgname[8:].replace('_', '-')
    apkbuildname = APKBUILDNAME_TEMPLATE % modname
    if os.path.exists(apkbuildname):
        continue
    requirements = [APKBUILDNAME_PREFIX + r
                    for r in get_requirements(download_dir, package)]
    
    values = VALUES.copy()
    values['version'] = version.split('.tar',1)[0]
    values['modname'] = modname
    values['pkgname'] = pkgname
    values['required-modules'] = ' '.join(requirements)
    
    spec = re.sub(r'@@(.*?)@@',
                  lambda matchobj: values[matchobj.group(1)],
                  template)

    os.mkdir(apkbuildname)
    os.system("newapkbuild %s" % apkbuildname)
    os.chdir(apkbuildname)
    open('APKBUILD', 'w').write(spec)
    print "%s written." % apkbuildname
    os.system("abuild checksum")
    commitmsg = "\"Initial APKBUILD for trytond-%s\"" % modname
    os.system("git add APKBUILD && git commit APKBUILD -m %s" % commitmsg)
    os.chdir(basepath)