aboutsummaryrefslogtreecommitdiff
#!/usr/bin/env python3
#
# Copyright (C) 2021 jahoti <jahoti@tilde.team>
# Licensing information is collated in the `copyright` file

"""
A partial re-implementation of Hydrilla in Python, for testing purposes.

This will eventually be replaced with a build of the actual thing.
"""

import json, os, sys

def load_db(path):
	DB = {'script': {}, 'bag': {}, 'page': {}}
	if path[-1] != os.sep:
		path += os.sep
	
	DB['path'] = path
	for file in os.listdir(path):
		subdir = path + file
		
		if (os.st.S_IFMT(os.stat(subdir).st_mode) & os.st.S_IFDIR) == 0:
			continue
		
		with open(subdir + os.sep + 'index.json') as f:
			data = json.load(f)
		
		type_, data['file'] = data['type'], file
		name_key = 'pattern' if type_ == 'page' else 'name'
		
		DB[type_][data[name_key]] = data
		del data['type'], data[name_key]
		if type_ == 'script':
			with open(path + file + os.sep + data['location']) as f:
				data['text'] = f.read()
	
	return DB


def mkhachette(patterns):
	patterns = set(patterns)
	if '*' in patterns:
		patterns.remove('*')
		patterns.update(DB['page'].keys())
	
	scripts, bags, pages, new_bags = {}, {}, {}, []
	for pattern in patterns:
		pages[pattern] = data = DB['page'][pattern]
		type_, name = data['payload']
		if type_ == 'script':
			scripts[name] = DB['script'][name]
		else:
			new_bags.append(name)
	
	while new_bags:
		name = new_bags.pop(0)
		if name in bags:
			continue
		
		bags[name] = data = DB['bag'][name]['components']
		for type_, name in data:
			if type_ == 'script':
				scripts[name] = DB['script'][name]
			else:
				new_bags.append(name)
	
	items, path = [], DB['path']
	for script, data in scripts.items():
		item = {'hash': data['sha256']}
		with open(path + data['file'] + os.sep + data['location']) as f:
			item['text'] = f.read()
		
		items.append({'s' + script: item})
	
	for bag, data in bags.items():
		items.append({'b' + bag: [[type_[0], name] for type_, name in data]})
	
	for page, data in pages.items():
		type_, name = data['payload']
		items.append({'p' + page: {'components': [type_[0], name]}})
	
	return items


if __name__ == '__main__':
	if len(sys.argv) < 3 or not os.path.isdir(sys.argv[1]):
		sys.stderr.write('Usage: %s [hydrilla content path] (page pattern (page pattern (...)))' % sys.argv[0])
		sys.exit(1)
	
	DB = load_db(sys.argv[1])
	print(json.dumps(mkhachette(sys.argv[2:])))