-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
template
executable file
·120 lines (111 loc) · 3.63 KB
/
template
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
from argparse import ArgumentParser
from os import chmod, mkdir, stat, walk
from os.path import dirname, join
from sys import argv
SPECIAL_HIDDEN = {'.git_hooks_pre-commit', '.github', '.gitignore'}
SPECIAL_IGNORE = {
'env',
'script/cibuild-template',
'script/cibuild-setup-py-template',
'script/look-for-changes',
'script/template',
'script/update-requirements-template',
'tmp',
}
source_dir = dirname(dirname(argv[0]))
source_dir_len = len(source_dir) + 1
parser = ArgumentParser(description='Create a skeletal octoDNS module.')
parser.add_argument(
'--module',
type=str,
required=True,
help='octoDNS module name, e.g. [octodns_something]',
)
parser.add_argument(
'--provider',
type=str,
required=True,
help='octoDNS provider name, e.g. [SomethingProvider]',
)
parser.add_argument(
'--name',
type=str,
required=True,
help='Name of the provider/service, e.g. Something',
)
parser.add_argument(
'--link',
type=str,
required=True,
help='Link to information about the provider, e.g. '
'[https://www.something.com/docs/api]',
)
parser.add_argument(
'--author',
type=str,
required=True,
help="octoDNS module author's name, e.g. Ross McFarland",
)
parser.add_argument(
'--author-email',
type=str,
required=True,
help="octoDNS module author's email, e.g. " 'rwmcfa1@gmail.com',
)
options = parser.parse_args()
options.module_dashed = options.module.replace('_', '-')
options.module_stripped = options.module.replace('octodns_', '')
def copy(source, target):
# Pull in contents and do simple templating
with open(source) as fh:
contents = (
fh.read()
.replace('{MODULE}', options.module)
.replace('{MODULE_DASHED}', options.module_dashed)
.replace('{MODULE_STRIPPED}', options.module_stripped)
.replace('{PROVIDER}', options.provider)
.replace('{NAME}', options.name)
.replace('{LINK}', options.link)
.replace('{AUTHOR}', options.author)
.replace('{AUTHOR_EMAIL}', options.author_email)
.replace('./script/cibuild-template', './script/cibuild')
.replace(
'./script/cibuild-setup-py-template',
'./script/cibuild-setup-py',
)
)
# special case to switch from the template ci to the real one ^
# If there's a header skip to its end marker
try:
index = contents.index('{EOH}')
contents = contents[index + 7 :]
except ValueError:
pass
# Write out templated contents
with open(target, 'w') as fh:
fh.write(contents)
# Preserve permissions
st = stat(source)
chmod(target, st.st_mode)
for root, dirs, files in walk(source_dir):
target_dir = root[source_dir_len:].replace('{MODULE}', options.module)
if target_dir.startswith('.') and not any(
[target_dir.startswith(s) for s in SPECIAL_HIDDEN]
):
continue
for directory in dirs:
if directory.startswith('.') and directory not in SPECIAL_HIDDEN:
continue
target = join(target_dir, directory).replace('{MODULE}', options.module)
if any([target.startswith(si) for si in SPECIAL_IGNORE]):
continue
mkdir(target)
for file in files:
if file.startswith('.') and file not in SPECIAL_HIDDEN:
continue
source = join(root, file)
target = join(target_dir, file).replace('{MODULE}', options.module)
if any([target.startswith(si) for si in SPECIAL_IGNORE]):
continue
copy(source, target)