Newer
Older
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
"""
Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The OpenAirInterface Software Alliance licenses this file to You under
the OAI Public License, Version 1.1 (the "License"); you may not use this file
except in compliance with the License.
You may obtain a copy of the License at
http://www.openairinterface.org/?page_id=698
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
------------------------------------------------------------------------------
For more information about the OpenAirInterface (OAI) Software Alliance:
contact@openairinterface.org
------------------------------------------------------------------------------
"""
from subprocess import PIPE,STDOUT
import time
import subprocess
import logging
import argparse
import re
import sys
logging.basicConfig(
level=logging.DEBUG,
format="%(message)s"
)
## Global variables
DOCUMENT_FOLDER = '../docs'
SLEEP_BETWEEN_COMMANDS = 2
SLEEP_BETWEEN_HEADERS = 5
DOCKER_COMPOSE_DIRECTORY='../docker-compose'
def _parse_args() -> argparse.Namespace:
"""Parse the command line args
Returns:
argparse.Namespace: the created parser
"""
example_text = '''example:
python3 checkTutorial.py --tutorial DEPLOY_SA5G_BASIC_STATIC_UE_IP.md'''
parser = argparse.ArgumentParser(description='Run the tutorials to see they are executing fine',
epilog=example_text,
formatter_class=argparse.RawDescriptionHelpFormatter)
# Tutorial Name
parser.add_argument(
'--tutorial', '-t',
action='store',
required=True,
help='name of the tutorial markdown file',
)
return parser.parse_args()
def subprocess_call(command,cwd):
popen = subprocess.Popen(command,shell=True,universal_newlines=True,cwd=cwd,stdout=PIPE)
for stdout_line in iter(popen.stdout.readline, ""):
yield stdout_line
popen.stdout.close()
return_code = popen.wait()
if return_code:
raise subprocess.CalledProcessError(return_code, command)
def extract_h2_blocks(headers,text):
text = text.split('\n')
positions = []
header_blocks = dict()
temp_header_blocks = dict()
for header in headers:
header_blocks.update({header:[]})
for position,line in enumerate(text):
if header in line:
temp_header_blocks[position]=header
positions.append(position)
for key,value in enumerate(positions):
if key < len(positions)-1:
for n in range(positions[key],positions[key+1]):
header_blocks[temp_header_blocks[value]].append(text[n])
else:
for n in range(positions[key],len(text)-key):
header_blocks[temp_header_blocks[value]].append(text[n])
return header_blocks
def execute_shell_command(h2_blocks):
for value in h2_blocks:
logging.info('\033[0;32m {}\033[0m'.format(value))
shell_blocks = re.findall(r"`{3} shell\n([\S\s]+?)`{3}",'\n'.join(h2_blocks[value]))
for block in shell_blocks:
commands = re.findall(r"docker-compose-host \$: (.*)",block)
for command in commands:
logging.info('\033[0;31m Executing command "{}"\033[0m'.format(command))
for output in subprocess_call(command=command, cwd=DOCKER_COMPOSE_DIRECTORY):
print(output)
time.sleep(SLEEP_BETWEEN_COMMANDS)
time.sleep(SLEEP_BETWEEN_HEADERS)
return 0
def check_tutorial(name):
filename = DOCUMENT_FOLDER + '/' + name
with open(filename, 'r') as f:
text = f.read()
h2 = re.findall(r"## (.*)\n",text)
h2_blocks = extract_h2_blocks(h2,text)
rc = execute_shell_command(h2_blocks)
print("Finish checking the tutorial {} with exit code {}".format(name,str(rc)))
sys.exit(rc)
if __name__ == '__main__':
# Parse the arguments to get the deployment instruction
args = _parse_args()
check_tutorial(args.tutorial)