aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjahoti <jahoti@tilde.team>2021-08-17 00:00:00 +0000
committerjahoti <jahoti@tilde.team>2021-08-17 00:00:00 +0000
commit9e280d4527e57cb4037fd81fb433c16f165de094 (patch)
tree77bb69a271223982192e8eb9c5833a3b529b1cbe
parente9b7f4d7bf624ccb0ed503f20b85288a575d020d (diff)
downloadbrowser-extension-9e280d4527e57cb4037fd81fb433c16f165de094.tar.gz
browser-extension-9e280d4527e57cb4037fd81fb433c16f165de094.zip
Begin work on a Hydrilla-compatible virtual website for testing
The file test/gorilla.py will help with testing respositories. It also provides a CLI Hydrilla > Hachette fix converter.
-rwxr-xr-xtest/gorilla.py82
1 files changed, 82 insertions, 0 deletions
diff --git a/test/gorilla.py b/test/gorilla.py
new file mode 100755
index 0000000..cd6cc94
--- /dev/null
+++ b/test/gorilla.py
@@ -0,0 +1,82 @@
+#!/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):
+ with open(path + file + 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):
+ 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:])))