-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_file_infor.py
More file actions
90 lines (80 loc) · 3.46 KB
/
update_file_infor.py
File metadata and controls
90 lines (80 loc) · 3.46 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
import fileinput
import re
class UpdateDatabaseInfo:
file_path = ''
ip_address = ''
port = ''
database = ''
username = ''
def __init__(self, file_path, ip_address, port, database, username):
self.file_path = file_path
self.ip_address = ip_address
self.port = port
self.database = database
self.username = username
def updateTomcat(self):
file_data = ''
regex = '"(.*)"'
with open(self.file_path, 'r', encoding='utf-8') as file:
for line in file:
if 'dataSource.user' in line:
line = re.sub(regex, '"' + self.username + '"', line)
elif 'dataSource.serverName' in line:
line = re.sub(regex, '"' + self.ip_address + '"', line)
elif 'dataSource.portNumber' in line:
line = re.sub(regex, '"' + self.port + '"', line)
elif 'dataSource.databaseName' in line:
line = re.sub(regex, '"' + self.database + '"', line)
file_data += line
with open(self.file_path, 'w', encoding='utf-8') as file:
file.write(file_data)
print('tomcat database info is updated')
def updateJetty(self):
file_data = ''
regex = '>(.*)<'
with open(self.file_path, 'r', encoding='utf-8') as file:
for line in file:
if 'jdbcUrl' in line:
line = re.sub(regex,
'>jdbc:postgresql://' + self.ip_address + ':' + self.port + '/' + self.database + '<',
line)
elif 'username' in line:
line = re.sub(regex, '>' + self.username + '<', line)
file_data += line
with open(self.file_path, 'w', encoding='utf-8') as file:
file.write(file_data)
print('jetty database info is updated')
def updateTask(self):
file_data = ''
regex = '=[a-z:/\d_\.]+'
with open(self.file_path, 'r', encoding='utf-8') as file:
for line in file:
if 'resource.ds1.jdbcUrl' in line:
if '#' not in line:
print(
'new value:' + '=jdbc:postgresql://' + self.ip_address + ':' + self.port + '/' + self.database)
line = re.sub(regex,
'=jdbc:postgresql://' + self.ip_address + ':' + self.port + '/' + self.database,
line)
elif 'resource.ds1.xaProperties.user' in line:
line = re.sub(regex, '=' + self.username, line)
file_data += line
with open(self.file_path, 'w', encoding='utf-8') as file:
file.write(file_data)
print('task database info is updated')
if __name__ == '__main__':
context_path = 'C:\\cbxsoftware\\apache-tomcat-8.0.47\\conf\\context.xml'
jetty_path = 'C:\cbxsoftware\cbx-develop\cbx-biz\settings\jetty9env.xml'
task_path = 'C:\cbxsoftware\cbx-develop\cbx-biz\\task\DBSetting.properties'
ip_address = '192.168.5.132'
# ip_address = 'localhost'
# ip_address = '192.168.1.105'
port = '5432'
database = 'cbx6_u_3'
username = 'postgres'
info = UpdateDatabaseInfo(context_path, ip_address, port, database, username)
info.updateTomcat()
info.file_path = jetty_path
info.updateJetty()
info.file_path = task_path
info.updateTask()