1#! /usr/bin/env python
2# encoding: utf-8
3# Thomas Nagy, 2011
4
5"""
6Obtain packages, unpack them in a location, and add associated uselib variables
7(CFLAGS_pkgname, LIBPATH_pkgname, etc).
8
9The default is use a Dependencies.txt file in the source directory.
10
11This is a work in progress.
12
13Usage:
14
15def options(opt):
16	opt.load('package')
17
18def configure(conf):
19	conf.load_packages()
20"""
21
22from waflib import Logs
23from waflib.Configure import conf
24
25try:
26	from urllib import request
27except ImportError:
28	from urllib import urlopen
29else:
30	urlopen = request.urlopen
31
32
33CACHEVAR = 'WAFCACHE_PACKAGE'
34
35@conf
36def get_package_cache_dir(self):
37	cache = None
38	if CACHEVAR in conf.environ:
39		cache = conf.environ[CACHEVAR]
40		cache = self.root.make_node(cache)
41	elif self.env[CACHEVAR]:
42		cache = self.env[CACHEVAR]
43		cache = self.root.make_node(cache)
44	else:
45		cache = self.srcnode.make_node('.wafcache_package')
46	cache.mkdir()
47	return cache
48
49@conf
50def download_archive(self, src, dst):
51	for x in self.env.PACKAGE_REPO:
52		url = '/'.join((x, src))
53		try:
54			web = urlopen(url)
55			try:
56				if web.getcode() != 200:
57					continue
58			except AttributeError:
59				pass
60		except Exception:
61			# on python3 urlopen throws an exception
62			# python 2.3 does not have getcode and throws an exception to fail
63			continue
64		else:
65			tmp = self.root.make_node(dst)
66			tmp.write(web.read())
67			Logs.warn('Downloaded %s from %s', tmp.abspath(), url)
68			break
69	else:
70		self.fatal('Could not get the package %s' % src)
71
72@conf
73def load_packages(self):
74	self.get_package_cache_dir()
75	# read the dependencies, get the archives, ..
76
77