-
Notifications
You must be signed in to change notification settings - Fork 3k
/
generate_cgmanifest.py
159 lines (133 loc) · 4.82 KB
/
generate_cgmanifest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
import argparse
import csv
import json
import os
import re
import subprocess
import sys
from dataclasses import dataclass
from pathlib import PurePosixPath
from urllib.parse import urlparse
import requests
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("--username", required=True, help="Github username")
parser.add_argument("--token", required=True, help="Github access token")
return parser.parse_args()
args = parse_arguments()
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
REPO_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, ".."))
package_name = None
package_filename = None
package_url = None
registrations = []
@dataclass(frozen=True)
class GitDep:
commit: str
url: str
git_deps = {}
def add_github_dep(name, parsed_url):
segments = parsed_url.path.split("/")
org_name = segments[1]
repo_name = segments[2]
if segments[3] != "archive":
print("unrecognized github url path:" + parsed_url.path)
return
git_repo_url = f"https://github.com/{org_name}/{repo_name}.git"
# For example, the path might be like '/myorg/myrepo/archive/5a5f8a5935762397aa68429b5493084ff970f774.zip'
# The last segment, segments[4], is '5a5f8a5935762397aa68429b5493084ff970f774.zip'
if len(segments) == 5 and re.match(r"[0-9a-f]{40}", PurePosixPath(segments[4]).stem):
commit = PurePosixPath(segments[4]).stem
dep = GitDep(commit, git_repo_url)
if dep not in git_deps:
git_deps[dep] = name
else:
# TODO: support urls like: https://github.com/onnx/onnx-tensorrt/archive/refs/tags/release/7.1.zip
if len(segments) == 5:
tag = PurePosixPath(segments[4]).stem
if tag.endswith(".tar"):
tag = PurePosixPath(tag).stem
elif segments[4] == "refs" and segments[5] == "tags":
tag = PurePosixPath(segments[6]).stem
if tag.endswith(".tar"):
tag = PurePosixPath(tag).stem
else:
print("unrecognized github url path:" + parsed_url.path)
return
# Make a REST call to convert to tag to a git commit
url = f"https://api.github.com/repos/{org_name}/{repo_name}/git/refs/tags/{tag}"
print(f"requesting {url} ...")
res = requests.get(url, auth=(args.username, args.token))
response_json = res.json()
tag_object = response_json["object"]
if tag_object["type"] == "commit":
commit = tag_object["sha"]
elif tag_object["type"] == "tag":
res = requests.get(tag_object["url"], auth=(args.username, args.token))
commit = res.json()["object"]["sha"]
else:
print("unrecognized github url path:" + parsed_url.path)
return
dep = GitDep(commit, git_repo_url)
if dep not in git_deps:
git_deps[dep] = name
def normalize_path_separators(path):
return path.replace(os.path.sep, "/")
proc = subprocess.run(
[
"git",
"submodule",
"foreach",
"--quiet",
"'{}' '{}' $toplevel/$sm_path".format(
normalize_path_separators(sys.executable),
normalize_path_separators(os.path.join(SCRIPT_DIR, "print_submodule_info.py")),
),
],
check=True,
cwd=REPO_DIR,
capture_output=True,
text=True,
)
submodule_lines = proc.stdout.splitlines()
for submodule_line in submodule_lines:
(absolute_path, url, commit) = submodule_line.split(" ")
git_deps[GitDep(commit, url)] = (
f"git submodule at {normalize_path_separators(os.path.relpath(absolute_path, REPO_DIR))}"
)
with open(os.path.join(SCRIPT_DIR, "..", "cmake", "deps.txt")) as f:
depfile_reader = csv.reader(f, delimiter=";")
for row in depfile_reader:
if len(row) < 3:
continue
name = row[0]
# Lines start with "#" are comments
if name.startswith("#"):
continue
url = row[1]
parsed_url = urlparse(url)
# TODO: add support for gitlab
if parsed_url.hostname == "github.com":
add_github_dep(name, parsed_url)
else:
print("unrecognized url:" + url)
for git_dep, comment in git_deps.items():
registration = {
"component": {
"type": "git",
"git": {
"commitHash": git_dep.commit,
"repositoryUrl": git_dep.url,
},
"comments": comment,
}
}
registrations.append(registration)
cgmanifest = {
"$schema": "https://json.schemastore.org/component-detection-manifest.json",
"Version": 1,
"Registrations": registrations,
}
with open(os.path.join(SCRIPT_DIR, "generated", "cgmanifest.json"), mode="w") as generated_cgmanifest_file:
print(json.dumps(cgmanifest, indent=2), file=generated_cgmanifest_file)