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 #decodeurl("cvs://anoncvs:anonymous@cvs.handhelds.org/cvs;module=familiar/dist/ipkg;tag=V0-99-81")
42 #('cvs', 'cvs.handhelds.org', '/cvs', 'anoncvs', 'anonymous', {'tag': 'V0-99-81', 'module': 'familiar/dist/ipkg'})
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 = []):
175 def fetch_uri(uri, basename, dl, md5, d):
176 if os.path.exists(dl):
177 # file exists, but we didnt complete it.. trying again..
178 fetchcmd = data.getVar("RESUMECOMMAND", d, 1)
180 fetchcmd = data.getVar("FETCHCOMMAND", d, 1)
182 bb.note("fetch " + uri)
183 fetchcmd = fetchcmd.replace("${URI}", uri)
184 fetchcmd = fetchcmd.replace("${FILE}", basename)
185 bb.debug(2, "executing " + fetchcmd)
186 ret = os.system(fetchcmd)
190 # check if sourceforge did send us to the mirror page
191 dl_dir = data.getVar("DL_DIR", d, True)
192 if not os.path.exists(dl):
193 os.system("rm %s*" % dl) # FIXME shell quote it
194 bb.debug(2,"sourceforge.net send us to the mirror on %s" % basename)
197 # supposedly complete.. write out md5sum
198 if bb.which(data.getVar('PATH', d), 'md5sum'):
200 md5pipe = os.popen('md5sum ' + dl)
201 md5data = (md5pipe.readline().split() or [ "" ])[0]
205 md5out = file(md5, 'w')
206 md5out.write(md5data)
209 md5out = file(md5, 'w')
217 localdata = data.createCopy(d)
218 data.setVar('OVERRIDES', "wget:" + data.getVar('OVERRIDES', localdata), localdata)
219 data.update_data(localdata)
223 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(uri, localdata))
224 basename = os.path.basename(path)
225 dl = self.localpath(uri, d)
226 dl = data.expand(dl, localdata)
229 if os.path.exists(md5):
230 # complete, nothing to see here..
233 premirrors = [ i.split() for i in (data.getVar('PREMIRRORS', localdata, 1) or "").split('\n') if i ]
234 for (find, replace) in premirrors:
235 newuri = uri_replace(uri, find, replace, d)
237 if fetch_uri(newuri, basename, dl, md5, localdata):
244 if fetch_uri(uri, basename, dl, md5, localdata):
248 mirrors = [ i.split() for i in (data.getVar('MIRRORS', localdata, 1) or "").split('\n') if i ]
249 for (find, replace) in mirrors:
250 newuri = uri_replace(uri, find, replace, d)
252 if fetch_uri(newuri, basename, dl, md5, localdata):
257 raise FetchError(uri)
262 methods.append(Wget())
265 """Class to fetch a module or modules from cvs repositories"""
266 def supports(url, d):
267 """Check to see if a given url can be fetched with cvs.
268 Expects supplied url in list form, as outputted by bb.decodeurl().
270 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(url, d))
271 return type in ['cvs', 'pserver']
272 supports = staticmethod(supports)
274 def localpath(url, d):
275 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(url, d))
276 if "localpath" in parm:
277 # if user overrides local path, use it.
278 return parm["localpath"]
280 if not "module" in parm:
281 raise MissingParameterError("cvs method needs a 'module' parameter")
283 module = parm["module"]
292 date = data.getVar("CVSDATE", d, 1) or data.getVar("DATE", d, 1)
296 return os.path.join(data.getVar("DL_DIR", d, 1),data.expand('%s_%s_%s_%s.tar.gz' % ( module.replace('/', '.'), host, tag, date), d))
297 localpath = staticmethod(localpath)
299 def go(self, d, urls = []):
304 localdata = data.createCopy(d)
305 data.setVar('OVERRIDES', "cvs:%s" % data.getVar('OVERRIDES', localdata), localdata)
306 data.update_data(localdata)
309 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(loc, localdata))
310 if not "module" in parm:
311 raise MissingParameterError("cvs method needs a 'module' parameter")
313 module = parm["module"]
315 dlfile = self.localpath(loc, localdata)
316 dldir = data.getVar('DL_DIR', localdata, 1)
317 # if local path contains the cvs
318 # module, consider the dir above it to be the
320 # pos = dlfile.find(module)
322 # dldir = dlfile[:pos]
324 # dldir = os.path.dirname(dlfile)
337 date = data.getVar("CVSDATE", d, 1) or data.getVar("DATE", d, 1)
342 method = parm["method"]
346 if "localdir" in parm:
347 localdir = parm["localdir"]
354 cvs_rsh = parm["rsh"]
356 tarfn = data.expand('%s_%s_%s_%s.tar.gz' % (module.replace('/', '.'), host, tag, date), localdata)
357 data.setVar('TARFILES', dlfile, localdata)
358 data.setVar('TARFN', tarfn, localdata)
360 dl = os.path.join(dldir, tarfn)
361 if os.access(dl, os.R_OK):
362 bb.debug(1, "%s already exists, skipping cvs checkout." % tarfn)
365 pn = data.getVar('PN', d, 1)
366 cvs_tarball_stash = None
368 cvs_tarball_stash = data.getVar('CVS_TARBALL_STASH_%s' % pn, d, 1)
369 if cvs_tarball_stash == None:
370 cvs_tarball_stash = data.getVar('CVS_TARBALL_STASH', d, 1)
371 if cvs_tarball_stash:
372 fetchcmd = data.getVar("FETCHCOMMAND_wget", d, 1)
373 uri = cvs_tarball_stash + tarfn
374 bb.note("fetch " + uri)
375 fetchcmd = fetchcmd.replace("${URI}", uri)
376 ret = os.system(fetchcmd)
378 bb.note("Fetched %s from tarball stash, skipping checkout" % tarfn)
382 options.append("-D %s" % date)
384 options.append("-r %s" % tag)
386 olddir = os.path.abspath(os.getcwd())
387 os.chdir(data.expand(dldir, localdata))
393 cvsroot = ":" + method + ":" + user
395 cvsroot += ":" + pswd
396 cvsroot += "@" + host + ":" + path
398 data.setVar('CVSROOT', cvsroot, localdata)
399 data.setVar('CVSCOOPTS', " ".join(options), localdata)
400 data.setVar('CVSMODULE', module, localdata)
401 cvscmd = data.getVar('FETCHCOMMAND', localdata, 1)
402 cvsupdatecmd = data.getVar('UPDATECOMMAND', localdata, 1)
405 cvscmd = "CVS_RSH=\"%s\" %s" % (cvs_rsh, cvscmd)
406 cvsupdatecmd = "CVS_RSH=\"%s\" %s" % (cvs_rsh, cvsupdatecmd)
408 # create module directory
409 bb.debug(2, "Fetch: checking for module directory")
410 pkg=data.expand('${PN}', d)
411 pkgdir=os.path.join(data.expand('${CVSDIR}', localdata), pkg)
412 moddir=os.path.join(pkgdir,localdir)
413 if os.access(os.path.join(moddir,'CVS'), os.R_OK):
414 bb.note("Update " + loc)
415 # update sources there
417 myret = os.system(cvsupdatecmd)
419 bb.note("Fetch " + loc)
420 # check out sources there
423 bb.debug(1, "Running %s" % cvscmd)
424 myret = os.system(cvscmd)
431 raise FetchError(module)
435 # tar them up to a defined filename
436 myret = os.system("tar -czf %s %s" % (os.path.join(dldir,tarfn), os.path.basename(moddir)))
445 methods.append(Cvs())
448 def supports(url, d):
449 """Check to see if a given url can be fetched via bitkeeper.
450 Expects supplied url in list form, as outputted by bb.decodeurl().
452 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(url, d))
453 return type in ['bk']
454 supports = staticmethod(supports)
459 def supports(url, d):
460 """Check to see if a given url can be fetched in the local filesystem.
461 Expects supplied url in list form, as outputted by bb.decodeurl().
463 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(url, d))
464 return type in ['file','patch']
465 supports = staticmethod(supports)
467 def localpath(url, d):
468 """Return the local filename of a given url assuming a successful fetch.
470 path = url.split("://")[1]
473 filespath = data.getVar('FILESPATH', d, 1)
475 newpath = bb.which(filespath, path)
477 filesdir = data.getVar('FILESDIR', d, 1)
479 newpath = os.path.join(filesdir, path)
481 localpath = staticmethod(localpath)
483 def go(self, urls = []):
484 """Fetch urls (no-op for Local method)"""
485 # no need to fetch local files, we'll deal with them in place.
488 methods.append(Local())
491 """Class to fetch a module or modules from svn repositories"""
492 def supports(url, d):
493 """Check to see if a given url can be fetched with svn.
494 Expects supplied url in list form, as outputted by bb.decodeurl().
496 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(url, d))
497 return type in ['svn']
498 supports = staticmethod(supports)
500 def localpath(url, d):
501 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(url, d))
502 if "localpath" in parm:
503 # if user overrides local path, use it.
504 return parm["localpath"]
506 if not "module" in parm:
507 raise MissingParameterError("svn method needs a 'module' parameter")
509 module = parm["module"]
511 revision = parm['rev']
515 date = data.getVar("CVSDATE", d, 1) or data.getVar("DATE", d, 1)
517 return os.path.join(data.getVar("DL_DIR", d, 1),data.expand('%s_%s_%s_%s.tar.gz' % ( module.replace('/', '.'), host, revision, date), d))
518 localpath = staticmethod(localpath)
520 def go(self, d, urls = []):
525 localdata = data.createCopy(d)
526 data.setVar('OVERRIDES', "svn:%s" % data.getVar('OVERRIDES', localdata), localdata)
527 data.update_data(localdata)
530 (type, host, path, user, pswd, parm) = bb.decodeurl(data.expand(loc, localdata))
531 if not "module" in parm:
532 raise MissingParameterError("svn method needs a 'module' parameter")
534 module = parm["module"]
536 dlfile = self.localpath(loc, localdata)
537 dldir = data.getVar('DL_DIR', localdata, 1)
538 # if local path contains the svn
539 # module, consider the dir above it to be the
541 # pos = dlfile.find(module)
543 # dldir = dlfile[:pos]
545 # dldir = os.path.dirname(dlfile)
550 revision = parm['rev']
554 date = data.getVar("CVSDATE", d, 1) or data.getVar("DATE", d, 1)
557 method = parm["method"]
562 proto = parm["proto"]
569 svn_rsh = parm["rsh"]
571 tarfn = data.expand('%s_%s_%s_%s.tar.gz' % (module.replace('/', '.'), host, revision, date), localdata)
572 data.setVar('TARFILES', dlfile, localdata)
573 data.setVar('TARFN', tarfn, localdata)
575 dl = os.path.join(dldir, tarfn)
576 if os.access(dl, os.R_OK):
577 bb.debug(1, "%s already exists, skipping svn checkout." % tarfn)
580 svn_tarball_stash = data.getVar('CVS_TARBALL_STASH', d, 1)
581 if svn_tarball_stash:
582 fetchcmd = data.getVar("FETCHCOMMAND_wget", d, 1)
583 uri = svn_tarball_stash + tarfn
584 bb.note("fetch " + uri)
585 fetchcmd = fetchcmd.replace("${URI}", uri)
586 ret = os.system(fetchcmd)
588 bb.note("Fetched %s from tarball stash, skipping checkout" % tarfn)
591 olddir = os.path.abspath(os.getcwd())
592 os.chdir(data.expand(dldir, localdata))
595 # svnroot = ":" + method + ":" + user
597 # svnroot += ":" + pswd
598 svnroot = host + path
600 data.setVar('SVNROOT', svnroot, localdata)
601 data.setVar('SVNCOOPTS', " ".join(options), localdata)
602 data.setVar('SVNMODULE', module, localdata)
603 svncmd = data.getVar('FETCHCOMMAND', localdata, 1)
604 svncmd = "svn co %s://%s/%s" % (proto, svnroot, module)
607 svncmd = "svn co -r %s %s://%s/%s" % (revision, proto, svnroot, module)
609 svncmd = "svn_RSH=\"%s\" %s" % (svn_rsh, svncmd)
611 # create temp directory
612 bb.debug(2, "Fetch: creating temporary directory")
613 bb.mkdirhier(data.expand('${WORKDIR}', localdata))
614 data.setVar('TMPBASE', data.expand('${WORKDIR}/oesvn.XXXXXX', localdata), localdata)
615 tmppipe = os.popen(data.getVar('MKTEMPDIRCMD', localdata, 1) or "false")
616 tmpfile = tmppipe.readline().strip()
618 bb.error("Fetch: unable to create temporary directory.. make sure 'mktemp' is in the PATH.")
619 raise FetchError(module)
621 # check out sources there
623 bb.note("Fetch " + loc)
624 bb.debug(1, "Running %s" % svncmd)
625 myret = os.system(svncmd)
631 raise FetchError(module)
633 os.chdir(os.path.join(tmpfile, os.path.dirname(module)))
634 # tar them up to a defined filename
635 myret = os.system("tar -czf %s %s" % (os.path.join(dldir,tarfn), os.path.basename(module)))
642 os.system('rm -rf %s' % tmpfile)
646 methods.append(Svn())