Skip to content

Instantly share code, notes, and snippets.

@alexivaner
Last active March 29, 2023 09:07
Show Gist options
  • Save alexivaner/b1f0938230bbd0e700a1d0a72ee43489 to your computer and use it in GitHub Desktop.
Save alexivaner/b1f0938230bbd0e700a1d0a72ee43489 to your computer and use it in GitHub Desktop.
This code is for default package name of webrtc to custom package name
import os
import re
import argparse
import multiprocessing
def process_files(folder_path, old_str, new_str):
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
replace_in_files(file_path, old_str, new_str)
def process_files_parallel(folder_path, old_str, new_str):
pool = multiprocessing.Pool()
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".txt"):
file_path = os.path.join(root, file)
pool.apply_async(replace_in_files, args=(file_path, old_str, new_str))
pool.close()
pool.join()
def replace_in_files(file_path, old_str, new_str):
try:
with open(file_path, 'r', encoding='utf-8') as file:
# print(f"Processing: Processing {file_path}....")
content = file.read()
# Use regular expression to match whole words and case
# pattern = r"\b{}\b".format(old_str)
regex = re.compile(old_str)
if regex.search(content):
# Replace old_str with new_str in the file content
new_content = regex.sub(new_str, content)
with open(file_path, 'w', encoding='utf-8') as file:
file.write(new_content)
print(f"SUCCESS: Replace {file_path}....")
except UnicodeDecodeError:
print(f"UnicodeDecodeError Error: Skipping {file_path}...")
pass
except FileNotFoundError:
print(f"File not found Error: Skipping...")
pass
def rename_folders(folder_path, old_str, new_str, parallel=False):
if parallel:
pool = multiprocessing.Pool()
for root, dirs, files in os.walk(folder_path, topdown=False):
for folder in dirs:
# if (folder == "out" or folder =="sdk" or folder=="rtc_base"):
if (old_str in folder):
# print(f"Proscessing: Processing {folder}....")
# Use regular expression to match whole words and case
pattern = r"\b{}\b".format(old_str)
regex = re.compile(pattern)
if regex.search(folder):
# Replace old_str with new_str in the directory name
new_dirname = regex.sub(new_str, folder)
old_path = os.path.join(root, folder)
new_path = os.path.join(root, new_dirname)
print(f"SUCCESS: Rename {old_path} to {new_path}")
pool.apply_async(os.rename, args=(old_path, new_path))
pool.close()
pool.join()
else:
for root, dirs, files in os.walk(folder_path, topdown=False):
for folder in dirs:
if old_str in folder:
# Use regular expression to match whole words and case
pattern = r"\b{}\b".format(old_str)
regex = re.compile(pattern)
if regex.search(folder):
# Replace old_str with new_str in the directory name
new_dirname = regex.sub(new_str, folder)
old_path = os.path.join(root, folder)
new_path = os.path.join(root, new_dirname)
os.rename(old_path, new_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Replace all instances of a string in all files in a directory.")
parser.add_argument("root_dir", help="The root directory to search for files.")
# parser.add_argument("old_str", help="The string to replace.")
parser.add_argument("new_str", help="The new string to replace with.")
parser.add_argument("--parallel", action="store_true", help="Process files in parallel.")
args = parser.parse_args()
# how to use
# python change_webrtc_name.py src/ imedia --parallel
if args.parallel:
# rename_folders(args.root_dir, "webrtc", args.new_str+"webrtc", True)
rename_folders("src/sdk", "webrtc", args.new_str+"webrtc", True)
rename_folders("src/out", "webrtc", args.new_str+"webrtc", True)
rename_folders("src/rtc_base", "webrtc", args.new_str+"webrtc", True)
rename_folders("src/modules", "webrtc", args.new_str+"webrtc", True)
#Change back to original
# rename_folders(args.root_dir, "imediawebrtc", "webrtc", True)
else:
# rename_folders(args.root_dir, "webrtc", args.new_str+"webrtc")
rename_folders("src/sdk", "webrtc", args.new_str+"webrtc")
rename_folders("src/out", "webrtc", args.new_str+"webrtc")
rename_folders("src/rtc_base", "webrtc", args.new_str+"webrtc")
rename_folders("src/modules", "webrtc", args.new_str+"webrtc", True)
#Change back to original
# rename_folders(args.root_dir, "imediawebrtc", "webrtc", True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment