Skip to content

Commit dd7a285

Browse files
copy_files.py: copy to temp directory first
1 parent c520a5b commit dd7a285

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

migrate/oldwiki/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ output/
22
cache/
33
images/
44
logs/
5+
temp_copy/
56
__pycache__/

migrate/oldwiki/copy_files.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,58 @@
66
OUTPUT_FUNCTIONS = "../../functions"
77
OUTPUT_EVENTS = "../../events"
88

9+
# Set this to True to copy to a temporary directory first
10+
# Then you can manually review the copied files before moving them to the final destination
11+
COPY_TO_TEMP_DIR = True
12+
TEMP_DIR = "./temp_copy"
13+
914
def copy_files(page_type, source_dir, target_dir):
15+
16+
copied_function_names = set()
17+
# First copy YAML files
1018
for root, dirs, files in os.walk(source_dir):
1119
for file in files:
1220
if file.endswith(".yaml"):
1321
src_path = os.path.join(root, file)
1422
rel_path = os.path.relpath(src_path, source_dir)
1523
dest_path = os.path.join(target_dir, rel_path)
24+
copy_to_dest_path = os.path.join(TEMP_DIR, page_type, rel_path) if COPY_TO_TEMP_DIR else dest_path
1625

17-
if not os.path.exists(dest_path):
18-
# Don't copy because it doesn't exist in the output
19-
print(f"(YAML) Skipping {dest_path} because it doesn't exist in the output")
20-
continue
21-
else:
26+
if os.path.exists(dest_path):
2227
with open(dest_path, 'r', encoding='utf-8') as dest_file:
2328
content = dest_file.read()
2429
if 'incomplete: true' not in content:
25-
print(f"(YAML) Skipping {dest_path} because it's not marked as incomplete")
30+
print(f"(YAML) Skipping {dest_path} because it is not incomplete")
2631
continue
2732

28-
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
29-
print(f"(YAML) Copying {src_path} to {dest_path}")
33+
os.makedirs(os.path.dirname(copy_to_dest_path), exist_ok=True)
34+
print(f"(YAML) Copying {src_path} to {copy_to_dest_path}")
3035
with open(src_path, 'r', encoding='utf-8') as src_file:
31-
with open(dest_path, 'w', encoding='utf-8') as dest_file:
36+
with open(copy_to_dest_path, 'w', encoding='utf-8') as dest_file:
3237
src_file_content = src_file.read()
3338
# Hack: replace name: y with name: "y"
3439
# because that's a YAML reserved keyword xD
3540
src_file_content = src_file_content.replace("name: y", 'name: "y"')
3641
dest_file.write(src_file_content)
42+
copied_function_names.add(file[:-5]) # Remove .yaml extension
3743

38-
elif file.endswith(".lua"):
39-
# Copy all .lua files too
44+
# Copy corresponding .lua files too
45+
for root, dirs, files in os.walk(source_dir):
46+
for file in files:
47+
if file.endswith(".lua"):
48+
# Ignore if we haven't copied the corresponding YAML file
49+
function_name = file[:-4] # Remove .lua extension
50+
if function_name not in copied_function_names:
51+
print(f"(Lua) Skipping {file} because corresponding YAML was not copied")
52+
continue
4053
src_path = os.path.join(root, file)
4154
rel_path = os.path.relpath(src_path, source_dir)
4255
dest_path = os.path.join(target_dir, rel_path)
56+
copy_to_dest_path = os.path.join(TEMP_DIR, page_type, rel_path) if COPY_TO_TEMP_DIR else dest_path
4357
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
44-
print(f"(Lua) Copying {src_path} to {dest_path}")
58+
print(f"(Lua) Copying {src_path} to {copy_to_dest_path}")
4559
with open(src_path, 'r', encoding='utf-8') as src_file:
46-
with open(dest_path, 'w', encoding='utf-8') as dest_file:
60+
with open(copy_to_dest_path, 'w', encoding='utf-8') as dest_file:
4761
dest_file.write(src_file.read())
4862

4963
if __name__ == "__main__":

0 commit comments

Comments
 (0)