| Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
|  | 2 | # | 
|  | 3 | # Copyright OpenEmbedded Contributors | 
|  | 4 | # | 
|  | 5 | # SPDX-License-Identifier: MIT | 
|  | 6 | # | 
|  | 7 |  | 
|  | 8 | # This file was copied from poky(or oe-core)/scripts/oe-setup-layers by running | 
|  | 9 | # | 
|  | 10 | # bitbake-layers create-layers-setup destdir | 
|  | 11 | # | 
|  | 12 | # It is recommended that you do not modify this file directly, but rather re-run the above command to get the freshest upstream copy. | 
| Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 13 | # | 
|  | 14 | # This script is idempotent. Subsequent runs only change what is necessary to | 
|  | 15 | # ensure your layers match your configuration. | 
| Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 16 |  | 
|  | 17 | import argparse | 
|  | 18 | import json | 
|  | 19 | import os | 
|  | 20 | import subprocess | 
|  | 21 |  | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 22 | def _is_repo_git_repo(repodir): | 
| Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 23 | try: | 
| Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 24 | curr_toplevel = subprocess.check_output("git -C %s rev-parse --show-toplevel" % repodir, shell=True, stderr=subprocess.DEVNULL) | 
|  | 25 | if curr_toplevel.strip().decode("utf-8") == repodir: | 
|  | 26 | return True | 
| Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 27 | except subprocess.CalledProcessError: | 
| Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 28 | pass | 
|  | 29 | return False | 
| Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 30 |  | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 31 | def _is_repo_at_rev(repodir, rev): | 
| Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 32 | try: | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 33 | curr_rev = subprocess.check_output("git -C %s rev-parse HEAD" % repodir, shell=True, stderr=subprocess.DEVNULL) | 
| Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 34 | if curr_rev.strip().decode("utf-8") == rev: | 
|  | 35 | return True | 
|  | 36 | except subprocess.CalledProcessError: | 
|  | 37 | pass | 
|  | 38 | return False | 
|  | 39 |  | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 40 | def _is_repo_at_remote_uri(repodir, remote, uri): | 
| Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 41 | try: | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 42 | curr_uri = subprocess.check_output("git -C %s remote get-url %s" % (repodir, remote), shell=True, stderr=subprocess.DEVNULL) | 
| Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 43 | if curr_uri.strip().decode("utf-8") == uri: | 
|  | 44 | return True | 
|  | 45 | except subprocess.CalledProcessError: | 
|  | 46 | pass | 
|  | 47 | return False | 
|  | 48 |  | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 49 | def _contains_submodules(repodir): | 
|  | 50 | return os.path.exists(os.path.join(repodir,".gitmodules")) | 
| Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 51 |  | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 52 | def _do_checkout(args, json): | 
|  | 53 | repos = json['sources'] | 
|  | 54 | for r_name in repos: | 
|  | 55 | r_data = repos[r_name] | 
|  | 56 | repodir = os.path.abspath(os.path.join(args['destdir'], r_data['path'])) | 
|  | 57 |  | 
|  | 58 | if 'contains_this_file' in r_data.keys(): | 
| Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 59 | force_arg = 'force_bootstraplayer_checkout' | 
|  | 60 | if not args[force_arg]: | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 61 | print('Note: not checking out source {repo}, use {repoflag} to override.'.format(repo=r_name, repoflag='--force-bootstraplayer-checkout')) | 
| Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 62 | continue | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 63 | r_remote = r_data['git-remote'] | 
|  | 64 | rev = r_remote['rev'] | 
|  | 65 | desc = r_remote['describe'] | 
| Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 66 | if not desc: | 
|  | 67 | desc = rev[:10] | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 68 | branch = r_remote['branch'] | 
|  | 69 | remotes = r_remote['remotes'] | 
| Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 70 |  | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 71 | print('\nSetting up source {}, revision {}, branch {}'.format(r_name, desc, branch)) | 
|  | 72 | if not _is_repo_git_repo(repodir): | 
|  | 73 | cmd = 'git init -q {}'.format(repodir) | 
| Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 74 | print("Running '{}'".format(cmd)) | 
|  | 75 | subprocess.check_output(cmd, shell=True) | 
| Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 76 |  | 
|  | 77 | for remote in remotes: | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 78 | if not _is_repo_at_remote_uri(repodir, remote, remotes[remote]['uri']): | 
| Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 79 | cmd = "git remote remove {} > /dev/null 2>&1; git remote add {} {}".format(remote, remote, remotes[remote]['uri']) | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 80 | print("Running '{}' in {}".format(cmd, repodir)) | 
|  | 81 | subprocess.check_output(cmd, shell=True, cwd=repodir) | 
| Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 82 |  | 
|  | 83 | cmd = "git fetch -q {} || true".format(remote) | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 84 | print("Running '{}' in {}".format(cmd, repodir)) | 
|  | 85 | subprocess.check_output(cmd, shell=True, cwd=repodir) | 
| Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 86 |  | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 87 | if not _is_repo_at_rev(repodir, rev): | 
| Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 88 | cmd = "git fetch -q --all || true" | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 89 | print("Running '{}' in {}".format(cmd, repodir)) | 
|  | 90 | subprocess.check_output(cmd, shell=True, cwd=repodir) | 
| Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 91 |  | 
| Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 92 | cmd = 'git checkout -q {}'.format(rev) | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 93 | print("Running '{}' in {}".format(cmd, repodir)) | 
|  | 94 | subprocess.check_output(cmd, shell=True, cwd=repodir) | 
|  | 95 |  | 
|  | 96 | if _contains_submodules(repodir): | 
|  | 97 | print("Repo {} contains submodules, use 'git submodule update' to ensure they are up to date".format(repodir)) | 
| Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 98 |  | 
| Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 99 | 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/") | 
|  | 100 |  | 
|  | 101 | parser.add_argument('--force-bootstraplayer-checkout', action='store_true', | 
|  | 102 | 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).') | 
|  | 103 |  | 
|  | 104 | try: | 
|  | 105 | defaultdest = os.path.dirname(subprocess.check_output('git rev-parse --show-toplevel', universal_newlines=True, shell=True, cwd=os.path.dirname(__file__))) | 
|  | 106 | except subprocess.CalledProcessError as e: | 
|  | 107 | defaultdest = os.path.abspath(".") | 
|  | 108 |  | 
|  | 109 | parser.add_argument('--destdir', default=defaultdest, help='Where to check out the layers (default is {defaultdest}).'.format(defaultdest=defaultdest)) | 
|  | 110 | parser.add_argument('--jsondata', default=__file__+".json", help='File containing the layer data in json format (default is {defaultjson}).'.format(defaultjson=__file__+".json")) | 
|  | 111 |  | 
|  | 112 | args = parser.parse_args() | 
|  | 113 |  | 
|  | 114 | with open(args.jsondata) as f: | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 115 | json_f = json.load(f) | 
| Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 116 |  | 
|  | 117 | supported_versions = ["1.0"] | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 118 | if json_f["version"] not in supported_versions: | 
|  | 119 | raise Exception("File {} has version {}, which is not in supported versions: {}".format(args.jsondata, json_f["version"], supported_versions)) | 
| Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 120 |  | 
| Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 121 | _do_checkout(vars(args), json_f) |