makesetup.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. # SPDX-License-Identifier: GPL-2.0-only
  5. #
  6. import logging
  7. import os
  8. import stat
  9. import sys
  10. import shutil
  11. import bb.utils
  12. import bb.process
  13. from bblayers.common import LayerPlugin
  14. logger = logging.getLogger('bitbake-layers')
  15. sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
  16. import oe.buildcfg
  17. def plugin_init(plugins):
  18. return MakeSetupPlugin()
  19. class MakeSetupPlugin(LayerPlugin):
  20. def _get_repo_path(self, layer_path):
  21. repo_path, _ = bb.process.run('git rev-parse --show-toplevel', cwd=layer_path)
  22. return repo_path.strip()
  23. def _get_remotes(self, repo_path):
  24. remotes = {}
  25. remotes_list,_ = bb.process.run('git remote', cwd=repo_path)
  26. for r in remotes_list.split():
  27. uri,_ = bb.process.run('git remote get-url {r}'.format(r=r), cwd=repo_path)
  28. remotes[r] = {'uri':uri.strip()}
  29. return remotes
  30. def _get_describe(self, repo_path):
  31. try:
  32. describe,_ = bb.process.run('git describe --tags', cwd=repo_path)
  33. except bb.process.ExecutionError:
  34. return ""
  35. return describe.strip()
  36. def make_repo_config(self, destdir):
  37. """ This is a helper function for the writer plugins that discovers currently confugured layers.
  38. The writers do not have to use it, but it can save a bit of work and avoid duplicated code, hence it is
  39. available here. """
  40. repos = {}
  41. layers = oe.buildcfg.get_layer_revisions(self.tinfoil.config_data)
  42. try:
  43. destdir_repo = self._get_repo_path(destdir)
  44. except bb.process.ExecutionError:
  45. destdir_repo = None
  46. for (l_path, l_name, l_branch, l_rev, l_ismodified) in layers:
  47. if l_name == 'workspace':
  48. continue
  49. if l_ismodified:
  50. logger.error("Layer {name} in {path} has uncommitted modifications or is not in a git repository.".format(name=l_name,path=l_path))
  51. return
  52. repo_path = self._get_repo_path(l_path)
  53. if repo_path not in repos.keys():
  54. repos[repo_path] = {'path':os.path.basename(repo_path),'git-remote':{'rev':l_rev, 'branch':l_branch, 'remotes':self._get_remotes(repo_path), 'describe':self._get_describe(repo_path)}}
  55. if repo_path == destdir_repo:
  56. repos[repo_path]['contains_this_file'] = True
  57. if not repos[repo_path]['git-remote']['remotes'] and not repos[repo_path]['contains_this_file']:
  58. logger.error("Layer repository in {path} does not have any remotes configured. Please add at least one with 'git remote add'.".format(path=repo_path))
  59. return
  60. top_path = os.path.commonpath([os.path.dirname(r) for r in repos.keys()])
  61. repos_nopaths = {}
  62. for r in repos.keys():
  63. r_nopath = os.path.basename(r)
  64. repos_nopaths[r_nopath] = repos[r]
  65. r_relpath = os.path.relpath(r, top_path)
  66. repos_nopaths[r_nopath]['path'] = r_relpath
  67. return repos_nopaths
  68. def do_make_setup(self, args):
  69. """ Writes out a configuration file and/or a script that replicate the directory structure and revisions of the layers in a current build. """
  70. for p in self.plugins:
  71. if str(p) == args.writer:
  72. p.do_write(self, args)
  73. def register_commands(self, sp):
  74. parser_setup_layers = self.add_command(sp, 'create-layers-setup', self.do_make_setup, parserecipes=False)
  75. parser_setup_layers.add_argument('destdir',
  76. help='Directory where to write the output\n(if it is inside one of the layers, the layer becomes a bootstrap repository and thus will be excluded from fetching).')
  77. parser_setup_layers.add_argument('--output-prefix', '-o',
  78. help='File name prefix for the output files, if the default (setup-layers) is undesirable.')
  79. self.plugins = []
  80. for path in (self.tinfoil.config_data.getVar('BBPATH').split(':')):
  81. pluginpath = os.path.join(path, 'lib', 'bblayers', 'setupwriters')
  82. bb.utils.load_plugins(logger, self.plugins, pluginpath)
  83. parser_setup_layers.add_argument('--writer', '-w', choices=[str(p) for p in self.plugins], help="Choose the output format (defaults to oe-setup-layers).\n\nCurrently supported options are:\noe-setup-layers - a self-contained python script and a json config for it.\n\n", default="oe-setup-layers")
  84. for plugin in self.plugins:
  85. if hasattr(plugin, 'register_arguments'):
  86. plugin.register_arguments(parser_setup_layers)