@@ -35,26 +35,16 @@ def get_branch_files(branch):
3535 files = result .stdout .decode ().splitlines ()
3636 return set (files )
3737
38- def delete_unique_files (branch ):
38+ def get_unused_files (branch ):
3939 """Delete files that are unique to branch2."""
4040 # Get the files in each branch
41- files_branch1 = get_branch_files (MASTER_BRANCH )
42- files_branch2 = get_branch_files (branch )
41+ files_branch_master = get_branch_files (MASTER_BRANCH )
42+ files_branch_lang = get_branch_files (branch )
4343
4444 # Find the files that are in branch2 but not in branch1
45- unique_files = files_branch2 - files_branch1
45+ unique_files = files_branch_lang - files_branch_master
4646
47- if unique_files :
48- # Switch to the second branch
49- subprocess .run (["git" , "checkout" , branch ])
50-
51- # Delete the unique files from the second branch
52- for file in unique_files :
53- subprocess .run (["git" , "rm" , file ])
54-
55- subprocess .run (["git" , "checkout" , MASTER_BRANCH ])
56-
57- print (f"[+] Deleted { len (unique_files )} files from branch: { branch } " )
47+ return unique_files
5848
5949
6050def cp_translation_to_repo_dir_and_check_gh_branch (branch , temp_folder , translate_files ):
@@ -64,6 +54,14 @@ def cp_translation_to_repo_dir_and_check_gh_branch(branch, temp_folder, translat
6454 subprocess .run (['git' , 'checkout' , '-b' , branch ])
6555 else :
6656 subprocess .run (['git' , 'checkout' , branch ])
57+
58+ # Get files to delete
59+ files_to_delete = get_unused_files (branch )
60+
61+ # Delete files
62+ for file in files_to_delete :
63+ os .remove (file )
64+ print (f"[+] Deleted { file } " )
6765
6866 # Walk through source directory
6967 for dirpath , dirnames , filenames in os .walk (temp_folder ):
@@ -80,15 +78,12 @@ def cp_translation_to_repo_dir_and_check_gh_branch(branch, temp_folder, translat
8078 shutil .copy2 (src_file , dest_path )
8179 if not "/images/" in src_file :
8280 print (f"[+] Copied from { src_file } to { file_name } " )
83-
84- print (f"Translated files copied to branch: { branch } " )
8581
8682 if translate_files :
8783 commit_and_push (translate_files , branch )
8884 else :
8985 print ("No commiting anything, leaving in language branch" )
9086
91-
9287def commit_and_push (translate_files , branch ):
9388 # Define the commands we want to run
9489 commands = [
@@ -239,8 +234,7 @@ def split_text(text, model):
239234 return chunks
240235
241236
242- def copy_dirs (source_path , dest_path ):
243- folder_names = ["theme/" , "src/images/" ]
237+ def copy_dirs (source_path , dest_path , folder_names ):
244238 for folder_name in folder_names :
245239 source_folder = os .path .join (source_path , folder_name )
246240 destination_folder = os .path .join (dest_path , folder_name )
@@ -407,7 +401,7 @@ def translate_directory(language, source_path, dest_path, model, num_threads, cl
407401 print (f'Translation generated an exception: { exc } ' )
408402
409403 # Delete possibly removed files from the master branch
410- delete_unique_files (branch )
404+ delete_unused_files (branch )
411405
412406 #elif args.directory:
413407 # Translate everything
@@ -421,7 +415,8 @@ def translate_directory(language, source_path, dest_path, model, num_threads, cl
421415 copy_files (source_folder , dest_folder )
422416
423417 # Copy .gitbook folder
424- copy_dirs (source_folder , dest_folder )
418+ folder_names = ["theme/" , "src/images/" ]
419+ copy_dirs (source_folder , dest_folder , folder_names )
425420
426421 # Create the branch and copy the translated files
427422 cp_translation_to_repo_dir_and_check_gh_branch (branch , dest_folder , translate_files )
0 commit comments