__init__.py 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  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. # Copyright (C) 2012 Intel Corporation
  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 signal
  27. import logging
  28. import urllib.request, urllib.parse, urllib.error
  29. if 'git' not in urllib.parse.uses_netloc:
  30. urllib.parse.uses_netloc.append('git')
  31. import operator
  32. import collections
  33. import subprocess
  34. import pickle
  35. import bb.persist_data, bb.utils
  36. import bb.checksum
  37. from bb import data
  38. import bb.process
  39. __version__ = "2"
  40. _checksum_cache = bb.checksum.FileChecksumCache()
  41. logger = logging.getLogger("BitBake.Fetcher")
  42. class BBFetchException(Exception):
  43. """Class all fetch exceptions inherit from"""
  44. def __init__(self, message):
  45. self.msg = message
  46. Exception.__init__(self, message)
  47. def __str__(self):
  48. return self.msg
  49. class UntrustedUrl(BBFetchException):
  50. """Exception raised when encountering a host not listed in BB_ALLOWED_NETWORKS"""
  51. def __init__(self, url, message=''):
  52. if message:
  53. msg = message
  54. else:
  55. msg = "The URL: '%s' is not trusted and cannot be used" % url
  56. self.url = url
  57. BBFetchException.__init__(self, msg)
  58. self.args = (url,)
  59. class MalformedUrl(BBFetchException):
  60. """Exception raised when encountering an invalid url"""
  61. def __init__(self, url, message=''):
  62. if message:
  63. msg = message
  64. else:
  65. msg = "The URL: '%s' is invalid and cannot be interpreted" % url
  66. self.url = url
  67. BBFetchException.__init__(self, msg)
  68. self.args = (url,)
  69. class FetchError(BBFetchException):
  70. """General fetcher exception when something happens incorrectly"""
  71. def __init__(self, message, url = None):
  72. if url:
  73. msg = "Fetcher failure for URL: '%s'. %s" % (url, message)
  74. else:
  75. msg = "Fetcher failure: %s" % message
  76. self.url = url
  77. BBFetchException.__init__(self, msg)
  78. self.args = (message, url)
  79. class ChecksumError(FetchError):
  80. """Exception when mismatched checksum encountered"""
  81. def __init__(self, message, url = None, checksum = None):
  82. self.checksum = checksum
  83. FetchError.__init__(self, message, url)
  84. class NoChecksumError(FetchError):
  85. """Exception when no checksum is specified, but BB_STRICT_CHECKSUM is set"""
  86. class UnpackError(BBFetchException):
  87. """General fetcher exception when something happens incorrectly when unpacking"""
  88. def __init__(self, message, url):
  89. msg = "Unpack failure for URL: '%s'. %s" % (url, message)
  90. self.url = url
  91. BBFetchException.__init__(self, msg)
  92. self.args = (message, url)
  93. class NoMethodError(BBFetchException):
  94. """Exception raised when there is no method to obtain a supplied url or set of urls"""
  95. def __init__(self, url):
  96. msg = "Could not find a fetcher which supports the URL: '%s'" % url
  97. self.url = url
  98. BBFetchException.__init__(self, msg)
  99. self.args = (url,)
  100. class MissingParameterError(BBFetchException):
  101. """Exception raised when a fetch method is missing a critical parameter in the url"""
  102. def __init__(self, missing, url):
  103. msg = "URL: '%s' is missing the required parameter '%s'" % (url, missing)
  104. self.url = url
  105. self.missing = missing
  106. BBFetchException.__init__(self, msg)
  107. self.args = (missing, url)
  108. class ParameterError(BBFetchException):
  109. """Exception raised when a url cannot be proccessed due to invalid parameters."""
  110. def __init__(self, message, url):
  111. msg = "URL: '%s' has invalid parameters. %s" % (url, message)
  112. self.url = url
  113. BBFetchException.__init__(self, msg)
  114. self.args = (message, url)
  115. class NetworkAccess(BBFetchException):
  116. """Exception raised when network access is disabled but it is required."""
  117. def __init__(self, url, cmd):
  118. msg = "Network access disabled through BB_NO_NETWORK (or set indirectly due to use of BB_FETCH_PREMIRRORONLY) but access requested with command %s (for url %s)" % (cmd, url)
  119. self.url = url
  120. self.cmd = cmd
  121. BBFetchException.__init__(self, msg)
  122. self.args = (url, cmd)
  123. class NonLocalMethod(Exception):
  124. def __init__(self):
  125. Exception.__init__(self)
  126. class URI(object):
  127. """
  128. A class representing a generic URI, with methods for
  129. accessing the URI components, and stringifies to the
  130. URI.
  131. It is constructed by calling it with a URI, or setting
  132. the attributes manually:
  133. uri = URI("http://example.com/")
  134. uri = URI()
  135. uri.scheme = 'http'
  136. uri.hostname = 'example.com'
  137. uri.path = '/'
  138. It has the following attributes:
  139. * scheme (read/write)
  140. * userinfo (authentication information) (read/write)
  141. * username (read/write)
  142. * password (read/write)
  143. Note, password is deprecated as of RFC 3986.
  144. * hostname (read/write)
  145. * port (read/write)
  146. * hostport (read only)
  147. "hostname:port", if both are set, otherwise just "hostname"
  148. * path (read/write)
  149. * path_quoted (read/write)
  150. A URI quoted version of path
  151. * params (dict) (read/write)
  152. * query (dict) (read/write)
  153. * relative (bool) (read only)
  154. True if this is a "relative URI", (e.g. file:foo.diff)
  155. It stringifies to the URI itself.
  156. Some notes about relative URIs: while it's specified that
  157. a URI beginning with <scheme>:// should either be directly
  158. followed by a hostname or a /, the old URI handling of the
  159. fetch2 library did not comform to this. Therefore, this URI
  160. class has some kludges to make sure that URIs are parsed in
  161. a way comforming to bitbake's current usage. This URI class
  162. supports the following:
  163. file:relative/path.diff (IETF compliant)
  164. git:relative/path.git (IETF compliant)
  165. git:///absolute/path.git (IETF compliant)
  166. file:///absolute/path.diff (IETF compliant)
  167. file://relative/path.diff (not IETF compliant)
  168. But it does not support the following:
  169. file://hostname/absolute/path.diff (would be IETF compliant)
  170. Note that the last case only applies to a list of
  171. "whitelisted" schemes (currently only file://), that requires
  172. its URIs to not have a network location.
  173. """
  174. _relative_schemes = ['file', 'git']
  175. _netloc_forbidden = ['file']
  176. def __init__(self, uri=None):
  177. self.scheme = ''
  178. self.userinfo = ''
  179. self.hostname = ''
  180. self.port = None
  181. self._path = ''
  182. self.params = {}
  183. self.query = {}
  184. self.relative = False
  185. if not uri:
  186. return
  187. # We hijack the URL parameters, since the way bitbake uses
  188. # them are not quite RFC compliant.
  189. uri, param_str = (uri.split(";", 1) + [None])[:2]
  190. urlp = urllib.parse.urlparse(uri)
  191. self.scheme = urlp.scheme
  192. reparse = 0
  193. # Coerce urlparse to make URI scheme use netloc
  194. if not self.scheme in urllib.parse.uses_netloc:
  195. urllib.parse.uses_params.append(self.scheme)
  196. reparse = 1
  197. # Make urlparse happy(/ier) by converting local resources
  198. # to RFC compliant URL format. E.g.:
  199. # file://foo.diff -> file:foo.diff
  200. if urlp.scheme in self._netloc_forbidden:
  201. uri = re.sub("(?<=:)//(?!/)", "", uri, 1)
  202. reparse = 1
  203. if reparse:
  204. urlp = urllib.parse.urlparse(uri)
  205. # Identify if the URI is relative or not
  206. if urlp.scheme in self._relative_schemes and \
  207. re.compile("^\w+:(?!//)").match(uri):
  208. self.relative = True
  209. if not self.relative:
  210. self.hostname = urlp.hostname or ''
  211. self.port = urlp.port
  212. self.userinfo += urlp.username or ''
  213. if urlp.password:
  214. self.userinfo += ':%s' % urlp.password
  215. self.path = urllib.parse.unquote(urlp.path)
  216. if param_str:
  217. self.params = self._param_str_split(param_str, ";")
  218. if urlp.query:
  219. self.query = self._param_str_split(urlp.query, "&")
  220. def __str__(self):
  221. userinfo = self.userinfo
  222. if userinfo:
  223. userinfo += '@'
  224. return "%s:%s%s%s%s%s%s" % (
  225. self.scheme,
  226. '' if self.relative else '//',
  227. userinfo,
  228. self.hostport,
  229. self.path_quoted,
  230. self._query_str(),
  231. self._param_str())
  232. def _param_str(self):
  233. return (
  234. ''.join([';', self._param_str_join(self.params, ";")])
  235. if self.params else '')
  236. def _query_str(self):
  237. return (
  238. ''.join(['?', self._param_str_join(self.query, "&")])
  239. if self.query else '')
  240. def _param_str_split(self, string, elmdelim, kvdelim="="):
  241. ret = collections.OrderedDict()
  242. for k, v in [x.split(kvdelim, 1) for x in string.split(elmdelim)]:
  243. ret[k] = v
  244. return ret
  245. def _param_str_join(self, dict_, elmdelim, kvdelim="="):
  246. return elmdelim.join([kvdelim.join([k, v]) for k, v in dict_.items()])
  247. @property
  248. def hostport(self):
  249. if not self.port:
  250. return self.hostname
  251. return "%s:%d" % (self.hostname, self.port)
  252. @property
  253. def path_quoted(self):
  254. return urllib.parse.quote(self.path)
  255. @path_quoted.setter
  256. def path_quoted(self, path):
  257. self.path = urllib.parse.unquote(path)
  258. @property
  259. def path(self):
  260. return self._path
  261. @path.setter
  262. def path(self, path):
  263. self._path = path
  264. if not path or re.compile("^/").match(path):
  265. self.relative = False
  266. else:
  267. self.relative = True
  268. @property
  269. def username(self):
  270. if self.userinfo:
  271. return (self.userinfo.split(":", 1))[0]
  272. return ''
  273. @username.setter
  274. def username(self, username):
  275. password = self.password
  276. self.userinfo = username
  277. if password:
  278. self.userinfo += ":%s" % password
  279. @property
  280. def password(self):
  281. if self.userinfo and ":" in self.userinfo:
  282. return (self.userinfo.split(":", 1))[1]
  283. return ''
  284. @password.setter
  285. def password(self, password):
  286. self.userinfo = "%s:%s" % (self.username, password)
  287. def decodeurl(url):
  288. """Decodes an URL into the tokens (scheme, network location, path,
  289. user, password, parameters).
  290. """
  291. m = re.compile('(?P<type>[^:]*)://((?P<user>[^/]+)@)?(?P<location>[^;]+)(;(?P<parm>.*))?').match(url)
  292. if not m:
  293. raise MalformedUrl(url)
  294. type = m.group('type')
  295. location = m.group('location')
  296. if not location:
  297. raise MalformedUrl(url)
  298. user = m.group('user')
  299. parm = m.group('parm')
  300. locidx = location.find('/')
  301. if locidx != -1 and type.lower() != 'file':
  302. host = location[:locidx]
  303. path = location[locidx:]
  304. elif type.lower() == 'file':
  305. host = ""
  306. path = location
  307. else:
  308. host = location
  309. path = ""
  310. if user:
  311. m = re.compile('(?P<user>[^:]+)(:?(?P<pswd>.*))').match(user)
  312. if m:
  313. user = m.group('user')
  314. pswd = m.group('pswd')
  315. else:
  316. user = ''
  317. pswd = ''
  318. p = collections.OrderedDict()
  319. if parm:
  320. for s in parm.split(';'):
  321. if s:
  322. if not '=' in s:
  323. raise MalformedUrl(url, "The URL: '%s' is invalid: parameter %s does not specify a value (missing '=')" % (url, s))
  324. s1, s2 = s.split('=')
  325. p[s1] = s2
  326. return type, host, urllib.parse.unquote(path), user, pswd, p
  327. def encodeurl(decoded):
  328. """Encodes a URL from tokens (scheme, network location, path,
  329. user, password, parameters).
  330. """
  331. type, host, path, user, pswd, p = decoded
  332. if not path:
  333. raise MissingParameterError('path', "encoded from the data %s" % str(decoded))
  334. if not type:
  335. raise MissingParameterError('type', "encoded from the data %s" % str(decoded))
  336. url = '%s://' % type
  337. if user and type != "file":
  338. url += "%s" % user
  339. if pswd:
  340. url += ":%s" % pswd
  341. url += "@"
  342. if host and type != "file":
  343. url += "%s" % host
  344. # Standardise path to ensure comparisons work
  345. while '//' in path:
  346. path = path.replace("//", "/")
  347. url += "%s" % urllib.parse.quote(path)
  348. if p:
  349. for parm in p:
  350. url += ";%s=%s" % (parm, p[parm])
  351. return url
  352. def uri_replace(ud, uri_find, uri_replace, replacements, d):
  353. if not ud.url or not uri_find or not uri_replace:
  354. logger.error("uri_replace: passed an undefined value, not replacing")
  355. return None
  356. uri_decoded = list(decodeurl(ud.url))
  357. uri_find_decoded = list(decodeurl(uri_find))
  358. uri_replace_decoded = list(decodeurl(uri_replace))
  359. logger.debug(2, "For url %s comparing %s to %s" % (uri_decoded, uri_find_decoded, uri_replace_decoded))
  360. result_decoded = ['', '', '', '', '', {}]
  361. for loc, i in enumerate(uri_find_decoded):
  362. result_decoded[loc] = uri_decoded[loc]
  363. regexp = i
  364. if loc == 0 and regexp and not regexp.endswith("$"):
  365. # Leaving the type unanchored can mean "https" matching "file" can become "files"
  366. # which is clearly undesirable.
  367. regexp += "$"
  368. if loc == 5:
  369. # Handle URL parameters
  370. if i:
  371. # Any specified URL parameters must match
  372. for k in uri_replace_decoded[loc]:
  373. if uri_decoded[loc][k] != uri_replace_decoded[loc][k]:
  374. return None
  375. # Overwrite any specified replacement parameters
  376. for k in uri_replace_decoded[loc]:
  377. for l in replacements:
  378. uri_replace_decoded[loc][k] = uri_replace_decoded[loc][k].replace(l, replacements[l])
  379. result_decoded[loc][k] = uri_replace_decoded[loc][k]
  380. elif (re.match(regexp, uri_decoded[loc])):
  381. if not uri_replace_decoded[loc]:
  382. result_decoded[loc] = ""
  383. else:
  384. for k in replacements:
  385. uri_replace_decoded[loc] = uri_replace_decoded[loc].replace(k, replacements[k])
  386. #bb.note("%s %s %s" % (regexp, uri_replace_decoded[loc], uri_decoded[loc]))
  387. result_decoded[loc] = re.sub(regexp, uri_replace_decoded[loc], uri_decoded[loc], 1)
  388. if loc == 2:
  389. # Handle path manipulations
  390. basename = None
  391. if uri_decoded[0] != uri_replace_decoded[0] and ud.mirrortarball:
  392. # If the source and destination url types differ, must be a mirrortarball mapping
  393. basename = os.path.basename(ud.mirrortarball)
  394. # Kill parameters, they make no sense for mirror tarballs
  395. uri_decoded[5] = {}
  396. elif ud.localpath and ud.method.supports_checksum(ud):
  397. basename = os.path.basename(ud.localpath)
  398. if basename and not result_decoded[loc].endswith(basename):
  399. result_decoded[loc] = os.path.join(result_decoded[loc], basename)
  400. else:
  401. return None
  402. result = encodeurl(result_decoded)
  403. if result == ud.url:
  404. return None
  405. logger.debug(2, "For url %s returning %s" % (ud.url, result))
  406. return result
  407. methods = []
  408. urldata_cache = {}
  409. saved_headrevs = {}
  410. def fetcher_init(d):
  411. """
  412. Called to initialize the fetchers once the configuration data is known.
  413. Calls before this must not hit the cache.
  414. """
  415. # When to drop SCM head revisions controlled by user policy
  416. srcrev_policy = d.getVar('BB_SRCREV_POLICY') or "clear"
  417. if srcrev_policy == "cache":
  418. logger.debug(1, "Keeping SRCREV cache due to cache policy of: %s", srcrev_policy)
  419. elif srcrev_policy == "clear":
  420. logger.debug(1, "Clearing SRCREV cache due to cache policy of: %s", srcrev_policy)
  421. revs = bb.persist_data.persist('BB_URI_HEADREVS', d)
  422. try:
  423. bb.fetch2.saved_headrevs = revs.items()
  424. except:
  425. pass
  426. revs.clear()
  427. else:
  428. raise FetchError("Invalid SRCREV cache policy of: %s" % srcrev_policy)
  429. _checksum_cache.init_cache(d)
  430. for m in methods:
  431. if hasattr(m, "init"):
  432. m.init(d)
  433. def fetcher_parse_save():
  434. _checksum_cache.save_extras()
  435. def fetcher_parse_done():
  436. _checksum_cache.save_merge()
  437. def fetcher_compare_revisions():
  438. """
  439. Compare the revisions in the persistant cache with current values and
  440. return true/false on whether they've changed.
  441. """
  442. data = bb.persist_data.persist('BB_URI_HEADREVS', d).items()
  443. data2 = bb.fetch2.saved_headrevs
  444. changed = False
  445. for key in data:
  446. if key not in data2 or data2[key] != data[key]:
  447. logger.debug(1, "%s changed", key)
  448. changed = True
  449. return True
  450. else:
  451. logger.debug(2, "%s did not change", key)
  452. return False
  453. def mirror_from_string(data):
  454. return [ i.split() for i in (data or "").replace('\\n','\n').split('\n') if i ]
  455. def verify_checksum(ud, d, precomputed={}):
  456. """
  457. verify the MD5 and SHA256 checksum for downloaded src
  458. Raises a FetchError if one or both of the SRC_URI checksums do not match
  459. the downloaded file, or if BB_STRICT_CHECKSUM is set and there are no
  460. checksums specified.
  461. Returns a dict of checksums that can be stored in a done stamp file and
  462. passed in as precomputed parameter in a later call to avoid re-computing
  463. the checksums from the file. This allows verifying the checksums of the
  464. file against those in the recipe each time, rather than only after
  465. downloading. See https://bugzilla.yoctoproject.org/show_bug.cgi?id=5571.
  466. """
  467. _MD5_KEY = "md5"
  468. _SHA256_KEY = "sha256"
  469. if ud.ignore_checksums or not ud.method.supports_checksum(ud):
  470. return {}
  471. if _MD5_KEY in precomputed:
  472. md5data = precomputed[_MD5_KEY]
  473. else:
  474. md5data = bb.utils.md5_file(ud.localpath)
  475. if _SHA256_KEY in precomputed:
  476. sha256data = precomputed[_SHA256_KEY]
  477. else:
  478. sha256data = bb.utils.sha256_file(ud.localpath)
  479. if ud.method.recommends_checksum(ud) and not ud.md5_expected and not ud.sha256_expected:
  480. # If strict checking enabled and neither sum defined, raise error
  481. strict = d.getVar("BB_STRICT_CHECKSUM") or "0"
  482. if strict == "1":
  483. logger.error('No checksum specified for %s, please add at least one to the recipe:\n'
  484. 'SRC_URI[%s] = "%s"\nSRC_URI[%s] = "%s"' %
  485. (ud.localpath, ud.md5_name, md5data,
  486. ud.sha256_name, sha256data))
  487. raise NoChecksumError('Missing SRC_URI checksum', ud.url)
  488. # Log missing sums so user can more easily add them
  489. logger.warning('Missing md5 SRC_URI checksum for %s, consider adding to the recipe:\n'
  490. 'SRC_URI[%s] = "%s"',
  491. ud.localpath, ud.md5_name, md5data)
  492. logger.warning('Missing sha256 SRC_URI checksum for %s, consider adding to the recipe:\n'
  493. 'SRC_URI[%s] = "%s"',
  494. ud.localpath, ud.sha256_name, sha256data)
  495. # We want to alert the user if a checksum is defined in the recipe but
  496. # it does not match.
  497. msg = ""
  498. mismatch = False
  499. if ud.md5_expected and ud.md5_expected != md5data:
  500. msg = msg + "\nFile: '%s' has %s checksum %s when %s was expected" % (ud.localpath, 'md5', md5data, ud.md5_expected)
  501. mismatch = True;
  502. if ud.sha256_expected and ud.sha256_expected != sha256data:
  503. msg = msg + "\nFile: '%s' has %s checksum %s when %s was expected" % (ud.localpath, 'sha256', sha256data, ud.sha256_expected)
  504. mismatch = True;
  505. if mismatch:
  506. msg = msg + '\nIf this change is expected (e.g. you have upgraded to a new version without updating the checksums) then you can use these lines within the recipe:\nSRC_URI[%s] = "%s"\nSRC_URI[%s] = "%s"\nOtherwise you should retry the download and/or check with upstream to determine if the file has become corrupted or otherwise unexpectedly modified.\n' % (ud.md5_name, md5data, ud.sha256_name, sha256data)
  507. if len(msg):
  508. raise ChecksumError('Checksum mismatch!%s' % msg, ud.url, md5data)
  509. return {
  510. _MD5_KEY: md5data,
  511. _SHA256_KEY: sha256data
  512. }
  513. def verify_donestamp(ud, d, origud=None):
  514. """
  515. Check whether the done stamp file has the right checksums (if the fetch
  516. method supports them). If it doesn't, delete the done stamp and force
  517. a re-download.
  518. Returns True, if the donestamp exists and is valid, False otherwise. When
  519. returning False, any existing done stamps are removed.
  520. """
  521. if not ud.needdonestamp:
  522. return True
  523. if not os.path.exists(ud.donestamp):
  524. return False
  525. if (not ud.method.supports_checksum(ud) or
  526. (origud and not origud.method.supports_checksum(origud))):
  527. # done stamp exists, checksums not supported; assume the local file is
  528. # current
  529. return True
  530. if not os.path.exists(ud.localpath):
  531. # done stamp exists, but the downloaded file does not; the done stamp
  532. # must be incorrect, re-trigger the download
  533. bb.utils.remove(ud.donestamp)
  534. return False
  535. precomputed_checksums = {}
  536. # Only re-use the precomputed checksums if the donestamp is newer than the
  537. # file. Do not rely on the mtime of directories, though. If ud.localpath is
  538. # a directory, there will probably not be any checksums anyway.
  539. if (os.path.isdir(ud.localpath) or
  540. os.path.getmtime(ud.localpath) < os.path.getmtime(ud.donestamp)):
  541. try:
  542. with open(ud.donestamp, "rb") as cachefile:
  543. pickled = pickle.Unpickler(cachefile)
  544. precomputed_checksums.update(pickled.load())
  545. except Exception as e:
  546. # Avoid the warnings on the upgrade path from emtpy done stamp
  547. # files to those containing the checksums.
  548. if not isinstance(e, EOFError):
  549. # Ignore errors, they aren't fatal
  550. logger.warning("Couldn't load checksums from donestamp %s: %s "
  551. "(msg: %s)" % (ud.donestamp, type(e).__name__,
  552. str(e)))
  553. try:
  554. checksums = verify_checksum(ud, d, precomputed_checksums)
  555. # If the cache file did not have the checksums, compute and store them
  556. # as an upgrade path from the previous done stamp file format.
  557. if checksums != precomputed_checksums:
  558. with open(ud.donestamp, "wb") as cachefile:
  559. p = pickle.Pickler(cachefile, 2)
  560. p.dump(checksums)
  561. return True
  562. except ChecksumError as e:
  563. # Checksums failed to verify, trigger re-download and remove the
  564. # incorrect stamp file.
  565. logger.warning("Checksum mismatch for local file %s\n"
  566. "Cleaning and trying again." % ud.localpath)
  567. if os.path.exists(ud.localpath):
  568. rename_bad_checksum(ud, e.checksum)
  569. bb.utils.remove(ud.donestamp)
  570. return False
  571. def update_stamp(ud, d):
  572. """
  573. donestamp is file stamp indicating the whole fetching is done
  574. this function update the stamp after verifying the checksum
  575. """
  576. if not ud.needdonestamp:
  577. return
  578. if os.path.exists(ud.donestamp):
  579. # Touch the done stamp file to show active use of the download
  580. try:
  581. os.utime(ud.donestamp, None)
  582. except:
  583. # Errors aren't fatal here
  584. pass
  585. else:
  586. try:
  587. checksums = verify_checksum(ud, d)
  588. # Store the checksums for later re-verification against the recipe
  589. with open(ud.donestamp, "wb") as cachefile:
  590. p = pickle.Pickler(cachefile, 2)
  591. p.dump(checksums)
  592. except ChecksumError as e:
  593. # Checksums failed to verify, trigger re-download and remove the
  594. # incorrect stamp file.
  595. logger.warning("Checksum mismatch for local file %s\n"
  596. "Cleaning and trying again." % ud.localpath)
  597. if os.path.exists(ud.localpath):
  598. rename_bad_checksum(ud, e.checksum)
  599. bb.utils.remove(ud.donestamp)
  600. raise
  601. def subprocess_setup():
  602. # Python installs a SIGPIPE handler by default. This is usually not what
  603. # non-Python subprocesses expect.
  604. # SIGPIPE errors are known issues with gzip/bash
  605. signal.signal(signal.SIGPIPE, signal.SIG_DFL)
  606. def get_autorev(d):
  607. # only not cache src rev in autorev case
  608. if d.getVar('BB_SRCREV_POLICY') != "cache":
  609. d.setVar('BB_DONT_CACHE', '1')
  610. return "AUTOINC"
  611. def get_srcrev(d, method_name='sortable_revision'):
  612. """
  613. Return the revsion string, usually for use in the version string (PV) of the current package
  614. Most packages usually only have one SCM so we just pass on the call.
  615. In the multi SCM case, we build a value based on SRCREV_FORMAT which must
  616. have been set.
  617. The idea here is that we put the string "AUTOINC+" into return value if the revisions are not
  618. incremental, other code is then responsible for turning that into an increasing value (if needed)
  619. A method_name can be supplied to retrieve an alternatively formatted revision from a fetcher, if
  620. that fetcher provides a method with the given name and the same signature as sortable_revision.
  621. """
  622. scms = []
  623. fetcher = Fetch(d.getVar('SRC_URI').split(), d)
  624. urldata = fetcher.ud
  625. for u in urldata:
  626. if urldata[u].method.supports_srcrev():
  627. scms.append(u)
  628. if len(scms) == 0:
  629. raise FetchError("SRCREV was used yet no valid SCM was found in SRC_URI")
  630. if len(scms) == 1 and len(urldata[scms[0]].names) == 1:
  631. autoinc, rev = getattr(urldata[scms[0]].method, method_name)(urldata[scms[0]], d, urldata[scms[0]].names[0])
  632. if len(rev) > 10:
  633. rev = rev[:10]
  634. if autoinc:
  635. return "AUTOINC+" + rev
  636. return rev
  637. #
  638. # Mutiple SCMs are in SRC_URI so we resort to SRCREV_FORMAT
  639. #
  640. format = d.getVar('SRCREV_FORMAT')
  641. if not format:
  642. raise FetchError("The SRCREV_FORMAT variable must be set when multiple SCMs are used.")
  643. name_to_rev = {}
  644. seenautoinc = False
  645. for scm in scms:
  646. ud = urldata[scm]
  647. for name in ud.names:
  648. autoinc, rev = getattr(ud.method, method_name)(ud, d, name)
  649. seenautoinc = seenautoinc or autoinc
  650. if len(rev) > 10:
  651. rev = rev[:10]
  652. name_to_rev[name] = rev
  653. # Replace names by revisions in the SRCREV_FORMAT string. The approach used
  654. # here can handle names being prefixes of other names and names appearing
  655. # as substrings in revisions (in which case the name should not be
  656. # expanded). The '|' regular expression operator tries matches from left to
  657. # right, so we need to sort the names with the longest ones first.
  658. names_descending_len = sorted(name_to_rev, key=len, reverse=True)
  659. name_to_rev_re = "|".join(re.escape(name) for name in names_descending_len)
  660. format = re.sub(name_to_rev_re, lambda match: name_to_rev[match.group(0)], format)
  661. if seenautoinc:
  662. format = "AUTOINC+" + format
  663. return format
  664. def localpath(url, d):
  665. fetcher = bb.fetch2.Fetch([url], d)
  666. return fetcher.localpath(url)
  667. def runfetchcmd(cmd, d, quiet=False, cleanup=None, log=None, workdir=None):
  668. """
  669. Run cmd returning the command output
  670. Raise an error if interrupted or cmd fails
  671. Optionally echo command output to stdout
  672. Optionally remove the files/directories listed in cleanup upon failure
  673. """
  674. # Need to export PATH as binary could be in metadata paths
  675. # rather than host provided
  676. # Also include some other variables.
  677. # FIXME: Should really include all export varaiables?
  678. exportvars = ['HOME', 'PATH',
  679. 'HTTP_PROXY', 'http_proxy',
  680. 'HTTPS_PROXY', 'https_proxy',
  681. 'FTP_PROXY', 'ftp_proxy',
  682. 'FTPS_PROXY', 'ftps_proxy',
  683. 'NO_PROXY', 'no_proxy',
  684. 'ALL_PROXY', 'all_proxy',
  685. 'GIT_PROXY_COMMAND',
  686. 'GIT_SSL_CAINFO',
  687. 'GIT_SMART_HTTP',
  688. 'SSH_AUTH_SOCK', 'SSH_AGENT_PID',
  689. 'SOCKS5_USER', 'SOCKS5_PASSWD',
  690. 'DBUS_SESSION_BUS_ADDRESS',
  691. 'P4CONFIG']
  692. if not cleanup:
  693. cleanup = []
  694. origenv = d.getVar("BB_ORIGENV", False)
  695. for var in exportvars:
  696. val = d.getVar(var) or (origenv and origenv.getVar(var))
  697. if val:
  698. cmd = 'export ' + var + '=\"%s\"; %s' % (val, cmd)
  699. logger.debug(1, "Running %s", cmd)
  700. success = False
  701. error_message = ""
  702. try:
  703. (output, errors) = bb.process.run(cmd, log=log, shell=True, stderr=subprocess.PIPE, cwd=workdir)
  704. success = True
  705. except bb.process.NotFoundError as e:
  706. error_message = "Fetch command %s" % (e.command)
  707. except bb.process.ExecutionError as e:
  708. if e.stdout:
  709. output = "output:\n%s\n%s" % (e.stdout, e.stderr)
  710. elif e.stderr:
  711. output = "output:\n%s" % e.stderr
  712. else:
  713. output = "no output"
  714. error_message = "Fetch command %s failed with exit code %s, %s" % (e.command, e.exitcode, output)
  715. except bb.process.CmdError as e:
  716. error_message = "Fetch command %s could not be run:\n%s" % (e.command, e.msg)
  717. if not success:
  718. for f in cleanup:
  719. try:
  720. bb.utils.remove(f, True)
  721. except OSError:
  722. pass
  723. raise FetchError(error_message)
  724. return output
  725. def check_network_access(d, info = "", url = None):
  726. """
  727. log remote network access, and error if BB_NO_NETWORK is set
  728. """
  729. if d.getVar("BB_NO_NETWORK") == "1":
  730. raise NetworkAccess(url, info)
  731. else:
  732. logger.debug(1, "Fetcher accessed the network with the command %s" % info)
  733. def build_mirroruris(origud, mirrors, ld):
  734. uris = []
  735. uds = []
  736. replacements = {}
  737. replacements["TYPE"] = origud.type
  738. replacements["HOST"] = origud.host
  739. replacements["PATH"] = origud.path
  740. replacements["BASENAME"] = origud.path.split("/")[-1]
  741. replacements["MIRRORNAME"] = origud.host.replace(':','.') + origud.path.replace('/', '.').replace('*', '.')
  742. def adduri(ud, uris, uds, mirrors):
  743. for line in mirrors:
  744. try:
  745. (find, replace) = line
  746. except ValueError:
  747. continue
  748. newuri = uri_replace(ud, find, replace, replacements, ld)
  749. if not newuri or newuri in uris or newuri == origud.url:
  750. continue
  751. if not trusted_network(ld, newuri):
  752. logger.debug(1, "Mirror %s not in the list of trusted networks, skipping" % (newuri))
  753. continue
  754. # Create a local copy of the mirrors minus the current line
  755. # this will prevent us from recursively processing the same line
  756. # as well as indirect recursion A -> B -> C -> A
  757. localmirrors = list(mirrors)
  758. localmirrors.remove(line)
  759. try:
  760. newud = FetchData(newuri, ld)
  761. newud.setup_localpath(ld)
  762. except bb.fetch2.BBFetchException as e:
  763. logger.debug(1, "Mirror fetch failure for url %s (original url: %s)" % (newuri, origud.url))
  764. logger.debug(1, str(e))
  765. try:
  766. # setup_localpath of file:// urls may fail, we should still see
  767. # if mirrors of the url exist
  768. adduri(newud, uris, uds, localmirrors)
  769. except UnboundLocalError:
  770. pass
  771. continue
  772. uris.append(newuri)
  773. uds.append(newud)
  774. adduri(newud, uris, uds, localmirrors)
  775. adduri(origud, uris, uds, mirrors)
  776. return uris, uds
  777. def rename_bad_checksum(ud, suffix):
  778. """
  779. Renames files to have suffix from parameter
  780. """
  781. if ud.localpath is None:
  782. return
  783. new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix)
  784. bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath))
  785. bb.utils.movefile(ud.localpath, new_localpath)
  786. def try_mirror_url(fetch, origud, ud, ld, check = False):
  787. # Return of None or a value means we're finished
  788. # False means try another url
  789. if ud.lockfile and ud.lockfile != origud.lockfile:
  790. lf = bb.utils.lockfile(ud.lockfile)
  791. try:
  792. if check:
  793. found = ud.method.checkstatus(fetch, ud, ld)
  794. if found:
  795. return found
  796. return False
  797. if not verify_donestamp(ud, ld, origud) or ud.method.need_update(ud, ld):
  798. ud.method.download(ud, ld)
  799. if hasattr(ud.method,"build_mirror_data"):
  800. ud.method.build_mirror_data(ud, ld)
  801. if not ud.localpath or not os.path.exists(ud.localpath):
  802. return False
  803. if ud.localpath == origud.localpath:
  804. return ud.localpath
  805. # We may be obtaining a mirror tarball which needs further processing by the real fetcher
  806. # If that tarball is a local file:// we need to provide a symlink to it
  807. dldir = ld.getVar("DL_DIR")
  808. if origud.mirrortarball and os.path.basename(ud.localpath) == os.path.basename(origud.mirrortarball) \
  809. and os.path.basename(ud.localpath) != os.path.basename(origud.localpath):
  810. # Create donestamp in old format to avoid triggering a re-download
  811. if ud.donestamp:
  812. bb.utils.mkdirhier(os.path.dirname(ud.donestamp))
  813. open(ud.donestamp, 'w').close()
  814. dest = os.path.join(dldir, os.path.basename(ud.localpath))
  815. if not os.path.exists(dest):
  816. os.symlink(ud.localpath, dest)
  817. if not verify_donestamp(origud, ld) or origud.method.need_update(origud, ld):
  818. origud.method.download(origud, ld)
  819. if hasattr(origud.method,"build_mirror_data"):
  820. origud.method.build_mirror_data(origud, ld)
  821. return origud.localpath
  822. # Otherwise the result is a local file:// and we symlink to it
  823. if not os.path.exists(origud.localpath):
  824. if os.path.islink(origud.localpath):
  825. # Broken symbolic link
  826. os.unlink(origud.localpath)
  827. os.symlink(ud.localpath, origud.localpath)
  828. update_stamp(origud, ld)
  829. return ud.localpath
  830. except bb.fetch2.NetworkAccess:
  831. raise
  832. except bb.fetch2.BBFetchException as e:
  833. if isinstance(e, ChecksumError):
  834. logger.warning("Mirror checksum failure for url %s (original url: %s)\nCleaning and trying again." % (ud.url, origud.url))
  835. logger.warning(str(e))
  836. if os.path.exists(ud.localpath):
  837. rename_bad_checksum(ud, e.checksum)
  838. elif isinstance(e, NoChecksumError):
  839. raise
  840. else:
  841. logger.debug(1, "Mirror fetch failure for url %s (original url: %s)" % (ud.url, origud.url))
  842. logger.debug(1, str(e))
  843. try:
  844. ud.method.clean(ud, ld)
  845. except UnboundLocalError:
  846. pass
  847. return False
  848. finally:
  849. if ud.lockfile and ud.lockfile != origud.lockfile:
  850. bb.utils.unlockfile(lf)
  851. def try_mirrors(fetch, d, origud, mirrors, check = False):
  852. """
  853. Try to use a mirrored version of the sources.
  854. This method will be automatically called before the fetchers go.
  855. d Is a bb.data instance
  856. uri is the original uri we're trying to download
  857. mirrors is the list of mirrors we're going to try
  858. """
  859. ld = d.createCopy()
  860. uris, uds = build_mirroruris(origud, mirrors, ld)
  861. for index, uri in enumerate(uris):
  862. ret = try_mirror_url(fetch, origud, uds[index], ld, check)
  863. if ret != False:
  864. return ret
  865. return None
  866. def trusted_network(d, url):
  867. """
  868. Use a trusted url during download if networking is enabled and
  869. BB_ALLOWED_NETWORKS is set globally or for a specific recipe.
  870. Note: modifies SRC_URI & mirrors.
  871. """
  872. if d.getVar('BB_NO_NETWORK') == "1":
  873. return True
  874. pkgname = d.expand(d.getVar('PN', False))
  875. trusted_hosts = d.getVarFlag('BB_ALLOWED_NETWORKS', pkgname, False)
  876. if not trusted_hosts:
  877. trusted_hosts = d.getVar('BB_ALLOWED_NETWORKS')
  878. # Not enabled.
  879. if not trusted_hosts:
  880. return True
  881. scheme, network, path, user, passwd, param = decodeurl(url)
  882. if not network:
  883. return True
  884. network = network.split(':')[0]
  885. network = network.lower()
  886. for host in trusted_hosts.split(" "):
  887. host = host.lower()
  888. if host.startswith("*.") and ("." + network).endswith(host[1:]):
  889. return True
  890. if host == network:
  891. return True
  892. return False
  893. def srcrev_internal_helper(ud, d, name):
  894. """
  895. Return:
  896. a) a source revision if specified
  897. b) latest revision if SRCREV="AUTOINC"
  898. c) None if not specified
  899. """
  900. srcrev = None
  901. pn = d.getVar("PN")
  902. attempts = []
  903. if name != '' and pn:
  904. attempts.append("SRCREV_%s_pn-%s" % (name, pn))
  905. if name != '':
  906. attempts.append("SRCREV_%s" % name)
  907. if pn:
  908. attempts.append("SRCREV_pn-%s" % pn)
  909. attempts.append("SRCREV")
  910. for a in attempts:
  911. srcrev = d.getVar(a)
  912. if srcrev and srcrev != "INVALID":
  913. break
  914. if 'rev' in ud.parm and 'tag' in ud.parm:
  915. raise FetchError("Please specify a ;rev= parameter or a ;tag= parameter in the url %s but not both." % (ud.url))
  916. if 'rev' in ud.parm or 'tag' in ud.parm:
  917. if 'rev' in ud.parm:
  918. parmrev = ud.parm['rev']
  919. else:
  920. parmrev = ud.parm['tag']
  921. if srcrev == "INVALID" or not srcrev:
  922. return parmrev
  923. if srcrev != parmrev:
  924. raise FetchError("Conflicting revisions (%s from SRCREV and %s from the url) found, please spcify one valid value" % (srcrev, parmrev))
  925. return parmrev
  926. if srcrev == "INVALID" or not srcrev:
  927. raise FetchError("Please set a valid SRCREV for url %s (possible key names are %s, or use a ;rev=X URL parameter)" % (str(attempts), ud.url), ud.url)
  928. if srcrev == "AUTOINC":
  929. srcrev = ud.method.latest_revision(ud, d, name)
  930. return srcrev
  931. def get_checksum_file_list(d):
  932. """ Get a list of files checksum in SRC_URI
  933. Returns the resolved local paths of all local file entries in
  934. SRC_URI as a space-separated string
  935. """
  936. fetch = Fetch([], d, cache = False, localonly = True)
  937. dl_dir = d.getVar('DL_DIR')
  938. filelist = []
  939. for u in fetch.urls:
  940. ud = fetch.ud[u]
  941. if ud and isinstance(ud.method, local.Local):
  942. paths = ud.method.localpaths(ud, d)
  943. for f in paths:
  944. pth = ud.decodedurl
  945. if '*' in pth:
  946. f = os.path.join(os.path.abspath(f), pth)
  947. if f.startswith(dl_dir):
  948. # The local fetcher's behaviour is to return a path under DL_DIR if it couldn't find the file anywhere else
  949. if os.path.exists(f):
  950. bb.warn("Getting checksum for %s SRC_URI entry %s: file not found except in DL_DIR" % (d.getVar('PN'), os.path.basename(f)))
  951. else:
  952. bb.warn("Unable to get checksum for %s SRC_URI entry %s: file could not be found" % (d.getVar('PN'), os.path.basename(f)))
  953. filelist.append(f + ":" + str(os.path.exists(f)))
  954. return " ".join(filelist)
  955. def get_file_checksums(filelist, pn):
  956. """Get a list of the checksums for a list of local files
  957. Returns the checksums for a list of local files, caching the results as
  958. it proceeds
  959. """
  960. return _checksum_cache.get_checksums(filelist, pn)
  961. class FetchData(object):
  962. """
  963. A class which represents the fetcher state for a given URI.
  964. """
  965. def __init__(self, url, d, localonly = False):
  966. # localpath is the location of a downloaded result. If not set, the file is local.
  967. self.donestamp = None
  968. self.needdonestamp = True
  969. self.localfile = ""
  970. self.localpath = None
  971. self.lockfile = None
  972. self.mirrortarball = None
  973. self.basename = None
  974. self.basepath = None
  975. (self.type, self.host, self.path, self.user, self.pswd, self.parm) = decodeurl(data.expand(url, d))
  976. self.date = self.getSRCDate(d)
  977. self.url = url
  978. if not self.user and "user" in self.parm:
  979. self.user = self.parm["user"]
  980. if not self.pswd and "pswd" in self.parm:
  981. self.pswd = self.parm["pswd"]
  982. self.setup = False
  983. if "name" in self.parm:
  984. self.md5_name = "%s.md5sum" % self.parm["name"]
  985. self.sha256_name = "%s.sha256sum" % self.parm["name"]
  986. else:
  987. self.md5_name = "md5sum"
  988. self.sha256_name = "sha256sum"
  989. if self.md5_name in self.parm:
  990. self.md5_expected = self.parm[self.md5_name]
  991. elif self.type not in ["http", "https", "ftp", "ftps", "sftp"]:
  992. self.md5_expected = None
  993. else:
  994. self.md5_expected = d.getVarFlag("SRC_URI", self.md5_name)
  995. if self.sha256_name in self.parm:
  996. self.sha256_expected = self.parm[self.sha256_name]
  997. elif self.type not in ["http", "https", "ftp", "ftps", "sftp"]:
  998. self.sha256_expected = None
  999. else:
  1000. self.sha256_expected = d.getVarFlag("SRC_URI", self.sha256_name)
  1001. self.ignore_checksums = False
  1002. self.names = self.parm.get("name",'default').split(',')
  1003. self.method = None
  1004. for m in methods:
  1005. if m.supports(self, d):
  1006. self.method = m
  1007. break
  1008. if not self.method:
  1009. raise NoMethodError(url)
  1010. if localonly and not isinstance(self.method, local.Local):
  1011. raise NonLocalMethod()
  1012. if self.parm.get("proto", None) and "protocol" not in self.parm:
  1013. logger.warning('Consider updating %s recipe to use "protocol" not "proto" in SRC_URI.', d.getVar('PN'))
  1014. self.parm["protocol"] = self.parm.get("proto", None)
  1015. if hasattr(self.method, "urldata_init"):
  1016. self.method.urldata_init(self, d)
  1017. if "localpath" in self.parm:
  1018. # if user sets localpath for file, use it instead.
  1019. self.localpath = self.parm["localpath"]
  1020. self.basename = os.path.basename(self.localpath)
  1021. elif self.localfile:
  1022. self.localpath = self.method.localpath(self, d)
  1023. dldir = d.getVar("DL_DIR")
  1024. if not self.needdonestamp:
  1025. return
  1026. # Note: .done and .lock files should always be in DL_DIR whereas localpath may not be.
  1027. if self.localpath and self.localpath.startswith(dldir):
  1028. basepath = self.localpath
  1029. elif self.localpath:
  1030. basepath = dldir + os.sep + os.path.basename(self.localpath)
  1031. elif self.basepath or self.basename:
  1032. basepath = dldir + os.sep + (self.basepath or self.basename)
  1033. else:
  1034. bb.fatal("Can't determine lock path for url %s" % url)
  1035. self.donestamp = basepath + '.done'
  1036. self.lockfile = basepath + '.lock'
  1037. def setup_revisons(self, d):
  1038. self.revisions = {}
  1039. for name in self.names:
  1040. self.revisions[name] = srcrev_internal_helper(self, d, name)
  1041. # add compatibility code for non name specified case
  1042. if len(self.names) == 1:
  1043. self.revision = self.revisions[self.names[0]]
  1044. def setup_localpath(self, d):
  1045. if not self.localpath:
  1046. self.localpath = self.method.localpath(self, d)
  1047. def getSRCDate(self, d):
  1048. """
  1049. Return the SRC Date for the component
  1050. d the bb.data module
  1051. """
  1052. if "srcdate" in self.parm:
  1053. return self.parm['srcdate']
  1054. pn = d.getVar("PN")
  1055. if pn:
  1056. return d.getVar("SRCDATE_%s" % pn) or d.getVar("SRCDATE") or d.getVar("DATE")
  1057. return d.getVar("SRCDATE") or d.getVar("DATE")
  1058. class FetchMethod(object):
  1059. """Base class for 'fetch'ing data"""
  1060. def __init__(self, urls=None):
  1061. self.urls = []
  1062. def supports(self, urldata, d):
  1063. """
  1064. Check to see if this fetch class supports a given url.
  1065. """
  1066. return 0
  1067. def localpath(self, urldata, d):
  1068. """
  1069. Return the local filename of a given url assuming a successful fetch.
  1070. Can also setup variables in urldata for use in go (saving code duplication
  1071. and duplicate code execution)
  1072. """
  1073. return os.path.join(d.getVar("DL_DIR"), urldata.localfile)
  1074. def supports_checksum(self, urldata):
  1075. """
  1076. Is localpath something that can be represented by a checksum?
  1077. """
  1078. # We cannot compute checksums for directories
  1079. if os.path.isdir(urldata.localpath) == True:
  1080. return False
  1081. if urldata.localpath.find("*") != -1:
  1082. return False
  1083. return True
  1084. def recommends_checksum(self, urldata):
  1085. """
  1086. Is the backend on where checksumming is recommended (should warnings
  1087. be displayed if there is no checksum)?
  1088. """
  1089. return False
  1090. def _strip_leading_slashes(self, relpath):
  1091. """
  1092. Remove leading slash as os.path.join can't cope
  1093. """
  1094. while os.path.isabs(relpath):
  1095. relpath = relpath[1:]
  1096. return relpath
  1097. def setUrls(self, urls):
  1098. self.__urls = urls
  1099. def getUrls(self):
  1100. return self.__urls
  1101. urls = property(getUrls, setUrls, None, "Urls property")
  1102. def need_update(self, ud, d):
  1103. """
  1104. Force a fetch, even if localpath exists?
  1105. """
  1106. if os.path.exists(ud.localpath):
  1107. return False
  1108. return True
  1109. def supports_srcrev(self):
  1110. """
  1111. The fetcher supports auto source revisions (SRCREV)
  1112. """
  1113. return False
  1114. def download(self, urldata, d):
  1115. """
  1116. Fetch urls
  1117. Assumes localpath was called first
  1118. """
  1119. raise NoMethodError(url)
  1120. def unpack(self, urldata, rootdir, data):
  1121. iterate = False
  1122. file = urldata.localpath
  1123. # Localpath can't deal with 'dir/*' entries, so it converts them to '.',
  1124. # but it must be corrected back for local files copying
  1125. if urldata.basename == '*' and file.endswith('/.'):
  1126. file = '%s/%s' % (file.rstrip('/.'), urldata.path)
  1127. try:
  1128. unpack = bb.utils.to_boolean(urldata.parm.get('unpack'), True)
  1129. except ValueError as exc:
  1130. bb.fatal("Invalid value for 'unpack' parameter for %s: %s" %
  1131. (file, urldata.parm.get('unpack')))
  1132. base, ext = os.path.splitext(file)
  1133. if ext in ['.gz', '.bz2', '.Z', '.xz', '.lz']:
  1134. efile = os.path.join(rootdir, os.path.basename(base))
  1135. else:
  1136. efile = file
  1137. cmd = None
  1138. if unpack:
  1139. if file.endswith('.tar'):
  1140. cmd = 'tar x --no-same-owner -f %s' % file
  1141. elif file.endswith('.tgz') or file.endswith('.tar.gz') or file.endswith('.tar.Z'):
  1142. cmd = 'tar xz --no-same-owner -f %s' % file
  1143. elif file.endswith('.tbz') or file.endswith('.tbz2') or file.endswith('.tar.bz2'):
  1144. cmd = 'bzip2 -dc %s | tar x --no-same-owner -f -' % file
  1145. elif file.endswith('.gz') or file.endswith('.Z') or file.endswith('.z'):
  1146. cmd = 'gzip -dc %s > %s' % (file, efile)
  1147. elif file.endswith('.bz2'):
  1148. cmd = 'bzip2 -dc %s > %s' % (file, efile)
  1149. elif file.endswith('.tar.xz'):
  1150. cmd = 'xz -dc %s | tar x --no-same-owner -f -' % file
  1151. elif file.endswith('.xz'):
  1152. cmd = 'xz -dc %s > %s' % (file, efile)
  1153. elif file.endswith('.tar.lz'):
  1154. cmd = 'lzip -dc %s | tar x --no-same-owner -f -' % file
  1155. elif file.endswith('.lz'):
  1156. cmd = 'lzip -dc %s > %s' % (file, efile)
  1157. elif file.endswith('.zip') or file.endswith('.jar'):
  1158. try:
  1159. dos = bb.utils.to_boolean(urldata.parm.get('dos'), False)
  1160. except ValueError as exc:
  1161. bb.fatal("Invalid value for 'dos' parameter for %s: %s" %
  1162. (file, urldata.parm.get('dos')))
  1163. cmd = 'unzip -q -o'
  1164. if dos:
  1165. cmd = '%s -a' % cmd
  1166. cmd = "%s '%s'" % (cmd, file)
  1167. elif file.endswith('.rpm') or file.endswith('.srpm'):
  1168. if 'extract' in urldata.parm:
  1169. unpack_file = urldata.parm.get('extract')
  1170. cmd = 'rpm2cpio.sh %s | cpio -id %s' % (file, unpack_file)
  1171. iterate = True
  1172. iterate_file = unpack_file
  1173. else:
  1174. cmd = 'rpm2cpio.sh %s | cpio -id' % (file)
  1175. elif file.endswith('.deb') or file.endswith('.ipk'):
  1176. output = subprocess.check_output('ar -t %s' % file, preexec_fn=subprocess_setup, shell=True)
  1177. datafile = None
  1178. if output:
  1179. for line in output.decode().splitlines():
  1180. if line.startswith('data.tar.'):
  1181. datafile = line
  1182. break
  1183. else:
  1184. raise UnpackError("Unable to unpack deb/ipk package - does not contain data.tar.* file", urldata.url)
  1185. else:
  1186. raise UnpackError("Unable to unpack deb/ipk package - could not list contents", urldata.url)
  1187. cmd = 'ar x %s %s && tar --no-same-owner -xpf %s && rm %s' % (file, datafile, datafile, datafile)
  1188. elif file.endswith('.tar.7z'):
  1189. cmd = '7z x -so %s | tar xf - ' % file
  1190. elif file.endswith('.7z'):
  1191. cmd = '7za x -y %s 1>/dev/null' % file
  1192. # If 'subdir' param exists, create a dir and use it as destination for unpack cmd
  1193. if 'subdir' in urldata.parm:
  1194. subdir = urldata.parm.get('subdir')
  1195. if os.path.isabs(subdir):
  1196. if not os.path.realpath(subdir).startswith(os.path.realpath(rootdir)):
  1197. raise UnpackError("subdir argument isn't a subdirectory of unpack root %s" % rootdir, urldata.url)
  1198. unpackdir = subdir
  1199. else:
  1200. unpackdir = os.path.join(rootdir, subdir)
  1201. bb.utils.mkdirhier(unpackdir)
  1202. else:
  1203. unpackdir = rootdir
  1204. if not unpack or not cmd:
  1205. # If file == dest, then avoid any copies, as we already put the file into dest!
  1206. dest = os.path.join(unpackdir, os.path.basename(file))
  1207. if file != dest and not (os.path.exists(dest) and os.path.samefile(file, dest)):
  1208. destdir = '.'
  1209. # For file:// entries all intermediate dirs in path must be created at destination
  1210. if urldata.type == "file":
  1211. # Trailing '/' does a copying to wrong place
  1212. urlpath = urldata.path.rstrip('/')
  1213. # Want files places relative to cwd so no leading '/'
  1214. urlpath = urlpath.lstrip('/')
  1215. if urlpath.find("/") != -1:
  1216. destdir = urlpath.rsplit("/", 1)[0] + '/'
  1217. bb.utils.mkdirhier("%s/%s" % (unpackdir, destdir))
  1218. cmd = 'cp -fpPRH %s %s' % (file, destdir)
  1219. if not cmd:
  1220. return
  1221. path = data.getVar('PATH')
  1222. if path:
  1223. cmd = "PATH=\"%s\" %s" % (path, cmd)
  1224. bb.note("Unpacking %s to %s/" % (file, unpackdir))
  1225. ret = subprocess.call(cmd, preexec_fn=subprocess_setup, shell=True, cwd=unpackdir)
  1226. if ret != 0:
  1227. raise UnpackError("Unpack command %s failed with return value %s" % (cmd, ret), urldata.url)
  1228. if iterate is True:
  1229. iterate_urldata = urldata
  1230. iterate_urldata.localpath = "%s/%s" % (rootdir, iterate_file)
  1231. self.unpack(urldata, rootdir, data)
  1232. return
  1233. def clean(self, urldata, d):
  1234. """
  1235. Clean any existing full or partial download
  1236. """
  1237. bb.utils.remove(urldata.localpath)
  1238. def try_premirror(self, urldata, d):
  1239. """
  1240. Should premirrors be used?
  1241. """
  1242. return True
  1243. def checkstatus(self, fetch, urldata, d):
  1244. """
  1245. Check the status of a URL
  1246. Assumes localpath was called first
  1247. """
  1248. logger.info("URL %s could not be checked for status since no method exists.", url)
  1249. return True
  1250. def latest_revision(self, ud, d, name):
  1251. """
  1252. Look in the cache for the latest revision, if not present ask the SCM.
  1253. """
  1254. if not hasattr(self, "_latest_revision"):
  1255. raise ParameterError("The fetcher for this URL does not support _latest_revision", url)
  1256. revs = bb.persist_data.persist('BB_URI_HEADREVS', d)
  1257. key = self.generate_revision_key(ud, d, name)
  1258. try:
  1259. return revs[key]
  1260. except KeyError:
  1261. revs[key] = rev = self._latest_revision(ud, d, name)
  1262. return rev
  1263. def sortable_revision(self, ud, d, name):
  1264. latest_rev = self._build_revision(ud, d, name)
  1265. return True, str(latest_rev)
  1266. def generate_revision_key(self, ud, d, name):
  1267. key = self._revision_key(ud, d, name)
  1268. return "%s-%s" % (key, d.getVar("PN") or "")
  1269. class Fetch(object):
  1270. def __init__(self, urls, d, cache = True, localonly = False, connection_cache = None):
  1271. if localonly and cache:
  1272. raise Exception("bb.fetch2.Fetch.__init__: cannot set cache and localonly at same time")
  1273. if len(urls) == 0:
  1274. urls = d.getVar("SRC_URI").split()
  1275. self.urls = urls
  1276. self.d = d
  1277. self.ud = {}
  1278. self.connection_cache = connection_cache
  1279. fn = d.getVar('FILE')
  1280. mc = d.getVar('__BBMULTICONFIG') or ""
  1281. if cache and fn and mc + fn in urldata_cache:
  1282. self.ud = urldata_cache[mc + fn]
  1283. for url in urls:
  1284. if url not in self.ud:
  1285. try:
  1286. self.ud[url] = FetchData(url, d, localonly)
  1287. except NonLocalMethod:
  1288. if localonly:
  1289. self.ud[url] = None
  1290. pass
  1291. if fn and cache:
  1292. urldata_cache[mc + fn] = self.ud
  1293. def localpath(self, url):
  1294. if url not in self.urls:
  1295. self.ud[url] = FetchData(url, self.d)
  1296. self.ud[url].setup_localpath(self.d)
  1297. return self.d.expand(self.ud[url].localpath)
  1298. def localpaths(self):
  1299. """
  1300. Return a list of the local filenames, assuming successful fetch
  1301. """
  1302. local = []
  1303. for u in self.urls:
  1304. ud = self.ud[u]
  1305. ud.setup_localpath(self.d)
  1306. local.append(ud.localpath)
  1307. return local
  1308. def download(self, urls=None):
  1309. """
  1310. Fetch all urls
  1311. """
  1312. if not urls:
  1313. urls = self.urls
  1314. network = self.d.getVar("BB_NO_NETWORK")
  1315. premirroronly = (self.d.getVar("BB_FETCH_PREMIRRORONLY") == "1")
  1316. for u in urls:
  1317. ud = self.ud[u]
  1318. ud.setup_localpath(self.d)
  1319. m = ud.method
  1320. localpath = ""
  1321. if ud.lockfile:
  1322. lf = bb.utils.lockfile(ud.lockfile)
  1323. try:
  1324. self.d.setVar("BB_NO_NETWORK", network)
  1325. if verify_donestamp(ud, self.d) and not m.need_update(ud, self.d):
  1326. localpath = ud.localpath
  1327. elif m.try_premirror(ud, self.d):
  1328. logger.debug(1, "Trying PREMIRRORS")
  1329. mirrors = mirror_from_string(self.d.getVar('PREMIRRORS'))
  1330. localpath = try_mirrors(self, self.d, ud, mirrors, False)
  1331. if premirroronly:
  1332. self.d.setVar("BB_NO_NETWORK", "1")
  1333. firsterr = None
  1334. verified_stamp = verify_donestamp(ud, self.d)
  1335. if not localpath and (not verified_stamp or m.need_update(ud, self.d)):
  1336. try:
  1337. if not trusted_network(self.d, ud.url):
  1338. raise UntrustedUrl(ud.url)
  1339. logger.debug(1, "Trying Upstream")
  1340. m.download(ud, self.d)
  1341. if hasattr(m, "build_mirror_data"):
  1342. m.build_mirror_data(ud, self.d)
  1343. localpath = ud.localpath
  1344. # early checksum verify, so that if checksum mismatched,
  1345. # fetcher still have chance to fetch from mirror
  1346. update_stamp(ud, self.d)
  1347. except bb.fetch2.NetworkAccess:
  1348. raise
  1349. except BBFetchException as e:
  1350. if isinstance(e, ChecksumError):
  1351. logger.warning("Checksum failure encountered with download of %s - will attempt other sources if available" % u)
  1352. logger.debug(1, str(e))
  1353. if os.path.exists(ud.localpath):
  1354. rename_bad_checksum(ud, e.checksum)
  1355. elif isinstance(e, NoChecksumError):
  1356. raise
  1357. else:
  1358. logger.warning('Failed to fetch URL %s, attempting MIRRORS if available' % u)
  1359. logger.debug(1, str(e))
  1360. firsterr = e
  1361. # Remove any incomplete fetch
  1362. if not verified_stamp:
  1363. m.clean(ud, self.d)
  1364. logger.debug(1, "Trying MIRRORS")
  1365. mirrors = mirror_from_string(self.d.getVar('MIRRORS'))
  1366. localpath = try_mirrors(self, self.d, ud, mirrors)
  1367. if not localpath or ((not os.path.exists(localpath)) and localpath.find("*") == -1):
  1368. if firsterr:
  1369. logger.error(str(firsterr))
  1370. raise FetchError("Unable to fetch URL from any source.", u)
  1371. update_stamp(ud, self.d)
  1372. except BBFetchException as e:
  1373. if isinstance(e, ChecksumError):
  1374. logger.error("Checksum failure fetching %s" % u)
  1375. raise
  1376. finally:
  1377. if ud.lockfile:
  1378. bb.utils.unlockfile(lf)
  1379. def checkstatus(self, urls=None):
  1380. """
  1381. Check all urls exist upstream
  1382. """
  1383. if not urls:
  1384. urls = self.urls
  1385. for u in urls:
  1386. ud = self.ud[u]
  1387. ud.setup_localpath(self.d)
  1388. m = ud.method
  1389. logger.debug(1, "Testing URL %s", u)
  1390. # First try checking uri, u, from PREMIRRORS
  1391. mirrors = mirror_from_string(self.d.getVar('PREMIRRORS'))
  1392. ret = try_mirrors(self, self.d, ud, mirrors, True)
  1393. if not ret:
  1394. # Next try checking from the original uri, u
  1395. try:
  1396. ret = m.checkstatus(self, ud, self.d)
  1397. except:
  1398. # Finally, try checking uri, u, from MIRRORS
  1399. mirrors = mirror_from_string(self.d.getVar('MIRRORS'))
  1400. ret = try_mirrors(self, self.d, ud, mirrors, True)
  1401. if not ret:
  1402. raise FetchError("URL %s doesn't work" % u, u)
  1403. def unpack(self, root, urls=None):
  1404. """
  1405. Check all urls exist upstream
  1406. """
  1407. if not urls:
  1408. urls = self.urls
  1409. for u in urls:
  1410. ud = self.ud[u]
  1411. ud.setup_localpath(self.d)
  1412. if ud.lockfile:
  1413. lf = bb.utils.lockfile(ud.lockfile)
  1414. ud.method.unpack(ud, root, self.d)
  1415. if ud.lockfile:
  1416. bb.utils.unlockfile(lf)
  1417. def clean(self, urls=None):
  1418. """
  1419. Clean files that the fetcher gets or places
  1420. """
  1421. if not urls:
  1422. urls = self.urls
  1423. for url in urls:
  1424. if url not in self.ud:
  1425. self.ud[url] = FetchData(url, d)
  1426. ud = self.ud[url]
  1427. ud.setup_localpath(self.d)
  1428. if not ud.localfile and ud.localpath is None:
  1429. continue
  1430. if ud.lockfile:
  1431. lf = bb.utils.lockfile(ud.lockfile)
  1432. ud.method.clean(ud, self.d)
  1433. if ud.donestamp:
  1434. bb.utils.remove(ud.donestamp)
  1435. if ud.lockfile:
  1436. bb.utils.unlockfile(lf)
  1437. class FetchConnectionCache(object):
  1438. """
  1439. A class which represents an container for socket connections.
  1440. """
  1441. def __init__(self):
  1442. self.cache = {}
  1443. def get_connection_name(self, host, port):
  1444. return host + ':' + str(port)
  1445. def add_connection(self, host, port, connection):
  1446. cn = self.get_connection_name(host, port)
  1447. if cn not in self.cache:
  1448. self.cache[cn] = connection
  1449. def get_connection(self, host, port):
  1450. connection = None
  1451. cn = self.get_connection_name(host, port)
  1452. if cn in self.cache:
  1453. connection = self.cache[cn]
  1454. return connection
  1455. def remove_connection(self, host, port):
  1456. cn = self.get_connection_name(host, port)
  1457. if cn in self.cache:
  1458. self.cache[cn].close()
  1459. del self.cache[cn]
  1460. def close_connections(self):
  1461. for cn in list(self.cache.keys()):
  1462. self.cache[cn].close()
  1463. del self.cache[cn]
  1464. from . import cvs
  1465. from . import git
  1466. from . import gitsm
  1467. from . import gitannex
  1468. from . import local
  1469. from . import svn
  1470. from . import wget
  1471. from . import ssh
  1472. from . import sftp
  1473. from . import perforce
  1474. from . import bzr
  1475. from . import hg
  1476. from . import osc
  1477. from . import repo
  1478. from . import clearcase
  1479. from . import npm
  1480. methods.append(local.Local())
  1481. methods.append(wget.Wget())
  1482. methods.append(svn.Svn())
  1483. methods.append(git.Git())
  1484. methods.append(gitsm.GitSM())
  1485. methods.append(gitannex.GitANNEX())
  1486. methods.append(cvs.Cvs())
  1487. methods.append(ssh.SSH())
  1488. methods.append(sftp.SFTP())
  1489. methods.append(perforce.Perforce())
  1490. methods.append(bzr.Bzr())
  1491. methods.append(hg.Hg())
  1492. methods.append(osc.Osc())
  1493. methods.append(repo.Repo())
  1494. methods.append(clearcase.ClearCase())
  1495. methods.append(npm.Npm())