-
Notifications
You must be signed in to change notification settings - Fork 0
/
pakit_tests.py
executable file
·241 lines (194 loc) · 5.96 KB
/
pakit_tests.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Automated bot for testing recipes.
"""
from __future__ import absolute_import, print_function
import argparse
from argparse import RawDescriptionHelpFormatter as RawDescriptionHelp
import glob
import mmap
import os
import re
import sys
__version__ = '0.1.0'
TEMPLATE = """\"\"\"
Do not edit this file.
Just rerun the program to regenerate tests.
\"\"\"
from __future__ import absolute_import
import os
import shutil
import subprocess
import tempfile
CONFIG_TEMPLATE = \"\"\"pakit:
command:
timeout: 120
defaults:
repo: stable
log:
enabled: true
file: {0}
level: debug
paths:
link: {1}
prefix: {2}
recipes: {3}
source: {4}
recipe:
update_interval: 86400
uris:
- uri: test_recipes
{5}
\"\"\"
def write_config(tail):
\"\"\"
Write config for a test execution to path.
Args:
tail: Vaild yaml to affix to the end of CONFIG_TEMPLATE.
Returns:
Path to temp directory.
\"\"\"
root = tempfile.mkdtemp()
recipe_d = os.path.join(root, 'recipe')
os.mkdir(recipe_d)
os.symlink('ROOT_RECS',
os.path.join(recipe_d, 'test_recipes'))
with open(os.path.join(root, 'pakit.yml'), 'w') as fout:
fout.write(CONFIG_TEMPLATE.format(
os.path.join(root, 'main.log'),
os.path.join(root, 'link'),
os.path.join(root, 'prefix'),
recipe_d,
os.path.join(root, 'source'),
tail
))
return root
def delete_it(path):
\"\"\"
File or folder, it is deleted.
Args:
path: path to a file or dir
\"\"\"
try:
shutil.rmtree(path)
except OSError:
try:
os.remove(path)
except OSError:
pass
class RecipeTest(object):
def setup_method(self, method):
recipe = type(self).__name__.replace('Test_', '').split('::')[0]
repo = method.__name__.replace('test_', '')
self.temp_d = write_config(recipe + ':\\n repo: ' + repo)
self.args = ['pakit', '--conf',
os.path.join(self.temp_d, 'pakit.yml'), 'install', recipe]
self.new_env = os.environ.copy()
new_path = os.environ['PATH'] + ':' + os.path.join(self.temp_d,
'link', 'bin')
self.new_env.update({'PATH': new_path})
def teardown_method(self, _):
delete_it(self.temp_d)
"""
def create_args_parser():
"""
Create the program argument parser.
Returns:
An argparse parser object.
"""
prog_name = os.path.basename(os.path.dirname(sys.argv[0]))
mesg = """
This script will (re)generate tests for recipes.
It will OVERWRITE existing tests.
"""
mesg = mesg[0:-5]
parser = argparse.ArgumentParser(prog=prog_name, description=mesg,
formatter_class=RawDescriptionHelp)
parser.add_argument('-v', '--version', action='version',
version='pakit_tests {0}'.format(__version__))
parser.add_argument('recipes_root', help='the folder containing recipes')
parser.add_argument('output', nargs='?', default='tests/test_recipes.py',
help="""relative path from recipes root to test file,
default: tests/test_recipes.py""")
return parser
def extract_repo_names(text):
"""
Given a string, extract all keys from the string.
Returns:
List of keys in the string.
"""
matcher = re.compile(r'\'(\w+)\':')
results = [matched.group(1) for matched in matcher.finditer(text)]
return results
def extract_repo_block(text):
"""
Given a string, extract ONLY the repos dictionary block.
Returns:
A string containing only required block.
"""
return re.search(r'(self.repos\s*=\s*{.*?})', text, re.DOTALL).group(1)
def format_lines(recipes):
"""
Transform the dictionary to lines to write.
Returns:
Lines to write to test file.
"""
lines = []
class_line = '\nclass Test_{0}(RecipeTest):'
repo_line = """ def test_{0}(self):
assert subprocess.call(self.args, cwd=self.temp_d,
env=self.new_env) == 0
"""
for recipe in sorted(recipes):
repo_name = recipes[recipe]
lines.append(class_line.format(recipe))
for repo_name in recipes[recipe]:
lines.extend(repo_line.format(repo_name).split('\n'))
return lines[0:-1]
def scan_recipes(recipe_d):
"""
Scan the recipe directory and return relevant data.
"""
data = {}
matcher = re.compile(r'class\s+\S+\(Recipe\)')
candidates = [fname for fname in glob.glob(os.path.join(recipe_d, '*.py'))]
for fname in candidates:
short_name = os.path.basename(fname)[0:-3]
with open(fname, 'r+') as fin:
text = mmap.mmap(fin.fileno(), 0)
if matcher.search(text) is not None:
data[short_name] = extract_repo_names(extract_repo_block(text))
return data
def write_file(root, test_file):
"""
Write the test file as requested.
"""
try:
os.makedirs(os.path.dirname(test_file))
except OSError:
pass
text = TEMPLATE.replace('ROOT_RECS', root) + \
'\n'.join(format_lines(scan_recipes(root)))
with open(test_file, 'w') as fout:
fout.write(text)
def main(argv=None):
"""
The main entry point for this program.
Args:
argv: A list of program options, if None use sys.argv.
"""
if argv is None:
argv = sys.argv
parser = create_args_parser()
args = parser.parse_args(argv[1:])
root = os.path.abspath(args.recipes_root)
if os.path.isabs(args.output):
test_file = os.path.join(root, args.output)
else:
test_file = os.path.join(root, args.output)
print('Scanning recipes under: ' + root)
print('Writing tests to: ' + test_file)
write_file(root, test_file)
if __name__ == "__main__":
main() # pragma: no cover