-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject.py
More file actions
87 lines (63 loc) · 2.54 KB
/
Project.py
File metadata and controls
87 lines (63 loc) · 2.54 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import SCons
import os
from typing import TYPE_CHECKING
import SCons.Environment
from git import Repo
from .Action import Action
from .Toolset import Toolset
if TYPE_CHECKING:
from .Solution import Solution
class Project:
def __init__(self, name: str, parent: 'Solution|Project', path_relative_to_parent: str, output_path_root_relative_to_parent: str, git_url: str|None = None):
from .Solution import Solution
self.name = name
self.path_relative_to_parent = path_relative_to_parent
self.output_path_root_relative_to_parent = output_path_root_relative_to_parent
self.git_url = git_url
self.parent = parent
self.elements = []
self.environment: SCons.Environment.Environment = parent.environment.Clone()
self.toolsets = {}
@property
def absolute_path(self) -> str:
return os.path.join(self.parent.absolute_path, self.path_relative_to_parent)
@property
def absolute_output_path(self) -> str:
return os.path.join(self.parent.absolute_output_path, self.output_path_root_relative_to_parent)
# ensure_project_exists at the given path
# and if not, it clones the project including its submodules
def verify_project_exist(self) -> bool:
if not os.path.exists(self.absolute_path):
try:
if self.git_url is None:
raise ValueError(f'Project "{self.name}" does not exist and no git url is provided to clone it.')
print(f'{self.absolute_path} does not exist. Cloning from {self.git_url} branch "main"... ', end='')
repo = Repo.clone_from(self.git_url, self.absolute_path, branch='main', recursive=True)
print(f'Done')
return True
except Exception as e:
print(f'Failed with Error: {e}')
return False
else:
return True
# Adds a project to the list of projects
def add_sub_project(self, name: str, path_relative_to_solution: str, output_path_root_relative_to_parent: str, git_url: str|None = None)->'Project':
p = Project(name, self, path_relative_to_solution, output_path_root_relative_to_parent, git_url)
self.elements.append(p)
return p
# check if toolset exists in the project or its parents
def find_toolset(self, name: str)->Toolset|None:
if name in self.toolsets:
return self.toolsets[name]
return self.parent.find_toolset(name)
# Add Action to the project
def add_action(self, action: Action)->None:
self.elements.append(action)
def submit_action(self):
for element in self.elements:
if isinstance(element, Action):
element.submit_action()
elif isinstance(element, Project):
element.submit_action()
else:
raise ValueError(f'Invalid element type: {element}')