-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcreate-plugin.py
More file actions
101 lines (78 loc) · 2.43 KB
/
Copy pathcreate-plugin.py
File metadata and controls
101 lines (78 loc) · 2.43 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Red Hat
# Licensed under The MIT License (MIT)
# http://opensource.org/licenses/MIT
#
import argparse
import errno
import logging
import os
PLUGIN_TEMPLATE = '''
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Red Hat
# Licensed under The MIT License (MIT)
# http://opensource.org/licenses/MIT
#
import json
from pdc_client import plugin_helpers
class {class_name}(plugin_helpers.PDCClientPlugin):
def register(self):
pass
PLUGIN_CLASSES = [{class_name}]
'''
TEST_TEMPLATE = '''
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Red Hat
# Licensed under The MIT License (MIT)
# http://opensource.org/licenses/MIT
#
from pdc_client.test_helpers import CLITestCase
from pdc_client.runner import Runner
class {name}TestCase(CLITestCase):
def setUp(self):
self.runner = Runner()
self.runner.setup()
'''
def write_file(name, contents):
try:
with open(name, 'wx') as f:
f.write(contents)
except IOError as err:
if err.errno == errno.EEXIST:
logging.warning('Not overwriting %s', name)
else:
logging.error('Unhandled exception', exc_info=err)
else:
logging.info('Created %s (%d bytes)', name, len(contents))
def create_plugin(name):
filepath = os.path.join('pdc_client', 'plugins', name + '.py')
class_name = '{0}Plugin'.format(name.capitalize())
data = PLUGIN_TEMPLATE.format(class_name=class_name).lstrip()
write_file(filepath, data)
def create_tests(name):
path = os.path.join('pdc_client', 'tests', name)
try:
os.makedirs(os.path.join(path, 'data'))
logging.info('Created %s/', os.path.join(path, 'data'))
except os.error:
pass
write_file(os.path.join(path, '__init__.py'), '')
data = TEST_TEMPLATE.format(name=name.capitalize()).lstrip()
write_file(os.path.join(path, 'tests.py'), data)
def main():
parser = argparse.ArgumentParser('create-plugin.py')
parser.add_argument('name', metavar='NAME')
parser.add_argument('--skip-tests', action='store_true')
parser.add_argument('--verbose', action='store_true')
options = parser.parse_args()
logging.basicConfig(level=logging.INFO if options.verbose else logging.WARNING,
format='%(levelname)s: %(message)s')
name = options.name
create_plugin(name)
if not options.skip_tests:
create_tests(name)
if __name__ == '__main__':
main()