blob: d0bc9f1667464cf94c826a16c81a9c8442f370bb [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#!/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 Geisslerc5535c92023-01-27 16:10:19 -060013#
14# This script is idempotent. Subsequent runs only change what is necessary to
15# ensure your layers match your configuration.
Patrick Williams92b42cb2022-09-03 06:53:57 -050016
17import argparse
18import json
19import os
20import subprocess
21
Andrew Geisslerc5535c92023-01-27 16:10:19 -060022def _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
31def _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
40def _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 Williams92b42cb2022-09-03 06:53:57 -050049def _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 Geisslerc5535c92023-01-27 16:10:19 -060069 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 Williams92b42cb2022-09-03 06:53:57 -050073
74 for remote in remotes:
Andrew Geisslerc5535c92023-01-27 16:10:19 -060075 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 Williams92b42cb2022-09-03 06:53:57 -050086 print("Running '{}' in {}".format(cmd, layerdir))
87 subprocess.check_output(cmd, shell=True, cwd=layerdir)
88
Andrew Geisslerc5535c92023-01-27 16:10:19 -060089 cmd = 'git checkout -q {}'.format(rev)
Patrick Williams92b42cb2022-09-03 06:53:57 -050090 print("Running '{}' in {}".format(cmd, layerdir))
91 subprocess.check_output(cmd, shell=True, cwd=layerdir)
92
Patrick Williams92b42cb2022-09-03 06:53:57 -050093parser = 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
95parser.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
98try:
99 defaultdest = os.path.dirname(subprocess.check_output('git rev-parse --show-toplevel', universal_newlines=True, shell=True, cwd=os.path.dirname(__file__)))
100except subprocess.CalledProcessError as e:
101 defaultdest = os.path.abspath(".")
102
103parser.add_argument('--destdir', default=defaultdest, help='Where to check out the layers (default is {defaultdest}).'.format(defaultdest=defaultdest))
104parser.add_argument('--jsondata', default=__file__+".json", help='File containing the layer data in json format (default is {defaultjson}).'.format(defaultjson=__file__+".json"))
105
106args = parser.parse_args()
107
108with open(args.jsondata) as f:
109 json = json.load(f)
110
111supported_versions = ["1.0"]
112if 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)