2 # ex:ts=4:sw=4:sts=4:et
3 # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
5 BitBake 'Fetch' implementations
7 Classes for obtaining upstream sources for the
10 Copyright (C) 2003, 2004 Chris Larson
12 This program is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free Software
14 Foundation; either version 2 of the License, or (at your option) any later
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 USA.
25 Based on functions from the base bb module, Copyright 2003 Holger Schurig
32 class FetchError(Exception):
33 """Exception raised when a download fails"""
35 class NoMethodError(Exception):
36 """Exception raised when there is no method to obtain a supplied url or set of urls"""
38 class MissingParameterError(Exception):
39 """Exception raised when a fetch method is missing a critical parameter in the url"""
41 class MD5SumError(Exception):
42 """Exception raised when a MD5SUM of a file does not match the expected one"""
44 def uri_replace(uri, uri_find, uri_replace, d):
45 # bb.note("uri_replace: operating on %s" % uri)
46 if not uri or not uri_find or not uri_replace:
47 bb.debug(1, "uri_replace: passed an undefined value, not replacing")
48 uri_decoded = list(bb.decodeurl(uri))
49 uri_find_decoded = list(bb.decodeurl(uri_find))
50 uri_replace_decoded = list(bb.decodeurl(uri_replace))
51 result_decoded = ['','','','','',{}]
52 for i in uri_find_decoded:
53 loc = uri_find_decoded.index(i)
54 result_decoded[loc] = uri_decoded[loc]
56 if type(i) == types.StringType:
58 if (re.match(i, uri_decoded[loc])):
59 result_decoded[loc] = re.sub(i, uri_replace_decoded[loc], uri_decoded[loc])
60 if uri_find_decoded.index(i) == 2:
62 localfn = bb.fetch.localpath(uri, d)
64 result_decoded[loc] = os.path.dirname(result_decoded[loc]) + "/" + os.path.basename(bb.fetch.localpath(uri, d))
65 # bb.note("uri_replace: matching %s against %s and replacing with %s" % (i, uri_decoded[loc], uri_replace_decoded[loc]))
67 # bb.note("uri_replace: no match")
71 # FIXME: apply replacements against options
72 return bb.encodeurl(result_decoded)
76 def init(urls = [], d = None):
78 bb.debug(2,"BUG init called with None as data object!!!")
97 """Return a list of the local filenames, assuming successful fetch"""
101 local.append(m.localpath(u, d))
104 def localpath(url, d):
106 if m.supports(url, d):
107 return m.localpath(url, d)
111 """Base class for 'fetch'ing data"""
113 def __init__(self, urls = []):
116 if self.supports(bb.decodeurl(url), d) is 1:
117 self.urls.append(url)
119 def supports(url, d):
120 """Check to see if this fetch class supports a given url.
121 Expects supplied url in list form, as outputted by bb.decodeurl().
124 supports = staticmethod(supports)
126 def localpath(url, d):
127 """Return the local filename of a given url assuming a successful fetch.
130 localpath = staticmethod(localpath)
132 def setUrls(self, urls):
138 urls = property(getUrls, setUrls, None, "Urls property")
140 def setData(self, data):
146 data = property(getData, setData, None, "Data property")
148 def go(self, urls = []):
150 raise NoMethodError("Missing implementation for url")
153 """Class to fetch urls via 'wget'"""
154 def supports(url, d):
155 """Check to see if a given url can be fetched using wget.
156 Expects supplied url in list form, as outputted by bb.decodeurl().
158 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(url, d))
159 return type in ['http','https','ftp']
160 supports = staticmethod(supports)
162 def localpath(url, d):
163 # strip off parameters
164 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(url, d))
165 if "localpath" in parm:
166 # if user overrides local path, use it.
167 return parm["localpath"]
168 url = bb.encodeurl([type, host, path, user, pswd, {}])
170 return os.path.join(data.getVar("DL_DIR", d), os.path.basename(url))
171 localpath = staticmethod(localpath)
173 def go(self, d, urls = []):
176 def md5_sum(basename, data):
178 Fast and incomplete OVERRIDE implementation for MD5SUM handling
179 MD5SUM_basename = "SUM" and fallback to MD5SUM_basename
181 var = "MD5SUM_%s" % basename
182 return getVar(var, data) or get("MD5SUM",data)
185 def fetch_uri(uri, basename, dl, md5, parm, d):
186 if os.path.exists(dl):
187 # file exists, but we didnt complete it.. trying again..
188 fetchcmd = data.getVar("RESUMECOMMAND", d, 1)
190 fetchcmd = data.getVar("FETCHCOMMAND", d, 1)
192 bb.note("fetch " + uri)
193 fetchcmd = fetchcmd.replace("${URI}", uri)
194 fetchcmd = fetchcmd.replace("${FILE}", basename)
195 bb.debug(2, "executing " + fetchcmd)
196 ret = os.system(fetchcmd)
200 # check if sourceforge did send us to the mirror page
201 dl_dir = data.getVar("DL_DIR", d, True)
202 if not os.path.exists(dl):
203 os.system("rm %s*" % dl) # FIXME shell quote it
204 bb.debug(2,"sourceforge.net send us to the mirror on %s" % basename)
207 # supposedly complete.. write out md5sum
208 if bb.which(data.getVar('PATH', d), 'md5sum'):
210 md5pipe = os.popen('md5sum ' + dl)
211 md5data = (md5pipe.readline().split() or [ "" ])[0]
215 md5out = file(md5, 'w')
216 md5out.write(md5data)
219 md5out = file(md5, 'w')
227 localdata = data.createCopy(d)
228 data.setVar('OVERRIDES', "wget:" + data.getVar('OVERRIDES', localdata), localdata)
229 data.update_data(localdata)
233 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(uri, localdata))
234 basename = os.path.basename(path)
235 dl = self.localpath(uri, d)
236 dl = data.expand(dl, localdata)
239 if os.path.exists(md5):
240 # complete, nothing to see here..
243 premirrors = [ i.split() for i in (data.getVar('PREMIRRORS', localdata, 1) or "").split('\n') if i ]
244 for (find, replace) in premirrors:
245 newuri = uri_replace(uri, find, replace, d)
247 if fetch_uri(newuri, basename, dl, md5, parm, localdata):
254 if fetch_uri(uri, basename, dl, md5, parm, localdata):
258 mirrors = [ i.split() for i in (data.getVar('MIRRORS', localdata, 1) or "").split('\n') if i ]
259 for (find, replace) in mirrors:
260 newuri = uri_replace(uri, find, replace, d)
262 if fetch_uri(newuri, basename, dl, md5, parm, localdata):
267 raise FetchError(uri)
272 methods.append(Wget())
275 """Class to fetch a module or modules from cvs repositories"""
276 def supports(url, d):
277 """Check to see if a given url can be fetched with cvs.
278 Expects supplied url in list form, as outputted by bb.decodeurl().
280 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(url, d))
281 return type in ['cvs', 'pserver']
282 supports = staticmethod(supports)
284 def localpath(url, d):
285 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(url, d))
286 if "localpath" in parm:
287 # if user overrides local path, use it.
288 return parm["localpath"]
290 if not "module" in parm:
291 raise MissingParameterError("cvs method needs a 'module' parameter")
293 module = parm["module"]
302 date = data.getVar("CVSDATE", d, 1) or data.getVar("DATE", d, 1)
306 return os.path.join(data.getVar("DL_DIR", d, 1),data.expand('%s_%s_%s_%s.tar.gz' % ( module.replace('/', '.'), host, tag, date), d))
307 localpath = staticmethod(localpath)
309 def go(self, d, urls = []):
314 localdata = data.createCopy(d)
315 data.setVar('OVERRIDES', "cvs:%s" % data.getVar('OVERRIDES', localdata), localdata)
316 data.update_data(localdata)
319 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(loc, localdata))
320 if not "module" in parm:
321 raise MissingParameterError("cvs method needs a 'module' parameter")
323 module = parm["module"]
325 dlfile = self.localpath(loc, localdata)
326 dldir = data.getVar('DL_DIR', localdata, 1)
327 # if local path contains the cvs
328 # module, consider the dir above it to be the
330 # pos = dlfile.find(module)
332 # dldir = dlfile[:pos]
334 # dldir = os.path.dirname(dlfile)
347 date = data.getVar("CVSDATE", d, 1) or data.getVar("DATE", d, 1)
352 method = parm["method"]
356 if "localdir" in parm:
357 localdir = parm["localdir"]
364 cvs_rsh = parm["rsh"]
366 tarfn = data.expand('%s_%s_%s_%s.tar.gz' % (module.replace('/', '.'), host, tag, date), localdata)
367 data.setVar('TARFILES', dlfile, localdata)
368 data.setVar('TARFN', tarfn, localdata)
370 dl = os.path.join(dldir, tarfn)
371 if os.access(dl, os.R_OK):
372 bb.debug(1, "%s already exists, skipping cvs checkout." % tarfn)
375 pn = data.getVar('PN', d, 1)
376 cvs_tarball_stash = None
378 cvs_tarball_stash = data.getVar('CVS_TARBALL_STASH_%s' % pn, d, 1)
379 if cvs_tarball_stash == None:
380 cvs_tarball_stash = data.getVar('CVS_TARBALL_STASH', d, 1)
381 if cvs_tarball_stash:
382 fetchcmd = data.getVar("FETCHCOMMAND_wget", d, 1)
383 uri = cvs_tarball_stash + tarfn
384 bb.note("fetch " + uri)
385 fetchcmd = fetchcmd.replace("${URI}", uri)
386 ret = os.system(fetchcmd)
388 bb.note("Fetched %s from tarball stash, skipping checkout" % tarfn)
392 options.append("-D %s" % date)
394 options.append("-r %s" % tag)
396 olddir = os.path.abspath(os.getcwd())
397 os.chdir(data.expand(dldir, localdata))
403 cvsroot = ":" + method + ":" + user
405 cvsroot += ":" + pswd
406 cvsroot += "@" + host + ":" + path
408 data.setVar('CVSROOT', cvsroot, localdata)
409 data.setVar('CVSCOOPTS', " ".join(options), localdata)
410 data.setVar('CVSMODULE', module, localdata)
411 cvscmd = data.getVar('FETCHCOMMAND', localdata, 1)
412 cvsupdatecmd = data.getVar('UPDATECOMMAND', localdata, 1)
415 cvscmd = "CVS_RSH=\"%s\" %s" % (cvs_rsh, cvscmd)
416 cvsupdatecmd = "CVS_RSH=\"%s\" %s" % (cvs_rsh, cvsupdatecmd)
418 # create module directory
419 bb.debug(2, "Fetch: checking for module directory")
420 pkg=data.expand('${PN}', d)
421 pkgdir=os.path.join(data.expand('${CVSDIR}', localdata), pkg)
422 moddir=os.path.join(pkgdir,localdir)
423 if os.access(os.path.join(moddir,'CVS'), os.R_OK):
424 bb.note("Update " + loc)
425 # update sources there
427 myret = os.system(cvsupdatecmd)
429 bb.note("Fetch " + loc)
430 # check out sources there
433 bb.debug(1, "Running %s" % cvscmd)
434 myret = os.system(cvscmd)
441 raise FetchError(module)
445 # tar them up to a defined filename
446 myret = os.system("tar -czf %s %s" % (os.path.join(dldir,tarfn), os.path.basename(moddir)))
455 methods.append(Cvs())
458 def supports(url, d):
459 """Check to see if a given url can be fetched via bitkeeper.
460 Expects supplied url in list form, as outputted by bb.decodeurl().
462 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(url, d))
463 return type in ['bk']
464 supports = staticmethod(supports)
469 def supports(url, d):
470 """Check to see if a given url can be fetched in the local filesystem.
471 Expects supplied url in list form, as outputted by bb.decodeurl().
473 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(url, d))
474 return type in ['file','patch']
475 supports = staticmethod(supports)
477 def localpath(url, d):
478 """Return the local filename of a given url assuming a successful fetch.
480 path = url.split("://")[1]
483 filespath = data.getVar('FILESPATH', d, 1)
485 newpath = bb.which(filespath, path)
487 filesdir = data.getVar('FILESDIR', d, 1)
489 newpath = os.path.join(filesdir, path)
491 localpath = staticmethod(localpath)
493 def go(self, urls = []):
494 """Fetch urls (no-op for Local method)"""
495 # no need to fetch local files, we'll deal with them in place.
498 methods.append(Local())
501 """Class to fetch a module or modules from svn repositories"""
502 def supports(url, d):
503 """Check to see if a given url can be fetched with svn.
504 Expects supplied url in list form, as outputted by bb.decodeurl().
506 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(url, d))
507 return type in ['svn']
508 supports = staticmethod(supports)
510 def localpath(url, d):
511 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(url, d))
512 if "localpath" in parm:
513 # if user overrides local path, use it.
514 return parm["localpath"]
516 if not "module" in parm:
517 raise MissingParameterError("svn method needs a 'module' parameter")
519 module = parm["module"]
521 revision = parm['rev']
525 date = data.getVar("CVSDATE", d, 1) or data.getVar("DATE", d, 1)
527 return os.path.join(data.getVar("DL_DIR", d, 1),data.expand('%s_%s_%s_%s.tar.gz' % ( module.replace('/', '.'), host, revision, date), d))
528 localpath = staticmethod(localpath)
530 def go(self, d, urls = []):
535 localdata = data.createCopy(d)
536 data.setVar('OVERRIDES', "svn:%s" % data.getVar('OVERRIDES', localdata), localdata)
537 data.update_data(localdata)
540 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(loc, localdata))
541 if not "module" in parm:
542 raise MissingParameterError("svn method needs a 'module' parameter")
544 module = parm["module"]
546 dlfile = self.localpath(loc, localdata)
547 dldir = data.getVar('DL_DIR', localdata, 1)
548 # if local path contains the svn
549 # module, consider the dir above it to be the
551 # pos = dlfile.find(module)
553 # dldir = dlfile[:pos]
555 # dldir = os.path.dirname(dlfile)
560 revision = parm['rev']
564 date = data.getVar("CVSDATE", d, 1) or data.getVar("DATE", d, 1)
567 method = parm["method"]
572 proto = parm["proto"]
579 svn_rsh = parm["rsh"]
581 tarfn = data.expand('%s_%s_%s_%s.tar.gz' % (module.replace('/', '.'), host, revision, date), localdata)
582 data.setVar('TARFILES', dlfile, localdata)
583 data.setVar('TARFN', tarfn, localdata)
585 dl = os.path.join(dldir, tarfn)
586 if os.access(dl, os.R_OK):
587 bb.debug(1, "%s already exists, skipping svn checkout." % tarfn)
590 svn_tarball_stash = data.getVar('CVS_TARBALL_STASH', d, 1)
591 if svn_tarball_stash:
592 fetchcmd = data.getVar("FETCHCOMMAND_wget", d, 1)
593 uri = svn_tarball_stash + tarfn
594 bb.note("fetch " + uri)
595 fetchcmd = fetchcmd.replace("${URI}", uri)
596 ret = os.system(fetchcmd)
598 bb.note("Fetched %s from tarball stash, skipping checkout" % tarfn)
601 olddir = os.path.abspath(os.getcwd())
602 os.chdir(data.expand(dldir, localdata))
605 # svnroot = ":" + method + ":" + user
607 # svnroot += ":" + pswd
608 svnroot = host + path
610 data.setVar('SVNROOT', svnroot, localdata)
611 data.setVar('SVNCOOPTS', " ".join(options), localdata)
612 data.setVar('SVNMODULE', module, localdata)
613 svncmd = data.getVar('FETCHCOMMAND', localdata, 1)
614 svncmd = "svn co %s://%s/%s" % (proto, svnroot, module)
617 svncmd = "svn co -r %s %s://%s/%s" % (revision, proto, svnroot, module)
619 svncmd = "svn_RSH=\"%s\" %s" % (svn_rsh, svncmd)
621 # create temp directory
622 bb.debug(2, "Fetch: creating temporary directory")
623 bb.mkdirhier(data.expand('${WORKDIR}', localdata))
624 data.setVar('TMPBASE', data.expand('${WORKDIR}/oesvn.XXXXXX', localdata), localdata)
625 tmppipe = os.popen(data.getVar('MKTEMPDIRCMD', localdata, 1) or "false")
626 tmpfile = tmppipe.readline().strip()
628 bb.error("Fetch: unable to create temporary directory.. make sure 'mktemp' is in the PATH.")
629 raise FetchError(module)
631 # check out sources there
633 bb.note("Fetch " + loc)
634 bb.debug(1, "Running %s" % svncmd)
635 myret = os.system(svncmd)
641 raise FetchError(module)
643 os.chdir(os.path.join(tmpfile, os.path.dirname(module)))
644 # tar them up to a defined filename
645 myret = os.system("tar -czf %s %s" % (os.path.join(dldir,tarfn), os.path.basename(module)))
652 os.system('rm -rf %s' % tmpfile)
656 methods.append(Svn())