-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmass_string_replacer2.7.py
195 lines (152 loc) · 6.08 KB
/
mass_string_replacer2.7.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
import yaml_27
import random
import os
import re
import sys
from shutil import copyfile
import glob
class RandomString:
source = ""
length = 0
once = False
myResult = ""
@staticmethod
def fromDict(d ):
result = RandomString();
result.source = d.get('source',"");
result.length = d.get('length', 0);
result.once = d.get('remember', False);
return result;
def newRandom(self):
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= ""
# Per-file replacement
text = {}
regex = {}
# Refrences:
vars = []
randoms = []
@staticmethod
def fromDict(d):
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 = None
randoms = {}
vars = {}
actions = []
@staticmethod
def fromDict(d) :
result = Config();
result.vars = d.get('vars', {});
result.randomseed = d.get('randomseed', None);
random.seed(result.randomseed);
result.randoms = {};
randomDict = 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):
with open(path, 'r') as stream:
try:
return yaml_27.safe_load(stream);
except yaml_27.YAMLError as ex:
print(ex);
return None;
def replaceString(text, src , dst ) :
return text.replace(src, dst)
def textMatchCount(text , src ):
return text.count(src);
def replaceRegex(text, regex , dst ) :
return re.sub(regex, dst,text)
def regexMatchCount(text , regex ) :
return len(re.findall(regex,text));
def processYAML(config ):
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 file '" + file_path_item + "'")
with open(readPath, 'r') as file:
fileText = file.read()
# Simple text replace:
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:
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:
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 = "_ ` - _ ` - ` _ ` - ` _ ` - ` _ ` - ` _ ` - ` _ ` - ` _ ` -"
envValue = 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:
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 ): # log path
if not silent:
print("[*] " + s);
def d(s , prefix = "-"): # log detail
if not silent:
print("\t" + prefix + " " + s);
config = Config.fromDict(loadYAML(sys.argv[1]));
dryRun = not "--wet" in sys.argv
silent = "--silent" in sys.argv
usebackups = "--backup" in sys.argv and not dryRun
processYAML(config);