sdk.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. # Development tool - sdk-update command plugin
  2. #
  3. # Copyright (C) 2015-2016 Intel Corporation
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License version 2 as
  7. # published by the Free Software Foundation.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License along
  15. # with this program; if not, write to the Free Software Foundation, Inc.,
  16. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. import os
  18. import subprocess
  19. import logging
  20. import glob
  21. import shutil
  22. import errno
  23. import sys
  24. import tempfile
  25. import re
  26. from devtool import exec_build_env_command, setup_tinfoil, parse_recipe, DevtoolError
  27. logger = logging.getLogger('devtool')
  28. def parse_locked_sigs(sigfile_path):
  29. """Return <pn:task>:<hash> dictionary"""
  30. sig_dict = {}
  31. with open(sigfile_path) as f:
  32. lines = f.readlines()
  33. for line in lines:
  34. if ':' in line:
  35. taskkey, _, hashval = line.rpartition(':')
  36. sig_dict[taskkey.strip()] = hashval.split()[0]
  37. return sig_dict
  38. def generate_update_dict(sigfile_new, sigfile_old):
  39. """Return a dict containing <pn:task>:<hash> which indicates what need to be updated"""
  40. update_dict = {}
  41. sigdict_new = parse_locked_sigs(sigfile_new)
  42. sigdict_old = parse_locked_sigs(sigfile_old)
  43. for k in sigdict_new:
  44. if k not in sigdict_old:
  45. update_dict[k] = sigdict_new[k]
  46. continue
  47. if sigdict_new[k] != sigdict_old[k]:
  48. update_dict[k] = sigdict_new[k]
  49. continue
  50. return update_dict
  51. def get_sstate_objects(update_dict, sstate_dir):
  52. """Return a list containing sstate objects which are to be installed"""
  53. sstate_objects = []
  54. for k in update_dict:
  55. files = set()
  56. hashval = update_dict[k]
  57. p = sstate_dir + '/' + hashval[:2] + '/*' + hashval + '*.tgz'
  58. files |= set(glob.glob(p))
  59. p = sstate_dir + '/*/' + hashval[:2] + '/*' + hashval + '*.tgz'
  60. files |= set(glob.glob(p))
  61. files = list(files)
  62. if len(files) == 1:
  63. sstate_objects.extend(files)
  64. elif len(files) > 1:
  65. logger.error("More than one matching sstate object found for %s" % hashval)
  66. return sstate_objects
  67. def mkdir(d):
  68. try:
  69. os.makedirs(d)
  70. except OSError as e:
  71. if e.errno != errno.EEXIST:
  72. raise e
  73. def install_sstate_objects(sstate_objects, src_sdk, dest_sdk):
  74. """Install sstate objects into destination SDK"""
  75. sstate_dir = os.path.join(dest_sdk, 'sstate-cache')
  76. if not os.path.exists(sstate_dir):
  77. logger.error("Missing sstate-cache directory in %s, it might not be an extensible SDK." % dest_sdk)
  78. raise
  79. for sb in sstate_objects:
  80. dst = sb.replace(src_sdk, dest_sdk)
  81. destdir = os.path.dirname(dst)
  82. mkdir(destdir)
  83. logger.debug("Copying %s to %s" % (sb, dst))
  84. shutil.copy(sb, dst)
  85. def check_manifest(fn, basepath):
  86. import bb.utils
  87. changedfiles = []
  88. with open(fn, 'r') as f:
  89. for line in f:
  90. splitline = line.split()
  91. if len(splitline) > 1:
  92. chksum = splitline[0]
  93. fpath = splitline[1]
  94. curr_chksum = bb.utils.sha256_file(os.path.join(basepath, fpath))
  95. if chksum != curr_chksum:
  96. logger.debug('File %s changed: old csum = %s, new = %s' % (os.path.join(basepath, fpath), curr_chksum, chksum))
  97. changedfiles.append(fpath)
  98. return changedfiles
  99. def sdk_update(args, config, basepath, workspace):
  100. """Entry point for devtool sdk-update command"""
  101. updateserver = args.updateserver
  102. if not updateserver:
  103. updateserver = config.get('SDK', 'updateserver', '')
  104. logger.debug("updateserver: %s" % updateserver)
  105. # Make sure we are using sdk-update from within SDK
  106. logger.debug("basepath = %s" % basepath)
  107. old_locked_sig_file_path = os.path.join(basepath, 'conf/locked-sigs.inc')
  108. if not os.path.exists(old_locked_sig_file_path):
  109. logger.error("Not using devtool's sdk-update command from within an extensible SDK. Please specify correct basepath via --basepath option")
  110. return -1
  111. else:
  112. logger.debug("Found conf/locked-sigs.inc in %s" % basepath)
  113. if not '://' in updateserver:
  114. logger.error("Update server must be a URL")
  115. return -1
  116. layers_dir = os.path.join(basepath, 'layers')
  117. conf_dir = os.path.join(basepath, 'conf')
  118. # Grab variable values
  119. tinfoil = setup_tinfoil(config_only=True, basepath=basepath)
  120. try:
  121. stamps_dir = tinfoil.config_data.getVar('STAMPS_DIR')
  122. sstate_mirrors = tinfoil.config_data.getVar('SSTATE_MIRRORS')
  123. site_conf_version = tinfoil.config_data.getVar('SITE_CONF_VERSION')
  124. finally:
  125. tinfoil.shutdown()
  126. tmpsdk_dir = tempfile.mkdtemp()
  127. try:
  128. os.makedirs(os.path.join(tmpsdk_dir, 'conf'))
  129. new_locked_sig_file_path = os.path.join(tmpsdk_dir, 'conf', 'locked-sigs.inc')
  130. # Fetch manifest from server
  131. tmpmanifest = os.path.join(tmpsdk_dir, 'conf', 'sdk-conf-manifest')
  132. ret = subprocess.call("wget -q -O %s %s/conf/sdk-conf-manifest" % (tmpmanifest, updateserver), shell=True)
  133. changedfiles = check_manifest(tmpmanifest, basepath)
  134. if not changedfiles:
  135. logger.info("Already up-to-date")
  136. return 0
  137. # Update metadata
  138. logger.debug("Updating metadata via git ...")
  139. #Check for the status before doing a fetch and reset
  140. if os.path.exists(os.path.join(basepath, 'layers/.git')):
  141. out = subprocess.check_output("git status --porcelain", shell=True, cwd=layers_dir)
  142. if not out:
  143. ret = subprocess.call("git fetch --all; git reset --hard", shell=True, cwd=layers_dir)
  144. else:
  145. logger.error("Failed to update metadata as there have been changes made to it. Aborting.");
  146. logger.error("Changed files:\n%s" % out);
  147. return -1
  148. else:
  149. ret = -1
  150. if ret != 0:
  151. ret = subprocess.call("git clone %s/layers/.git" % updateserver, shell=True, cwd=tmpsdk_dir)
  152. if ret != 0:
  153. logger.error("Updating metadata via git failed")
  154. return ret
  155. logger.debug("Updating conf files ...")
  156. for changedfile in changedfiles:
  157. ret = subprocess.call("wget -q -O %s %s/%s" % (changedfile, updateserver, changedfile), shell=True, cwd=tmpsdk_dir)
  158. if ret != 0:
  159. logger.error("Updating %s failed" % changedfile)
  160. return ret
  161. # Check if UNINATIVE_CHECKSUM changed
  162. uninative = False
  163. if 'conf/local.conf' in changedfiles:
  164. def read_uninative_checksums(fn):
  165. chksumitems = []
  166. with open(fn, 'r') as f:
  167. for line in f:
  168. if line.startswith('UNINATIVE_CHECKSUM'):
  169. splitline = re.split(r'[\[\]"\']', line)
  170. if len(splitline) > 3:
  171. chksumitems.append((splitline[1], splitline[3]))
  172. return chksumitems
  173. oldsums = read_uninative_checksums(os.path.join(basepath, 'conf/local.conf'))
  174. newsums = read_uninative_checksums(os.path.join(tmpsdk_dir, 'conf/local.conf'))
  175. if oldsums != newsums:
  176. uninative = True
  177. for buildarch, chksum in newsums:
  178. uninative_file = os.path.join('downloads', 'uninative', chksum, '%s-nativesdk-libc.tar.bz2' % buildarch)
  179. mkdir(os.path.join(tmpsdk_dir, os.path.dirname(uninative_file)))
  180. ret = subprocess.call("wget -q -O %s %s/%s" % (uninative_file, updateserver, uninative_file), shell=True, cwd=tmpsdk_dir)
  181. # Ok, all is well at this point - move everything over
  182. tmplayers_dir = os.path.join(tmpsdk_dir, 'layers')
  183. if os.path.exists(tmplayers_dir):
  184. shutil.rmtree(layers_dir)
  185. shutil.move(tmplayers_dir, layers_dir)
  186. for changedfile in changedfiles:
  187. destfile = os.path.join(basepath, changedfile)
  188. os.remove(destfile)
  189. shutil.move(os.path.join(tmpsdk_dir, changedfile), destfile)
  190. os.remove(os.path.join(conf_dir, 'sdk-conf-manifest'))
  191. shutil.move(tmpmanifest, conf_dir)
  192. if uninative:
  193. shutil.rmtree(os.path.join(basepath, 'downloads', 'uninative'))
  194. shutil.move(os.path.join(tmpsdk_dir, 'downloads', 'uninative'), os.path.join(basepath, 'downloads'))
  195. if not sstate_mirrors:
  196. with open(os.path.join(conf_dir, 'site.conf'), 'a') as f:
  197. f.write('SCONF_VERSION = "%s"\n' % site_conf_version)
  198. f.write('SSTATE_MIRRORS_append = " file://.* %s/sstate-cache/PATH \\n "\n' % updateserver)
  199. finally:
  200. shutil.rmtree(tmpsdk_dir)
  201. if not args.skip_prepare:
  202. # Find all potentially updateable tasks
  203. sdk_update_targets = []
  204. tasks = ['do_populate_sysroot', 'do_packagedata']
  205. for root, _, files in os.walk(stamps_dir):
  206. for fn in files:
  207. if not '.sigdata.' in fn:
  208. for task in tasks:
  209. if '.%s.' % task in fn or '.%s_setscene.' % task in fn:
  210. sdk_update_targets.append('%s:%s' % (os.path.basename(root), task))
  211. # Run bitbake command for the whole SDK
  212. logger.info("Preparing build system... (This may take some time.)")
  213. try:
  214. exec_build_env_command(config.init_path, basepath, 'bitbake --setscene-only %s' % ' '.join(sdk_update_targets), stderr=subprocess.STDOUT)
  215. output, _ = exec_build_env_command(config.init_path, basepath, 'bitbake -n %s' % ' '.join(sdk_update_targets), stderr=subprocess.STDOUT)
  216. runlines = []
  217. for line in output.splitlines():
  218. if 'Running task ' in line:
  219. runlines.append(line)
  220. if runlines:
  221. logger.error('Unexecuted tasks found in preparation log:\n %s' % '\n '.join(runlines))
  222. return -1
  223. except bb.process.ExecutionError as e:
  224. logger.error('Preparation failed:\n%s' % e.stdout)
  225. return -1
  226. return 0
  227. def sdk_install(args, config, basepath, workspace):
  228. """Entry point for the devtool sdk-install command"""
  229. import oe.recipeutils
  230. import bb.process
  231. for recipe in args.recipename:
  232. if recipe in workspace:
  233. raise DevtoolError('recipe %s is a recipe in your workspace' % recipe)
  234. tasks = ['do_populate_sysroot', 'do_packagedata']
  235. stampprefixes = {}
  236. def checkstamp(recipe):
  237. stampprefix = stampprefixes[recipe]
  238. stamps = glob.glob(stampprefix + '*')
  239. for stamp in stamps:
  240. if '.sigdata.' not in stamp and stamp.startswith((stampprefix + '.', stampprefix + '_setscene.')):
  241. return True
  242. else:
  243. return False
  244. install_recipes = []
  245. tinfoil = setup_tinfoil(config_only=False, basepath=basepath)
  246. try:
  247. for recipe in args.recipename:
  248. rd = parse_recipe(config, tinfoil, recipe, True)
  249. if not rd:
  250. return 1
  251. stampprefixes[recipe] = '%s.%s' % (rd.getVar('STAMP'), tasks[0])
  252. if checkstamp(recipe):
  253. logger.info('%s is already installed' % recipe)
  254. else:
  255. install_recipes.append(recipe)
  256. finally:
  257. tinfoil.shutdown()
  258. if install_recipes:
  259. logger.info('Installing %s...' % ', '.join(install_recipes))
  260. install_tasks = []
  261. for recipe in install_recipes:
  262. for task in tasks:
  263. if recipe.endswith('-native') and 'package' in task:
  264. continue
  265. install_tasks.append('%s:%s' % (recipe, task))
  266. options = ''
  267. if not args.allow_build:
  268. options += ' --setscene-only'
  269. try:
  270. exec_build_env_command(config.init_path, basepath, 'bitbake %s %s' % (options, ' '.join(install_tasks)), watch=True)
  271. except bb.process.ExecutionError as e:
  272. raise DevtoolError('Failed to install %s:\n%s' % (recipe, str(e)))
  273. failed = False
  274. for recipe in install_recipes:
  275. if checkstamp(recipe):
  276. logger.info('Successfully installed %s' % recipe)
  277. else:
  278. raise DevtoolError('Failed to install %s - unavailable' % recipe)
  279. failed = True
  280. if failed:
  281. return 2
  282. def register_commands(subparsers, context):
  283. """Register devtool subcommands from the sdk plugin"""
  284. if context.fixed_setup:
  285. parser_sdk = subparsers.add_parser('sdk-update',
  286. help='Update SDK components',
  287. description='Updates installed SDK components from a remote server',
  288. group='sdk')
  289. updateserver = context.config.get('SDK', 'updateserver', '')
  290. if updateserver:
  291. parser_sdk.add_argument('updateserver', help='The update server to fetch latest SDK components from (default %s)' % updateserver, nargs='?')
  292. else:
  293. parser_sdk.add_argument('updateserver', help='The update server to fetch latest SDK components from')
  294. parser_sdk.add_argument('--skip-prepare', action="store_true", help='Skip re-preparing the build system after updating (for debugging only)')
  295. parser_sdk.set_defaults(func=sdk_update)
  296. parser_sdk_install = subparsers.add_parser('sdk-install',
  297. help='Install additional SDK components',
  298. description='Installs additional recipe development files into the SDK. (You can use "devtool search" to find available recipes.)',
  299. group='sdk')
  300. parser_sdk_install.add_argument('recipename', help='Name of the recipe to install the development artifacts for', nargs='+')
  301. parser_sdk_install.add_argument('-s', '--allow-build', help='Allow building requested item(s) from source', action='store_true')
  302. parser_sdk_install.set_defaults(func=sdk_install)