Split repo generation functionality out of blenderpack.py
Moved repo.json generation to make_repo.py Package/addon parsing is getting a bit messy, this can be cleaned up when we have a clearer idea of what a package is. For now just make it work.
This commit is contained in:
@@ -60,94 +60,6 @@ def fetch(url, pipe):
|
||||
# def json(self):
|
||||
# return json.dumps(self.__dict__)
|
||||
|
||||
def parse_blinfo(source: str) -> dict:
|
||||
"""Parse a python file and return its bl_info dict, if there is one (else return None)"""
|
||||
|
||||
tree = ast.parse(source)
|
||||
|
||||
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)
|
||||
|
||||
raise BadAddon('No bl_info found')
|
||||
|
||||
|
||||
def extract_blinfo(path: pathlib.Path) -> dict:
|
||||
"""Extract bl_info dict from addon at path (can be single file, module, or zip)"""
|
||||
|
||||
source = None
|
||||
# get last component of path
|
||||
addon_name = path.name
|
||||
|
||||
if path.is_dir():
|
||||
with open(path / '__init__.py', 'r') as f:
|
||||
source = f.read()
|
||||
else:
|
||||
|
||||
# HACK: perhaps not the best approach determining filetype..?
|
||||
try:
|
||||
with zipfile.ZipFile(str(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 path.open() as f:
|
||||
source = f.read()
|
||||
|
||||
if source == None:
|
||||
raise RuntimeError("Could not read addon '%s'" % addon_name)
|
||||
|
||||
return parse_blinfo(source)
|
||||
|
||||
|
||||
|
||||
def make_repo(repopath: pathlib.Path):
|
||||
"""Make repo.json for files in directory 'repopath'"""
|
||||
|
||||
repo_data = {}
|
||||
package_data = []
|
||||
|
||||
if not repopath.is_dir():
|
||||
raise FileNotFoundError(repopath)
|
||||
|
||||
for addon_path in repopath.iterdir():
|
||||
package_datum = {}
|
||||
addon = addon_path.name
|
||||
|
||||
try:
|
||||
bl_info = extract_blinfo(addon_path)
|
||||
except BadAddon as err:
|
||||
log.warning('Could not extract bl_info from {}: {}'.format(addon_path, err))
|
||||
continue
|
||||
|
||||
if not REQUIRED_KEYS.issubset(set(bl_info)):
|
||||
log.warning(
|
||||
"Required key(s) '{}' not found in bl_info of '{}'".format(
|
||||
"', '".join(REQUIRED_KEYS.difference(set(bl_info))), addon)
|
||||
)
|
||||
|
||||
package_datum['bl_info'] = bl_info
|
||||
package_datum['type'] = 'addon'
|
||||
package_data.append(package_datum)
|
||||
|
||||
repo_data['packages'] = package_data
|
||||
|
||||
log.info("Repository generation successful")
|
||||
cwd = pathlib.Path.cwd()
|
||||
dump_repo(cwd, repo_data)
|
||||
log.info("repo.json written to %s" % cwd)
|
||||
|
||||
def dump_repo(repo_path: pathlib.Path, repo_data: dict):
|
||||
with (repo_path / 'repo.json').open('w', encoding='utf-8') as repo_file:
|
||||
json.dump(repo_data, repo_file, indent=4, sort_keys=True)
|
||||
|
||||
|
||||
def main():
|
||||
|
Reference in New Issue
Block a user