1#!/usr/bin/python
2
3def retrieve_from_wiki():
4  import git, tempfile, shutil, os
5  try:
6    t = tempfile.mkdtemp()
7    git.Repo.clone_from('https://github.com/blitzpp/blitz.wiki.git', t, branch='master', depth=1)
8    with open(os.path.join(t, 'Features.md'), 'r') as f:
9      return f.readlines()
10  finally:
11    shutil.rmtree(t)
12data = retrieve_from_wiki()
13print('--', 'read', len(data), 'line[s]')
14
15
16def parse_the_tags(lines):
17  import re
18  from collections import defaultdict
19  p = re.compile(r'<!---[a-z]{3}-->')
20  curr = None
21  last = None
22  data = []
23  for line in lines:
24    m = p.match(line)
25    if m is not None:
26      last = curr
27      curr = line[5:8]
28      if curr == last: continue
29      if curr == 'src': data.append(defaultdict(list))
30    elif curr is not None:
31      if line[0:3] != '```': data[-1][curr].append(line)
32  return data
33data = parse_the_tags(data)
34print('--', 'read', len(data), 'block[s]')
35
36
37def compile_and_check(examples):
38  import tempfile, shutil, os, subprocess
39  path = os.getcwd()
40  errcnt = 0
41  for exno, example in enumerate(examples):
42    print('--', 'example', exno)
43    try:
44      # writing src to example.cpp
45      t = tempfile.mkdtemp()
46      with open(os.path.join(t, 'example.cpp'), 'w') as f:
47        f.writelines(example['src'])
48
49      # executing commands from cmd
50      os.chdir(t)
51      logfile = os.path.join(t, 'output.log')
52      with open(logfile, 'w') as f:
53        for cmd in example['cmd']:
54          cmd = cmd[2:].strip()
55          print('--', 'calling', cmd)
56          subprocess.call(cmd.split(' '), stderr=subprocess.STDOUT, stdout=f)
57
58      # comparing with out
59      with open(logfile, 'r') as f:
60        for i, line in enumerate(f.readlines()):
61          if example['out'][i] != line:
62            print('--', 'error')
63            errcnt += 1
64    except:
65      print('--', 'error')
66      with open(logfile, 'r') as fin: print fin.read()
67    finally:
68      os.chdir(path)
69      shutil.rmtree(t)
70  return errcnt
71nerr = compile_and_check(data)
72
73import sys
74if nerr != 0: sys.exit(1)
75