-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplacer.py
92 lines (72 loc) · 2.5 KB
/
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
#
# Vidizayn
#
import os
def getEnvironmentCode(folder, prodFile, devFile, prodBranch, currentBranch):
"""Replaceses placeholders in files from MAPPING dictionary.
Args:
folder: root folder of given files
prodFile: file to use if current branch is prod
devFile: ile to use if current branch is dev
prodBranch: prod branch
currentBranch: current branch
Returns:
String
"""
fileToRead = prodFile if prodBranch == currentBranch else devFile
fileToRead = folder + '/' + fileToRead
if os.path.isfile(fileToRead):
with open(fileToRead, 'r', encoding='utf8') as f :
return f.read()
else:
return ''
def replaceWithMapping(filePath):
"""Replaceses files placeholders from MAPPING dictionary.
Args:
filePath: file to replace.
Returns:
void
"""
global MAPPING
with open(filePath, 'r', encoding='utf8') as file :
fileContent = file.read()
print('File: ' + filePath)
willReplace = False
for textToFind in MAPPING:
if fileContent.find(textToFind) > -1:
willReplace = True
fileContent = fileContent.replace(textToFind, MAPPING[textToFind])
print('Replaced: ' + textToFind)
else:
print('Not found: ' + textToFind)
if willReplace :
with open(filePath, 'w', encoding='utf8') as f:
f.write(fileContent)
def searchFileByExtension(folderPath, ext):
"""Searches file by extension in given path recursively.
Args:
folderPath: root folder to search files.
ext: file extension
Returns:
void
"""
for f in os.listdir(folderPath):
filePath = os.path.join(folderPath, f)
if f.endswith(ext):
replaceWithMapping(filePath)
elif os.path.isdir(filePath):
searchFileByExtension(filePath, ext)
CURRENT_PATH = os.getcwd();
PUBLIC_PATH = CURRENT_PATH + '/public'
ANALYTICS_PATH = CURRENT_PATH + '/analytics'
PRODUCTION_BRANCH = 'live'
CURRENT_BRACNH = os.environ.get('TRAVIS_BRANCH')
GA_CODE = getEnvironmentCode(ANALYTICS_PATH, 'ga_prod.txt', 'ga_dev.txt', PRODUCTION_BRANCH, CURRENT_BRACNH)
OM_CODE = getEnvironmentCode(ANALYTICS_PATH, 'om_prod.txt', 'om_dev.txt', PRODUCTION_BRANCH, CURRENT_BRACNH)
MAPPING = {
'<!-- {{ GA_CODE }} -->': GA_CODE,
'<!-- {{ OM_CODE }} -->': OM_CODE
}
print( CURRENT_BRACNH )
searchFileByExtension(PUBLIC_PATH, '.html')