oe-setup-layers 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright OpenEmbedded Contributors
  4. #
  5. # SPDX-License-Identifier: MIT
  6. #
  7. # This file was copied from poky(or oe-core)/scripts/oe-setup-layers by running
  8. #
  9. # bitbake-layers create-layers-setup destdir
  10. #
  11. # It is recommended that you do not modify this file directly, but rather re-run the above command to get the freshest upstream copy.
  12. #
  13. # This script is idempotent. Subsequent runs only change what is necessary to
  14. # ensure your layers match your configuration.
  15. import argparse
  16. import json
  17. import os
  18. import subprocess
  19. def _is_repo_git_repo(repodir):
  20. try:
  21. curr_toplevel = subprocess.check_output("git -C %s rev-parse --show-toplevel" % repodir, shell=True, stderr=subprocess.DEVNULL)
  22. if curr_toplevel.strip().decode("utf-8") == repodir:
  23. return True
  24. except subprocess.CalledProcessError:
  25. pass
  26. return False
  27. def _is_repo_at_rev(repodir, rev):
  28. try:
  29. curr_rev = subprocess.check_output("git -C %s rev-parse HEAD" % repodir, shell=True, stderr=subprocess.DEVNULL)
  30. if curr_rev.strip().decode("utf-8") == rev:
  31. return True
  32. except subprocess.CalledProcessError:
  33. pass
  34. return False
  35. def _is_repo_at_remote_uri(repodir, remote, uri):
  36. try:
  37. curr_uri = subprocess.check_output("git -C %s remote get-url %s" % (repodir, remote), shell=True, stderr=subprocess.DEVNULL)
  38. if curr_uri.strip().decode("utf-8") == uri:
  39. return True
  40. except subprocess.CalledProcessError:
  41. pass
  42. return False
  43. def _contains_submodules(repodir):
  44. return os.path.exists(os.path.join(repodir,".gitmodules"))
  45. def _write_layer_list(dest, repodirs):
  46. layers = []
  47. for r in repodirs:
  48. for root, dirs, files in os.walk(r):
  49. if os.path.basename(root) == 'conf' and 'layer.conf' in files:
  50. layers.append(os.path.relpath(os.path.dirname(root), dest))
  51. layers_f = os.path.join(dest, ".oe-layers.json")
  52. print("Writing list of layers into {}".format(layers_f))
  53. with open(layers_f, 'w') as f:
  54. json.dump({"version":"1.0","layers":layers}, f, sort_keys=True, indent=4)
  55. def _do_checkout(args, json):
  56. repos = json['sources']
  57. repodirs = []
  58. oesetupbuild = None
  59. for r_name in repos:
  60. r_data = repos[r_name]
  61. repodir = os.path.abspath(os.path.join(args['destdir'], r_data['path']))
  62. repodirs.append(repodir)
  63. if 'contains_this_file' in r_data.keys():
  64. force_arg = 'force_bootstraplayer_checkout'
  65. if not args[force_arg]:
  66. print('Note: not checking out source {repo}, use {repoflag} to override.'.format(repo=r_name, repoflag='--force-bootstraplayer-checkout'))
  67. continue
  68. r_remote = r_data['git-remote']
  69. rev = r_remote['rev']
  70. desc = r_remote['describe']
  71. if not desc:
  72. desc = rev[:10]
  73. branch = r_remote['branch']
  74. remotes = r_remote['remotes']
  75. print('\nSetting up source {}, revision {}, branch {}'.format(r_name, desc, branch))
  76. if not _is_repo_git_repo(repodir):
  77. cmd = 'git init -q {}'.format(repodir)
  78. print("Running '{}'".format(cmd))
  79. subprocess.check_output(cmd, shell=True)
  80. for remote in remotes:
  81. if not _is_repo_at_remote_uri(repodir, remote, remotes[remote]['uri']):
  82. cmd = "git remote remove {} > /dev/null 2>&1; git remote add {} {}".format(remote, remote, remotes[remote]['uri'])
  83. print("Running '{}' in {}".format(cmd, repodir))
  84. subprocess.check_output(cmd, shell=True, cwd=repodir)
  85. cmd = "git fetch -q {} || true".format(remote)
  86. print("Running '{}' in {}".format(cmd, repodir))
  87. subprocess.check_output(cmd, shell=True, cwd=repodir)
  88. if not _is_repo_at_rev(repodir, rev):
  89. cmd = "git fetch -q --all || true"
  90. print("Running '{}' in {}".format(cmd, repodir))
  91. subprocess.check_output(cmd, shell=True, cwd=repodir)
  92. cmd = 'git checkout -q {}'.format(rev)
  93. print("Running '{}' in {}".format(cmd, repodir))
  94. subprocess.check_output(cmd, shell=True, cwd=repodir)
  95. if _contains_submodules(repodir):
  96. print("Repo {} contains submodules, use 'git submodule update' to ensure they are up to date".format(repodir))
  97. if os.path.exists(os.path.join(repodir, 'scripts/oe-setup-build')):
  98. oesetupbuild = os.path.join(repodir, 'scripts/oe-setup-build')
  99. _write_layer_list(args['destdir'], repodirs)
  100. if oesetupbuild:
  101. oesetupbuild_symlink = os.path.join(args['destdir'], 'setup-build')
  102. if os.path.exists(oesetupbuild_symlink):
  103. os.remove(oesetupbuild_symlink)
  104. os.symlink(os.path.relpath(oesetupbuild,args['destdir']),oesetupbuild_symlink)
  105. print("\nRun '{}' to list available build configuration templates and set up a build from one of them.".format(oesetupbuild_symlink))
  106. parser = argparse.ArgumentParser(description="A self contained python script that fetches all the needed layers and sets them to correct revisions using data in a json format from a separate file. The json data can be created from an active build directory with 'bitbake-layers create-layers-setup destdir' and there's a sample file and a schema in meta/files/")
  107. parser.add_argument('--force-bootstraplayer-checkout', action='store_true',
  108. help='Force the checkout of the layer containing this file (by default it is presumed that as this script is in it, the layer is already in place).')
  109. try:
  110. defaultdest = os.path.dirname(subprocess.check_output('git rev-parse --show-toplevel', universal_newlines=True, shell=True, cwd=os.path.dirname(__file__)))
  111. except subprocess.CalledProcessError as e:
  112. defaultdest = os.path.abspath(".")
  113. parser.add_argument('--destdir', default=defaultdest, help='Where to check out the layers (default is {defaultdest}).'.format(defaultdest=defaultdest))
  114. parser.add_argument('--jsondata', default=__file__+".json", help='File containing the layer data in json format (default is {defaultjson}).'.format(defaultjson=__file__+".json"))
  115. args = parser.parse_args()
  116. with open(args.jsondata) as f:
  117. json_f = json.load(f)
  118. supported_versions = ["1.0"]
  119. if json_f["version"] not in supported_versions:
  120. raise Exception("File {} has version {}, which is not in supported versions: {}".format(args.jsondata, json_f["version"], supported_versions))
  121. _do_checkout(vars(args), json_f)