-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPMHelper.py
More file actions
168 lines (123 loc) · 6.2 KB
/
Copy pathAPMHelper.py
File metadata and controls
168 lines (123 loc) · 6.2 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
from pathlib import Path
import shutil
from FileIO.FileUtility import *
from Base.AgoraSDKInfo import *
class APMHelper:
__instance = None
__initialized = False
__Args = None
__sdkinfo = None
def __new__(cls, *args, **kwargs):
if not cls.__instance:
cls.__instance = super().__new__(cls, *args, **kwargs)
cls.__instance.__initialized = False
return cls.__instance
def __init__(self) -> None:
if not self.__initialized:
super().__init__()
## Init
self.__initialized = True
def Init(self,Args):
self.__Args = Args
self.__sdkinfo = AgoraSDKInfo(Args.agorasdk,
Args.sdkisaudioonly,
Args.agorasdktype,
Args.agorapluginver,
Args.pluginname,
Args.agorasdkbuildconfig,
Args.pluginatlasname)
def Get():
return APMHelper()
def GetSDKInfo(self):
return self.__sdkinfo
def replace_in_file(self,file_path, content_replacements):
content = file_path.read_text(encoding='utf-8')
for old, new in content_replacements.items():
content = content.replace(old, new)
file_path.write_text(content, encoding='utf-8')
def rename_file(self,file_path, name_replacements):
new_filename = file_path.name
## replace [filename] if matches
for old, new in name_replacements.items():
new_filename = new_filename.replace(old, new)
## if [filename] replaced, return new [filepath]
if new_filename != file_path.name:
new_file_path = file_path.with_name(new_filename)
file_path.rename(new_file_path)
return new_file_path
return file_path
def rename_directory(self,directory, name_replacements):
new_directory_name = directory.name
for old, new in name_replacements.items():
new_directory_name = new_directory_name.replace(old, new)
if new_directory_name != directory.name:
new_directory_path = directory.parent / new_directory_name
directory.rename(new_directory_path)
return new_directory_path
return directory
def should_exclude(self,item, exclude_paths):
## 1. covert [item] to the absolute path
## 2. covert [excluded] to the absolute path
## 3. check if [item] is a subpath of [excluded]
## 4. at any time the result is true, it would return true
return any(Path(item).resolve().is_relative_to(Path(excluded).resolve()) for excluded in exclude_paths)
def process_files_and_directories(self,directory, extensions, content_replacements, name_replacements, exclude_paths):
# loop over files:
## rglob [DFS]: so the parent would be replaced first
## Ex.
## Src: AgoraPlugin/Source/AgoraPlugin/Public
## should be:AgoraVoicePlugin/Source/[AgoraVoicePlugin]/Plugin
for item in directory.rglob('*'):
if item.is_dir():
if self.should_exclude(item, exclude_paths):
PrintLog(f"Exclude path {item}")
continue
## task01: rename the dir
renamed_directory = self.rename_directory(item, name_replacements)
## process all files in the currrent path:
for file_path in renamed_directory.glob('*'):
if file_path.is_file() and any(file_path.suffix == ext for ext in extensions):
## task01: rename the file
new_file_path = self.rename_file(file_path, name_replacements)
## task02: replace the content
self.replace_in_file(new_file_path, content_replacements)
elif item.is_file() and any(item.suffix == ext for ext in extensions):
if self.should_exclude(item, exclude_paths):
PrintLog(f"Exclude file path {item}")
continue
## task01: rename the file
new_file_path = self.rename_file(item, name_replacements)
## task02: replace the content
self.replace_in_file(new_file_path, content_replacements)
def CopyDirWithContentReplaced(
self,
## Ex.
## path_src_dir = Path('/Users/admin/Documents/PluginWorkDir/Agora-Unreal-RTC-SDK/Agora-Unreal-SDK-CPP/AgoraPlugin')
## path_dst_dir = Path('/Users/admin/Documents/PluginWorkDir/Agora-Unreal-RTC-SDK/Agora-Unreal-SDK-CPP/AgoraVoicePlugin')
path_src_dir,
path_dst_dir,
# pattern01: [extensions]: what kind of files need to be checked
extensions = ['.cpp', '.h', '.cs', '.uplugin'],
# pattern02: [content_replacements]: [old , new] replace the old one with the new one
content_replacements = {
'AgoraPlugin': 'AgoraVoicePlugin',
'AGORAPLUGIN_API': 'AGORAVOICEPLUGIN_API',
'FAgoraPluginModule': 'FAgoraVoicePluginModule'
},
# pattern03: [name_replacements] [old , new] replace the old one with the new one
name_replacements = {
'AgoraPlugin': 'AgoraVoicePlugin' # 示例
},
## Pattern04 [exclude_paths] : [already_replaced_path]
# Ex.
## Src: AgoraPlugin/Source/AgoraPlugin/Public
## should be:AgoraVoicePlugin/Source/[AgoraVoicePlugin]/Plugin
## Ex.
# exclude_path01 = path_dst_dir / "Source/AgoraVoicePlugin/Public/AgoraCppPlugin/include"
# exclude_paths = [exclude_path01]
exclude_paths = []
):
if path_dst_dir.exists():
FileUtility.DeleteDir(path_dst_dir)
FileUtility.CopyDir(path_src_dir,path_dst_dir,True,bmac_use_shutil = True)
self.process_files_and_directories(path_dst_dir, extensions, content_replacements, name_replacements, exclude_paths)