/
add_new_plugin.py
87 lines (69 loc) · 2.12 KB
/
add_new_plugin.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
import os
import click
ROOT_DIRECTORY = os.path.abspath(
os.path.join(os.path.dirname(__file__), os.pardir)
)
PLUGIN_DIRECTORY = os.path.join(ROOT_DIRECTORY, 'detectem/plugins')
PLUGIN_DIRECTORIES = [
d for d in os.listdir(PLUGIN_DIRECTORY)
if os.path.isdir(os.path.join(PLUGIN_DIRECTORY, d)) and d != '__pycache__'
]
@click.command()
@click.option(
'--matcher',
type=click.Choice(['url', 'body', 'header', 'xpath']),
required=True,
help='Set the matcher type.',
)
@click.option(
'--category',
type=click.Choice(PLUGIN_DIRECTORIES),
required=True,
help='Set plugin category.',
)
@click.argument('name')
def main(name, category, matcher):
create_plugin_file(name, category, matcher)
create_test_file(name, matcher)
def create_plugin_file(name, category, matcher):
plugin_template = '''
from detectem.plugin import Plugin
class {title}Plugin(Plugin):
name = '{name}'
homepage = ''
matchers = [
{{'{matcher}': 'Plugin signature v(?P<version>[0-9\.]+)'}},
]
"""
js_matchers = [
{{'check': '', 'version': ''}},
]
"""
'''.format(name=name, title=name.title(), matcher=matcher).lstrip()
plugin_filename = name + '.py'
plugin_filepath = os.path.join(
PLUGIN_DIRECTORY, category, plugin_filename
)
if os.path.exists(plugin_filepath):
raise FileExistsError('Plugin file already exists.')
with open(plugin_filepath, mode='w') as f:
f.write(plugin_template)
print('Created plugin file at {}'.format(plugin_filepath))
def create_test_file(name, matcher):
test_template = '''
- plugin: {name}
matches:
- {matcher}:
version:
'''.format(name=name, matcher=matcher).lstrip()
test_filename = name + '.yml'
test_filepath = os.path.join(
ROOT_DIRECTORY, 'tests', 'plugins', 'fixtures', test_filename
)
if os.path.exists(test_filepath):
raise FileExistsError('Test file already exists.')
with open(test_filepath, mode='w') as f:
f.write(test_template)
print('Created test file at {}'.format(test_filepath))
if __name__ == "__main__":
main()