150 lines
4.2 KiB
Python
Executable File
150 lines
4.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
# HACK: seems 'requests' module bundled with blender isn't bundled with 'idna' module. So force system python for now
|
|
import sys
|
|
sys.path.insert(0, '/usr/lib/python3.6/site-packages')
|
|
|
|
import requests
|
|
import json
|
|
import os
|
|
import ast
|
|
import argparse
|
|
import zipfile
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
REQUIRED_KEYS = set(['name', 'blender', 'version'])
|
|
SCHEMA_VERSION = 1
|
|
|
|
def report(msg):
|
|
print("blenderpack:", msg, file=sys.stderr)
|
|
|
|
def fatal_report(msg, code=1, ex=None):
|
|
if __name__ == '__main__':
|
|
# report("fatal: %s" % msg)
|
|
log.fatal(msg)
|
|
exit(code)
|
|
elif ex is not None:
|
|
raise Exception(msg) from ex
|
|
else:
|
|
raise RuntimeError("blenderpack: Unhandled fatal exception: %s" % msg)
|
|
|
|
|
|
def fetch(url):
|
|
# TODO: do conditional request
|
|
re = requests.get(url)
|
|
print(re.json())
|
|
|
|
def parse_blinfo(source, addon_name="unknown"):
|
|
"""Parse a python file and return its bl_info dict, if there is one (else return None)"""
|
|
|
|
try:
|
|
tree = ast.parse(source)
|
|
except SyntaxError as ex:
|
|
log.warning('Skipping addon: SyntaxError in %s: %s', addon_name, ex)
|
|
return None
|
|
|
|
for body in tree.body:
|
|
if body.__class__ != ast.Assign:
|
|
continue
|
|
if len(body.targets) != 1:
|
|
continue
|
|
if getattr(body.targets[0], 'id', '') != 'bl_info':
|
|
continue
|
|
|
|
return ast.literal_eval(body.value)
|
|
|
|
log.warning('Unable to find bl_info dict in %s', addon_name)
|
|
return None
|
|
|
|
|
|
def extract_blinfo(path):
|
|
"""Extract bl_info dict from addon at path (can be single file, module, or zip)"""
|
|
|
|
source = None
|
|
# get last component of path, even with trailing slash
|
|
addon_name = os.path.split(path.rstrip(os.path.sep))[1]
|
|
|
|
if os.path.isdir(path):
|
|
with open(os.path.join(path, '__init__.py'), 'r') as f:
|
|
source = f.read()
|
|
else:
|
|
|
|
# HACK: perhaps not the best approach determining filetype..?
|
|
try:
|
|
with zipfile.ZipFile(path, 'r') as z:
|
|
for fname in z.namelist():
|
|
# HACK: this seems potentially fragile; depends on zipfile listing root contents first
|
|
if fname.endswith('__init__.py'):
|
|
source = z.read(fname)
|
|
break
|
|
except zipfile.BadZipFile:
|
|
with open(path, 'r') as f:
|
|
source = f.read()
|
|
|
|
if source == None:
|
|
raise RuntimeError("Could not read addon '%s'" % addon_name)
|
|
|
|
return parse_blinfo(source, addon_name)
|
|
|
|
|
|
|
|
def make_repo(outpath):
|
|
"""Make repo.json for files in directory 'outpath'"""
|
|
|
|
repo_data = {}
|
|
package_data = []
|
|
|
|
try:
|
|
for addon in os.listdir(outpath):
|
|
package_datum = {}
|
|
addon_path = os.path.join(outpath, addon)
|
|
|
|
try:
|
|
bl_info = extract_blinfo(addon_path)
|
|
if not bl_info:
|
|
raise Exception("No bl_info found in %s" % addon)
|
|
except Exception as err:
|
|
fatal_report('Could not extract bl_info from {}: {}'.format(addon_path, err))
|
|
|
|
if not REQUIRED_KEYS.issubset(set(bl_info)):
|
|
fatal_report(
|
|
"Required key(s) '{}' not found in bl_info of '{}'".format(
|
|
"', '".join(REQUIRED_KEYS.difference(set(bl_info))), addon),
|
|
ex=Exception()
|
|
)
|
|
|
|
package_datum['bl_info'] = bl_info
|
|
package_datum['type'] = 'addon'
|
|
package_data.append(package_datum)
|
|
|
|
repo_data['packages'] = package_data
|
|
with open(os.path.join(outpath, "repo.json"), 'w', encoding='utf-8') as repo_file:
|
|
json.dump(repo_data, repo_file, indent=4, sort_keys=True)
|
|
|
|
|
|
except FileNotFoundError:
|
|
fatal_report("No such file or directory: '%s'" % outpath)
|
|
|
|
|
|
|
|
def main():
|
|
pass
|
|
# print(args)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description='Search, install, and manage packages for Blender')
|
|
subparsers = parser.add_subparsers()
|
|
|
|
make = subparsers.add_parser('make')
|
|
make.add_argument('path')
|
|
make.set_defaults(func=lambda args: make_repo(args.path))
|
|
|
|
args = parser.parse_args()
|
|
args.func(args)
|
|
|
|
main()
|
|
|