Repository navigation
Expand file tree
/
Copy pathcommon.py
More file actions
171 lines (141 loc) · 7.75 KB
/
Copy pathcommon.py
File metadata and controls
171 lines (141 loc) · 7.75 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#####IMPORTS######
import os
import time
import json
class CommonWebPageFuncs:
def __init__(self):
#Could these come from a config? Also, inconsistent naming scheme. Please change.
self.ValidationPath = os.getenv("ValidationPath")
self.GIT_PULL_REQUEST = os.getenv("GIT_PULL_REQUEST", "false")
self.GIT_PULL_REQUEST_TITLE = os.getenv("GIT_PULL_REQUEST_TITLE", "")
self.GIT_PULL_REQUEST_LINK = os.getenv("GIT_PULL_REQUEST_LINK", "")
self.GIT_COMMIT = os.getenv("GIT_COMMIT", "")
self.GIT_TOKEN = os.getenv("GitHubToken", "")
#Some variables to read from a config file - this is set up as an example. May want to expand this later.
with open(f"{self.ValidationPath}/git_setup.json") as config_file: #THis config file can be passed as an argument to the class init, do we want to do this?
data = json.load(config_file)
#Git config
self.GIT_USER = data["Commit"]["Username"]
self.GIT_EMAIL = data["Commit"]["Email"]
#Validation setup
self.WEBPAGE_BRANCH = data["Validation"]["WebPageBranch"]
self.WEBPAGE_FOLDER = data["Validation"]["WebPageFolder"]
self.VALIDATION_GIT_PATH = data["Validation"]["Path"]
self.VALIDATION_GIT_BRANCH = data["Validation"]["CodeBranch"]
#Software setup
self.SOFTWARE_NAME = data["Software"]["Name"]
self.SOFTWARE_GIT_PATH = data["Software"]["Path"]
def checkout_validation_webpage_branch(self):
"""
Function that checks out the validation webpage branch from git.
The function checks if the Webpage directory exists within ValidationPath. If not, it clones the gh-pages branch of the Validation repository from GitHub into the Webpage directory.
Additionally, it sets up default git user configurations if not already configured.
Arguments:
- self.ValidationPath: The path to the WCSim validation directory.
- self.GIT_PULL_REQUEST: The state of the GIT CI pull request ("true" or "false").
- self.GIT_COMMIT: The commit ID of the GIT CI build.
"""
os.chdir(self.ValidationPath)
webpage_path = os.path.join(self.ValidationPath, "Webpage")
if not os.path.isdir(webpage_path):
os.system(f"git clone https://{self.VALIDATION_GIT_PATH} --single-branch --depth 1 -b {self.WEBPAGE_BRANCH} {self.WEBPAGE_FOLDER}")
os.chdir("Webpage")
# Add a default user, otherwise git complains
os.system("git config user.name 'WCSim CI'")
os.system("git config user.email 'wcsim@wcsim.wcsim'")
os.chdir(f"{self.ValidationPath}")
self.GIT_PULL_REQUEST = str(self.GIT_PULL_REQUEST) if self.GIT_PULL_REQUEST is not None else "false"
print("Showing GIT commit")
print(self.GIT_COMMIT)
print("Showing GIT pull request")
print(self.GIT_PULL_REQUEST)
def update_webpage(self):
"""
Function that updates the webpage.
This function cleans up old folders, adds and commits changes to the git repository, and pushes changes to the remote repository.
It includes a loop to handle potential conflicts when multiple jobs attempt to change the webpage simultaneously.
Arguments:
- self.ValidationPath: The path to the WCSim validation directory.
- self.GIT_COMMIT: The commit ID of the GIT CI build.
- self.GIT_TOKEN: The GitHub token for authentication.
"""
os.chdir(os.path.join(self.ValidationPath, "Webpage"))
# Clean up old folders
folder = 0
folderlist_filename = os.path.join(self.ValidationPath, "Webpage", "folderlist")
new_folderlist_filename = folderlist_filename + '_new'
with open(folderlist_filename, 'r') as folderlist_file:
with open(new_folderlist_filename, 'w') as new_folderlist_file:
for line in folderlist_file:
folder += 1
if folder >= 35:
os.system(f"git rm -r {line.strip()}")
else:
new_folderlist_file.write(line)
os.system(f'/bin/mv -f {new_folderlist_filename} {folderlist_filename}')
# Setup the commit
print("Adding")
os.system("git add --all")
os.system(f"git commit -a -m 'CI update: new pages for {self.GIT_COMMIT}'")
# Setup a loop to prevent clashes when multiple jobs change the webpage at the same time
# Make it a for loop, so there isn't an infinite loop
# 100 attempts, 15 seconds between = 25 minutes of trying
# The CI is set up such that files will be touched by one job at once,
# so it's just a matter of keeping pulling until we happen to be at the front of the queue
for iattempt in range(101):
# Get the latest version of the webpage
os.system("git pull --rebase")
# Attempt to push
push_command = f"git push https://{self.GIT_USER}:{self.GIT_TOKEN}@{self.VALIDATION_GIT_PATH} {self.WEBPAGE_BRANCH}"
if os.system(push_command) == 0:
break
# Have a rest before trying again
time.sleep(15)
def add_entry(self, TESTWEBPAGE, color, link, text):
"""
Function to add an entry to a test webpage.
This function appends an HTML table row to a specified test webpage file.
Arguments:
- TESTWEBPAGE: The file path of the test webpage.
- color: The background color of the table cell.
- link: The hyperlink to be inserted in the table cell.
- text: The text content of the table cell.
"""
with open(TESTWEBPAGE, 'a') as f:
f.write(f'''
<tr>
<td bgcolor="{color}">{link}{text}</td>
</tr>
''')
def check_diff(self, TESTWEBPAGE, diff_path, diff_file, ref_file, test_file, file_type):
"""
Function to compare differences between reference and test files and update a test webpage accordingly.
This function compares the content of a reference file with a test file using the 'diff' command.
If differences are found, it adds an entry to the test webpage indicating the difference.
If no differences are found, it adds an entry to the test webpage indicating the test passed.
Arguments:
- TESTWEBPAGE: The file path of the test webpage.
- diff_path: The directory path where the diff file will be stored.
- diff_file: The name of the diff file.
- ref_file: The file path of the reference file.
- test_file: The file path of the test file.
- file_type: The type of file being compared.
Returns:
- 1 if differences are found between the files.
- 0 if no differences are found between the files.
"""
diff_command = f"diff {ref_file} {test_file}"
diff_output = os.popen(diff_command).read()
if diff_output:
self.add_entry(TESTWEBPAGE, "#FF0000", f"<a href='{diff_file}'>", file_type)
print(f"{file_type}:")
print(diff_output)
with open(f"{diff_file}", "w") as df:
df.write(diff_output)
os.system(f"mv {diff_file} {diff_path}")
return 1 #Exit with a diff found
elif not os.path.isfile(f"{ref_file}"):
self.add_entry(TESTWEBPAGE, "#FF00FF", "", f"Reference {file_type} not found")
else:
self.add_entry(TESTWEBPAGE, "#00FF00", "", f"{file_type} pass")
return 0 #Exit with no diff found