blob: 6fbfefd656f99112237ea9e434cd23ee7e3c3fea [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 Geissler6aa7eec2023-03-03 12:41:14 -060022def _is_repo_git_repo(repodir):
Andrew Geisslerc5535c92023-01-27 16:10:19 -060023 try:
Andrew Geissler20137392023-10-12 04:59:14 -060024 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 Geisslerc5535c92023-01-27 16:10:19 -060027 except subprocess.CalledProcessError:
Andrew Geissler20137392023-10-12 04:59:14 -060028 pass
29 return False
Andrew Geisslerc5535c92023-01-27 16:10:19 -060030
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060031def _is_repo_at_rev(repodir, rev):
Andrew Geisslerc5535c92023-01-27 16:10:19 -060032 try:
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060033 curr_rev = subprocess.check_output("git -C %s rev-parse HEAD" % repodir, shell=True, stderr=subprocess.DEVNULL)
Andrew Geisslerc5535c92023-01-27 16:10:19 -060034 if curr_rev.strip().decode("utf-8") == rev:
35 return True
36 except subprocess.CalledProcessError:
37 pass
38 return False
39
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060040def _is_repo_at_remote_uri(repodir, remote, uri):
Andrew Geisslerc5535c92023-01-27 16:10:19 -060041 try:
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060042 curr_uri = subprocess.check_output("git -C %s remote get-url %s" % (repodir, remote), shell=True, stderr=subprocess.DEVNULL)
Andrew Geisslerc5535c92023-01-27 16:10:19 -060043 if curr_uri.strip().decode("utf-8") == uri:
44 return True
45 except subprocess.CalledProcessError:
46 pass
47 return False
48
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060049def _contains_submodules(repodir):
50 return os.path.exists(os.path.join(repodir,".gitmodules"))
Patrick Williams92b42cb2022-09-03 06:53:57 -050051
Patrick Williams73bd93f2024-02-20 08:07:48 -060052def _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 Geissler6aa7eec2023-03-03 12:41:14 -060063def _do_checkout(args, json):
64 repos = json['sources']
Patrick Williams73bd93f2024-02-20 08:07:48 -060065 repodirs = []
66 oesetupbuild = None
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060067 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 Williams73bd93f2024-02-20 08:07:48 -060070 repodirs.append(repodir)
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060071
72 if 'contains_this_file' in r_data.keys():
Patrick Williams92b42cb2022-09-03 06:53:57 -050073 force_arg = 'force_bootstraplayer_checkout'
74 if not args[force_arg]:
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060075 print('Note: not checking out source {repo}, use {repoflag} to override.'.format(repo=r_name, repoflag='--force-bootstraplayer-checkout'))
Patrick Williams92b42cb2022-09-03 06:53:57 -050076 continue
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060077 r_remote = r_data['git-remote']
78 rev = r_remote['rev']
79 desc = r_remote['describe']
Patrick Williams92b42cb2022-09-03 06:53:57 -050080 if not desc:
81 desc = rev[:10]
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060082 branch = r_remote['branch']
83 remotes = r_remote['remotes']
Patrick Williams92b42cb2022-09-03 06:53:57 -050084
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060085 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 Geisslerc5535c92023-01-27 16:10:19 -060088 print("Running '{}'".format(cmd))
89 subprocess.check_output(cmd, shell=True)
Patrick Williams92b42cb2022-09-03 06:53:57 -050090
91 for remote in remotes:
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060092 if not _is_repo_at_remote_uri(repodir, remote, remotes[remote]['uri']):
Andrew Geisslerc5535c92023-01-27 16:10:19 -060093 cmd = "git remote remove {} > /dev/null 2>&1; git remote add {} {}".format(remote, remote, remotes[remote]['uri'])
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060094 print("Running '{}' in {}".format(cmd, repodir))
95 subprocess.check_output(cmd, shell=True, cwd=repodir)
Andrew Geisslerc5535c92023-01-27 16:10:19 -060096
97 cmd = "git fetch -q {} || true".format(remote)
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060098 print("Running '{}' in {}".format(cmd, repodir))
99 subprocess.check_output(cmd, shell=True, cwd=repodir)
Andrew Geisslerc5535c92023-01-27 16:10:19 -0600100
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600101 if not _is_repo_at_rev(repodir, rev):
Andrew Geisslerc5535c92023-01-27 16:10:19 -0600102 cmd = "git fetch -q --all || true"
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600103 print("Running '{}' in {}".format(cmd, repodir))
104 subprocess.check_output(cmd, shell=True, cwd=repodir)
Patrick Williams92b42cb2022-09-03 06:53:57 -0500105
Andrew Geisslerc5535c92023-01-27 16:10:19 -0600106 cmd = 'git checkout -q {}'.format(rev)
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600107 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 Williams73bd93f2024-02-20 08:07:48 -0600112 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 Williams92b42cb2022-09-03 06:53:57 -0500123
Patrick Williams92b42cb2022-09-03 06:53:57 -0500124parser = 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
126parser.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
129try:
130 defaultdest = os.path.dirname(subprocess.check_output('git rev-parse --show-toplevel', universal_newlines=True, shell=True, cwd=os.path.dirname(__file__)))
131except subprocess.CalledProcessError as e:
132 defaultdest = os.path.abspath(".")
133
134parser.add_argument('--destdir', default=defaultdest, help='Where to check out the layers (default is {defaultdest}).'.format(defaultdest=defaultdest))
135parser.add_argument('--jsondata', default=__file__+".json", help='File containing the layer data in json format (default is {defaultjson}).'.format(defaultjson=__file__+".json"))
136
137args = parser.parse_args()
138
139with open(args.jsondata) as f:
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600140 json_f = json.load(f)
Patrick Williams92b42cb2022-09-03 06:53:57 -0500141
142supported_versions = ["1.0"]
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600143if 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 Williams92b42cb2022-09-03 06:53:57 -0500145
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600146_do_checkout(vars(args), json_f)