Skip to content

Commit 1c2d5f6

Browse files
authored
Create gen_tools_json.py
1 parent 7bb55d2 commit 1c2d5f6

File tree

1 file changed

+234
-0
lines changed

1 file changed

+234
-0
lines changed

tools/gen_tools_json.py

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
#!/usr/bin/env python
2+
# python tools/gen_tools_json.py -i $IDF_PATH -j components/arduino/package/package_esp32_index.template.json -o out/
3+
4+
from __future__ import print_function
5+
6+
__author__ = "Hristo Gochkov"
7+
__version__ = "2023"
8+
9+
import os
10+
import shutil
11+
import errno
12+
import os.path
13+
import json
14+
import platform
15+
import sys
16+
import stat
17+
import argparse
18+
import re
19+
import requests
20+
21+
if sys.version_info[0] == 3:
22+
unicode = lambda s: str(s)
23+
24+
release_manifests = []
25+
26+
def replace_if_xz(system):
27+
if not system['url'].endswith(".tar.xz"):
28+
return system
29+
30+
new_url = system['url'].replace(".tar.xz", ".tar.gz")
31+
new_name = system['archiveFileName'].replace(".tar.xz", ".tar.gz")
32+
new_checksum = ""
33+
new_size = 0
34+
35+
# let's get the checksum file from the release
36+
release_manifest_url = ""
37+
# parse the download url to extract all info needed for the checksum file url
38+
urlx = re.findall("^https://github.com/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/releases/download/([a-zA-Z0-9_\-.]+)/([a-zA-Z0-9_\-.]+)$", new_url)
39+
if urlx and len(urlx) > 0:
40+
(owner, proj, version, filename) = urlx[0]
41+
release_manifest_url = "https://github.com/%s/%s/releases/download/%s/%s-%s-checksum.sha256" % (owner, proj, version, proj, version)
42+
else:
43+
print("No manifest match")
44+
return system
45+
46+
# check if we have already downloaded and parsed that manifest
47+
manifest_index = 0
48+
manifest_found = False
49+
for manifest in release_manifests:
50+
if manifest['url'] == release_manifest_url:
51+
manifest_found = True
52+
break
53+
manifest_index = manifest_index + 1
54+
55+
# download and parse manifest
56+
if not manifest_found:
57+
manifest = {
58+
"url": release_manifest_url,
59+
"files": []
60+
}
61+
release_manifest_contents = requests.get(release_manifest_url).text
62+
x = re.findall("\s([a-zA-Z0-9_\-.]+):\s([0-9]+)\s[a-z]+\\n([a-f0-9]+)\s\*.*", release_manifest_contents)
63+
if x and len(x) > 0:
64+
for line in x:
65+
(filename, size, checksum) = line
66+
file = {
67+
"name":filename,
68+
"checksum":checksum,
69+
"size":size
70+
}
71+
manifest["files"].append(file)
72+
else:
73+
print("No line match")
74+
return system
75+
76+
release_manifests.append(manifest)
77+
78+
# find the new file in the list and get it's size and checksum
79+
for file in release_manifests[manifest_index]['files']:
80+
if file['name'] == new_name:
81+
system['url'] = new_url
82+
system['archiveFileName'] = new_name
83+
system["checksum"] = "SHA-256:"+file['checksum']
84+
system["size"] = file['size']
85+
break
86+
return system
87+
88+
if __name__ == '__main__':
89+
90+
parser = argparse.ArgumentParser(
91+
prog = 'gen_tools_json',
92+
description = 'Update Arduino package index with the tolls found in ESP-IDF')
93+
parser.add_argument('-i', '--esp-idf', dest='idf_path', required=True, help='Path to ESP-IDF')
94+
parser.add_argument('-j', '--pkg-json', dest='arduino_json', required=False, help='path to Arduino package json')
95+
parser.add_argument('-o', '--out-path', dest='out_path', required=True, help='Output path to store the update package json')
96+
args = parser.parse_args()
97+
98+
simple_output = False
99+
if args.arduino_json == None:
100+
print('Source was not selected')
101+
simple_output = True
102+
else:
103+
print('Source {0}.'.format(args.arduino_json))
104+
105+
idf_path = args.idf_path;
106+
arduino_json = args.arduino_json;
107+
out_path = args.out_path;
108+
109+
# settings
110+
arduino_tools = ["xtensa-esp32-elf","xtensa-esp32s2-elf","xtensa-esp32s3-elf","xtensa-esp-elf-gdb","riscv32-esp-elf","riscv32-esp-elf-gdb","openocd-esp32"]
111+
112+
# code start
113+
farray = {"packages":[{"platforms":[{"toolsDependencies":[]}],"tools":[]}]}
114+
if simple_output == False:
115+
farray = json.load(open(arduino_json))
116+
117+
idf_tools = json.load(open(idf_path + '/tools/tools.json'))
118+
for tool in idf_tools['tools']:
119+
try:
120+
tool_index = arduino_tools.index(tool['name'])
121+
except:
122+
continue
123+
tool_name = tool['name']
124+
tool_version = tool['versions'][0]['name']
125+
if tool_name.endswith('-elf'):
126+
tool_name += '-gcc'
127+
print('Found {0}, version: {1}'.format(tool_name, tool_version))
128+
129+
if simple_output == False:
130+
dep_found = False
131+
dep_skip = False
132+
for dep in farray['packages'][0]['platforms'][0]['toolsDependencies']:
133+
if dep['name'] == tool_name:
134+
if dep['version'] == tool_version:
135+
print('Skipping {0}. Same version {1}'.format(tool_name, tool_version))
136+
dep_skip = True
137+
break
138+
print('Updating dependency version of {0} from {1} to {2}'.format(tool_name, dep['version'], tool_version))
139+
dep['version'] = tool_version
140+
dep_found = True
141+
if dep_skip == True:
142+
continue
143+
if dep_found == False:
144+
print('Adding new dependency: {0} version {1}'.format(tool_name, tool_version))
145+
deps = {
146+
"packager": "esp32",
147+
"name": tool_name,
148+
"version": tool_version
149+
}
150+
farray['packages'][0]['platforms'][0]['toolsDependencies'].append(deps)
151+
else:
152+
print('Adding dependency: {0} version {1}'.format(tool_name, tool_version))
153+
deps = {
154+
"packager": "esp32",
155+
"name": tool_name,
156+
"version": tool_version
157+
}
158+
farray['packages'][0]['platforms'][0]['toolsDependencies'].append(deps)
159+
160+
systems = []
161+
for arch in tool['versions'][0]:
162+
if arch == 'name' or arch == 'status':
163+
continue
164+
tool_data = tool['versions'][0][arch]
165+
166+
system = {
167+
"host": '',
168+
"url": tool_data['url'],
169+
"archiveFileName": os.path.basename(tool_data['url']),
170+
"checksum": "SHA-256:"+tool_data['sha256'],
171+
"size": str(tool_data['size'])
172+
}
173+
174+
if arch == "win32":
175+
system["host"] = "i686-mingw32";
176+
elif arch == "win64":
177+
system["host"] = "x86_64-mingw32";
178+
elif arch == "macos-arm64":
179+
system["host"] = "arm64-apple-darwin";
180+
elif arch == "macos":
181+
system["host"] = "x86_64-apple-darwin";
182+
elif arch == "linux-amd64":
183+
system["host"] = "x86_64-pc-linux-gnu";
184+
elif arch == "linux-i686":
185+
system["host"] = "i686-pc-linux-gnu";
186+
elif arch == "linux-arm64":
187+
system["host"] = "aarch64-linux-gnu";
188+
elif arch == "linux-armel":
189+
system["host"] = "arm-linux-gnueabihf";
190+
elif arch == "linux-armhf":
191+
# system["host"] = "arm-linux-gnueabihf";
192+
continue
193+
else :
194+
continue
195+
196+
system = replace_if_xz(system)
197+
198+
systems.append(system)
199+
200+
if simple_output == False:
201+
tool_found = False
202+
for t in farray['packages'][0]['tools']:
203+
if t['name'] == tool_name:
204+
t['version'] = tool_version
205+
t['systems'] = systems
206+
tool_found = True
207+
print('Updating binaries of {0} to version {1}'.format(tool_name, tool_version))
208+
if tool_found == False:
209+
print('Adding new tool: {0} version {1}'.format(tool_name, tool_version))
210+
tools = {
211+
"name": tool_name,
212+
"version": tool_version,
213+
"systems": systems
214+
}
215+
farray['packages'][0]['tools'].append(tools)
216+
else:
217+
print('Adding tool: {0} version {1}'.format(tool_name, tool_version))
218+
tools = {
219+
"name": tool_name,
220+
"version": tool_version,
221+
"systems": systems
222+
}
223+
farray['packages'][0]['tools'].append(tools)
224+
225+
json_str = json.dumps(farray, indent=2)
226+
out_file = out_path + "tools.json"
227+
if simple_output == False:
228+
out_file = out_path + os.path.basename(arduino_json)
229+
230+
with open(out_file, "w") as f:
231+
f.write(json_str+"\n")
232+
f.close()
233+
# print(json_str)
234+
print('{0} generated'.format(out_file))

0 commit comments

Comments
 (0)