oe-setup-layers 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 _do_checkout(args, json):
  46. repos = json['sources']
  47. for r_name in repos:
  48. r_data = repos[r_name]
  49. repodir = os.path.abspath(os.path.join(args['destdir'], r_data['path']))
  50. if 'contains_this_file' in r_data.keys():
  51. force_arg = 'force_bootstraplayer_checkout'
  52. if not args[force_arg]:
  53. print('Note: not checking out source {repo}, use {repoflag} to override.'.format(repo=r_name, repoflag='--force-bootstraplayer-checkout'))
  54. continue
  55. r_remote = r_data['git-remote']
  56. rev = r_remote['rev']
  57. desc = r_remote['describe']
  58. if not desc:
  59. desc = rev[:10]
  60. branch = r_remote['branch']
  61. remotes = r_remote['remotes']
  62. print('\nSetting up source {}, revision {}, branch {}'.format(r_name, desc, branch))
  63. if not _is_repo_git_repo(repodir):
  64. cmd = 'git init -q {}'.format(repodir)
  65. print("Running '{}'".format(cmd))
  66. subprocess.check_output(cmd, shell=True)
  67. for remote in remotes:
  68. if not _is_repo_at_remote_uri(repodir, remote, remotes[remote]['uri']):
  69. cmd = "git remote remove {} > /dev/null 2>&1; git remote add {} {}".format(remote, remote, remotes[remote]['uri'])
  70. print("Running '{}' in {}".format(cmd, repodir))
  71. subprocess.check_output(cmd, shell=True, cwd=repodir)
  72. cmd = "git fetch -q {} || true".format(remote)
  73. print("Running '{}' in {}".format(cmd, repodir))
  74. subprocess.check_output(cmd, shell=True, cwd=repodir)
  75. if not _is_repo_at_rev(repodir, rev):
  76. cmd = "git fetch -q --all || true"
  77. print("Running '{}' in {}".format(cmd, repodir))
  78. subprocess.check_output(cmd, shell=True, cwd=repodir)
  79. cmd = 'git checkout -q {}'.format(rev)
  80. print("Running '{}' in {}".format(cmd, repodir))
  81. subprocess.check_output(cmd, shell=True, cwd=repodir)
  82. if _contains_submodules(repodir):
  83. print("Repo {} contains submodules, use 'git submodule update' to ensure they are up to date".format(repodir))
  84. 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/")
  85. parser.add_argument('--force-bootstraplayer-checkout', action='store_true',
  86. 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).')
  87. try:
  88. defaultdest = os.path.dirname(subprocess.check_output('git rev-parse --show-toplevel', universal_newlines=True, shell=True, cwd=os.path.dirname(__file__)))
  89. except subprocess.CalledProcessError as e:
  90. defaultdest = os.path.abspath(".")
  91. parser.add_argument('--destdir', default=defaultdest, help='Where to check out the layers (default is {defaultdest}).'.format(defaultdest=defaultdest))
  92. parser.add_argument('--jsondata', default=__file__+".json", help='File containing the layer data in json format (default is {defaultjson}).'.format(defaultjson=__file__+".json"))
  93. args = parser.parse_args()
  94. with open(args.jsondata) as f:
  95. json_f = json.load(f)
  96. supported_versions = ["1.0"]
  97. if json_f["version"] not in supported_versions:
  98. raise Exception("File {} has version {}, which is not in supported versions: {}".format(args.jsondata, json_f["version"], supported_versions))
  99. _do_checkout(vars(args), json_f)