utils.py 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  1. # ex:ts=4:sw=4:sts=4:et
  2. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  3. """
  4. BitBake Utility Functions
  5. """
  6. # Copyright (C) 2004 Michael Lauer
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License version 2 as
  10. # published by the Free Software Foundation.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. import re, fcntl, os, string, stat, shutil, time
  21. import sys
  22. import errno
  23. import logging
  24. import bb
  25. import bb.msg
  26. import multiprocessing
  27. import fcntl
  28. import imp
  29. import itertools
  30. import subprocess
  31. import glob
  32. import fnmatch
  33. import traceback
  34. import errno
  35. import signal
  36. import ast
  37. import collections
  38. import copy
  39. from subprocess import getstatusoutput
  40. from contextlib import contextmanager
  41. from ctypes import cdll
  42. logger = logging.getLogger("BitBake.Util")
  43. python_extensions = [e for e, _, _ in imp.get_suffixes()]
  44. def clean_context():
  45. return {
  46. "os": os,
  47. "bb": bb,
  48. "time": time,
  49. }
  50. def get_context():
  51. return _context
  52. def set_context(ctx):
  53. _context = ctx
  54. # Context used in better_exec, eval
  55. _context = clean_context()
  56. class VersionStringException(Exception):
  57. """Exception raised when an invalid version specification is found"""
  58. def explode_version(s):
  59. r = []
  60. alpha_regexp = re.compile('^([a-zA-Z]+)(.*)$')
  61. numeric_regexp = re.compile('^(\d+)(.*)$')
  62. while (s != ''):
  63. if s[0] in string.digits:
  64. m = numeric_regexp.match(s)
  65. r.append((0, int(m.group(1))))
  66. s = m.group(2)
  67. continue
  68. if s[0] in string.ascii_letters:
  69. m = alpha_regexp.match(s)
  70. r.append((1, m.group(1)))
  71. s = m.group(2)
  72. continue
  73. if s[0] == '~':
  74. r.append((-1, s[0]))
  75. else:
  76. r.append((2, s[0]))
  77. s = s[1:]
  78. return r
  79. def split_version(s):
  80. """Split a version string into its constituent parts (PE, PV, PR)"""
  81. s = s.strip(" <>=")
  82. e = 0
  83. if s.count(':'):
  84. e = int(s.split(":")[0])
  85. s = s.split(":")[1]
  86. r = ""
  87. if s.count('-'):
  88. r = s.rsplit("-", 1)[1]
  89. s = s.rsplit("-", 1)[0]
  90. v = s
  91. return (e, v, r)
  92. def vercmp_part(a, b):
  93. va = explode_version(a)
  94. vb = explode_version(b)
  95. while True:
  96. if va == []:
  97. (oa, ca) = (0, None)
  98. else:
  99. (oa, ca) = va.pop(0)
  100. if vb == []:
  101. (ob, cb) = (0, None)
  102. else:
  103. (ob, cb) = vb.pop(0)
  104. if (oa, ca) == (0, None) and (ob, cb) == (0, None):
  105. return 0
  106. if oa < ob:
  107. return -1
  108. elif oa > ob:
  109. return 1
  110. elif ca is None:
  111. return -1
  112. elif cb is None:
  113. return 1
  114. elif ca < cb:
  115. return -1
  116. elif ca > cb:
  117. return 1
  118. def vercmp(ta, tb):
  119. (ea, va, ra) = ta
  120. (eb, vb, rb) = tb
  121. r = int(ea or 0) - int(eb or 0)
  122. if (r == 0):
  123. r = vercmp_part(va, vb)
  124. if (r == 0):
  125. r = vercmp_part(ra, rb)
  126. return r
  127. def vercmp_string(a, b):
  128. ta = split_version(a)
  129. tb = split_version(b)
  130. return vercmp(ta, tb)
  131. def vercmp_string_op(a, b, op):
  132. """
  133. Compare two versions and check if the specified comparison operator matches the result of the comparison.
  134. This function is fairly liberal about what operators it will accept since there are a variety of styles
  135. depending on the context.
  136. """
  137. res = vercmp_string(a, b)
  138. if op in ('=', '=='):
  139. return res == 0
  140. elif op == '<=':
  141. return res <= 0
  142. elif op == '>=':
  143. return res >= 0
  144. elif op in ('>', '>>'):
  145. return res > 0
  146. elif op in ('<', '<<'):
  147. return res < 0
  148. elif op == '!=':
  149. return res != 0
  150. else:
  151. raise VersionStringException('Unsupported comparison operator "%s"' % op)
  152. def explode_deps(s):
  153. """
  154. Take an RDEPENDS style string of format:
  155. "DEPEND1 (optional version) DEPEND2 (optional version) ..."
  156. and return a list of dependencies.
  157. Version information is ignored.
  158. """
  159. r = []
  160. l = s.split()
  161. flag = False
  162. for i in l:
  163. if i[0] == '(':
  164. flag = True
  165. #j = []
  166. if not flag:
  167. r.append(i)
  168. #else:
  169. # j.append(i)
  170. if flag and i.endswith(')'):
  171. flag = False
  172. # Ignore version
  173. #r[-1] += ' ' + ' '.join(j)
  174. return r
  175. def explode_dep_versions2(s, *, sort=True):
  176. """
  177. Take an RDEPENDS style string of format:
  178. "DEPEND1 (optional version) DEPEND2 (optional version) ..."
  179. and return a dictionary of dependencies and versions.
  180. """
  181. r = collections.OrderedDict()
  182. l = s.replace(",", "").split()
  183. lastdep = None
  184. lastcmp = ""
  185. lastver = ""
  186. incmp = False
  187. inversion = False
  188. for i in l:
  189. if i[0] == '(':
  190. incmp = True
  191. i = i[1:].strip()
  192. if not i:
  193. continue
  194. if incmp:
  195. incmp = False
  196. inversion = True
  197. # This list is based on behavior and supported comparisons from deb, opkg and rpm.
  198. #
  199. # Even though =<, <<, ==, !=, =>, and >> may not be supported,
  200. # we list each possibly valid item.
  201. # The build system is responsible for validation of what it supports.
  202. if i.startswith(('<=', '=<', '<<', '==', '!=', '>=', '=>', '>>')):
  203. lastcmp = i[0:2]
  204. i = i[2:]
  205. elif i.startswith(('<', '>', '=')):
  206. lastcmp = i[0:1]
  207. i = i[1:]
  208. else:
  209. # This is an unsupported case!
  210. raise VersionStringException('Invalid version specification in "(%s" - invalid or missing operator' % i)
  211. lastcmp = (i or "")
  212. i = ""
  213. i.strip()
  214. if not i:
  215. continue
  216. if inversion:
  217. if i.endswith(')'):
  218. i = i[:-1] or ""
  219. inversion = False
  220. if lastver and i:
  221. lastver += " "
  222. if i:
  223. lastver += i
  224. if lastdep not in r:
  225. r[lastdep] = []
  226. r[lastdep].append(lastcmp + " " + lastver)
  227. continue
  228. #if not inversion:
  229. lastdep = i
  230. lastver = ""
  231. lastcmp = ""
  232. if not (i in r and r[i]):
  233. r[lastdep] = []
  234. if sort:
  235. r = collections.OrderedDict(sorted(r.items(), key=lambda x: x[0]))
  236. return r
  237. def explode_dep_versions(s):
  238. r = explode_dep_versions2(s)
  239. for d in r:
  240. if not r[d]:
  241. r[d] = None
  242. continue
  243. if len(r[d]) > 1:
  244. bb.warn("explode_dep_versions(): Item %s appeared in dependency string '%s' multiple times with different values. explode_dep_versions cannot cope with this." % (d, s))
  245. r[d] = r[d][0]
  246. return r
  247. def join_deps(deps, commasep=True):
  248. """
  249. Take the result from explode_dep_versions and generate a dependency string
  250. """
  251. result = []
  252. for dep in deps:
  253. if deps[dep]:
  254. if isinstance(deps[dep], list):
  255. for v in deps[dep]:
  256. result.append(dep + " (" + v + ")")
  257. else:
  258. result.append(dep + " (" + deps[dep] + ")")
  259. else:
  260. result.append(dep)
  261. if commasep:
  262. return ", ".join(result)
  263. else:
  264. return " ".join(result)
  265. def _print_trace(body, line):
  266. """
  267. Print the Environment of a Text Body
  268. """
  269. error = []
  270. # print the environment of the method
  271. min_line = max(1, line-4)
  272. max_line = min(line + 4, len(body))
  273. for i in range(min_line, max_line + 1):
  274. if line == i:
  275. error.append(' *** %.4d:%s' % (i, body[i-1].rstrip()))
  276. else:
  277. error.append(' %.4d:%s' % (i, body[i-1].rstrip()))
  278. return error
  279. def better_compile(text, file, realfile, mode = "exec", lineno = 0):
  280. """
  281. A better compile method. This method
  282. will print the offending lines.
  283. """
  284. try:
  285. cache = bb.methodpool.compile_cache(text)
  286. if cache:
  287. return cache
  288. # We can't add to the linenumbers for compile, we can pad to the correct number of blank lines though
  289. text2 = "\n" * int(lineno) + text
  290. code = compile(text2, realfile, mode)
  291. bb.methodpool.compile_cache_add(text, code)
  292. return code
  293. except Exception as e:
  294. error = []
  295. # split the text into lines again
  296. body = text.split('\n')
  297. error.append("Error in compiling python function in %s, line %s:\n" % (realfile, lineno))
  298. if hasattr(e, "lineno"):
  299. error.append("The code lines resulting in this error were:")
  300. error.extend(_print_trace(body, e.lineno))
  301. else:
  302. error.append("The function causing this error was:")
  303. for line in body:
  304. error.append(line)
  305. error.append("%s: %s" % (e.__class__.__name__, str(e)))
  306. logger.error("\n".join(error))
  307. e = bb.BBHandledException(e)
  308. raise e
  309. def _print_exception(t, value, tb, realfile, text, context):
  310. error = []
  311. try:
  312. exception = traceback.format_exception_only(t, value)
  313. error.append('Error executing a python function in %s:\n' % realfile)
  314. # Strip 'us' from the stack (better_exec call) unless that was where the
  315. # error came from
  316. if tb.tb_next is not None:
  317. tb = tb.tb_next
  318. textarray = text.split('\n')
  319. linefailed = tb.tb_lineno
  320. tbextract = traceback.extract_tb(tb)
  321. tbformat = traceback.format_list(tbextract)
  322. error.append("The stack trace of python calls that resulted in this exception/failure was:")
  323. error.append("File: '%s', lineno: %s, function: %s" % (tbextract[0][0], tbextract[0][1], tbextract[0][2]))
  324. error.extend(_print_trace(textarray, linefailed))
  325. # See if this is a function we constructed and has calls back into other functions in
  326. # "text". If so, try and improve the context of the error by diving down the trace
  327. level = 0
  328. nexttb = tb.tb_next
  329. while nexttb is not None and (level+1) < len(tbextract):
  330. error.append("File: '%s', lineno: %s, function: %s" % (tbextract[level+1][0], tbextract[level+1][1], tbextract[level+1][2]))
  331. if tbextract[level][0] == tbextract[level+1][0] and tbextract[level+1][2] == tbextract[level][0]:
  332. # The code was possibly in the string we compiled ourselves
  333. error.extend(_print_trace(textarray, tbextract[level+1][1]))
  334. elif tbextract[level+1][0].startswith("/"):
  335. # The code looks like it might be in a file, try and load it
  336. try:
  337. with open(tbextract[level+1][0], "r") as f:
  338. text = f.readlines()
  339. error.extend(_print_trace(text, tbextract[level+1][1]))
  340. except:
  341. error.append(tbformat[level+1])
  342. else:
  343. error.append(tbformat[level+1])
  344. nexttb = tb.tb_next
  345. level = level + 1
  346. error.append("Exception: %s" % ''.join(exception))
  347. # If the exception is from spwaning a task, let's be helpful and display
  348. # the output (which hopefully includes stderr).
  349. if isinstance(value, subprocess.CalledProcessError) and value.output:
  350. error.append("Subprocess output:")
  351. error.append(value.output.decode("utf-8", errors="ignore"))
  352. finally:
  353. logger.error("\n".join(error))
  354. def better_exec(code, context, text = None, realfile = "<code>", pythonexception=False):
  355. """
  356. Similiar to better_compile, better_exec will
  357. print the lines that are responsible for the
  358. error.
  359. """
  360. import bb.parse
  361. if not text:
  362. text = code
  363. if not hasattr(code, "co_filename"):
  364. code = better_compile(code, realfile, realfile)
  365. try:
  366. exec(code, get_context(), context)
  367. except (bb.BBHandledException, bb.parse.SkipRecipe, bb.build.FuncFailed, bb.data_smart.ExpansionError):
  368. # Error already shown so passthrough, no need for traceback
  369. raise
  370. except Exception as e:
  371. if pythonexception:
  372. raise
  373. (t, value, tb) = sys.exc_info()
  374. try:
  375. _print_exception(t, value, tb, realfile, text, context)
  376. except Exception as e:
  377. logger.error("Exception handler error: %s" % str(e))
  378. e = bb.BBHandledException(e)
  379. raise e
  380. def simple_exec(code, context):
  381. exec(code, get_context(), context)
  382. def better_eval(source, locals, extraglobals = None):
  383. ctx = get_context()
  384. if extraglobals:
  385. ctx = copy.copy(ctx)
  386. for g in extraglobals:
  387. ctx[g] = extraglobals[g]
  388. return eval(source, ctx, locals)
  389. @contextmanager
  390. def fileslocked(files):
  391. """Context manager for locking and unlocking file locks."""
  392. locks = []
  393. if files:
  394. for lockfile in files:
  395. locks.append(bb.utils.lockfile(lockfile))
  396. yield
  397. for lock in locks:
  398. bb.utils.unlockfile(lock)
  399. @contextmanager
  400. def timeout(seconds):
  401. def timeout_handler(signum, frame):
  402. pass
  403. original_handler = signal.signal(signal.SIGALRM, timeout_handler)
  404. try:
  405. signal.alarm(seconds)
  406. yield
  407. finally:
  408. signal.alarm(0)
  409. signal.signal(signal.SIGALRM, original_handler)
  410. def lockfile(name, shared=False, retry=True, block=False):
  411. """
  412. Use the specified file as a lock file, return when the lock has
  413. been acquired. Returns a variable to pass to unlockfile().
  414. Parameters:
  415. retry: True to re-try locking if it fails, False otherwise
  416. block: True to block until the lock succeeds, False otherwise
  417. The retry and block parameters are kind of equivalent unless you
  418. consider the possibility of sending a signal to the process to break
  419. out - at which point you want block=True rather than retry=True.
  420. """
  421. dirname = os.path.dirname(name)
  422. mkdirhier(dirname)
  423. if not os.access(dirname, os.W_OK):
  424. logger.error("Unable to acquire lock '%s', directory is not writable",
  425. name)
  426. sys.exit(1)
  427. op = fcntl.LOCK_EX
  428. if shared:
  429. op = fcntl.LOCK_SH
  430. if not retry and not block:
  431. op = op | fcntl.LOCK_NB
  432. while True:
  433. # If we leave the lockfiles lying around there is no problem
  434. # but we should clean up after ourselves. This gives potential
  435. # for races though. To work around this, when we acquire the lock
  436. # we check the file we locked was still the lock file on disk.
  437. # by comparing inode numbers. If they don't match or the lockfile
  438. # no longer exists, we start again.
  439. # This implementation is unfair since the last person to request the
  440. # lock is the most likely to win it.
  441. try:
  442. lf = open(name, 'a+')
  443. fileno = lf.fileno()
  444. fcntl.flock(fileno, op)
  445. statinfo = os.fstat(fileno)
  446. if os.path.exists(lf.name):
  447. statinfo2 = os.stat(lf.name)
  448. if statinfo.st_ino == statinfo2.st_ino:
  449. return lf
  450. lf.close()
  451. except OSError as e:
  452. if e.errno == errno.EACCES:
  453. logger.error("Unable to acquire lock '%s', %s",
  454. e.strerror, name)
  455. sys.exit(1)
  456. try:
  457. lf.close()
  458. except Exception:
  459. pass
  460. pass
  461. if not retry:
  462. return None
  463. def unlockfile(lf):
  464. """
  465. Unlock a file locked using lockfile()
  466. """
  467. try:
  468. # If we had a shared lock, we need to promote to exclusive before
  469. # removing the lockfile. Attempt this, ignore failures.
  470. fcntl.flock(lf.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB)
  471. os.unlink(lf.name)
  472. except (IOError, OSError):
  473. pass
  474. fcntl.flock(lf.fileno(), fcntl.LOCK_UN)
  475. lf.close()
  476. def md5_file(filename):
  477. """
  478. Return the hex string representation of the MD5 checksum of filename.
  479. """
  480. import hashlib, mmap
  481. with open(filename, "rb") as f:
  482. m = hashlib.md5()
  483. try:
  484. with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as mm:
  485. for chunk in iter(lambda: mm.read(8192), b''):
  486. m.update(chunk)
  487. except ValueError:
  488. # You can't mmap() an empty file so silence this exception
  489. pass
  490. return m.hexdigest()
  491. def sha256_file(filename):
  492. """
  493. Return the hex string representation of the 256-bit SHA checksum of
  494. filename.
  495. """
  496. import hashlib
  497. s = hashlib.sha256()
  498. with open(filename, "rb") as f:
  499. for line in f:
  500. s.update(line)
  501. return s.hexdigest()
  502. def sha1_file(filename):
  503. """
  504. Return the hex string representation of the SHA1 checksum of the filename
  505. """
  506. import hashlib
  507. s = hashlib.sha1()
  508. with open(filename, "rb") as f:
  509. for line in f:
  510. s.update(line)
  511. return s.hexdigest()
  512. def preserved_envvars_exported():
  513. """Variables which are taken from the environment and placed in and exported
  514. from the metadata"""
  515. return [
  516. 'BB_TASKHASH',
  517. 'HOME',
  518. 'LOGNAME',
  519. 'PATH',
  520. 'PWD',
  521. 'SHELL',
  522. 'TERM',
  523. 'USER',
  524. 'LC_ALL',
  525. 'BBSERVER',
  526. ]
  527. def preserved_envvars():
  528. """Variables which are taken from the environment and placed in the metadata"""
  529. v = [
  530. 'BBPATH',
  531. 'BB_PRESERVE_ENV',
  532. 'BB_ENV_WHITELIST',
  533. 'BB_ENV_EXTRAWHITE',
  534. ]
  535. return v + preserved_envvars_exported()
  536. def filter_environment(good_vars):
  537. """
  538. Create a pristine environment for bitbake. This will remove variables that
  539. are not known and may influence the build in a negative way.
  540. """
  541. removed_vars = {}
  542. for key in list(os.environ):
  543. if key in good_vars:
  544. continue
  545. removed_vars[key] = os.environ[key]
  546. del os.environ[key]
  547. # If we spawn a python process, we need to have a UTF-8 locale, else python's file
  548. # access methods will use ascii. You can't change that mode once the interpreter is
  549. # started so we have to ensure a locale is set. Ideally we'd use C.UTF-8 but not all
  550. # distros support that and we need to set something.
  551. os.environ["LC_ALL"] = "en_US.UTF-8"
  552. if removed_vars:
  553. logger.debug(1, "Removed the following variables from the environment: %s", ", ".join(removed_vars.keys()))
  554. return removed_vars
  555. def approved_variables():
  556. """
  557. Determine and return the list of whitelisted variables which are approved
  558. to remain in the environment.
  559. """
  560. if 'BB_PRESERVE_ENV' in os.environ:
  561. return os.environ.keys()
  562. approved = []
  563. if 'BB_ENV_WHITELIST' in os.environ:
  564. approved = os.environ['BB_ENV_WHITELIST'].split()
  565. approved.extend(['BB_ENV_WHITELIST'])
  566. else:
  567. approved = preserved_envvars()
  568. if 'BB_ENV_EXTRAWHITE' in os.environ:
  569. approved.extend(os.environ['BB_ENV_EXTRAWHITE'].split())
  570. if 'BB_ENV_EXTRAWHITE' not in approved:
  571. approved.extend(['BB_ENV_EXTRAWHITE'])
  572. return approved
  573. def clean_environment():
  574. """
  575. Clean up any spurious environment variables. This will remove any
  576. variables the user hasn't chosen to preserve.
  577. """
  578. if 'BB_PRESERVE_ENV' not in os.environ:
  579. good_vars = approved_variables()
  580. return filter_environment(good_vars)
  581. return {}
  582. def empty_environment():
  583. """
  584. Remove all variables from the environment.
  585. """
  586. for s in list(os.environ.keys()):
  587. os.unsetenv(s)
  588. del os.environ[s]
  589. def build_environment(d):
  590. """
  591. Build an environment from all exported variables.
  592. """
  593. import bb.data
  594. for var in bb.data.keys(d):
  595. export = d.getVarFlag(var, "export", False)
  596. if export:
  597. os.environ[var] = d.getVar(var) or ""
  598. def _check_unsafe_delete_path(path):
  599. """
  600. Basic safeguard against recursively deleting something we shouldn't. If it returns True,
  601. the caller should raise an exception with an appropriate message.
  602. NOTE: This is NOT meant to be a security mechanism - just a guard against silly mistakes
  603. with potentially disastrous results.
  604. """
  605. extra = ''
  606. # HOME might not be /home/something, so in case we can get it, check against it
  607. homedir = os.environ.get('HOME', '')
  608. if homedir:
  609. extra = '|%s' % homedir
  610. if re.match('(/|//|/home|/home/[^/]*%s)$' % extra, os.path.abspath(path)):
  611. return True
  612. return False
  613. def remove(path, recurse=False):
  614. """Equivalent to rm -f or rm -rf"""
  615. if not path:
  616. return
  617. if recurse:
  618. for name in glob.glob(path):
  619. if _check_unsafe_delete_path(path):
  620. raise Exception('bb.utils.remove: called with dangerous path "%s" and recurse=True, refusing to delete!' % path)
  621. # shutil.rmtree(name) would be ideal but its too slow
  622. subprocess.check_call(['rm', '-rf'] + glob.glob(path))
  623. return
  624. for name in glob.glob(path):
  625. try:
  626. os.unlink(name)
  627. except OSError as exc:
  628. if exc.errno != errno.ENOENT:
  629. raise
  630. def prunedir(topdir):
  631. # Delete everything reachable from the directory named in 'topdir'.
  632. # CAUTION: This is dangerous!
  633. if _check_unsafe_delete_path(topdir):
  634. raise Exception('bb.utils.prunedir: called with dangerous path "%s", refusing to delete!' % topdir)
  635. for root, dirs, files in os.walk(topdir, topdown = False):
  636. for name in files:
  637. os.remove(os.path.join(root, name))
  638. for name in dirs:
  639. if os.path.islink(os.path.join(root, name)):
  640. os.remove(os.path.join(root, name))
  641. else:
  642. os.rmdir(os.path.join(root, name))
  643. os.rmdir(topdir)
  644. #
  645. # Could also use return re.compile("(%s)" % "|".join(map(re.escape, suffixes))).sub(lambda mo: "", var)
  646. # but thats possibly insane and suffixes is probably going to be small
  647. #
  648. def prune_suffix(var, suffixes, d):
  649. # See if var ends with any of the suffixes listed and
  650. # remove it if found
  651. for suffix in suffixes:
  652. if var.endswith(suffix):
  653. return var.replace(suffix, "")
  654. return var
  655. def mkdirhier(directory):
  656. """Create a directory like 'mkdir -p', but does not complain if
  657. directory already exists like os.makedirs
  658. """
  659. try:
  660. os.makedirs(directory)
  661. except OSError as e:
  662. if e.errno != errno.EEXIST:
  663. raise e
  664. def movefile(src, dest, newmtime = None, sstat = None):
  665. """Moves a file from src to dest, preserving all permissions and
  666. attributes; mtime will be preserved even when moving across
  667. filesystems. Returns true on success and false on failure. Move is
  668. atomic.
  669. """
  670. #print "movefile(" + src + "," + dest + "," + str(newmtime) + "," + str(sstat) + ")"
  671. try:
  672. if not sstat:
  673. sstat = os.lstat(src)
  674. except Exception as e:
  675. print("movefile: Stating source file failed...", e)
  676. return None
  677. destexists = 1
  678. try:
  679. dstat = os.lstat(dest)
  680. except:
  681. dstat = os.lstat(os.path.dirname(dest))
  682. destexists = 0
  683. if destexists:
  684. if stat.S_ISLNK(dstat[stat.ST_MODE]):
  685. try:
  686. os.unlink(dest)
  687. destexists = 0
  688. except Exception as e:
  689. pass
  690. if stat.S_ISLNK(sstat[stat.ST_MODE]):
  691. try:
  692. target = os.readlink(src)
  693. if destexists and not stat.S_ISDIR(dstat[stat.ST_MODE]):
  694. os.unlink(dest)
  695. os.symlink(target, dest)
  696. #os.lchown(dest,sstat[stat.ST_UID],sstat[stat.ST_GID])
  697. os.unlink(src)
  698. return os.lstat(dest)
  699. except Exception as e:
  700. print("movefile: failed to properly create symlink:", dest, "->", target, e)
  701. return None
  702. renamefailed = 1
  703. # os.rename needs to know the dest path ending with file name
  704. # so append the file name to a path only if it's a dir specified
  705. srcfname = os.path.basename(src)
  706. destpath = os.path.join(dest, srcfname) if os.path.isdir(dest) \
  707. else dest
  708. if sstat[stat.ST_DEV] == dstat[stat.ST_DEV]:
  709. try:
  710. os.rename(src, destpath)
  711. renamefailed = 0
  712. except Exception as e:
  713. if e.errno != errno.EXDEV:
  714. # Some random error.
  715. print("movefile: Failed to move", src, "to", dest, e)
  716. return None
  717. # Invalid cross-device-link 'bind' mounted or actually Cross-Device
  718. if renamefailed:
  719. didcopy = 0
  720. if stat.S_ISREG(sstat[stat.ST_MODE]):
  721. try: # For safety copy then move it over.
  722. shutil.copyfile(src, destpath + "#new")
  723. os.rename(destpath + "#new", destpath)
  724. didcopy = 1
  725. except Exception as e:
  726. print('movefile: copy', src, '->', dest, 'failed.', e)
  727. return None
  728. else:
  729. #we don't yet handle special, so we need to fall back to /bin/mv
  730. a = getstatusoutput("/bin/mv -f " + "'" + src + "' '" + dest + "'")
  731. if a[0] != 0:
  732. print("movefile: Failed to move special file:" + src + "' to '" + dest + "'", a)
  733. return None # failure
  734. try:
  735. if didcopy:
  736. os.lchown(destpath, sstat[stat.ST_UID], sstat[stat.ST_GID])
  737. os.chmod(destpath, stat.S_IMODE(sstat[stat.ST_MODE])) # Sticky is reset on chown
  738. os.unlink(src)
  739. except Exception as e:
  740. print("movefile: Failed to chown/chmod/unlink", dest, e)
  741. return None
  742. if newmtime:
  743. os.utime(destpath, (newmtime, newmtime))
  744. else:
  745. os.utime(destpath, (sstat[stat.ST_ATIME], sstat[stat.ST_MTIME]))
  746. newmtime = sstat[stat.ST_MTIME]
  747. return newmtime
  748. def copyfile(src, dest, newmtime = None, sstat = None):
  749. """
  750. Copies a file from src to dest, preserving all permissions and
  751. attributes; mtime will be preserved even when moving across
  752. filesystems. Returns true on success and false on failure.
  753. """
  754. #print "copyfile(" + src + "," + dest + "," + str(newmtime) + "," + str(sstat) + ")"
  755. try:
  756. if not sstat:
  757. sstat = os.lstat(src)
  758. except Exception as e:
  759. logger.warning("copyfile: stat of %s failed (%s)" % (src, e))
  760. return False
  761. destexists = 1
  762. try:
  763. dstat = os.lstat(dest)
  764. except:
  765. dstat = os.lstat(os.path.dirname(dest))
  766. destexists = 0
  767. if destexists:
  768. if stat.S_ISLNK(dstat[stat.ST_MODE]):
  769. try:
  770. os.unlink(dest)
  771. destexists = 0
  772. except Exception as e:
  773. pass
  774. if stat.S_ISLNK(sstat[stat.ST_MODE]):
  775. try:
  776. target = os.readlink(src)
  777. if destexists and not stat.S_ISDIR(dstat[stat.ST_MODE]):
  778. os.unlink(dest)
  779. os.symlink(target, dest)
  780. #os.lchown(dest,sstat[stat.ST_UID],sstat[stat.ST_GID])
  781. return os.lstat(dest)
  782. except Exception as e:
  783. logger.warning("copyfile: failed to create symlink %s to %s (%s)" % (dest, target, e))
  784. return False
  785. if stat.S_ISREG(sstat[stat.ST_MODE]):
  786. try:
  787. srcchown = False
  788. if not os.access(src, os.R_OK):
  789. # Make sure we can read it
  790. srcchown = True
  791. os.chmod(src, sstat[stat.ST_MODE] | stat.S_IRUSR)
  792. # For safety copy then move it over.
  793. shutil.copyfile(src, dest + "#new")
  794. os.rename(dest + "#new", dest)
  795. except Exception as e:
  796. logger.warning("copyfile: copy %s to %s failed (%s)" % (src, dest, e))
  797. return False
  798. finally:
  799. if srcchown:
  800. os.chmod(src, sstat[stat.ST_MODE])
  801. os.utime(src, (sstat[stat.ST_ATIME], sstat[stat.ST_MTIME]))
  802. else:
  803. #we don't yet handle special, so we need to fall back to /bin/mv
  804. a = getstatusoutput("/bin/cp -f " + "'" + src + "' '" + dest + "'")
  805. if a[0] != 0:
  806. logger.warning("copyfile: failed to copy special file %s to %s (%s)" % (src, dest, a))
  807. return False # failure
  808. try:
  809. os.lchown(dest, sstat[stat.ST_UID], sstat[stat.ST_GID])
  810. os.chmod(dest, stat.S_IMODE(sstat[stat.ST_MODE])) # Sticky is reset on chown
  811. except Exception as e:
  812. logger.warning("copyfile: failed to chown/chmod %s (%s)" % (dest, e))
  813. return False
  814. if newmtime:
  815. os.utime(dest, (newmtime, newmtime))
  816. else:
  817. os.utime(dest, (sstat[stat.ST_ATIME], sstat[stat.ST_MTIME]))
  818. newmtime = sstat[stat.ST_MTIME]
  819. return newmtime
  820. def break_hardlinks(src, sstat = None):
  821. """
  822. Ensures src is the only hardlink to this file. Other hardlinks,
  823. if any, are not affected (other than in their st_nlink value, of
  824. course). Returns true on success and false on failure.
  825. """
  826. try:
  827. if not sstat:
  828. sstat = os.lstat(src)
  829. except Exception as e:
  830. logger.warning("break_hardlinks: stat of %s failed (%s)" % (src, e))
  831. return False
  832. if sstat[stat.ST_NLINK] == 1:
  833. return True
  834. return copyfile(src, src, sstat=sstat)
  835. def which(path, item, direction = 0, history = False, executable=False):
  836. """
  837. Locate `item` in the list of paths `path` (colon separated string like $PATH).
  838. If `direction` is non-zero then the list is reversed.
  839. If `history` is True then the list of candidates also returned as result,history.
  840. If `executable` is True then the candidate has to be an executable file,
  841. otherwise the candidate simply has to exist.
  842. """
  843. if executable:
  844. is_candidate = lambda p: os.path.isfile(p) and os.access(p, os.X_OK)
  845. else:
  846. is_candidate = lambda p: os.path.exists(p)
  847. hist = []
  848. paths = (path or "").split(':')
  849. if direction != 0:
  850. paths.reverse()
  851. for p in paths:
  852. next = os.path.join(p, item)
  853. hist.append(next)
  854. if is_candidate(next):
  855. if not os.path.isabs(next):
  856. next = os.path.abspath(next)
  857. if history:
  858. return next, hist
  859. return next
  860. if history:
  861. return "", hist
  862. return ""
  863. def to_boolean(string, default=None):
  864. if not string:
  865. return default
  866. normalized = string.lower()
  867. if normalized in ("y", "yes", "1", "true"):
  868. return True
  869. elif normalized in ("n", "no", "0", "false"):
  870. return False
  871. else:
  872. raise ValueError("Invalid value for to_boolean: %s" % string)
  873. def contains(variable, checkvalues, truevalue, falsevalue, d):
  874. """Check if a variable contains all the values specified.
  875. Arguments:
  876. variable -- the variable name. This will be fetched and expanded (using
  877. d.getVar(variable)) and then split into a set().
  878. checkvalues -- if this is a string it is split on whitespace into a set(),
  879. otherwise coerced directly into a set().
  880. truevalue -- the value to return if checkvalues is a subset of variable.
  881. falsevalue -- the value to return if variable is empty or if checkvalues is
  882. not a subset of variable.
  883. d -- the data store.
  884. """
  885. val = d.getVar(variable)
  886. if not val:
  887. return falsevalue
  888. val = set(val.split())
  889. if isinstance(checkvalues, str):
  890. checkvalues = set(checkvalues.split())
  891. else:
  892. checkvalues = set(checkvalues)
  893. if checkvalues.issubset(val):
  894. return truevalue
  895. return falsevalue
  896. def contains_any(variable, checkvalues, truevalue, falsevalue, d):
  897. val = d.getVar(variable)
  898. if not val:
  899. return falsevalue
  900. val = set(val.split())
  901. if isinstance(checkvalues, str):
  902. checkvalues = set(checkvalues.split())
  903. else:
  904. checkvalues = set(checkvalues)
  905. if checkvalues & val:
  906. return truevalue
  907. return falsevalue
  908. def filter(variable, checkvalues, d):
  909. """Return all words in the variable that are present in the checkvalues.
  910. Arguments:
  911. variable -- the variable name. This will be fetched and expanded (using
  912. d.getVar(variable)) and then split into a set().
  913. checkvalues -- if this is a string it is split on whitespace into a set(),
  914. otherwise coerced directly into a set().
  915. d -- the data store.
  916. """
  917. val = d.getVar(variable)
  918. if not val:
  919. return ''
  920. val = set(val.split())
  921. if isinstance(checkvalues, str):
  922. checkvalues = set(checkvalues.split())
  923. else:
  924. checkvalues = set(checkvalues)
  925. return ' '.join(sorted(checkvalues & val))
  926. def cpu_count():
  927. return multiprocessing.cpu_count()
  928. def nonblockingfd(fd):
  929. fcntl.fcntl(fd, fcntl.F_SETFL, fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK)
  930. def process_profilelog(fn, pout = None):
  931. # Either call with a list of filenames and set pout or a filename and optionally pout.
  932. if not pout:
  933. pout = fn + '.processed'
  934. pout = open(pout, 'w')
  935. import pstats
  936. if isinstance(fn, list):
  937. p = pstats.Stats(*fn, stream=pout)
  938. else:
  939. p = pstats.Stats(fn, stream=pout)
  940. p.sort_stats('time')
  941. p.print_stats()
  942. p.print_callers()
  943. p.sort_stats('cumulative')
  944. p.print_stats()
  945. pout.flush()
  946. pout.close()
  947. #
  948. # Was present to work around multiprocessing pool bugs in python < 2.7.3
  949. #
  950. def multiprocessingpool(*args, **kwargs):
  951. import multiprocessing.pool
  952. #import multiprocessing.util
  953. #multiprocessing.util.log_to_stderr(10)
  954. # Deal with a multiprocessing bug where signals to the processes would be delayed until the work
  955. # completes. Putting in a timeout means the signals (like SIGINT/SIGTERM) get processed.
  956. def wrapper(func):
  957. def wrap(self, timeout=None):
  958. return func(self, timeout=timeout if timeout is not None else 1e100)
  959. return wrap
  960. multiprocessing.pool.IMapIterator.next = wrapper(multiprocessing.pool.IMapIterator.next)
  961. return multiprocessing.Pool(*args, **kwargs)
  962. def exec_flat_python_func(func, *args, **kwargs):
  963. """Execute a flat python function (defined with def funcname(args):...)"""
  964. # Prepare a small piece of python code which calls the requested function
  965. # To do this we need to prepare two things - a set of variables we can use to pass
  966. # the values of arguments into the calling function, and the list of arguments for
  967. # the function being called
  968. context = {}
  969. funcargs = []
  970. # Handle unnamed arguments
  971. aidx = 1
  972. for arg in args:
  973. argname = 'arg_%s' % aidx
  974. context[argname] = arg
  975. funcargs.append(argname)
  976. aidx += 1
  977. # Handle keyword arguments
  978. context.update(kwargs)
  979. funcargs.extend(['%s=%s' % (arg, arg) for arg in kwargs.keys()])
  980. code = 'retval = %s(%s)' % (func, ', '.join(funcargs))
  981. comp = bb.utils.better_compile(code, '<string>', '<string>')
  982. bb.utils.better_exec(comp, context, code, '<string>')
  983. return context['retval']
  984. def edit_metadata(meta_lines, variables, varfunc, match_overrides=False):
  985. """Edit lines from a recipe or config file and modify one or more
  986. specified variable values set in the file using a specified callback
  987. function. Lines are expected to have trailing newlines.
  988. Parameters:
  989. meta_lines: lines from the file; can be a list or an iterable
  990. (e.g. file pointer)
  991. variables: a list of variable names to look for. Functions
  992. may also be specified, but must be specified with '()' at
  993. the end of the name. Note that the function doesn't have
  994. any intrinsic understanding of _append, _prepend, _remove,
  995. or overrides, so these are considered as part of the name.
  996. These values go into a regular expression, so regular
  997. expression syntax is allowed.
  998. varfunc: callback function called for every variable matching
  999. one of the entries in the variables parameter. The function
  1000. should take four arguments:
  1001. varname: name of variable matched
  1002. origvalue: current value in file
  1003. op: the operator (e.g. '+=')
  1004. newlines: list of lines up to this point. You can use
  1005. this to prepend lines before this variable setting
  1006. if you wish.
  1007. and should return a four-element tuple:
  1008. newvalue: new value to substitute in, or None to drop
  1009. the variable setting entirely. (If the removal
  1010. results in two consecutive blank lines, one of the
  1011. blank lines will also be dropped).
  1012. newop: the operator to use - if you specify None here,
  1013. the original operation will be used.
  1014. indent: number of spaces to indent multi-line entries,
  1015. or -1 to indent up to the level of the assignment
  1016. and opening quote, or a string to use as the indent.
  1017. minbreak: True to allow the first element of a
  1018. multi-line value to continue on the same line as
  1019. the assignment, False to indent before the first
  1020. element.
  1021. To clarify, if you wish not to change the value, then you
  1022. would return like this: return origvalue, None, 0, True
  1023. match_overrides: True to match items with _overrides on the end,
  1024. False otherwise
  1025. Returns a tuple:
  1026. updated:
  1027. True if changes were made, False otherwise.
  1028. newlines:
  1029. Lines after processing
  1030. """
  1031. var_res = {}
  1032. if match_overrides:
  1033. override_re = '(_[a-zA-Z0-9-_$(){}]+)?'
  1034. else:
  1035. override_re = ''
  1036. for var in variables:
  1037. if var.endswith('()'):
  1038. var_res[var] = re.compile('^(%s%s)[ \\t]*\([ \\t]*\)[ \\t]*{' % (var[:-2].rstrip(), override_re))
  1039. else:
  1040. var_res[var] = re.compile('^(%s%s)[ \\t]*[?+:.]*=[+.]*[ \\t]*(["\'])' % (var, override_re))
  1041. updated = False
  1042. varset_start = ''
  1043. varlines = []
  1044. newlines = []
  1045. in_var = None
  1046. full_value = ''
  1047. var_end = ''
  1048. def handle_var_end():
  1049. prerun_newlines = newlines[:]
  1050. op = varset_start[len(in_var):].strip()
  1051. (newvalue, newop, indent, minbreak) = varfunc(in_var, full_value, op, newlines)
  1052. changed = (prerun_newlines != newlines)
  1053. if newvalue is None:
  1054. # Drop the value
  1055. return True
  1056. elif newvalue != full_value or (newop not in [None, op]):
  1057. if newop not in [None, op]:
  1058. # Callback changed the operator
  1059. varset_new = "%s %s" % (in_var, newop)
  1060. else:
  1061. varset_new = varset_start
  1062. if isinstance(indent, int):
  1063. if indent == -1:
  1064. indentspc = ' ' * (len(varset_new) + 2)
  1065. else:
  1066. indentspc = ' ' * indent
  1067. else:
  1068. indentspc = indent
  1069. if in_var.endswith('()'):
  1070. # A function definition
  1071. if isinstance(newvalue, list):
  1072. newlines.append('%s {\n%s%s\n}\n' % (varset_new, indentspc, ('\n%s' % indentspc).join(newvalue)))
  1073. else:
  1074. if not newvalue.startswith('\n'):
  1075. newvalue = '\n' + newvalue
  1076. if not newvalue.endswith('\n'):
  1077. newvalue = newvalue + '\n'
  1078. newlines.append('%s {%s}\n' % (varset_new, newvalue))
  1079. else:
  1080. # Normal variable
  1081. if isinstance(newvalue, list):
  1082. if not newvalue:
  1083. # Empty list -> empty string
  1084. newlines.append('%s ""\n' % varset_new)
  1085. elif minbreak:
  1086. # First item on first line
  1087. if len(newvalue) == 1:
  1088. newlines.append('%s "%s"\n' % (varset_new, newvalue[0]))
  1089. else:
  1090. newlines.append('%s "%s \\\n' % (varset_new, newvalue[0]))
  1091. for item in newvalue[1:]:
  1092. newlines.append('%s%s \\\n' % (indentspc, item))
  1093. newlines.append('%s"\n' % indentspc)
  1094. else:
  1095. # No item on first line
  1096. newlines.append('%s " \\\n' % varset_new)
  1097. for item in newvalue:
  1098. newlines.append('%s%s \\\n' % (indentspc, item))
  1099. newlines.append('%s"\n' % indentspc)
  1100. else:
  1101. newlines.append('%s "%s"\n' % (varset_new, newvalue))
  1102. return True
  1103. else:
  1104. # Put the old lines back where they were
  1105. newlines.extend(varlines)
  1106. # If newlines was touched by the function, we'll need to return True
  1107. return changed
  1108. checkspc = False
  1109. for line in meta_lines:
  1110. if in_var:
  1111. value = line.rstrip()
  1112. varlines.append(line)
  1113. if in_var.endswith('()'):
  1114. full_value += '\n' + value
  1115. else:
  1116. full_value += value[:-1]
  1117. if value.endswith(var_end):
  1118. if in_var.endswith('()'):
  1119. if full_value.count('{') - full_value.count('}') >= 0:
  1120. continue
  1121. full_value = full_value[:-1]
  1122. if handle_var_end():
  1123. updated = True
  1124. checkspc = True
  1125. in_var = None
  1126. else:
  1127. skip = False
  1128. for (varname, var_re) in var_res.items():
  1129. res = var_re.match(line)
  1130. if res:
  1131. isfunc = varname.endswith('()')
  1132. if isfunc:
  1133. splitvalue = line.split('{', 1)
  1134. var_end = '}'
  1135. else:
  1136. var_end = res.groups()[-1]
  1137. splitvalue = line.split(var_end, 1)
  1138. varset_start = splitvalue[0].rstrip()
  1139. value = splitvalue[1].rstrip()
  1140. if not isfunc and value.endswith('\\'):
  1141. value = value[:-1]
  1142. full_value = value
  1143. varlines = [line]
  1144. in_var = res.group(1)
  1145. if isfunc:
  1146. in_var += '()'
  1147. if value.endswith(var_end):
  1148. full_value = full_value[:-1]
  1149. if handle_var_end():
  1150. updated = True
  1151. checkspc = True
  1152. in_var = None
  1153. skip = True
  1154. break
  1155. if not skip:
  1156. if checkspc:
  1157. checkspc = False
  1158. if newlines and newlines[-1] == '\n' and line == '\n':
  1159. # Squash blank line if there are two consecutive blanks after a removal
  1160. continue
  1161. newlines.append(line)
  1162. return (updated, newlines)
  1163. def edit_metadata_file(meta_file, variables, varfunc):
  1164. """Edit a recipe or config file and modify one or more specified
  1165. variable values set in the file using a specified callback function.
  1166. The file is only written to if the value(s) actually change.
  1167. This is basically the file version of edit_metadata(), see that
  1168. function's description for parameter/usage information.
  1169. Returns True if the file was written to, False otherwise.
  1170. """
  1171. with open(meta_file, 'r') as f:
  1172. (updated, newlines) = edit_metadata(f, variables, varfunc)
  1173. if updated:
  1174. with open(meta_file, 'w') as f:
  1175. f.writelines(newlines)
  1176. return updated
  1177. def edit_bblayers_conf(bblayers_conf, add, remove, edit_cb=None):
  1178. """Edit bblayers.conf, adding and/or removing layers
  1179. Parameters:
  1180. bblayers_conf: path to bblayers.conf file to edit
  1181. add: layer path (or list of layer paths) to add; None or empty
  1182. list to add nothing
  1183. remove: layer path (or list of layer paths) to remove; None or
  1184. empty list to remove nothing
  1185. edit_cb: optional callback function that will be called after
  1186. processing adds/removes once per existing entry.
  1187. Returns a tuple:
  1188. notadded: list of layers specified to be added but weren't
  1189. (because they were already in the list)
  1190. notremoved: list of layers that were specified to be removed
  1191. but weren't (because they weren't in the list)
  1192. """
  1193. import fnmatch
  1194. def remove_trailing_sep(pth):
  1195. if pth and pth[-1] == os.sep:
  1196. pth = pth[:-1]
  1197. return pth
  1198. approved = bb.utils.approved_variables()
  1199. def canonicalise_path(pth):
  1200. pth = remove_trailing_sep(pth)
  1201. if 'HOME' in approved and '~' in pth:
  1202. pth = os.path.expanduser(pth)
  1203. return pth
  1204. def layerlist_param(value):
  1205. if not value:
  1206. return []
  1207. elif isinstance(value, list):
  1208. return [remove_trailing_sep(x) for x in value]
  1209. else:
  1210. return [remove_trailing_sep(value)]
  1211. addlayers = layerlist_param(add)
  1212. removelayers = layerlist_param(remove)
  1213. # Need to use a list here because we can't set non-local variables from a callback in python 2.x
  1214. bblayercalls = []
  1215. removed = []
  1216. plusequals = False
  1217. orig_bblayers = []
  1218. def handle_bblayers_firstpass(varname, origvalue, op, newlines):
  1219. bblayercalls.append(op)
  1220. if op == '=':
  1221. del orig_bblayers[:]
  1222. orig_bblayers.extend([canonicalise_path(x) for x in origvalue.split()])
  1223. return (origvalue, None, 2, False)
  1224. def handle_bblayers(varname, origvalue, op, newlines):
  1225. updated = False
  1226. bblayers = [remove_trailing_sep(x) for x in origvalue.split()]
  1227. if removelayers:
  1228. for removelayer in removelayers:
  1229. for layer in bblayers:
  1230. if fnmatch.fnmatch(canonicalise_path(layer), canonicalise_path(removelayer)):
  1231. updated = True
  1232. bblayers.remove(layer)
  1233. removed.append(removelayer)
  1234. break
  1235. if addlayers and not plusequals:
  1236. for addlayer in addlayers:
  1237. if addlayer not in bblayers:
  1238. updated = True
  1239. bblayers.append(addlayer)
  1240. del addlayers[:]
  1241. if edit_cb:
  1242. newlist = []
  1243. for layer in bblayers:
  1244. res = edit_cb(layer, canonicalise_path(layer))
  1245. if res != layer:
  1246. newlist.append(res)
  1247. updated = True
  1248. else:
  1249. newlist.append(layer)
  1250. bblayers = newlist
  1251. if updated:
  1252. if op == '+=' and not bblayers:
  1253. bblayers = None
  1254. return (bblayers, None, 2, False)
  1255. else:
  1256. return (origvalue, None, 2, False)
  1257. with open(bblayers_conf, 'r') as f:
  1258. (_, newlines) = edit_metadata(f, ['BBLAYERS'], handle_bblayers_firstpass)
  1259. if not bblayercalls:
  1260. raise Exception('Unable to find BBLAYERS in %s' % bblayers_conf)
  1261. # Try to do the "smart" thing depending on how the user has laid out
  1262. # their bblayers.conf file
  1263. if bblayercalls.count('+=') > 1:
  1264. plusequals = True
  1265. removelayers_canon = [canonicalise_path(layer) for layer in removelayers]
  1266. notadded = []
  1267. for layer in addlayers:
  1268. layer_canon = canonicalise_path(layer)
  1269. if layer_canon in orig_bblayers and not layer_canon in removelayers_canon:
  1270. notadded.append(layer)
  1271. notadded_canon = [canonicalise_path(layer) for layer in notadded]
  1272. addlayers[:] = [layer for layer in addlayers if canonicalise_path(layer) not in notadded_canon]
  1273. (updated, newlines) = edit_metadata(newlines, ['BBLAYERS'], handle_bblayers)
  1274. if addlayers:
  1275. # Still need to add these
  1276. for addlayer in addlayers:
  1277. newlines.append('BBLAYERS += "%s"\n' % addlayer)
  1278. updated = True
  1279. if updated:
  1280. with open(bblayers_conf, 'w') as f:
  1281. f.writelines(newlines)
  1282. notremoved = list(set(removelayers) - set(removed))
  1283. return (notadded, notremoved)
  1284. def get_file_layer(filename, d):
  1285. """Determine the collection (as defined by a layer's layer.conf file) containing the specified file"""
  1286. collections = (d.getVar('BBFILE_COLLECTIONS') or '').split()
  1287. collection_res = {}
  1288. for collection in collections:
  1289. collection_res[collection] = d.getVar('BBFILE_PATTERN_%s' % collection) or ''
  1290. def path_to_layer(path):
  1291. # Use longest path so we handle nested layers
  1292. matchlen = 0
  1293. match = None
  1294. for collection, regex in collection_res.items():
  1295. if len(regex) > matchlen and re.match(regex, path):
  1296. matchlen = len(regex)
  1297. match = collection
  1298. return match
  1299. result = None
  1300. bbfiles = (d.getVar('BBFILES') or '').split()
  1301. bbfilesmatch = False
  1302. for bbfilesentry in bbfiles:
  1303. if fnmatch.fnmatch(filename, bbfilesentry):
  1304. bbfilesmatch = True
  1305. result = path_to_layer(bbfilesentry)
  1306. if not bbfilesmatch:
  1307. # Probably a bbclass
  1308. result = path_to_layer(filename)
  1309. return result
  1310. # Constant taken from http://linux.die.net/include/linux/prctl.h
  1311. PR_SET_PDEATHSIG = 1
  1312. class PrCtlError(Exception):
  1313. pass
  1314. def signal_on_parent_exit(signame):
  1315. """
  1316. Trigger signame to be sent when the parent process dies
  1317. """
  1318. signum = getattr(signal, signame)
  1319. # http://linux.die.net/man/2/prctl
  1320. result = cdll['libc.so.6'].prctl(PR_SET_PDEATHSIG, signum)
  1321. if result != 0:
  1322. raise PrCtlError('prctl failed with error code %s' % result)
  1323. #
  1324. # Manually call the ioprio syscall. We could depend on other libs like psutil
  1325. # however this gets us enough of what we need to bitbake for now without the
  1326. # dependency
  1327. #
  1328. _unamearch = os.uname()[4]
  1329. IOPRIO_WHO_PROCESS = 1
  1330. IOPRIO_CLASS_SHIFT = 13
  1331. def ioprio_set(who, cls, value):
  1332. NR_ioprio_set = None
  1333. if _unamearch == "x86_64":
  1334. NR_ioprio_set = 251
  1335. elif _unamearch[0] == "i" and _unamearch[2:3] == "86":
  1336. NR_ioprio_set = 289
  1337. if NR_ioprio_set:
  1338. ioprio = value | (cls << IOPRIO_CLASS_SHIFT)
  1339. rc = cdll['libc.so.6'].syscall(NR_ioprio_set, IOPRIO_WHO_PROCESS, who, ioprio)
  1340. if rc != 0:
  1341. raise ValueError("Unable to set ioprio, syscall returned %s" % rc)
  1342. else:
  1343. bb.warn("Unable to set IO Prio for arch %s" % _unamearch)
  1344. def set_process_name(name):
  1345. from ctypes import cdll, byref, create_string_buffer
  1346. # This is nice to have for debugging, not essential
  1347. try:
  1348. libc = cdll.LoadLibrary('libc.so.6')
  1349. buf = create_string_buffer(bytes(name, 'utf-8'))
  1350. libc.prctl(15, byref(buf), 0, 0, 0)
  1351. except:
  1352. pass
  1353. # export common proxies variables from datastore to environment
  1354. def export_proxies(d):
  1355. import os
  1356. variables = ['http_proxy', 'HTTP_PROXY', 'https_proxy', 'HTTPS_PROXY',
  1357. 'ftp_proxy', 'FTP_PROXY', 'no_proxy', 'NO_PROXY',
  1358. 'GIT_PROXY_COMMAND']
  1359. exported = False
  1360. for v in variables:
  1361. if v in os.environ.keys():
  1362. exported = True
  1363. else:
  1364. v_proxy = d.getVar(v)
  1365. if v_proxy is not None:
  1366. os.environ[v] = v_proxy
  1367. exported = True
  1368. return exported
  1369. def load_plugins(logger, plugins, pluginpath):
  1370. def load_plugin(name):
  1371. logger.debug(1, 'Loading plugin %s' % name)
  1372. fp, pathname, description = imp.find_module(name, [pluginpath])
  1373. try:
  1374. return imp.load_module(name, fp, pathname, description)
  1375. finally:
  1376. if fp:
  1377. fp.close()
  1378. logger.debug(1, 'Loading plugins from %s...' % pluginpath)
  1379. expanded = (glob.glob(os.path.join(pluginpath, '*' + ext))
  1380. for ext in python_extensions)
  1381. files = itertools.chain.from_iterable(expanded)
  1382. names = set(os.path.splitext(os.path.basename(fn))[0] for fn in files)
  1383. for name in names:
  1384. if name != '__init__':
  1385. plugin = load_plugin(name)
  1386. if hasattr(plugin, 'plugin_init'):
  1387. obj = plugin.plugin_init(plugins)
  1388. plugins.append(obj or plugin)
  1389. else:
  1390. plugins.append(plugin)
  1391. class LogCatcher(logging.Handler):
  1392. """Logging handler for collecting logged messages so you can check them later"""
  1393. def __init__(self):
  1394. self.messages = []
  1395. logging.Handler.__init__(self, logging.WARNING)
  1396. def emit(self, record):
  1397. self.messages.append(bb.build.logformatter.format(record))
  1398. def contains(self, message):
  1399. return (message in self.messages)