forked from mbeddr/mps-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSolutionsRepositoryBuilder.py
More file actions
60 lines (52 loc) · 2.27 KB
/
SSolutionsRepositoryBuilder.py
File metadata and controls
60 lines (52 loc) · 2.27 KB
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
from timeit import default_timer as timer
import os
import sys
import zipfile
import shutil
from pathlib import Path
from mpscli.model.SRepository import SRepository
from mpscli.model.builder.SLanguageBuilder import SLanguageBuilder
from mpscli.model.builder.SSolutionBuilder import SSolutionBuilder
class SSolutionsRepositoryBuilder:
def __init__(self):
self.repo = SRepository()
def build(self, paths):
if isinstance(paths, str):
paths = [paths]
elif not isinstance(paths, list):
print("ERROR: paths should be either a string or a list of strings!")
sys.exit(1)
start = timer()
for path in paths:
if not os.path.exists(path):
print("ERROR: path", path, "does not exist!")
continue
if not os.path.isdir(path):
print("ERROR: path", path, "is not a directory!")
continue
print("building model from path:", path)
self.collect_solutions_from_sources(path)
self.collect_solutions_from_jars(path)
self.repo.languages = list(SLanguageBuilder.languages.values())
stop = timer()
duration = (stop - start)
print('duration for parsing modules: ' + str(duration) + ' seconds')
return self.repo
def collect_solutions_from_sources(self, path):
for pth in Path(path).rglob('*.msd'):
solutionBuilder = SSolutionBuilder()
solution = solutionBuilder.build_solution(pth)
if solution is not None:
self.repo.solutions.append(solution)
def collect_solutions_from_jars(self, path):
for jar_path in Path(path).rglob('*.jar'):
directory_where_to_extract = jar_path.parent / jar_path.name.replace(".", "_")
directory_where_to_extract.mkdir(parents=True, exist_ok=True)
with zipfile.ZipFile(jar_path) as jar:
jar.extractall(directory_where_to_extract)
print("path = ", directory_where_to_extract)
self.collect_solutions_from_sources(directory_where_to_extract)
try:
shutil.rmtree(directory_where_to_extract)
except OSError as e:
print("Error: %s - %s." % (e.filename, e.strerror))