-
Notifications
You must be signed in to change notification settings - Fork 9
/
pre_extra_script.py
155 lines (128 loc) · 5.08 KB
/
pre_extra_script.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
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
import json
import os
import gzip
import re
import platform
print("\033[94m╔═╗┌─┐┌─┐ ╔╗╔┌─┐┌┬┐ ╦═╗┌─┐┬ ┬┌┬┐┌─┐┬─┐ _|_\n║╣ └─┐├─┘32─║║║├─┤ │ ╠╦╝│ ││ │ │ ├┤ ├┬┘ | \n╚═╝└─┘┴ ╝╚╝┴ ┴ ┴ ╩╚═└─┘└─┘ ┴ └─┘┴└─\033[0m\n")
Import("env")
os.environ['PYTHONDONTWRITEBYTECODE'] = '1'
config = env.GetProjectConfig()
version = config.get("extra_script_settings", "version")
jekyll_install_status = False
system_platform = platform.system()
if os.system('jekyll -v') == 0:
# print('Jekyll is installed on your system.')
jekyll_install_status = True
else:
print(
'\033[91mJekyll is not installed on your system. Please install Jekyll.\033[0m')
if system_platform =="Linux":
# for linux user
def build_site():
env.Execute("build_web_pages.sh")
elif system_platform == "Windows":
# for windows user
def build_site():
env.Execute("build_web_pages.bat")
base_folder_path = './components/web_server/www/'
# check version in function.js file and update new version
def version_check():
pattern = r'version\s*=\s*"([\d.]+(?:_\w+)?)"'
# Define the new version number
new_version = version
filename = base_folder_path + 'html/js/functions.js'
# Open the JavaScript file
with open(filename, 'r+', encoding='utf-8') as js_file:
content = js_file.read()
if re.search(pattern, content):
old_version = re.search(pattern, content).group(1)
if old_version == new_version:
# print('Version already up to date.')
return
else:
new_content = re.sub(pattern, 'version="' + new_version + '"', content)
js_file.seek(0)
js_file.write(new_content)
js_file.truncate()
print(f'New version released: {new_version}')
else:
print('Version not found.')
return
# Try to remove the temporary file
try:
os.remove(filename + '.temp')
except FileNotFoundError:
pass
# gzip web pages and move to output/gzip folder
def gzip_web_pages():
dirpath = base_folder_path + 'output/html/'
output_dir = base_folder_path + 'output/gzip'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for subdir, dirs, files in os.walk(dirpath):
for filename in files:
file_path = subdir + os.sep + filename
if file_path.endswith(".html") or file_path.endswith(".css") or file_path.endswith(".js"):
new_filename = filename
if filename == "404.html":
new_filename = "error_404.html"
with open(file_path, 'rb') as f_in:
with gzip.open(os.path.join(output_dir, new_filename + '.gz'), 'wb') as f_out:
f_out.writelines(f_in)
def files_modtime(folder_path):
# Create a dictionary to store the modification times of each file
file_modtimes = {}
try:
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
file_modtime = os.path.getmtime(file_path)
file_modtimes[file_path] = file_modtime
return file_modtimes
except FileNotFoundError:
return file_modtimes
def read_json_file(file_path):
try:
with open(file_path, 'r') as file:
data = json.load(file)
return data
except FileNotFoundError:
return {"modtimes": ""}
def write_json_file(file_path, data):
with open(file_path, 'w') as file:
json.dump(data, file)
# check files modified or not
def file_changes_check():
# Call the function once to get the initial modification times
original_modtimes = files_modtime(
base_folder_path + 'output/html/')
modtime_json_file_path = '.pio/modtime_data.json'
data = read_json_file(modtime_json_file_path)
if data['modtimes'] != original_modtimes:
print("\033[91mFiles have been changed.\033[0m\n")
gzip_web_pages()
data['modtimes'] = original_modtimes
write_json_file(modtime_json_file_path, data)
# else:
# print("The files have not been changed.\n")
#checks website in development or production
def is_website_production_build():
try:
file_path = base_folder_path + "output/html/js/functions.js"
folder_path = base_folder_path + "output/gzip/"
word = "data/restart.json"
with open(file_path, 'r', encoding="utf-8") as file:
data = file.read()
if (not word in data):
print("\033[91mWebsite is in development\033[0m")
build_site()
# else:
# print("Web site is in production")
if not os.listdir(folder_path):
gzip_web_pages()
except FileNotFoundError:
build_site()
version_check()
if jekyll_install_status:
is_website_production_build()
file_changes_check()