package.py 1.6 KB

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