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. |
| 13 | |
| 14 | import argparse |
| 15 | import json |
| 16 | import os |
| 17 | import subprocess |
| 18 | |
| 19 | def _do_checkout(args, json): |
| 20 | layers = json['sources'] |
| 21 | for l_name in layers: |
| 22 | l_data = layers[l_name] |
| 23 | layerdir = os.path.abspath(os.path.join(args['destdir'], l_data['path'])) |
| 24 | |
| 25 | if 'contains_this_file' in l_data.keys(): |
| 26 | force_arg = 'force_bootstraplayer_checkout' |
| 27 | if not args[force_arg]: |
| 28 | print('Note: not checking out source {layer}, use {layerflag} to override.'.format(layer=l_name, layerflag='--force-bootstraplayer-checkout')) |
| 29 | continue |
| 30 | l_remote = l_data['git-remote'] |
| 31 | rev = l_remote['rev'] |
| 32 | desc = l_remote['describe'] |
| 33 | if not desc: |
| 34 | desc = rev[:10] |
| 35 | branch = l_remote['branch'] |
| 36 | remotes = l_remote['remotes'] |
| 37 | |
| 38 | print('\nSetting up source {}, revision {}, branch {}'.format(l_name, desc, branch)) |
| 39 | cmd = 'git init -q {}'.format(layerdir) |
| 40 | print("Running '{}'".format(cmd)) |
| 41 | subprocess.check_output(cmd, shell=True) |
| 42 | |
| 43 | for remote in remotes: |
| 44 | cmd = "git remote remove {} > /dev/null 2>&1; git remote add {} {}".format(remote, remote, remotes[remote]['uri']) |
| 45 | print("Running '{}' in {}".format(cmd, layerdir)) |
| 46 | subprocess.check_output(cmd, shell=True, cwd=layerdir) |
| 47 | |
| 48 | cmd = "git fetch -q {} || true".format(remote) |
| 49 | print("Running '{}' in {}".format(cmd, layerdir)) |
| 50 | subprocess.check_output(cmd, shell=True, cwd=layerdir) |
| 51 | |
| 52 | cmd = 'git checkout -q {}'.format(rev) |
| 53 | print("Running '{}' in {}".format(cmd, layerdir)) |
| 54 | subprocess.check_output(cmd, shell=True, cwd=layerdir) |
| 55 | |
| 56 | 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/") |
| 57 | |
| 58 | parser.add_argument('--force-bootstraplayer-checkout', action='store_true', |
| 59 | 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).') |
| 60 | |
| 61 | try: |
| 62 | defaultdest = os.path.dirname(subprocess.check_output('git rev-parse --show-toplevel', universal_newlines=True, shell=True, cwd=os.path.dirname(__file__))) |
| 63 | except subprocess.CalledProcessError as e: |
| 64 | defaultdest = os.path.abspath(".") |
| 65 | |
| 66 | parser.add_argument('--destdir', default=defaultdest, help='Where to check out the layers (default is {defaultdest}).'.format(defaultdest=defaultdest)) |
| 67 | parser.add_argument('--jsondata', default=__file__+".json", help='File containing the layer data in json format (default is {defaultjson}).'.format(defaultjson=__file__+".json")) |
| 68 | |
| 69 | args = parser.parse_args() |
| 70 | |
| 71 | with open(args.jsondata) as f: |
| 72 | json = json.load(f) |
| 73 | |
| 74 | supported_versions = ["1.0"] |
| 75 | if json["version"] not in supported_versions: |
| 76 | raise Exception("File {} has version {}, which is not in supported versions: {}".format(args.jsondata, json["version"], supported_versions)) |
| 77 | |
| 78 | _do_checkout(vars(args), json) |