__init__.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. # ex:ts=4:sw=4:sts=4:et
  2. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  3. """
  4. BitBake 'Fetch' implementations
  5. Classes for obtaining upstream sources for the
  6. BitBake build tools.
  7. """
  8. # Copyright (C) 2003, 2004 Chris Larson
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License version 2 as
  12. # published by the Free Software Foundation.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. # Based on functions from the base bb module, Copyright 2003 Holger Schurig
  24. import os, re, fcntl
  25. import bb
  26. from bb import data
  27. from bb import persist_data
  28. try:
  29. import cPickle as pickle
  30. except ImportError:
  31. import pickle
  32. class FetchError(Exception):
  33. """Exception raised when a download fails"""
  34. class NoMethodError(Exception):
  35. """Exception raised when there is no method to obtain a supplied url or set of urls"""
  36. class MissingParameterError(Exception):
  37. """Exception raised when a fetch method is missing a critical parameter in the url"""
  38. class ParameterError(Exception):
  39. """Exception raised when a url cannot be proccessed due to invalid parameters."""
  40. class MD5SumError(Exception):
  41. """Exception raised when a MD5SUM of a file does not match the expected one"""
  42. def uri_replace(uri, uri_find, uri_replace, d):
  43. # bb.msg.note(1, bb.msg.domain.Fetcher, "uri_replace: operating on %s" % uri)
  44. if not uri or not uri_find or not uri_replace:
  45. bb.msg.debug(1, bb.msg.domain.Fetcher, "uri_replace: passed an undefined value, not replacing")
  46. uri_decoded = list(bb.decodeurl(uri))
  47. uri_find_decoded = list(bb.decodeurl(uri_find))
  48. uri_replace_decoded = list(bb.decodeurl(uri_replace))
  49. result_decoded = ['','','','','',{}]
  50. for i in uri_find_decoded:
  51. loc = uri_find_decoded.index(i)
  52. result_decoded[loc] = uri_decoded[loc]
  53. import types
  54. if type(i) == types.StringType:
  55. import re
  56. if (re.match(i, uri_decoded[loc])):
  57. result_decoded[loc] = re.sub(i, uri_replace_decoded[loc], uri_decoded[loc])
  58. if uri_find_decoded.index(i) == 2:
  59. if d:
  60. localfn = bb.fetch.localpath(uri, d)
  61. if localfn:
  62. result_decoded[loc] = os.path.dirname(result_decoded[loc]) + "/" + os.path.basename(bb.fetch.localpath(uri, d))
  63. # bb.msg.note(1, bb.msg.domain.Fetcher, "uri_replace: matching %s against %s and replacing with %s" % (i, uri_decoded[loc], uri_replace_decoded[loc]))
  64. else:
  65. # bb.msg.note(1, bb.msg.domain.Fetcher, "uri_replace: no match")
  66. return uri
  67. # else:
  68. # for j in i.keys():
  69. # FIXME: apply replacements against options
  70. return bb.encodeurl(result_decoded)
  71. methods = []
  72. urldata_cache = {}
  73. def fetcher_init(d):
  74. """
  75. Called to initilize the fetchers once the configuration data is known
  76. Calls before this must not hit the cache.
  77. """
  78. pd = persist_data.PersistData(d)
  79. # When to drop SCM head revisions controled by user policy
  80. srcrev_policy = bb.data.getVar('BB_SRCREV_POLICY', d, 1) or "clear"
  81. if srcrev_policy == "cache":
  82. bb.msg.debug(1, bb.msg.domain.Fetcher, "Keeping SRCREV cache due to cache policy of: %s" % srcrev_policy)
  83. elif srcrev_policy == "clear":
  84. bb.msg.debug(1, bb.msg.domain.Fetcher, "Clearing SRCREV cache due to cache policy of: %s" % srcrev_policy)
  85. pd.delDomain("BB_URI_HEADREVS")
  86. else:
  87. bb.msg.fatal(bb.msg.domain.Fetcher, "Invalid SRCREV cache policy of: %s" % srcrev_policy)
  88. # Make sure our domains exist
  89. pd.addDomain("BB_URI_HEADREVS")
  90. pd.addDomain("BB_URI_LOCALCOUNT")
  91. # Function call order is usually:
  92. # 1. init
  93. # 2. go
  94. # 3. localpaths
  95. # localpath can be called at any time
  96. def init(urls, d, setup = True):
  97. urldata = {}
  98. fn = bb.data.getVar('FILE', d, 1)
  99. if fn in urldata_cache:
  100. urldata = urldata_cache[fn]
  101. for url in urls:
  102. if url not in urldata:
  103. urldata[url] = FetchData(url, d)
  104. if setup:
  105. for url in urldata:
  106. if not urldata[url].setup:
  107. urldata[url].setup_localpath(d)
  108. urldata_cache[fn] = urldata
  109. return urldata
  110. def go(d):
  111. """
  112. Fetch all urls
  113. init must have previously been called
  114. """
  115. urldata = init([], d, True)
  116. for u in urldata:
  117. ud = urldata[u]
  118. m = ud.method
  119. if ud.localfile:
  120. if not m.forcefetch(u, ud, d) and os.path.exists(ud.md5):
  121. # File already present along with md5 stamp file
  122. # Touch md5 file to show activity
  123. try:
  124. os.utime(ud.md5, None)
  125. except:
  126. # Errors aren't fatal here
  127. pass
  128. continue
  129. lf = bb.utils.lockfile(ud.lockfile)
  130. if not m.forcefetch(u, ud, d) and os.path.exists(ud.md5):
  131. # If someone else fetched this before we got the lock,
  132. # notice and don't try again
  133. try:
  134. os.utime(ud.md5, None)
  135. except:
  136. # Errors aren't fatal here
  137. pass
  138. bb.utils.unlockfile(lf)
  139. continue
  140. m.go(u, ud, d)
  141. if ud.localfile:
  142. if not m.forcefetch(u, ud, d):
  143. Fetch.write_md5sum(u, ud, d)
  144. bb.utils.unlockfile(lf)
  145. def checkstatus(d):
  146. """
  147. Check all urls exist upstream
  148. init must have previously been called
  149. """
  150. urldata = init([], d, True)
  151. for u in urldata:
  152. ud = urldata[u]
  153. m = ud.method
  154. bb.msg.note(1, bb.msg.domain.Fetcher, "Testing URL %s" % u)
  155. ret = m.checkstatus(u, ud, d)
  156. if not ret:
  157. bb.msg.fatal(bb.msg.domain.Fetcher, "URL %s doesn't work" % u)
  158. def localpaths(d):
  159. """
  160. Return a list of the local filenames, assuming successful fetch
  161. """
  162. local = []
  163. urldata = init([], d, True)
  164. for u in urldata:
  165. ud = urldata[u]
  166. local.append(ud.localpath)
  167. return local
  168. srcrev_internal_call = False
  169. def get_srcrev(d):
  170. """
  171. Return the version string for the current package
  172. (usually to be used as PV)
  173. Most packages usually only have one SCM so we just pass on the call.
  174. In the multi SCM case, we build a value based on SRCREV_FORMAT which must
  175. have been set.
  176. """
  177. #
  178. # Ugly code alert. localpath in the fetchers will try to evaluate SRCREV which
  179. # could translate into a call to here. If it does, we need to catch this
  180. # and provide some way so it knows get_srcrev is active instead of being
  181. # some number etc. hence the srcrev_internal_call tracking and the magic
  182. # "SRCREVINACTION" return value.
  183. #
  184. # Neater solutions welcome!
  185. #
  186. if bb.fetch.srcrev_internal_call:
  187. return "SRCREVINACTION"
  188. scms = []
  189. # Only call setup_localpath on URIs which suppports_srcrev()
  190. urldata = init(bb.data.getVar('SRC_URI', d, 1).split(), d, False)
  191. for u in urldata:
  192. ud = urldata[u]
  193. if ud.method.suppports_srcrev():
  194. if not ud.setup:
  195. ud.setup_localpath(d)
  196. scms.append(u)
  197. if len(scms) == 0:
  198. bb.msg.error(bb.msg.domain.Fetcher, "SRCREV was used yet no valid SCM was found in SRC_URI")
  199. raise ParameterError
  200. if len(scms) == 1:
  201. return urldata[scms[0]].method.sortable_revision(scms[0], urldata[scms[0]], d)
  202. #
  203. # Mutiple SCMs are in SRC_URI so we resort to SRCREV_FORMAT
  204. #
  205. format = bb.data.getVar('SRCREV_FORMAT', d, 1)
  206. if not format:
  207. bb.msg.error(bb.msg.domain.Fetcher, "The SRCREV_FORMAT variable must be set when multiple SCMs are used.")
  208. raise ParameterError
  209. for scm in scms:
  210. if 'name' in urldata[scm].parm:
  211. name = urldata[scm].parm["name"]
  212. rev = urldata[scm].method.sortable_revision(scm, urldata[scm], d)
  213. format = format.replace(name, rev)
  214. return format
  215. def localpath(url, d, cache = True):
  216. """
  217. Called from the parser with cache=False since the cache isn't ready
  218. at this point. Also called from classed in OE e.g. patch.bbclass
  219. """
  220. ud = init([url], d)
  221. if ud[url].method:
  222. return ud[url].localpath
  223. return url
  224. def runfetchcmd(cmd, d, quiet = False):
  225. """
  226. Run cmd returning the command output
  227. Raise an error if interrupted or cmd fails
  228. Optionally echo command output to stdout
  229. """
  230. # Need to export PATH as binary could be in metadata paths
  231. # rather than host provided
  232. # Also include some other variables.
  233. # FIXME: Should really include all export varaiables?
  234. exportvars = ['PATH', 'GIT_PROXY_HOST', 'GIT_PROXY_PORT', 'GIT_PROXY_COMMAND']
  235. for var in exportvars:
  236. val = data.getVar(var, d, True)
  237. if val:
  238. cmd = 'export ' + var + '=%s; %s' % (val, cmd)
  239. bb.msg.debug(1, bb.msg.domain.Fetcher, "Running %s" % cmd)
  240. # redirect stderr to stdout
  241. stdout_handle = os.popen(cmd + " 2>&1", "r")
  242. output = ""
  243. while 1:
  244. line = stdout_handle.readline()
  245. if not line:
  246. break
  247. if not quiet:
  248. print line,
  249. output += line
  250. status = stdout_handle.close() or 0
  251. signal = status >> 8
  252. exitstatus = status & 0xff
  253. if signal:
  254. raise FetchError("Fetch command %s failed with signal %s, output:\n%s" % (cmd, signal, output))
  255. elif status != 0:
  256. raise FetchError("Fetch command %s failed with exit code %s, output:\n%s" % (cmd, status, output))
  257. return output
  258. class FetchData(object):
  259. """
  260. A class which represents the fetcher state for a given URI.
  261. """
  262. def __init__(self, url, d):
  263. self.localfile = ""
  264. (self.type, self.host, self.path, self.user, self.pswd, self.parm) = bb.decodeurl(data.expand(url, d))
  265. self.date = Fetch.getSRCDate(self, d)
  266. self.url = url
  267. self.setup = False
  268. for m in methods:
  269. if m.supports(url, self, d):
  270. self.method = m
  271. return
  272. raise NoMethodError("Missing implementation for url %s" % url)
  273. def setup_localpath(self, d):
  274. self.setup = True
  275. if "localpath" in self.parm:
  276. # if user sets localpath for file, use it instead.
  277. self.localpath = self.parm["localpath"]
  278. else:
  279. bb.fetch.srcrev_internal_call = True
  280. self.localpath = self.method.localpath(self.url, self, d)
  281. bb.fetch.srcrev_internal_call = False
  282. # We have to clear data's internal caches since the cached value of SRCREV is now wrong.
  283. # Horrible...
  284. bb.data.delVar("ISHOULDNEVEREXIST", d)
  285. self.md5 = self.localpath + '.md5'
  286. self.lockfile = self.localpath + '.lock'
  287. class Fetch(object):
  288. """Base class for 'fetch'ing data"""
  289. def __init__(self, urls = []):
  290. self.urls = []
  291. def supports(self, url, urldata, d):
  292. """
  293. Check to see if this fetch class supports a given url.
  294. """
  295. return 0
  296. def localpath(self, url, urldata, d):
  297. """
  298. Return the local filename of a given url assuming a successful fetch.
  299. Can also setup variables in urldata for use in go (saving code duplication
  300. and duplicate code execution)
  301. """
  302. return url
  303. def setUrls(self, urls):
  304. self.__urls = urls
  305. def getUrls(self):
  306. return self.__urls
  307. urls = property(getUrls, setUrls, None, "Urls property")
  308. def forcefetch(self, url, urldata, d):
  309. """
  310. Force a fetch, even if localpath exists?
  311. """
  312. return False
  313. def suppports_srcrev(self):
  314. """
  315. The fetcher supports auto source revisions (SRCREV)
  316. """
  317. return False
  318. def go(self, url, urldata, d):
  319. """
  320. Fetch urls
  321. Assumes localpath was called first
  322. """
  323. raise NoMethodError("Missing implementation for url")
  324. def checkstatus(self, url, urldata, d):
  325. """
  326. Check the status of a URL
  327. Assumes localpath was called first
  328. """
  329. bb.msg.note(1, bb.msg.domain.Fetcher, "URL %s could not be checked for status since no method exists." % url)
  330. return True
  331. def getSRCDate(urldata, d):
  332. """
  333. Return the SRC Date for the component
  334. d the bb.data module
  335. """
  336. if "srcdate" in urldata.parm:
  337. return urldata.parm['srcdate']
  338. pn = data.getVar("PN", d, 1)
  339. if pn:
  340. return data.getVar("SRCDATE_%s" % pn, d, 1) or data.getVar("CVSDATE_%s" % pn, d, 1) or data.getVar("SRCDATE", d, 1) or data.getVar("CVSDATE", d, 1) or data.getVar("DATE", d, 1)
  341. return data.getVar("SRCDATE", d, 1) or data.getVar("CVSDATE", d, 1) or data.getVar("DATE", d, 1)
  342. getSRCDate = staticmethod(getSRCDate)
  343. def srcrev_internal_helper(ud, d):
  344. """
  345. Return:
  346. a) a source revision if specified
  347. b) True if auto srcrev is in action
  348. c) False otherwise
  349. """
  350. if 'rev' in ud.parm:
  351. return ud.parm['rev']
  352. if 'tag' in ud.parm:
  353. return ud.parm['tag']
  354. rev = None
  355. if 'name' in ud.parm:
  356. pn = data.getVar("PN", d, 1)
  357. rev = data.getVar("SRCREV_pn-" + pn + "_" + ud.parm['name'], d, 1)
  358. if not rev:
  359. rev = data.getVar("SRCREV", d, 1)
  360. if not rev:
  361. return False
  362. if rev is "SRCREVINACTION":
  363. return True
  364. return rev
  365. srcrev_internal_helper = staticmethod(srcrev_internal_helper)
  366. def try_mirror(d, tarfn):
  367. """
  368. Try to use a mirrored version of the sources. We do this
  369. to avoid massive loads on foreign cvs and svn servers.
  370. This method will be used by the different fetcher
  371. implementations.
  372. d Is a bb.data instance
  373. tarfn is the name of the tarball
  374. """
  375. tarpath = os.path.join(data.getVar("DL_DIR", d, 1), tarfn)
  376. if os.access(tarpath, os.R_OK):
  377. bb.msg.debug(1, bb.msg.domain.Fetcher, "%s already exists, skipping checkout." % tarfn)
  378. return True
  379. pn = data.getVar('PN', d, True)
  380. src_tarball_stash = None
  381. if pn:
  382. src_tarball_stash = (data.getVar('SRC_TARBALL_STASH_%s' % pn, d, True) or data.getVar('CVS_TARBALL_STASH_%s' % pn, d, True) or data.getVar('SRC_TARBALL_STASH', d, True) or data.getVar('CVS_TARBALL_STASH', d, True) or "").split()
  383. for stash in src_tarball_stash:
  384. fetchcmd = data.getVar("FETCHCOMMAND_mirror", d, True) or data.getVar("FETCHCOMMAND_wget", d, True)
  385. uri = stash + tarfn
  386. bb.msg.note(1, bb.msg.domain.Fetcher, "fetch " + uri)
  387. fetchcmd = fetchcmd.replace("${URI}", uri)
  388. ret = os.system(fetchcmd)
  389. if ret == 0:
  390. bb.msg.note(1, bb.msg.domain.Fetcher, "Fetched %s from tarball stash, skipping checkout" % tarfn)
  391. return True
  392. return False
  393. try_mirror = staticmethod(try_mirror)
  394. def verify_md5sum(ud, got_sum):
  395. """
  396. Verify the md5sum we wanted with the one we got
  397. """
  398. wanted_sum = None
  399. if 'md5sum' in ud.parm:
  400. wanted_sum = ud.parm['md5sum']
  401. if not wanted_sum:
  402. return True
  403. return wanted_sum == got_sum
  404. verify_md5sum = staticmethod(verify_md5sum)
  405. def write_md5sum(url, ud, d):
  406. if bb.which(data.getVar('PATH', d), 'md5sum'):
  407. try:
  408. md5pipe = os.popen('md5sum ' + ud.localpath)
  409. md5data = (md5pipe.readline().split() or [ "" ])[0]
  410. md5pipe.close()
  411. except OSError:
  412. md5data = ""
  413. # verify the md5sum
  414. if not Fetch.verify_md5sum(ud, md5data):
  415. raise MD5SumError(url)
  416. md5out = file(ud.md5, 'w')
  417. md5out.write(md5data)
  418. md5out.close()
  419. write_md5sum = staticmethod(write_md5sum)
  420. def latest_revision(self, url, ud, d):
  421. """
  422. Look in the cache for the latest revision, if not present ask the SCM.
  423. """
  424. if not hasattr(self, "_latest_revision"):
  425. raise ParameterError
  426. pd = persist_data.PersistData(d)
  427. key = self._revision_key(url, ud, d)
  428. rev = pd.getValue("BB_URI_HEADREVS", key)
  429. if rev != None:
  430. return str(rev)
  431. rev = self._latest_revision(url, ud, d)
  432. pd.setValue("BB_URI_HEADREVS", key, rev)
  433. return rev
  434. def sortable_revision(self, url, ud, d):
  435. """
  436. """
  437. if hasattr(self, "_sortable_revision"):
  438. return self._sortable_revision(url, ud, d)
  439. pd = persist_data.PersistData(d)
  440. key = self._revision_key(url, ud, d)
  441. latest_rev = self._build_revision(url, ud, d)
  442. last_rev = pd.getValue("BB_URI_LOCALCOUNT", key + "_rev")
  443. count = pd.getValue("BB_URI_LOCALCOUNT", key + "_count")
  444. if last_rev == latest_rev:
  445. return str(count + "+" + latest_rev)
  446. if count is None:
  447. count = "0"
  448. else:
  449. count = str(int(count) + 1)
  450. pd.setValue("BB_URI_LOCALCOUNT", key + "_rev", latest_rev)
  451. pd.setValue("BB_URI_LOCALCOUNT", key + "_count", count)
  452. return str(count + "+" + latest_rev)
  453. import cvs
  454. import git
  455. import local
  456. import svn
  457. import wget
  458. import svk
  459. import ssh
  460. import perforce
  461. import bzr
  462. import hg
  463. methods.append(local.Local())
  464. methods.append(wget.Wget())
  465. methods.append(svn.Svn())
  466. methods.append(git.Git())
  467. methods.append(cvs.Cvs())
  468. methods.append(svk.Svk())
  469. methods.append(ssh.SSH())
  470. methods.append(perforce.Perforce())
  471. methods.append(bzr.Bzr())
  472. methods.append(hg.Hg())