-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmass_string_replacer.py
198 lines (155 loc) · 6.5 KB
/
mass_string_replacer.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
import yaml
import typing
import random
import os
import re
import sys
from shutil import copyfile
import glob
class RandomString:
source : str = ""
length : int = 0
once : bool = False
myResult = ""
def fromDict(d : dict) -> 'RandomString' :
result = RandomString();
result.source = d.get('source',"");
result.length = d.get('length', 0);
result.once = d.get('remember', False);
return result;
def newRandom(self) -> str:
if self.source == "":
return ""
if self.myResult == "" or not self.once:
self.myResult = ''.join(random.choice(self.source) for i in range(self.length))
return self.myResult;
class Action:
path : str = ""
# Per-file replacement
text : dict = {}
regex: dict = {}
# Refrences:
vars : typing.List[str]
randoms : typing.List[str]
def fromDict(d: dict) -> 'Action':
result = Action();
result.path = d.get('path', "");
result.text = d.get('text', {});
result.regex = d.get('regex', {});
result.vars = d.get('vars', []);
result.randoms = d.get('randoms', []);
return result;
class Config:
randomseed : int
randoms : typing.Dict[str,RandomString]
vars : dict = {}
actions = typing.List[Action]
def fromDict(d: dict) -> 'Config':
result = Config();
result.vars = d.get('vars', {});
result.randomseed = d.get('randomseed', None);
random.seed(result.randomseed,version=2);
result.randoms = {};
randomDict : dict = d.get('randoms', {});
for key in randomDict.keys():
result.randoms[key] = RandomString.fromDict(randomDict[key]);
result.randoms[key].newRandom()
result.actions = [];
for actionObj in d.get('actions', []):
result.actions.append(Action.fromDict(actionObj));
return result;
def loadYAML(path: str):
with open(path, 'r') as stream:
try:
return yaml.safe_load(stream);
except yaml.YAMLError as ex:
print(ex);
return None;
def replaceString(text: str, src : str, dst :str) -> str:
return text.replace(src, dst)
def textMatchCount(text : str, src : str) -> int:
return text.count(src);
def replaceRegex(text: str, regex : str, dst : str) -> str:
return re.sub(regex, dst,text)
def regexMatchCount(text : str, regex : str) -> int:
return len(re.findall(regex,text));
def processYAML(config : Config):
action: Action
for action in config.actions:
p("Procesing file pattern '" + action.path + "'")
file_list = glob.glob(action.path);
for file_path_item in file_list:
if os.path.isfile(file_path_item):
readPath = file_path_item
if usebackups:
readPath = file_path_item + ".rep.backup";
if not os.path.exists(readPath):
copyfile(file_path_item, readPath)
p("Procesing single file '" + file_path_item + "'")
with open(readPath, 'r') as file:
fileText: str = file.read()
# Simple text replace:
textKey: str
simple_replace_count = 0
for textKey in action.text.keys():
simple_replace_count += textMatchCount(fileText, textKey);
fileText = replaceString(fileText, textKey, action.text[textKey]);
if simple_replace_count > 0:
d("Replaced " + str(simple_replace_count) + " simple text")
# Regex replace:
regexKey: str
regex_replace_count = 0
for regexKey in action.regex.keys():
regex_replace_count += regexMatchCount(fileText, regexKey);
fileText = replaceRegex(fileText, regexKey, action.regex[regexKey])
if regex_replace_count > 0:
d("Replaced " + str(regex_replace_count) + " regexes")
# Replace vars:
varKey: str
var_given_count = 0
var_env_count = 0
for varKey in action.vars:
if varKey in config.vars:
var_given_count += textMatchCount(fileText, "~{" + varKey + "}");
fileText = replaceString(fileText, "~{" + varKey + "}", config.vars[varKey])
else:
# Try to read it from env:
noEnv: str = "_ ` - _ ` - ` _ ` - ` _ ` - ` _ ` - ` _ ` - ` _ ` - ` _ ` -"
envValue: str = os.getenv(varKey, noEnv)
if not envValue == noEnv:
var_env_count += textMatchCount(fileText, "~{" + varKey + "}");
fileText = replaceString(fileText, "~{" + varKey + "}", envValue)
else:
d("Can't find variable/env named '" + varKey + "'","[ERR]")
if var_env_count + var_given_count > 0:
d("Replaced " + str(var_given_count) + " given var, " + str(var_env_count) + " from env.");
# Replace randoms:
randKey: str
rand_count = 0
for randKey in action.randoms:
if randKey in config.randoms:
rand_count += textMatchCount(fileText, randKey);
fileText = replaceString(fileText, "~{" + randKey + "}", config.randoms[randKey].newRandom())
else:
d("Can't find random named '" + randKey + "'", "[ERR]")
if rand_count > 0:
d("Replaced " + str(rand_count) + " randoms")
if not dryRun:
with open(file_path_item, 'w') as file:
file.truncate(0)
file.write(fileText)
else:
p("Result:\n" + fileText + "\n\n")
else:
p("Can't find file '" + file_path_item + "'")
def p(s : str): # log path
if not silent:
print("[*] " + s);
def d(s : str, prefix : str = "-"): # log detail
if not silent:
print("\t" + prefix + " " + s);
config = Config.fromDict(loadYAML(sys.argv[1]));
dryRun:bool = not "--wet" in sys.argv
silent:bool = "--silent" in sys.argv
usebackups:bool = "--backup" in sys.argv and not dryRun
processYAML(config);