blob: c8012fa6705eb13597f30bca9f9f42d121bbf8fa [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):
23 git_dir = os.path.join(repodir, ".git")
Andrew Geisslerc5535c92023-01-27 16:10:19 -060024 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
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
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060052def _do_checkout(args, json):
53 repos = json['sources']
54 for r_name in repos:
55 r_data = repos[r_name]
56 repodir = os.path.abspath(os.path.join(args['destdir'], r_data['path']))
57
58 if 'contains_this_file' in r_data.keys():
Patrick Williams92b42cb2022-09-03 06:53:57 -050059 force_arg = 'force_bootstraplayer_checkout'
60 if not args[force_arg]:
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060061 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 -050062 continue
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060063 r_remote = r_data['git-remote']
64 rev = r_remote['rev']
65 desc = r_remote['describe']
Patrick Williams92b42cb2022-09-03 06:53:57 -050066 if not desc:
67 desc = rev[:10]
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060068 branch = r_remote['branch']
69 remotes = r_remote['remotes']
Patrick Williams92b42cb2022-09-03 06:53:57 -050070
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060071 print('\nSetting up source {}, revision {}, branch {}'.format(r_name, desc, branch))
72 if not _is_repo_git_repo(repodir):
73 cmd = 'git init -q {}'.format(repodir)
Andrew Geisslerc5535c92023-01-27 16:10:19 -060074 print("Running '{}'".format(cmd))
75 subprocess.check_output(cmd, shell=True)
Patrick Williams92b42cb2022-09-03 06:53:57 -050076
77 for remote in remotes:
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060078 if not _is_repo_at_remote_uri(repodir, remote, remotes[remote]['uri']):
Andrew Geisslerc5535c92023-01-27 16:10:19 -060079 cmd = "git remote remove {} > /dev/null 2>&1; git remote add {} {}".format(remote, remote, remotes[remote]['uri'])
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060080 print("Running '{}' in {}".format(cmd, repodir))
81 subprocess.check_output(cmd, shell=True, cwd=repodir)
Andrew Geisslerc5535c92023-01-27 16:10:19 -060082
83 cmd = "git fetch -q {} || true".format(remote)
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060084 print("Running '{}' in {}".format(cmd, repodir))
85 subprocess.check_output(cmd, shell=True, cwd=repodir)
Andrew Geisslerc5535c92023-01-27 16:10:19 -060086
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060087 if not _is_repo_at_rev(repodir, rev):
Andrew Geisslerc5535c92023-01-27 16:10:19 -060088 cmd = "git fetch -q --all || true"
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060089 print("Running '{}' in {}".format(cmd, repodir))
90 subprocess.check_output(cmd, shell=True, cwd=repodir)
Patrick Williams92b42cb2022-09-03 06:53:57 -050091
Andrew Geisslerc5535c92023-01-27 16:10:19 -060092 cmd = 'git checkout -q {}'.format(rev)
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060093 print("Running '{}' in {}".format(cmd, repodir))
94 subprocess.check_output(cmd, shell=True, cwd=repodir)
95
96 if _contains_submodules(repodir):
97 print("Repo {} contains submodules, use 'git submodule update' to ensure they are up to date".format(repodir))
Patrick Williams92b42cb2022-09-03 06:53:57 -050098
Patrick Williams92b42cb2022-09-03 06:53:57 -050099parser = 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/")
100
101parser.add_argument('--force-bootstraplayer-checkout', action='store_true',
102 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).')
103
104try:
105 defaultdest = os.path.dirname(subprocess.check_output('git rev-parse --show-toplevel', universal_newlines=True, shell=True, cwd=os.path.dirname(__file__)))
106except subprocess.CalledProcessError as e:
107 defaultdest = os.path.abspath(".")
108
109parser.add_argument('--destdir', default=defaultdest, help='Where to check out the layers (default is {defaultdest}).'.format(defaultdest=defaultdest))
110parser.add_argument('--jsondata', default=__file__+".json", help='File containing the layer data in json format (default is {defaultjson}).'.format(defaultjson=__file__+".json"))
111
112args = parser.parse_args()
113
114with open(args.jsondata) as f:
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600115 json_f = json.load(f)
Patrick Williams92b42cb2022-09-03 06:53:57 -0500116
117supported_versions = ["1.0"]
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600118if json_f["version"] not in supported_versions:
119 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 -0500120
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600121_do_checkout(vars(args), json_f)