forked from kutelev/gost_forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·127 lines (104 loc) · 5.25 KB
/
build.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
#!/usr/bin/python3 -B
from argparse import ArgumentParser
from os import makedirs, listdir
from os.path import dirname, abspath, realpath, join as path_join, exists, isdir
from platform import system
from shutil import rmtree
from subprocess import CalledProcessError
from sys import exit
from typing import List
from re import compile
from document_builder import SpecificationBuilder, RegisterBuilder, ListOfElementsBuilder, TextDocumentBuilder, DocumentBuilderException
from common import print, check_call
project_pattern = compile(r'^[А-Я]{4}[-.]{1}\d{2,}\.\d{3,}[0-9.]{0,} \(.*\)$')
document_pattern = compile(r'^[А-Я]{4}[-.]{1}\d{2,}\.\d{3,}[0-9.]{0,} ((ВП|ПЭ3|РЭ) )?\(.*\)$')
def check_prerequisites():
if system() != 'Linux':
print('Error: nothing but Linux is supported.')
exit(1)
class Builder:
def __init__(self, root: str):
self.root = root
@staticmethod
def build_tools(script_path: str, force: bool):
if not force:
tools = ['2.106-form1', '2.106-form5', 'formgen', 'listofelgen']
if all(exists(path_join(script_path, 'tools', 'apps', tool, tool)) for tool in tools):
# All tools are already built.
return
print('Building tools ...')
tmp_path = path_join(script_path, 'tmp')
if exists(tmp_path):
rmtree(tmp_path)
makedirs(tmp_path)
check_call(['cmake', '..'], cwd=tmp_path)
check_call(['cmake', '-DCMAKE_BUILD_TYPE=Release', '-DCMAKE_VERBOSE_MAKEFILE=1', '-G', 'Unix Makefiles', '..'], cwd=tmp_path)
check_call(['cmake', '--build', '.', '--config', 'Release'], cwd=tmp_path)
rmtree(tmp_path)
print('Tools have been built.')
def enumerate_projects(self) -> List[str]:
def is_project(candidate: str):
return isdir(path_join(self.root, candidate)) and project_pattern.match(candidate) is not None
print('Enumerating projects ...')
projects = sorted(candidate for candidate in listdir(self.root) if is_project(candidate))
if not projects:
print('No projects have been found')
return []
print('The following projects have been found:')
for project in projects:
print(f'* {project}')
return projects
def enumerate_project_documents(self, project: str) -> List[str]:
def is_document(candidate: str):
return isdir(path_join(self.root, project, candidate)) and document_pattern.match(candidate) is not None
print(f'Enumerating project "{project}" documents ...')
documents = sorted(candidate for candidate in listdir(path_join(self.root, project)) if is_document(candidate))
if not documents:
print('No documents have been found')
return []
print('The following documents have been found:')
for document in documents:
print(f'* {document}')
return documents
def build_documents(self, project: str):
for document in self.enumerate_project_documents(project):
print(f'Building document "{document}" for project "{project}" ...')
document_type = document_pattern.match(document).group(2)
if document_type is None: # Спецификация
SpecificationBuilder(self.root, project, document).build()
elif document_type == 'ВП': # Ведомость покупных изделий
RegisterBuilder(self.root, project, document).build()
elif document_type == 'ПЭ3': # Перечень элементов
ListOfElementsBuilder(self.root, project, document).build()
elif document_type == 'РЭ': # Руководство по эксплуатации
TextDocumentBuilder(self.root, project, document).build()
else:
assert False, f'Unknown document type: {document_type}'
print(f'Document "{document}" of project "{project}" has been successfully built.')
def clean(self, project: str):
print(f'Cleaning working copy of project "{project}" ...')
check_call(['git', 'clean', '-ffdxq'], cwd=path_join(self.root, project))
def main() -> None:
script_dir = dirname(abspath(realpath(__file__)))
arg_parser = ArgumentParser(description='Documentation builder')
arg_parser.add_argument('--root', type=str, default=script_dir, help='A path to search for projects at')
arg_parser.add_argument('--clean', action='store_true', default=False, help='Clean working copy (git clean -ffdxq)')
arg_parser.add_argument('--force_tools_rebuild', action='store_true', default=False, help='Forcibly rebuild generating tools')
args = arg_parser.parse_args()
check_prerequisites()
builder = Builder(args.root)
builder.build_tools(script_dir, args.force_tools_rebuild)
for project in builder.enumerate_projects():
if args.clean:
builder.clean(project)
else:
builder.build_documents(project)
if __name__ == '__main__':
try:
main()
except (FileNotFoundError, CalledProcessError, DocumentBuilderException) as e:
print(f'Something went wrong: {str(e)}')
exit(1)
except Exception as e:
print(f'Critical error: {str(e)}')
exit(1)