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# This script is idempotent. Subsequent runs only change what is necessary to
15# ensure your layers match your configuration.
16
17import argparse
18import json
19import os
20import subprocess
21
22def _is_repo_git_repo(repodir):
23    try:
24        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
27    except subprocess.CalledProcessError:
28        pass
29    return False
30
31def _is_repo_at_rev(repodir, rev):
32    try:
33        curr_rev = subprocess.check_output("git -C %s rev-parse HEAD" % repodir, 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_repo_at_remote_uri(repodir, remote, uri):
41    try:
42        curr_uri = subprocess.check_output("git -C %s remote get-url %s" % (repodir, 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
49def _contains_submodules(repodir):
50    return os.path.exists(os.path.join(repodir,".gitmodules"))
51
52def _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
63def _do_checkout(args, json):
64    repos = json['sources']
65    repodirs = []
66    oesetupbuild = None
67    for r_name in repos:
68        r_data = repos[r_name]
69        repodir = os.path.abspath(os.path.join(args['destdir'], r_data['path']))
70        repodirs.append(repodir)
71
72        if 'contains_this_file' in r_data.keys():
73            force_arg = 'force_bootstraplayer_checkout'
74            if not args[force_arg]:
75                print('Note: not checking out source {repo}, use {repoflag} to override.'.format(repo=r_name, repoflag='--force-bootstraplayer-checkout'))
76                continue
77        r_remote = r_data['git-remote']
78        rev = r_remote['rev']
79        desc = r_remote['describe']
80        if not desc:
81            desc = rev[:10]
82        branch = r_remote['branch']
83        remotes = r_remote['remotes']
84
85        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)
88            print("Running '{}'".format(cmd))
89            subprocess.check_output(cmd, shell=True)
90
91        for remote in remotes:
92            if not _is_repo_at_remote_uri(repodir, remote, remotes[remote]['uri']):
93                cmd = "git remote remove {} > /dev/null 2>&1; git remote add {} {}".format(remote, remote, remotes[remote]['uri'])
94                print("Running '{}' in {}".format(cmd, repodir))
95                subprocess.check_output(cmd, shell=True, cwd=repodir)
96
97                cmd = "git fetch -q {} || true".format(remote)
98                print("Running '{}' in {}".format(cmd, repodir))
99                subprocess.check_output(cmd, shell=True, cwd=repodir)
100
101        if not _is_repo_at_rev(repodir, rev):
102            cmd = "git fetch -q --all || true"
103            print("Running '{}' in {}".format(cmd, repodir))
104            subprocess.check_output(cmd, shell=True, cwd=repodir)
105
106            cmd = 'git checkout -q {}'.format(rev)
107            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))
112        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))
123
124parser = 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:
140    json_f = json.load(f)
141
142supported_versions = ["1.0"]
143if 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))
145
146_do_checkout(vars(args), json_f)
147