-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrun_examples.py
59 lines (41 loc) · 2.12 KB
/
run_examples.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
import os
import argparse
import glob
import codecs
import re
import subprocess
import tempfile
import sys
exec_suffix = '.exe' if sys.platform.startswith('win32') else ''
def execute_example(name, code, template_path, output_path, include_dirs):
print(name, '...')
builddir = tempfile.mkdtemp()
print(builddir)
cppfile = os.path.join(builddir, 'main.hpp')
open(cppfile, 'w').write(code)
if subprocess.call(['cmake', '-DCMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE=' + builddir + '', '-DCMAKE_BUILD_TYPE=Release', '-DCMAKE_CXX_FLAGS=-I\"' + include_dirs + '\" -DSNIPPET=\\"' + cppfile + '\\"', template_path], cwd=builddir) == 0:
if subprocess.call(['cmake', '--build', '.', '--config', 'Release'], cwd=builddir) == 0:
subprocess.call(['ls', '-la'], cwd=builddir)
subprocess.call(['./main'], cwd=builddir)
output = subprocess.check_output([os.path.join(builddir, 'main')+exec_suffix], cwd=builddir)
print('ok, writing output...')
open(os.path.join(output_path, name) + '.md',
'w').write('```\n' + output.decode('utf-8').strip() + '\n```')
else:
print('Can`t configure example: {}'.format(name))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Run markdown code examples and save output & images')
parser.add_argument('input_path', help='path to markdown files')
parser.add_argument('template_path', help='path C++ project template')
parser.add_argument('include_dirs', help='include directories')
args = parser.parse_args()
print('Running examples in ', args.input_path + os.path.sep + '*.md', '...')
filenames = glob.glob(
args.input_path + os.path.sep + '*.md', recursive=True)
for filename in filenames:
print(filename, '...')
md = codecs.open(filename, 'r', encoding='utf-8').read()
for r in re.finditer(r'```c\+\+ tab="([a-zA-Z0-9_-]).cpp"\s+(.*?)\s+```', md, re.DOTALL | re.MULTILINE):
execute_example(r.group(1), r.group(2), args.template_path, os.path.join(
args.input_path, 'fragments'), args.include_dirs)