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 = bb.data.init()):
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 = bb.data.init()):
86 def go(d = bb.data.init()):
93 """Return a list of the local filenames, assuming successful fetch"""
97 local.append(m.localpath(u, d))
100 def localpath(url, d = bb.data.init()):
102 if m.supports(url, d):
103 return m.localpath(url, d)
107 """Base class for 'fetch'ing data"""
109 def __init__(self, urls = []):
112 if self.supports(bb.decodeurl(url), d) is 1:
113 self.urls.append(url)
115 def supports(url, d):
116 """Check to see if this fetch class supports a given url.
117 Expects supplied url in list form, as outputted by bb.decodeurl().
120 supports = staticmethod(supports)
122 def localpath(url, d = bb.data.init()):
123 """Return the local filename of a given url assuming a successful fetch.
126 localpath = staticmethod(localpath)
128 def setUrls(self, urls):
134 urls = property(getUrls, setUrls, None, "Urls property")
136 def setData(self, data):
142 data = property(getData, setData, None, "Data property")
144 def go(self, urls = []):
146 raise NoMethodError("Missing implementation for url")
149 """Class to fetch urls via 'wget'"""
150 def supports(url, d):
151 """Check to see if a given url can be fetched using wget.
152 Expects supplied url in list form, as outputted by bb.decodeurl().
154 (type, host, path, user, pswd, parm) = bb.decodeurl(bb.data.expand(url, d))
155 return type in ['http','https','ftp']
156 supports = staticmethod(supports)
158 def localpath(url, d):
159 # strip off parameters
160 (type, host, path, user, pswd, parm) = bb.decodeurl(bb.data.expand(url, d))
161 if "localpath" in parm:
162 # if user overrides local path, use it.
163 return parm["localpath"]
164 url = bb.encodeurl([type, host, path, user, pswd, {}])
165 return os.path.join(bb.data.getVar("DL_DIR", d), os.path.basename(url))
166 localpath = staticmethod(localpath)
168 def go(self, d = bb.data.init(), urls = []):
170 def fetch_uri(uri, basename, dl, md5, d):
171 if os.path.exists(dl):
172 # file exists, but we didnt complete it.. trying again..
173 fetchcmd = bb.data.getVar("RESUMECOMMAND", d, 1)
175 fetchcmd = bb.data.getVar("FETCHCOMMAND", d, 1)
177 bb.note("fetch " + uri)
178 fetchcmd = fetchcmd.replace("${URI}", uri)
179 fetchcmd = fetchcmd.replace("${FILE}", basename)
180 bb.debug(2, "executing " + fetchcmd)
181 ret = os.system(fetchcmd)
185 # supposedly complete.. write out md5sum
186 if bb.which(bb.data.getVar('PATH', d), 'md5sum'):
188 md5pipe = os.popen('md5sum ' + dl)
189 md5data = (md5pipe.readline().split() or [ "" ])[0]
193 md5out = file(md5, 'w')
194 md5out.write(md5data)
197 md5out = file(md5, 'w')
205 from copy import deepcopy
206 localdata = deepcopy(d)
207 bb.data.setVar('OVERRIDES', "wget:" + bb.data.getVar('OVERRIDES', localdata), localdata)
208 bb.data.update_data(localdata)
212 (type, host, path, user, pswd, parm) = bb.decodeurl(bb.data.expand(uri, localdata))
213 basename = os.path.basename(path)
214 dl = self.localpath(uri, d)
215 dl = bb.data.expand(dl, localdata)
218 if os.path.exists(md5):
219 # complete, nothing to see here..
222 premirrors = [ i.split() for i in (bb.data.getVar('PREMIRRORS', localdata, 1) or "").split('\n') if i ]
223 for (find, replace) in premirrors:
224 newuri = uri_replace(uri, find, replace)
226 if fetch_uri(newuri, basename, dl, md5, localdata):
233 if fetch_uri(uri, basename, dl, md5, localdata):
237 mirrors = [ i.split() for i in (bb.data.getVar('MIRRORS', localdata, 1) or "").split('\n') if i ]
238 for (find, replace) in mirrors:
239 newuri = uri_replace(uri, find, replace)
241 if fetch_uri(newuri, basename, dl, md5, localdata):
246 raise FetchError(uri)
251 methods.append(Wget())
254 """Class to fetch a module or modules from cvs repositories"""
255 def supports(url, d):
256 """Check to see if a given url can be fetched with cvs.
257 Expects supplied url in list form, as outputted by bb.decodeurl().
259 (type, host, path, user, pswd, parm) = bb.decodeurl(bb.data.expand(url, d))
260 return type in ['cvs', 'pserver']
261 supports = staticmethod(supports)
263 def localpath(url, d):
264 (type, host, path, user, pswd, parm) = bb.decodeurl(bb.data.expand(url, d))
265 if "localpath" in parm:
266 # if user overrides local path, use it.
267 return parm["localpath"]
269 if not "module" in parm:
270 raise MissingParameterError("cvs method needs a 'module' parameter")
272 module = parm["module"]
281 date = bb.data.getVar("CVSDATE", d, 1) or bb.data.getVar("DATE", d, 1)
285 return os.path.join(bb.data.getVar("DL_DIR", d, 1),bb.data.expand('%s_%s_%s_%s.tar.gz' % ( module.replace('/', '.'), host, tag, date), d))
286 localpath = staticmethod(localpath)
288 def go(self, d = bb.data.init(), urls = []):
293 from copy import deepcopy
294 localdata = deepcopy(d)
295 bb.data.setVar('OVERRIDES', "cvs:%s" % bb.data.getVar('OVERRIDES', localdata), localdata)
296 bb.data.update_data(localdata)
299 (type, host, path, user, pswd, parm) = bb.decodeurl(bb.data.expand(loc, localdata))
300 if not "module" in parm:
301 raise MissingParameterError("cvs method needs a 'module' parameter")
303 module = parm["module"]
305 dlfile = self.localpath(loc, localdata)
306 dldir = bb.data.getVar('DL_DIR', localdata, 1)
307 # if local path contains the cvs
308 # module, consider the dir above it to be the
310 # pos = dlfile.find(module)
312 # dldir = dlfile[:pos]
314 # dldir = os.path.dirname(dlfile)
327 date = bb.data.getVar("CVSDATE", d, 1) or bb.data.getVar("DATE", d, 1)
332 method = parm["method"]
336 if "localdir" in parm:
337 localdir = parm["localdir"]
344 cvs_rsh = parm["rsh"]
346 tarfn = bb.data.expand('%s_%s_%s_%s.tar.gz' % (module.replace('/', '.'), host, tag, date), localdata)
347 bb.data.setVar('TARFILES', dlfile, localdata)
348 bb.data.setVar('TARFN', tarfn, localdata)
350 dl = os.path.join(dldir, tarfn)
351 if os.access(dl, os.R_OK):
352 bb.debug(1, "%s already exists, skipping cvs checkout." % tarfn)
355 pn = bb.data.getVar('PN', d, 1)
356 cvs_tarball_stash = None
358 cvs_tarball_stash = bb.data.getVar('CVS_TARBALL_STASH_%s' % pn, d, 1)
359 if cvs_tarball_stash == None:
360 cvs_tarball_stash = bb.data.getVar('CVS_TARBALL_STASH', d, 1)
361 if cvs_tarball_stash:
362 fetchcmd = bb.data.getVar("FETCHCOMMAND_wget", d, 1)
363 uri = cvs_tarball_stash + tarfn
364 bb.note("fetch " + uri)
365 fetchcmd = fetchcmd.replace("${URI}", uri)
366 ret = os.system(fetchcmd)
368 bb.note("Fetched %s from tarball stash, skipping checkout" % tarfn)
372 options.append("-D %s" % date)
374 options.append("-r %s" % tag)
376 olddir = os.path.abspath(os.getcwd())
377 os.chdir(bb.data.expand(dldir, localdata))
383 cvsroot = ":" + method + ":" + user
385 cvsroot += ":" + pswd
386 cvsroot += "@" + host + ":" + path
388 bb.data.setVar('CVSROOT', cvsroot, localdata)
389 bb.data.setVar('CVSCOOPTS', " ".join(options), localdata)
390 bb.data.setVar('CVSMODULE', module, localdata)
391 cvscmd = bb.data.getVar('FETCHCOMMAND', localdata, 1)
392 cvsupdatecmd = bb.data.getVar('UPDATECOMMAND', localdata, 1)
395 cvscmd = "CVS_RSH=\"%s\" %s" % (cvs_rsh, cvscmd)
396 cvsupdatecmd = "CVS_RSH=\"%s\" %s" % (cvs_rsh, cvsupdatecmd)
398 # create module directory
399 bb.debug(2, "Fetch: checking for module directory")
400 pkg=bb.data.expand('${PN}', d)
401 pkgdir=os.path.join(bb.data.expand('${CVSDIR}', localdata), pkg)
402 moddir=os.path.join(pkgdir,localdir)
403 if os.access(os.path.join(moddir,'CVS'), os.R_OK):
404 bb.note("Update " + loc)
405 # update sources there
407 myret = os.system(cvsupdatecmd)
409 bb.note("Fetch " + loc)
410 # check out sources there
413 bb.debug(1, "Running %s" % cvscmd)
414 myret = os.system(cvscmd)
421 raise FetchError(module)
425 # tar them up to a defined filename
426 myret = os.system("tar -czf %s %s" % (os.path.join(dldir,tarfn), os.path.basename(moddir)))
435 methods.append(Cvs())
438 def supports(url, d):
439 """Check to see if a given url can be fetched via bitkeeper.
440 Expects supplied url in list form, as outputted by bb.decodeurl().
442 (type, host, path, user, pswd, parm) = bb.decodeurl(bb.data.expand(url, d))
443 return type in ['bk']
444 supports = staticmethod(supports)
449 def supports(url, d):
450 """Check to see if a given url can be fetched in the local filesystem.
451 Expects supplied url in list form, as outputted by bb.decodeurl().
453 (type, host, path, user, pswd, parm) = bb.decodeurl(bb.data.expand(url, d))
454 return type in ['file','patch']
455 supports = staticmethod(supports)
457 def localpath(url, d):
458 """Return the local filename of a given url assuming a successful fetch.
460 path = url.split("://")[1]
463 filespath = bb.data.getVar('FILESPATH', d, 1)
465 newpath = bb.which(filespath, path)
467 filesdir = bb.data.getVar('FILESDIR', d, 1)
469 newpath = os.path.join(filesdir, path)
471 localpath = staticmethod(localpath)
473 def go(self, urls = []):
474 """Fetch urls (no-op for Local method)"""
475 # no need to fetch local files, we'll deal with them in place.
478 methods.append(Local())
481 """Class to fetch a module or modules from svn repositories"""
482 def supports(url, d):
483 """Check to see if a given url can be fetched with svn.
484 Expects supplied url in list form, as outputted by bb.decodeurl().
486 (type, host, path, user, pswd, parm) = bb.decodeurl(bb.data.expand(url, d))
487 return type in ['svn']
488 supports = staticmethod(supports)
490 def localpath(url, d):
491 (type, host, path, user, pswd, parm) = bb.decodeurl(bb.data.expand(url, d))
492 if "localpath" in parm:
493 # if user overrides local path, use it.
494 return parm["localpath"]
496 if not "module" in parm:
497 raise MissingParameterError("svn method needs a 'module' parameter")
499 module = parm["module"]
501 revision = parm['rev']
505 date = bb.data.getVar("CVSDATE", d, 1) or bb.data.getVar("DATE", d, 1)
507 return os.path.join(bb.data.getVar("DL_DIR", d, 1),bb.data.expand('%s_%s_%s_%s.tar.gz' % ( module.replace('/', '.'), host, revision, date), d))
508 localpath = staticmethod(localpath)
510 def go(self, d = bb.data.init(), urls = []):
515 from copy import deepcopy
516 localdata = deepcopy(d)
517 bb.data.setVar('OVERRIDES', "svn:%s" % bb.data.getVar('OVERRIDES', localdata), localdata)
518 bb.data.update_data(localdata)
521 (type, host, path, user, pswd, parm) = bb.decodeurl(bb.data.expand(loc, localdata))
522 if not "module" in parm:
523 raise MissingParameterError("svn method needs a 'module' parameter")
525 module = parm["module"]
527 dlfile = self.localpath(loc, localdata)
528 dldir = bb.data.getVar('DL_DIR', localdata, 1)
529 # if local path contains the svn
530 # module, consider the dir above it to be the
532 # pos = dlfile.find(module)
534 # dldir = dlfile[:pos]
536 # dldir = os.path.dirname(dlfile)
541 revision = parm['rev']
545 date = bb.data.getVar("CVSDATE", d, 1) or bb.data.getVar("DATE", d, 1)
548 method = parm["method"]
553 proto = parm["proto"]
560 svn_rsh = parm["rsh"]
562 tarfn = bb.data.expand('%s_%s_%s_%s.tar.gz' % (module.replace('/', '.'), host, revision, date), localdata)
563 bb.data.setVar('TARFILES', dlfile, localdata)
564 bb.data.setVar('TARFN', tarfn, localdata)
566 dl = os.path.join(dldir, tarfn)
567 if os.access(dl, os.R_OK):
568 bb.debug(1, "%s already exists, skipping svn checkout." % tarfn)
571 svn_tarball_stash = bb.data.getVar('CVS_TARBALL_STASH', d, 1)
572 if svn_tarball_stash:
573 fetchcmd = bb.data.getVar("FETCHCOMMAND_wget", d, 1)
574 uri = svn_tarball_stash + tarfn
575 bb.note("fetch " + uri)
576 fetchcmd = fetchcmd.replace("${URI}", uri)
577 ret = os.system(fetchcmd)
579 bb.note("Fetched %s from tarball stash, skipping checkout" % tarfn)
582 olddir = os.path.abspath(os.getcwd())
583 os.chdir(bb.data.expand(dldir, localdata))
586 # svnroot = ":" + method + ":" + user
588 # svnroot += ":" + pswd
589 svnroot = host + path
591 bb.data.setVar('SVNROOT', svnroot, localdata)
592 bb.data.setVar('SVNCOOPTS', " ".join(options), localdata)
593 bb.data.setVar('SVNMODULE', module, localdata)
594 svncmd = bb.data.getVar('FETCHCOMMAND', localdata, 1)
595 svncmd = "svn co %s://%s/%s" % (proto, svnroot, module)
598 svncmd = "svn co -r %s %s://%s/%s" % (revision, proto, svnroot, module)
600 svncmd = "svn_RSH=\"%s\" %s" % (svn_rsh, svncmd)
602 # create temp directory
603 bb.debug(2, "Fetch: creating temporary directory")
604 bb.mkdirhier(bb.data.expand('${WORKDIR}', localdata))
605 bb.data.setVar('TMPBASE', bb.data.expand('${WORKDIR}/oesvn.XXXXXX', localdata), localdata)
606 tmppipe = os.popen(bb.data.getVar('MKTEMPDIRCMD', localdata, 1) or "false")
607 tmpfile = tmppipe.readline().strip()
609 bb.error("Fetch: unable to create temporary directory.. make sure 'mktemp' is in the PATH.")
610 raise FetchError(module)
612 # check out sources there
614 bb.note("Fetch " + loc)
615 bb.debug(1, "Running %s" % svncmd)
616 myret = os.system(svncmd)
622 raise FetchError(module)
624 os.chdir(os.path.join(tmpfile, os.path.dirname(module)))
625 # tar them up to a defined filename
626 myret = os.system("tar -czf %s %s" % (os.path.join(dldir,tarfn), os.path.basename(module)))
633 os.system('rm -rf %s' % tmpfile)
637 methods.append(Svn())