This repository has been archived on 2023-02-07. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-package-manager-addon/tools/generate-json.py

230 lines
8.8 KiB
Python

# ====================== BEGIN GPL LICENSE BLOCK ======================
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ======================= END GPL LICENSE BLOCK ========================
#
# This script is a prototype which generates a JSON file
# for use in the package manager add-on.
import os
import shlex
addons_directory = 'C:\\Users\\Peter\\Documents\\blender-git\\blender\\release\\scripts\\addons\\'
contrib_directory = 'C:\\Users\\Peter\\Documents\\blender-git\\blender\\release\\scripts\\addons_contrib\\'
index_directory = 'C:\\Users\\Peter\\Documents\\blender-package-manager-addon\\addons\\'
# This script is functional for now, but rather "hacky" and much of it
# will be replaced by a modified version of the code which Blender uses
# to parse add-on's bl_info dictionary.
def list_addons (dir, addon=""):
json_text = ""
for item in os.scandir(dir):
if item.name[0] == '.':
continue
if '.' not in item.name:
if addon not in item.name and addon != "":
continue
if item.is_dir() and (item.name == addon or addon == ""):
filename = os.path.join(item.path, '__init__.py')
if item.is_file() and '.py' in item and (item.name[:-3] == addon or addon == ""):
filename = item.path
try:
file_lines = open(filename, mode='r').readlines()
except FileNotFoundError:
continue
new_lines = []
for line in file_lines:
if not line.startswith('#'):
#line = line[:line.find('#')] FIXME did not take into
# account '#' symbols inside quotes.
if line.__len__() > 0:
new_lines.append(line)
file_text = ''.join(new_lines)
bl_info_at = file_text.find('bl_info')
file_text = file_text[bl_info_at:]
open_bracket_at = file_text.find('{')
offset = 0
quote_type = ""
in_quote = False
current_quote = "key"
escape_next = False
bl_info = {}
key = ""
value = ""
tuple_item = 0
for char in file_text[open_bracket_at+1:]:
if char == '\\':
escape_next = not escape_next
was_in_quote = in_quote
if (not escape_next and (char == "'" or char == '"') and (not in_quote or (in_quote and char == quote_type))):
in_quote = not in_quote
quote_type = char
if in_quote and quote_type == ')' and (char == "'" or char == '"'):
quote_type = char
value = ""
if not in_quote and char == '(':
in_quote = True
value = ()
quote_type = ')'
elif in_quote and char == quote_type and quote_type == ')':
in_quote = False
elif in_quote and char != quote_type:
if quote_type != ')':
if current_quote == "key":
key += char
else:
value += char
else:
if char == ',':
value = value + (tuple_item, )
tuple_item = 0
elif char.isdigit():
tuple_item = (tuple_item * 10) + int(char)
if not in_quote:
if char == ',' or char == '}':
current_quote = "key"
if key != "":
if value != "" or value != None:
bl_info[key] = value
else:
bl_info[key] = ""
key = ""
value = ""
tuple_item = 0
elif was_in_quote:
if current_quote == "value" and quote_type == ')':
current_quote = "key"
value = value + (tuple_item, )
bl_info[key] = value
key = ""
value = ""
tuple_item = 0
else:
current_quote = "value"
offset += 1
if not in_quote and char == '}':
break
#print(file_text[:offset+open_bracket_at+1])
#for k, v in bl_info.items():
# print('"' + k + '": ' + repr(v))
if item.is_dir():
json_text += bl_info_to_json(bl_info, item.name)
else:
json_text += bl_info_to_json(bl_info, item.name, is_zip = False)
return json_text
def bl_info_to_json (bl_info, addon_id, source = 'internal', is_zip = True):
required_keys = ['name', 'blender']
recommended_keys = ['author', 'description', 'location', 'wiki_url', 'category']
for key in required_keys:
if key not in bl_info:
print("Error: missing key \"" + key + "\" in add-on " + addon_id
+ "'s bl_info, or bl_info dict may be malformed")
return ""
for key in recommended_keys:
if key not in bl_info:
print("Warning: missing key \"" + key + "\" in add-on " + addon_id
+ "'s bl_info, or bl_info dict may be malformed")
author = ""
if 'author' in bl_info:
author = bl_info['author']
description = ""
if 'description' in bl_info:
description = bl_info['description']
tracker_url = ""
if 'tracker_url' in bl_info:
tracker_url = bl_info['tracker_url']
wiki_url = ""
if 'wiki_url' in bl_info:
wiki_url = bl_info['wiki_url']
location = ""
if 'location' in bl_info:
location = bl_info['location']
category = ""
if 'category' in bl_info:
category = bl_info['category']
warning = ""
if 'warning' in bl_info:
warning = bl_info['warning']
version = "unversioned"
if 'version' in bl_info:
version = '.'.join(map(str, bl_info['version']))
support = 'community'.lower()
if 'support' in bl_info:
support = bl_info['support'].lower()
json_text = ""
json_text += "\t\t\"" + addon_id + "\": {\n"
json_text += "\t\t\t\"source\": \"" + source + "\"\n"
json_text += "\t\t\t\"name\": \"" + bl_info['name'] + "\"\n"
json_text += "\t\t\t\"description\": \"" + description + "\"\n"
json_text += "\t\t\t\"author\": \"" + author + "\"\n"
json_text += "\t\t\t\"wiki_url\": \"" + wiki_url + "\"\n"
json_text += "\t\t\t\"tracker_url\": \"" + tracker_url + "\"\n"
json_text += "\t\t\t\"location\": \"" + location + "\"\n"
json_text += "\t\t\t\"category\": \"" + category + "\"\n"
json_text += "\t\t\t\"version\": {\n"
json_text += "\t\t\t\t\"" + version + "\": {\n"
json_text += "\t\t\t\t\t\"blender\": \"" + '.'.join(map(str, bl_info['blender'])) + "\"\n"
if warning != "":
json_text += "\t\t\t\t\t\"warning\": \"" + warning + "\"\n"
json_text += "\t\t\t\t\t\"support\": \"" + support + "\"\n"
if is_zip:
json_text += "\t\t\t\t\t\"filename\": \"" + version + ".zip\"\n"
else:
json_text += "\t\t\t\t\t\"filename\": \"" + version + ".py\"\n"
json_text += "\t\t\t\t}\n"
json_text += "\t\t\t}\n"
json_text += "\t\t}\n"
return json_text
addon = ""
json_text = "{\n"
json_text += "\t\"schema-version\": \"1\"\n"
json_text += "\t\"internal-url\": \"https://git.blender.org/gitweb/gitweb.cgi/blender-package-manager-addon.git/blob_plain/HEAD:/addons/\"\n"
json_text += "\t\"addons\": {\n"
json_text += list_addons(addons_directory, addon)
json_text += list_addons(contrib_directory, addon)
json_text += "\t}\n"
json_text += "}\n"
index_file = open(os.path.join(index_directory, 'index.json'), mode='w')
index_file.write(json_text)