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 | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 22 | def _is_layer_git_repo(layerdir): |
| 23 | git_dir = os.path.join(layerdir, ".git") |
| 24 | if not os.access(git_dir, os.R_OK): |
| 25 | return False |
| 26 | try: |
| 27 | return subprocess.check_output("git -C %s rev-parse --is-inside-git-dir" % git_dir, shell=True, stderr=subprocess.DEVNULL) |
| 28 | except subprocess.CalledProcessError: |
| 29 | return False |
| 30 | |
| 31 | def _is_layer_at_rev(layerdir, rev): |
| 32 | try: |
| 33 | curr_rev = subprocess.check_output("git -C %s rev-parse HEAD" % layerdir, shell=True, stderr=subprocess.DEVNULL) |
| 34 | if curr_rev.strip().decode("utf-8") == rev: |
| 35 | return True |
| 36 | except subprocess.CalledProcessError: |
| 37 | pass |
| 38 | return False |
| 39 | |
| 40 | def _is_layer_at_remote_uri(layerdir, remote, uri): |
| 41 | try: |
| 42 | curr_uri = subprocess.check_output("git -C %s remote get-url %s" % (layerdir, remote), shell=True, stderr=subprocess.DEVNULL) |
| 43 | if curr_uri.strip().decode("utf-8") == uri: |
| 44 | return True |
| 45 | except subprocess.CalledProcessError: |
| 46 | pass |
| 47 | return False |
| 48 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 49 | def _do_checkout(args, json): |
| 50 | layers = json['sources'] |
| 51 | for l_name in layers: |
| 52 | l_data = layers[l_name] |
| 53 | layerdir = os.path.abspath(os.path.join(args['destdir'], l_data['path'])) |
| 54 | |
| 55 | if 'contains_this_file' in l_data.keys(): |
| 56 | force_arg = 'force_bootstraplayer_checkout' |
| 57 | if not args[force_arg]: |
| 58 | print('Note: not checking out source {layer}, use {layerflag} to override.'.format(layer=l_name, layerflag='--force-bootstraplayer-checkout')) |
| 59 | continue |
| 60 | l_remote = l_data['git-remote'] |
| 61 | rev = l_remote['rev'] |
| 62 | desc = l_remote['describe'] |
| 63 | if not desc: |
| 64 | desc = rev[:10] |
| 65 | branch = l_remote['branch'] |
| 66 | remotes = l_remote['remotes'] |
| 67 | |
| 68 | print('\nSetting up source {}, revision {}, branch {}'.format(l_name, desc, branch)) |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 69 | if not _is_layer_git_repo(layerdir): |
| 70 | cmd = 'git init -q {}'.format(layerdir) |
| 71 | print("Running '{}'".format(cmd)) |
| 72 | subprocess.check_output(cmd, shell=True) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 73 | |
| 74 | for remote in remotes: |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 75 | if not _is_layer_at_remote_uri(layerdir, remote, remotes[remote]['uri']): |
| 76 | cmd = "git remote remove {} > /dev/null 2>&1; git remote add {} {}".format(remote, remote, remotes[remote]['uri']) |
| 77 | print("Running '{}' in {}".format(cmd, layerdir)) |
| 78 | subprocess.check_output(cmd, shell=True, cwd=layerdir) |
| 79 | |
| 80 | cmd = "git fetch -q {} || true".format(remote) |
| 81 | print("Running '{}' in {}".format(cmd, layerdir)) |
| 82 | subprocess.check_output(cmd, shell=True, cwd=layerdir) |
| 83 | |
| 84 | if not _is_layer_at_rev(layerdir, rev): |
| 85 | cmd = "git fetch -q --all || true" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 86 | print("Running '{}' in {}".format(cmd, layerdir)) |
| 87 | subprocess.check_output(cmd, shell=True, cwd=layerdir) |
| 88 | |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 89 | cmd = 'git checkout -q {}'.format(rev) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 90 | print("Running '{}' in {}".format(cmd, layerdir)) |
| 91 | subprocess.check_output(cmd, shell=True, cwd=layerdir) |
| 92 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 93 | 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/") |
| 94 | |
| 95 | parser.add_argument('--force-bootstraplayer-checkout', action='store_true', |
| 96 | 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).') |
| 97 | |
| 98 | try: |
| 99 | defaultdest = os.path.dirname(subprocess.check_output('git rev-parse --show-toplevel', universal_newlines=True, shell=True, cwd=os.path.dirname(__file__))) |
| 100 | except subprocess.CalledProcessError as e: |
| 101 | defaultdest = os.path.abspath(".") |
| 102 | |
| 103 | parser.add_argument('--destdir', default=defaultdest, help='Where to check out the layers (default is {defaultdest}).'.format(defaultdest=defaultdest)) |
| 104 | parser.add_argument('--jsondata', default=__file__+".json", help='File containing the layer data in json format (default is {defaultjson}).'.format(defaultjson=__file__+".json")) |
| 105 | |
| 106 | args = parser.parse_args() |
| 107 | |
| 108 | with open(args.jsondata) as f: |
| 109 | json = json.load(f) |
| 110 | |
| 111 | supported_versions = ["1.0"] |
| 112 | if json["version"] not in supported_versions: |
| 113 | raise Exception("File {} has version {}, which is not in supported versions: {}".format(args.jsondata, json["version"], supported_versions)) |
| 114 | |
| 115 | _do_checkout(vars(args), json) |