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 | |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 52 | def _write_layer_list(dest, repodirs): |
| 53 | layers = [] |
| 54 | for r in repodirs: |
| 55 | for root, dirs, files in os.walk(r): |
| 56 | if os.path.basename(root) == 'conf' and 'layer.conf' in files: |
| 57 | layers.append(os.path.relpath(os.path.dirname(root), dest)) |
| 58 | layers_f = os.path.join(dest, ".oe-layers.json") |
| 59 | print("Writing list of layers into {}".format(layers_f)) |
| 60 | with open(layers_f, 'w') as f: |
| 61 | json.dump({"version":"1.0","layers":layers}, f, sort_keys=True, indent=4) |
| 62 | |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 63 | def _do_checkout(args, json): |
| 64 | repos = json['sources'] |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 65 | repodirs = [] |
| 66 | oesetupbuild = None |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 67 | for r_name in repos: |
| 68 | r_data = repos[r_name] |
| 69 | repodir = os.path.abspath(os.path.join(args['destdir'], r_data['path'])) |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 70 | repodirs.append(repodir) |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 71 | |
| 72 | if 'contains_this_file' in r_data.keys(): |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 73 | force_arg = 'force_bootstraplayer_checkout' |
| 74 | if not args[force_arg]: |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 75 | 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] | 76 | continue |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 77 | r_remote = r_data['git-remote'] |
| 78 | rev = r_remote['rev'] |
| 79 | desc = r_remote['describe'] |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 80 | if not desc: |
| 81 | desc = rev[:10] |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 82 | branch = r_remote['branch'] |
| 83 | remotes = r_remote['remotes'] |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 84 | |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 85 | print('\nSetting up source {}, revision {}, branch {}'.format(r_name, desc, branch)) |
| 86 | if not _is_repo_git_repo(repodir): |
| 87 | cmd = 'git init -q {}'.format(repodir) |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 88 | print("Running '{}'".format(cmd)) |
| 89 | subprocess.check_output(cmd, shell=True) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 90 | |
| 91 | for remote in remotes: |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 92 | if not _is_repo_at_remote_uri(repodir, remote, remotes[remote]['uri']): |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 93 | 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] | 94 | print("Running '{}' in {}".format(cmd, repodir)) |
| 95 | subprocess.check_output(cmd, shell=True, cwd=repodir) |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 96 | |
| 97 | cmd = "git fetch -q {} || true".format(remote) |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 98 | print("Running '{}' in {}".format(cmd, repodir)) |
| 99 | subprocess.check_output(cmd, shell=True, cwd=repodir) |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 100 | |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 101 | if not _is_repo_at_rev(repodir, rev): |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 102 | cmd = "git fetch -q --all || true" |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 103 | print("Running '{}' in {}".format(cmd, repodir)) |
| 104 | subprocess.check_output(cmd, shell=True, cwd=repodir) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 105 | |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 106 | cmd = 'git checkout -q {}'.format(rev) |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 107 | print("Running '{}' in {}".format(cmd, repodir)) |
| 108 | subprocess.check_output(cmd, shell=True, cwd=repodir) |
| 109 | |
| 110 | if _contains_submodules(repodir): |
| 111 | print("Repo {} contains submodules, use 'git submodule update' to ensure they are up to date".format(repodir)) |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 112 | if os.path.exists(os.path.join(repodir, 'scripts/oe-setup-build')): |
| 113 | oesetupbuild = os.path.join(repodir, 'scripts/oe-setup-build') |
| 114 | |
| 115 | _write_layer_list(args['destdir'], repodirs) |
| 116 | |
| 117 | if oesetupbuild: |
| 118 | oesetupbuild_symlink = os.path.join(args['destdir'], 'setup-build') |
| 119 | if os.path.exists(oesetupbuild_symlink): |
| 120 | os.remove(oesetupbuild_symlink) |
| 121 | os.symlink(os.path.relpath(oesetupbuild,args['destdir']),oesetupbuild_symlink) |
| 122 | print("\nRun '{}' to list available build configuration templates and set up a build from one of them.".format(oesetupbuild_symlink)) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 123 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 124 | 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/") |
| 125 | |
| 126 | parser.add_argument('--force-bootstraplayer-checkout', action='store_true', |
| 127 | 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).') |
| 128 | |
| 129 | try: |
| 130 | defaultdest = os.path.dirname(subprocess.check_output('git rev-parse --show-toplevel', universal_newlines=True, shell=True, cwd=os.path.dirname(__file__))) |
| 131 | except subprocess.CalledProcessError as e: |
| 132 | defaultdest = os.path.abspath(".") |
| 133 | |
| 134 | parser.add_argument('--destdir', default=defaultdest, help='Where to check out the layers (default is {defaultdest}).'.format(defaultdest=defaultdest)) |
| 135 | parser.add_argument('--jsondata', default=__file__+".json", help='File containing the layer data in json format (default is {defaultjson}).'.format(defaultjson=__file__+".json")) |
| 136 | |
| 137 | args = parser.parse_args() |
| 138 | |
| 139 | with open(args.jsondata) as f: |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 140 | json_f = json.load(f) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 141 | |
| 142 | supported_versions = ["1.0"] |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 143 | if json_f["version"] not in supported_versions: |
| 144 | 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] | 145 | |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 146 | _do_checkout(vars(args), json_f) |