forked from Vector35/community-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_index.py
87 lines (74 loc) · 3.21 KB
/
generate_index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
''' Python script to generate a stub README.md files from a plugin.json file '''
import json
import argparse
import os
import sys
import configparser
from collections import OrderedDict
parser = argparse.ArgumentParser(description = 'Generate README.md index of all plugins')
parser.add_argument("-f", "--force", help = 'will automatically overwrite existing README', action='store_true')
args = parser.parse_args()
os.path.realpath(__file__)
basedir = os.path.join(os.path.dirname(os.path.realpath(__file__)))
pluginsdir = os.path.join(basedir, 'plugins')
pluginreadme = os.path.join(pluginsdir, 'README.md')
pluginjson = os.path.join(basedir, "plugins.json")
if not args.force and os.path.isfile(pluginreadme):
print("Cowardly refusing to overwrite an existing README. Remove or re-run with -f.")
sys.exit(0)
if not args.force and os.path.isfile(pluginjson):
print("Cowardly refusing to overwrite an existing plugins.json. Remove or re-run with -f.")
sys.exit(0)
config = configparser.ConfigParser()
config.readfp(open(os.path.join(basedir, '.gitmodules')))
submodules = {}
for section in list(config.items())[1:]:
sectionname = section[0].split('"')[1]
submodules[sectionname] = config.get(section[0], 'url')
template = '# Binary Ninja Plugins\n\n'
template += '''| PluginName | Author | License | Type | Description |
|------------|--------|---------|----------|-------------|
'''
plugins = []
for plugin in list(os.walk(pluginsdir))[0][1]:
try:
url = submodules[os.path.join('plugins', plugin)]
if url.endswith('.git'):
url = url[:-4]
authorlink = '/'.join(url.split('/')[:4])
except:
url = 'https://github.com/Vector35/community-plugins/tree/master/plugins/{plugin}'.format(plugin=plugin)
authorlink = 'https://github.com/Vector35/'
try:
data = json.load(open(os.path.join(pluginsdir, plugin, "plugin.json")), object_pairs_hook=OrderedDict, encoding="utf-8")['plugin']
except Exception as exc:
print(exc)
print("Failed to load {}, possible json error.".format(plugin))
continue
if (not url.startswith("http") or (plugin == "" )):
print("Failed to load {}, possible json error.".format(plugin))
continue
data['url'] = url
data['path'] = plugin
if 'type' not in data:
data['type'] = ['none']
plugins.append(data)
template += u'|[{name}]({url})|[{author}]({authorlink})|[{license}]({plugin}/LICENSE)|{plugintype}|{description}|\n'.format(name = data['name'],
url=url,
plugin=plugin,
author=data['author'],
authorlink=authorlink,
license=data['license']['name'],
plugintype=', '.join(sorted(data['type'])),
description=data['description'])
template += "\n\n"
print("Writing " + pluginjson)
try:
open(pluginjson, 'w').write(json.dumps(plugins, indent=4, sort_keys=True, ensure_ascii=False, encoding="utf-8").encode("utf-8"))
except:
open(pluginjson, 'w').write(json.dumps(plugins, indent=4, sort_keys=True, ensure_ascii=False))
print("Writing " + pluginreadme)
try:
open(pluginreadme, "w").write(template)
except:
open(pluginreadme, "w").write(template.encode('utf-8'))