2019-07-18 18:10:38 +00:00
|
|
|
import argparse
|
2020-01-02 23:34:48 +00:00
|
|
|
import configparser
|
2019-07-18 18:10:38 +00:00
|
|
|
import json
|
2020-01-02 23:34:48 +00:00
|
|
|
import re
|
2022-04-26 16:35:16 +00:00
|
|
|
from pathlib import Path
|
2020-01-02 23:34:48 +00:00
|
|
|
|
|
|
|
|
import pygit2
|
|
|
|
|
|
2019-07-18 18:10:38 +00:00
|
|
|
|
|
|
|
|
def format_component(submod):
|
2020-01-02 23:34:48 +00:00
|
|
|
return {"component": {"type": "git", "git": {"commitHash": str(submod.head_id), "repositoryUrl": submod.url}}}
|
|
|
|
|
|
2020-05-14 21:15:06 +00:00
|
|
|
|
2020-01-02 23:34:48 +00:00
|
|
|
def lookup_submodule(repo, submodule_path):
|
|
|
|
|
submodule = repo.lookup_submodule(submodule_path)
|
|
|
|
|
try:
|
|
|
|
|
# Some submodules have names which don't correspond to the actual path in the repo
|
|
|
|
|
# (e.g. 'git submodule init' was called with the --name option, or the submodule
|
|
|
|
|
# was moved and the old name was kept). listall_submodules() returns submodule paths,
|
|
|
|
|
# but pygit up to 1.0.1 requires the submodule name (not the path) in lookup_submodule
|
|
|
|
|
# to be able to access the URL and other properties.
|
|
|
|
|
# This seems to be a bug in pygit2, since its documentation says the submodules can
|
|
|
|
|
# be opened by path.
|
|
|
|
|
# If accessing the URL throws a RuntimeError, we get the submodule name manually from
|
|
|
|
|
# .gitmodules.
|
2023-04-17 17:11:44 +00:00
|
|
|
_ = submodule.url
|
2020-01-02 23:34:48 +00:00
|
|
|
return submodule
|
|
|
|
|
except RuntimeError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
config = configparser.ConfigParser()
|
2022-04-26 16:35:16 +00:00
|
|
|
config.read(Path(repo.workdir, ".gitmodules"))
|
2020-01-02 23:34:48 +00:00
|
|
|
for section in config.sections():
|
2022-04-26 16:35:16 +00:00
|
|
|
if config[section]["path"] == submodule_path:
|
2020-01-02 23:34:48 +00:00
|
|
|
name = re.fullmatch('submodule "(.*)"', section).group(1)
|
|
|
|
|
submodule = repo.lookup_submodule(name)
|
|
|
|
|
return submodule
|
|
|
|
|
raise NotImplementedError() # should not be reached
|
2019-07-18 18:10:38 +00:00
|
|
|
|
2020-05-14 21:15:06 +00:00
|
|
|
|
2019-07-18 18:10:38 +00:00
|
|
|
def process_component(repo):
|
2020-01-02 23:34:48 +00:00
|
|
|
return [lookup_submodule(repo, submod) for submod in repo.listall_submodules()]
|
2019-07-18 18:10:38 +00:00
|
|
|
|
2020-05-14 21:15:06 +00:00
|
|
|
|
2019-07-18 18:10:38 +00:00
|
|
|
def recursive_process(base_repo):
|
|
|
|
|
processed_subs = []
|
|
|
|
|
repos_to_process = [base_repo]
|
|
|
|
|
while repos_to_process:
|
|
|
|
|
repo = repos_to_process.pop()
|
|
|
|
|
submodules = process_component(repo)
|
|
|
|
|
processed_subs.extend(submodules)
|
|
|
|
|
repos_to_process.extend([mod.open() for mod in submodules])
|
2020-05-14 21:15:06 +00:00
|
|
|
return {"Registrations": [format_component(component) for component in processed_subs]}
|
|
|
|
|
|
2019-07-18 18:10:38 +00:00
|
|
|
|
|
|
|
|
def main(repo_path, output_file):
|
|
|
|
|
repo = pygit2.Repository(repo_path)
|
|
|
|
|
registrations = recursive_process(repo)
|
2022-04-26 16:35:16 +00:00
|
|
|
with open(output_file, "w") as f:
|
2019-07-18 18:10:38 +00:00
|
|
|
json.dump(registrations, f, indent=4, sort_keys=True)
|
|
|
|
|
|
2020-05-14 21:15:06 +00:00
|
|
|
|
2020-01-02 23:34:48 +00:00
|
|
|
if __name__ == "__main__":
|
2019-07-18 18:10:38 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
parser.add_argument("base_repository", help="path to base repository to get registrations for.")
|
|
|
|
|
parser.add_argument("-o", "--output", help="output file name.", default="cgmanifest.json")
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
main(args.base_repository, args.output)
|