forked from godotengine/godot-docs
-
Notifications
You must be signed in to change notification settings - Fork 16
/
migrate.py
380 lines (337 loc) · 15.9 KB
/
migrate.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
"""
## Migrate files from Godot to Redot
usage: Migrate [-h] [-e] [-t] [-v] input output
Simple file migrator. Uses str.replace to map from Godot to Redot. Also converts some filenames.
positional arguments:
input Input directory relative to current
output Output directory relative to current
options:
-h, --help show this help message and exit
-e, --extended Include unimplemented substitutions, don't use in production
-t, --tiny Exclude classes directory
-v, --verbose
Will replace specific godot strings with redot. It tries to ignore external projects and other things that shouldn't
change.
A distinction is made between unimplemented instances of the godot keyword (for instance references to the main
website), and implemented ones (like references to github repositories that are already compatible).
The idea is that, as the project is being migrated to the new name, the unimplemented mappings will gradually move
to the 'normal' mappings. This way the docs can grow along with the project, keeping broken links to a minimum. When
all is done, migrate.py has served its purpose and can be deleted.
It will recursively traverse all directories, targeting .rst and .md files. It will convert the text and filename
if necessary, and then save them to the output directory (default _migrated)
From there, the docs can be built in the normal way.
"""
import argparse
import fnmatch
import os
import re
from shutil import copyfile
import shutil
import sys
import codecs
from distutils.dir_util import copy_tree
defaultInputDirectory = '.'
defaultOutputDirectory = '_migrated'
defaultIncludeUnimplemented = False
defaultIgnoreClasses = True
defaultVerbose = False
encoding = 'utf-8'
filename_masks = ['.rst', '.md']
# Mappings that will currently lead to nowhere. Can be treated as a todo list.
mappings_unimplemented = [
# Non existing urls
('https://hosted.weblate.org/projects/godot-engine/godot-docs', 'https://hosted.weblate.org/projects/redot-engine/redot-docs'),
('https://hosted.weblate.org/engage/godot-engine/', 'https://hosted.weblate.org/engage/redot-engine/'),
('https://store.steampowered.com/app/404790/Godot_Engine/', 'https://store.steampowered.com/app/TODO'),
('https://flathub.org/apps/details/org.godotengine.Godot', 'https://flathub.org/apps/details/org.redot-engine.Redot'),
('https://godot.foundation', 'https://redot.foundation'),
('https://hosted.weblate.org/projects/godot-engine/godot/', 'https://hosted.weblate.org/projects/redot-engine/redot/'),
('https://hosted.weblate.org/projects/godot-engine/', 'https://hosted.weblate.org/projects/redot-engine/'),
('https://hosted.weblate.org/browse/godot-engine', 'https://hosted.weblate.org/browse/redot-engine'),
('https://repo1.maven.org/maven2/org/godotengine/godot/', 'https://repo1.maven.org/maven2/org/redot-engine/redot/'),
# The following mappings probably require changes to the core engine
('GodotEngine.epub', 'RedotEngine.epub'),
('godotengine.org/license', 'redotengine.org/license'),
('AsGodotDictionary', 'AsRedotDictionary'),
('GODOT_', 'REDOT_'),
('-godot-', '-redot-'),
('project.godot', 'project.redot'),
('Godot.Collections', 'Redot.Collections'),
('"Godot"', '"Redot"'),
('.godot/', '.redot/'),
('.godot.', '.redot.'),
('APPDATA%\\Godot\\', 'APPDATA%\\Redot\\'),
('AppData%\\Godot\\', 'AppData%\\Redot\\'),
('Caches/Godot/', 'Caches/Redot/'),
('cache/godot/', 'cache/redot/'),
('Support/Godot/', 'Support/Redot/'),
('config/godot/', 'config/redot/'),
('share/godot/', 'share/redot/'),
('org.godotengine.Godot', 'org.redotengine.Redot'),
('godot-ios-plugins', 'redot-ios-plugins'),
('godot-syntax-themes', 'redot-syntax-themes'),
('godot_skin', 'redot_skin'),
('godot_scene_node', 'redot_scene_node'),
('``godotengine/godot', '``redot-engine/redot'),
('>/Godot/', '>/Redot/'),
('``.godot``', '``.redot``'),
('``godot``', '``redot``'),
('/godot.', '/redot.'),
('GodotPhysics', 'RedotPhysics'),
('AsGodotObject', 'AsRedotObject'),
('non-Godot', 'non-Redot'),
('Godot-', 'Redot-'),
('libgodot', 'libredot'),
('godot.linuxbsd', 'redot.linuxbsd'),
('Godot.app', 'Redot.app'),
('MacOS/Godot', 'MacOS/Redot'),
('C:\\godot', 'C:\\redot'),
('GodotSharp', 'RedotSharp'),
('godot.gdkey', 'redot.gdkey'),
('godot-nir', 'redot-nir'),
('godot-angle', 'redot-angle'),
('godot-binary', 'redot-binary'),
('``Godot', '``Redot'),
('godot/modules', 'redot/modules'),
('godot_binary', 'redot_binary'),
('godotengine.org', 'redotengine.org'),
('godot-source', 'redot-source'),
('godot/bin', 'redot/bin'),
('gdb godot', 'gdb redot'),
('GODOT', 'REDOT'),
('USERNAME/godot', 'USERNAME/redot'),
('godot-xr', 'redot-xr'),
('godotisawesome', 'redotisawesome'),
('godot-cpp', 'redot-cpp'),
('GodotCPP', 'RedotCPP'),
('namespace godot', 'namespace redot'),
('godot_cpp', 'redot_cpp'),
('GodotObject', 'RedotObject'),
('GodotBot', 'RedotBot'),
('GodotPlugin', 'RedotPlugin'),
('org.godotengine', 'org.redotengine'),
('/godot>', '/redot>'),
('repos/godotengine/godot', 'repos/redot-engine/redot'),
('godot demo', 'redot demo'),
('godotsharp', 'redotsharp'),
]
# Mappings that should work on first migration
mappings = [
# These will have to change eventually.
('https://nightly.link/godotengine/godot-docs/workflows/build_offline_docs/master/godot-docs-html-stable.zip', 'https://download.redotengine.org/docs/redot-docs-html-stable.zip'),
('https://nightly.link/godotengine/godot-docs/workflows/build_offline_docs/master/godot-docs-html-master.zip', 'https://download.redotengine.org/docs/redot-docs-html-master.zip'),
('https://nightly.link/godotengine/godot-docs/workflows/build_offline_docs/master/godot-docs-epub-stable.zip', 'https://download.redotengine.org/docs/redot-docs-epub-stable.zip'),
('https://nightly.link/godotengine/godot-docs/workflows/build_offline_docs/master/godot-docs-epub-master.zip', 'https://download.redotengine.org/docs/redot-docs-epub-master.zip'),
# Table breakers
('| ``"Please include this when reporting the bug on: https://github.com/godotengine/godot/issues"`` |', '| ``"Please include this when reporting the bug on: https://github.com/redot-engine/godot/issues"``|'),
('https://github.com/godotengine/godot/pull/40364>`_ for more. |', 'https://github.com/redot-engine/redot/pull/40364>`_ for more. |'),
# Non existing internal urls
('https://chat.godotengine.org/', 'https://www.redotengine.org/'),
('https://editor.godotengine.org', 'https://www.redotengine.org'),
('https://forum.godotengine.org/', 'https://www.redotengine.org/'),
('https://fund.godotengine.org', 'https://www.redotengine.org'),
# Almost existing urls
('https://docs.godotengine.org/', '/'),
('https://docs.godotengine.org', 'https://docs.redotengine.org'),
('https://godotengine.org', 'https://redotengine.org'),
# Existing urls
('https://godotengine.org/community', 'https://www.redotengine.org'),
('https://nightly.link/godotengine/godot-docs/workflows/build_offline_docs/master/godot', 'https://nightly.link/redot-engine/redot-docs/workflows/build_offline_docs/master/redot'),
('https://github.com/godotengine/godot-docs/issues', 'https://github.com/redot-engine/redot-docs/issues'),
('https://github.com/godotengine/godot/blob/master', 'https://github.com/redot-engine/redot-engine/blob/master'),
('https://raw.githubusercontent.com/godotengine/godot/master', 'https://raw.githubusercontent.com/redot-engine/redot-engine/master'),
('https://github.com/godotengine/godot-demo-projects', 'https://github.com/redot-engine/redot-demo-projects'),
('https://discord.gg/bdcfAYM4W9', 'https://discord.gg/redot'),
('https://github.com/godotengine/godot', 'https://github.com/redot-engine/redot-engine'),
('https://github.com/godotengine/godot-proposals', 'https://github.com/redot-engine/redot-proposals'),
('https://raw.githubusercontent.com/godotengine/godot-docs', 'https://raw.githubusercontent.com/redot-engine/redot-docs'),
('https://github.com/godotengine/', 'https://github.com/redot-engine/'),
('https://chat.godotengine.org/', 'https://discord.gg/redot'),
# Generic replacements
('GODOT_COPYRIGHT.txt', 'REDOT_COPYRIGHT.txt'),
('godotengine.org', 'redotengine.org'),
('godot-docs', 'redot-docs'),
('GODOT ENGINE', 'REDOT ENGINE'),
('/bin/godot', '/bin/redot'),
('/Applications/Godot.app', '/Applications/Redot.app'),
('highlight=Godot', 'highlight=Redot'),
('/godot_', '/redot_'),
('/godot-', '/redot-'),
('_godot_', '_redot_'),
('``godot``', '``redot``'),
('Godot ', 'Redot '),
(' Godot', ' Redot'),
(' Godot.', ' Redot.'),
(' Godot?', ' Redot?'),
('Godot\'', 'Redot\''),
('Godot,', 'Redot,'),
('Godot:', 'Redot:'),
(' godot ', ' redot '),
('\nGodot.', '\nRedot.'),
('_godot\n', '_redot\n'),
('godot.gif', 'redot.gif'),
('godot.jpg', 'redot.jpg'),
('godot.png', 'redot.png'),
('"godot_', '"redot_'),
('"godotengine"', '"redot-engine"'),
('GodotEngine', 'RedotEngine'),
('godot-giscus', 'redot-giscus'),
('"godotengine/', '"redot-engine/'),
('godot_is_latest', 'redot_is_latest'),
('godot-edit-guideline', 'redot-edit-guideline'),
('_godot_', '_redot_'),
('to_godot', 'to_redot'),
('godot.html', 'redot.html'),
('by-godot', 'by-redot'),
('RocketChat', 'Discord'),
('MadeWithGodot', 'MadeWithRedot'),
(' if on_rtd else "(DEV) "', ''),
('<span class="fa fa-book"> Read the Docs</span>', '<span class="fa fa-book"> Versions</span>'),
("const homeUrl = baseUrl.split('/latest/')[0] + '/stable/';", "const homeUrl = '/en/stable';"),
('{% set listed_languages = ({"en":"#", "de":"#", "es":"#", "fr":"#"}).items() -%}', '{% set listed_languages = ({"en":"#"}).items() -%}'),
('({"stable":"#", "latest":"#"})', '({"stable":"/en/stable", "latest":"/en/latest"})'),
('Hosted by <a href="https://readthedocs.org">Read the Docs', 'Hosted by <a href="https://cloudflare.com">CloudFlare'),
('<a href="https://docs.readthedocs.io/page/privacy-policy.html">Privacy Policy</a>', ''),
('G-dot', 'Godot'),
(' godot_', ' redot_'),
('class_godotsharp', 'class_redotsharp'),
]
filename_mappings = [
('godot', 'redot'),
]
static_dirs = [
'**/img',
'**/files',
'_extensions',
'_static',
'_styleguides',
'_templates',
'_tools',
]
alphanumeric = [
'py',
'md',
'css',
'txt',
'css',
'js',
'html',
'csv',
'rst',
]
# force stdout encoding so it won't fail on print statements
if (sys.stdout.encoding != encoding):
sys.stdout = codecs.getwriter(encoding)(sys.stdout.buffer, 'strict')
sys.stdout.encoding = encoding
def is_target(filename):
return any(filename.lower().endswith(m) for m in filename_masks)
def ensureDirExists(outputName):
dirname = os.path.dirname(outputName)
try:
os.makedirs(dirname)
except FileExistsError:
pass
def generateOutputName(root, fileName, outputDirectory):
on = os.path.join('.', outputDirectory, root, fileName)
on = convertContent(on, filename_mappings)
ensureDirExists(on)
return on
def convertContent(content, mappings):
for mapping in mappings:
search, replace = mapping
if (search != ''):
content = content.replace(search, replace)
return content
def copyFile(root, filename, outputDirectory, verbose):
inputName = os.path.join(root, filename)
outputName = generateOutputName(root, inputName.replace('.\\', '').replace('./', ''), outputDirectory)
if verbose: print(f'Copying "{inputName}" to "{outputName}"')
shutil.copyfile(inputName, outputName)
def convertFile(root, filename, outputDirectory, includeUnimplemented, verbose):
inputName = os.path.join(root, filename)
outputName = generateOutputName(root, filename, outputDirectory)
if verbose: print(f'Converting "{inputName}" to "{outputName}"')
with open(inputName, mode = 'r', encoding = encoding) as input:
data = input.read()
if (includeUnimplemented):
data = convertContent(data, mappings_unimplemented)
data = convertContent(data, mappings)
ensureDirExists(outputName)
with open(outputName, mode = 'w', encoding = encoding) as output:
output.write(data)
def copyGlobalDir(inputDirectory, inputMask, outputDirectory, verbose):
for root, dirs, files in os.walk(inputDirectory):
if (inputMask in root and outputDirectory not in root):
for f in files:
inputName = os.path.join(root, f)
outputName = generateOutputName(root, f, outputDirectory)
ensureDirExists(outputName)
if verbose: print(f"Copying {inputName} to {outputName}")
copyfile(inputName, outputName)
def convertStaticDir(inputDirectory, outputDirectory, verbose):
for root, dirs, files in os.walk(inputDirectory):
if (outputDirectory not in root and '__' not in root):
for f in files:
if (f.split('.')[1] in alphanumeric):
convertFile(root, f, outputDirectory, True, verbose)
else:
copyFile(root, f, outputDirectory, verbose)
def migrate(inputDirectory, outputDirectory, includeUnimplemented, ignoreClasses, verbose):
outputsig = os.path.join('.', outputDirectory)
for root, dirs, files in os.walk(inputDirectory):
# ignore output path
if (root.startswith(outputsig)):
continue
if (ignoreClasses and 'classes' in root):
continue
items = filter(is_target, files)
for item in items:
convertFile(root, item, outputDirectory, includeUnimplemented, verbose)
def main():
inputDir = defaultInputDirectory
outputDir = defaultOutputDirectory
includeUnimplemented = defaultIncludeUnimplemented
ignoreClasses = defaultIgnoreClasses
verbose = defaultVerbose
parser = argparse.ArgumentParser(
prog='Migrate',
description='Simple file migrator. Uses str.replace to map from Godot to Redot. Also converts some filenames.',
epilog='Done. Made by @Craptain')
parser.add_argument('input', help='Input directory relative to current')
parser.add_argument('output', help='Output directory relative to current')
parser.add_argument('-e', '--extended', action='store_true', help='Include unimplemented substitutions, don\'t use in production')
parser.add_argument('-t', '--tiny', action='store_true', help='Exclude classes directory')
parser.add_argument('-v', '--verbose', action='store_true')
args = parser.parse_args()
verbose = args.verbose
if (verbose):
print("arguments:")
print(args)
inputDir = args.input
if (args.output != '.' and not args.output.startswith('/')):
outputDir = args.output
else:
print("output can't be . or start with /")
exit(1)
includeUnimplemented = args.extended
ignoreClasses = args.tiny
if (os.path.exists(outputDir)):
print(f"Deleting {outputDir}")
shutil.rmtree(outputDir)
print("Migrating...")
migrate(inputDir, outputDir, includeUnimplemented, ignoreClasses, verbose)
print("Copying config files...")
convertFile(inputDir, 'conf.py', outputDir, includeUnimplemented, verbose)
convertFile(inputDir, 'robots.txt', outputDir, includeUnimplemented, verbose)
copyFile(inputDir, 'favicon.ico', outputDir, verbose)
print("Copying static directories...")
for dir in static_dirs:
if ('**' in dir):
if verbose: print(f"Copying dirs with mask {dir}")
copyGlobalDir(inputDir, dir.split('/')[1], outputDir, verbose)
else:
if verbose: print(f"Converting dir {dir}")
convertStaticDir(dir, outputDir, verbose)
print(parser.epilog)
if __name__ == "__main__":
main()