-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfromdist.py
More file actions
256 lines (212 loc) Β· 9.82 KB
/
Copy pathfromdist.py
File metadata and controls
256 lines (212 loc) Β· 9.82 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
"""
This script automates the process of taking a 'dist' directory (typically from a frontend build process),
restructuring its contents into a Flask-compatible static and templates directory, and performing
URL rewrites within HTML files.
It performs the following main steps:
1. **Initialization**: Defines regex patterns for URL transformation and helper functions.
2. **Directory Setup**: Moves the 'static' folder from 'dist' to the project root, creates
'static/scripts', 'static/styles', and 'templates' directories.
3. **File Restructuring**: Iterates through files in the 'dist' directory:
* Moves `.js` and `.wasm` files to `static/scripts/` and renames them by removing hash suffixes
(e.g., `ui-a1b2c3d4.js` becomes `ui.js`).
* Moves `.css` files to `static/styles/` and renames them similarly.
* Moves `.html` files to the `templates/` directory.
4. **Cleanup**: Removes the original 'dist' directory.
5. **Template Processing**: Modifies `templates/index.html` to:
* Rewrite URLs to point to the new static asset locations.
* Replace a specific API connection string with a Flask URL (`/api/tree/{{tree_id}}`).
* Add CSRF token support.
* Remove `integrity` attributes from script/link tags.
6. **Locale Minification**: Minifies JSON locale files in `static/locales`.
7. **Finalization (User Confirmed)**:
* Copies files from an `internal` directory into the `static` directory, merging locale files.
* Renames `templates/index.html` to `templates/tree.html`.
This script is designed to be run after a frontend build (e.g., Vite, Webpack) to integrate the
generated assets into a Flask application structure.
"""
import re, os, pathlib, shutil, json
from os import listdir
from os.path import isfile, join
from typing import Dict
print("π Initializing URL pattern and transformation functions...")
PATTERN = re.compile(
r'(?P<prefix>\b(?:href|src)\s*=\s*|module_or_path\s*:\s*|from\s+)(?P<quote>["\']?)(?P<url>/[^"\'\s>]+?\.(?P<ext>css|js|wasm)(?:\?[^#"\']*)?(?:#[^"\']*)?)(?P=quote)',
flags=re.IGNORECASE,
)
filename_map: Dict[str, str] = {}
def clean_filename(filename: str) -> str:
"""Removes the hash from a filename (e.g., 'ui-a1b2c3d4.js' -> 'ui.js')."""
if "-" not in filename:
return filename
name, ext = os.path.splitext(filename)
parts = name.split("-")
return f"{parts[0]}{ext}"
def transform_url(url: str) -> str:
if url.startswith("//") or url.startswith("http://") or url.startswith("https://"):
return url
if url.startswith("/static/"):
return url
if url.startswith("/snippets/"):
return f"/static/scripts{url}"
path = url
frag = ""
if "#" in path:
path, frag = path.split("#", 1)
frag = "#" + frag
q = ""
if "?" in path:
path, q = path.split("?", 1)
q = "?" + q
basename = os.path.basename(path)
# Use the new, clean filename if it's in our map
if basename in filename_map:
basename = filename_map[basename]
ext = basename.split(".")[-1].lower()
if ext == "css":
new = f"/static/styles/{basename}{q}{frag}"
else:
new = f"/static/scripts/{basename}{q}{frag}"
return new
def replacer(match: re.Match) -> str:
prefix = match.group("prefix")
quote = match.group("quote")
url = match.group("url")
new = transform_url(url)
if new == url:
return match.group(0)
return f"{prefix}{quote}{new}{quote}"
def process_text_urls(text: str) -> str:
return PATTERN.sub(replacer, text)
def process_template_file(path: str, inplace: bool = True) -> None:
p = pathlib.Path(path)
print(f"π Processing file: {path}")
text = p.read_text(encoding="utf-8")
# Specific replacement for API connection
text = text.replace(
"let a=`main`;const b=new URLSearchParams(window.location.search);const c=b.get(`tree`);if(c){console.log(`Tree parameter from URL:`,c);a=c};initConnection(`http://127.0.0.1:5000/`+ a)",
"initConnection('/api/tree/{{tree_id}}')",
)
text = text.replace(
"additionalHeaders={}", 'additionalHeaders={"X-CSRFToken":"{{csrf_token()}}"}'
)
integrity_pattern = r'\s+integrity\s*=\s*(?:(["\']).*?\1|[^\s>]+)'
text = re.sub(integrity_pattern, "", text, flags=re.IGNORECASE)
# First, replace hashed filenames with clean ones
for old, new in filename_map.items():
print(old, new)
text = text.replace(old, new)
new_text = process_text_urls(text)
if new_text != text:
if inplace:
p.write_text(new_text.replace("\n", ""), encoding="utf-8")
print(f"β Modified: {path}")
else:
print(f"- No changes: {path}")
print("\nπ Organizing static and template files...")
dist_path = "dist"
static_path = "static"
if os.path.exists(static_path):
print(f"ποΈ Removing existing '{static_path}' directory...")
shutil.rmtree(static_path)
shutil.move(os.path.join(dist_path, "static"), static_path)
print(f"π Moved '{os.path.join(dist_path, 'static')}' to '{static_path}'")
for folder in [
os.path.join(static_path, "scripts"),
os.path.join(static_path, "styles"),
"templates",
]:
if not os.path.exists(folder):
os.makedirs(folder)
print(f"π Created directory: {folder}")
dist_files = [f for f in listdir(dist_path) if isfile(join(dist_path, f))]
for filename in dist_files:
new_filename = clean_filename(filename)
filename_map[filename] = new_filename
source = os.path.join(dist_path, filename)
if filename.endswith(".js") or filename.endswith(".wasm"):
destination = os.path.join(static_path, "scripts", new_filename)
shutil.move(source, destination)
print(f"π₯ Moved and renamed JS/WASM: {filename} -> {new_filename}")
elif filename.endswith(".css"):
destination = os.path.join(static_path, "styles", new_filename)
shutil.move(source, destination)
print(f"π₯ Moved and renamed CSS: {filename} -> {new_filename}")
elif filename.endswith(".html"):
shutil.move(source, "templates/" + filename)
print(f"π₯ Moved HTML file: {filename}")
shutil.move(
os.path.join(dist_path, "snippets/"), os.path.join(static_path, "scripts/snippets/")
)
print("ποΈ Removing empty 'dist' directory...")
shutil.rmtree(dist_path)
print("π§ Processing main template file...")
process_template_file("templates/index.html")
print("π Minifying locale files...")
mypath = "static/locales"
onlylang = [f for f in listdir(mypath) if isfile(join(mypath, f))]
for file in onlylang:
with open(f"static/locales/{file}", "r+", encoding="utf-8") as f:
content = json.load(f)
f.seek(0)
f.truncate()
json.dump(content, f, separators=(",", ":"))
print(f"β Minified locale file: {file}")
print("β¨ All tasks completed successfully!")
# --- START OF CORRECTED BLOCK ---
print("\n--- Finalization step of the migration ---")
confirmation = input(
"Do you want to finalize the migration (copy 'internal' files and rename 'index.html')? (y/n): "
)
if confirmation.lower().strip() in ["o", "oui", "y", "yes"]:
print("\nβ
Confirmation received. Executing final steps...")
# 1. Copy each file from the 'internal' folder to its destination
source_dir = "internal"
dest_base_dir = "static"
if os.path.isdir(source_dir):
print(f"\nπ Copying files from folder '{source_dir}' to '{dest_base_dir}'...")
# os.walk iterates through the source folder structure
for root, dirs, files in os.walk(source_dir):
for filename in files:
# Build the full path of the source file
source_file = os.path.join(root, filename)
# Create the destination path preserving the structure
# Example: 'internal/styles/style.css' -> 'static/styles/style.css'
relative_path = os.path.relpath(source_file, source_dir)
dest_file = os.path.join(dest_base_dir, relative_path)
# Ensure the destination directory exists
dest_folder = os.path.dirname(dest_file)
os.makedirs(dest_folder, exist_ok=True)
if "locales" in root:
with open(source_file, "r", encoding="utf-8") as f:
translations = json.load(f)
with open(dest_file, "r+", encoding="utf-8") as f:
existing_translations = json.load(f)
existing_translations.update(translations)
f.seek(0)
f.truncate()
json.dump(existing_translations, f, separators=(",", ":"))
print(f" -> Merged and minified {source_file} to {dest_file}")
continue # Skip file copy for locales as we merge
# Copy the file
shutil.copy2(source_file, dest_file)
print(f" -> Copied {source_file} to {dest_file}")
print("β Copying of 'internal' files completed.")
else:
print(
f"β οΈ Warning: Directory '{source_dir}' not found. Skipping file copy step."
)
# 2. Rename the templates/index.html file to templates/tree.html
source_html = "templates/index.html"
dest_html = "templates/tree.html"
print(f"\nπ Renaming '{source_html}' to '{dest_html}'...")
if os.path.exists(source_html):
# Ensure destination is removed if it exists to prevent error on some systems
if os.path.exists(dest_html):
os.remove(dest_html)
os.rename(source_html, dest_html)
print("β File renamed successfully.")
else:
print(f"β οΈ Warning: File '{source_html}' not found, rename skipped.")
print("\nπ Final migration completed successfully!")
else:
print("\nβ Operation cancelled by user. No final modifications were made.")