summaryrefslogtreecommitdiffstats
path: root/tryton-module2apkbuild.py
diff options
context:
space:
mode:
Diffstat (limited to 'tryton-module2apkbuild.py')
-rw-r--r--tryton-module2apkbuild.py126
1 files changed, 126 insertions, 0 deletions
diff --git a/tryton-module2apkbuild.py b/tryton-module2apkbuild.py
new file mode 100644
index 0000000..51572d3
--- /dev/null
+++ b/tryton-module2apkbuild.py
@@ -0,0 +1,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)