sdk.py 15 KB

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