wget.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python
  2. # ex:ts=4:sw=4:sts=4:et
  3. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  4. """
  5. BitBake 'Fetch' implementations
  6. Classes for obtaining upstream sources for the
  7. BitBake build tools.
  8. """
  9. # Copyright (C) 2003, 2004 Chris Larson
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License version 2 as
  13. # published by the Free Software Foundation.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License along
  21. # with this program; if not, write to the Free Software Foundation, Inc.,
  22. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. #
  24. # Based on functions from the base bb module, Copyright 2003 Holger Schurig
  25. import os, re
  26. import bb
  27. from bb import data
  28. from bb.fetch import Fetch
  29. from bb.fetch import FetchError
  30. from bb.fetch import uri_replace
  31. class Wget(Fetch):
  32. """Class to fetch urls via 'wget'"""
  33. def supports(self, url, ud, d):
  34. """
  35. Check to see if a given url can be fetched with cvs.
  36. """
  37. return ud.type in ['http','https','ftp']
  38. def localpath(self, url, ud, d):
  39. url = bb.encodeurl([ud.type, ud.host, ud.path, ud.user, ud.pswd, {}])
  40. ud.basename = os.path.basename(ud.path)
  41. ud.localfile = data.expand(os.path.basename(url), d)
  42. return os.path.join(data.getVar("DL_DIR", d, True), ud.localfile)
  43. def go(self, uri, ud, d):
  44. """Fetch urls"""
  45. def fetch_uri(uri, ud, d):
  46. if os.path.exists(ud.localpath):
  47. # file exists, but we didnt complete it.. trying again..
  48. fetchcmd = data.getVar("RESUMECOMMAND", d, 1)
  49. else:
  50. fetchcmd = data.getVar("FETCHCOMMAND", d, 1)
  51. bb.msg.note(1, bb.msg.domain.Fetcher, "fetch " + uri)
  52. fetchcmd = fetchcmd.replace("${URI}", uri)
  53. fetchcmd = fetchcmd.replace("${FILE}", ud.basename)
  54. bb.msg.debug(2, bb.msg.domain.Fetcher, "executing " + fetchcmd)
  55. ret = os.system(fetchcmd)
  56. if ret != 0:
  57. return False
  58. # check if sourceforge did send us to the mirror page
  59. if not os.path.exists(ud.localpath):
  60. os.system("rm %s*" % ud.localpath) # FIXME shell quote it
  61. bb.msg.debug(2, bb.msg.domain.Fetcher, "sourceforge.net send us to the mirror on %s" % ud.basename)
  62. return False
  63. return True
  64. localdata = data.createCopy(d)
  65. data.setVar('OVERRIDES', "wget:" + data.getVar('OVERRIDES', localdata), localdata)
  66. data.update_data(localdata)
  67. premirrors = [ i.split() for i in (data.getVar('PREMIRRORS', localdata, 1) or "").split('\n') if i ]
  68. for (find, replace) in premirrors:
  69. newuri = uri_replace(uri, find, replace, d)
  70. if newuri != uri:
  71. if fetch_uri(newuri, ud, localdata):
  72. return
  73. if fetch_uri(uri, ud, localdata):
  74. return
  75. # try mirrors
  76. mirrors = [ i.split() for i in (data.getVar('MIRRORS', localdata, 1) or "").split('\n') if i ]
  77. for (find, replace) in mirrors:
  78. newuri = uri_replace(uri, find, replace, d)
  79. if newuri != uri:
  80. if fetch_uri(newuri, ud, localdata):
  81. return
  82. raise FetchError(uri)