-
Notifications
You must be signed in to change notification settings - Fork 9
/
w34_uninstaller.py
110 lines (107 loc) · 5.71 KB
/
w34_uninstaller.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
from configobj import ConfigObj
import distutils.file_util
import traceback
import shutil
import time
import sys
import os
import re
KEYS_TO_DELETE = ['Weather34RealTime','Weather34WebServices','Weather34CloudCover','W34_DB_Backup','StdReport:w34Highcharts','StdReport:Weather34Report','StdReport:w34skinReport']
VALUES_TO_DELETE = ['Engine:Services:process_services:user.w34_db_backup.W34_DB_Backup','Engine:Services:process_services:user.weather34.Weather34RealTime','Engine:Services:xtype_services:user.lastnonzero.LastNonZeroService']
DIRS_TO_DELETE = ['www:','skins:Weather34','skins:w34Highcharts','skins:w34Highcharts-day']
FILES_TO_DELETE = ['user:w34highchartsSearchX.py','user:weather34.py','user:w34_db_backup.py','user:lastrain.py','user:ml.py','user:stats.py','user:lastnonzero.py']
class w34_uninstaller:
def __init__(self, conf_file):
try:
try:
response = raw_input("Do you want to UNinstall w34 Template Yes/No? ").strip().upper()
except:
response = input("Do you want to UNinstall w34 Template Yes/No? ").strip().upper()
if response != "YES":
sys.exit(0)
print ("!!!!!!!!!!!!UNINSTALLING W34 TEMPLATE!!!!!!!!!!!!!!")
try:
response = raw_input("Are you SURE you want to UNinstall w34 Template Yes/No? ").strip().upper()
except:
response = input("Are you SURE you want to UNinstall w34 Template Yes/No? ").strip().upper()
if response != "YES":
sys.exit(0)
conf_files = {}
if conf_file == None:
file_count = 1
files = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith(".conf")]
for f in files:
with open(f) as infile:
try:
d = eval(re.sub(".*\"##.*\n",'', infile.read()).replace("\n", "").replace("\t", ""))
if os.path.isdir(list(d["copy_paths"].split(","))[0]):
conf_files[file_count] = f
file_count += 1
except Exception as e:
pass
if len(conf_files) == 1:
conf_file = conf_files[1]
else:
if file_count > 1:
print("\nList of found w34_uninstaller conf files to uninstall that have existing weewx paths")
for f in range(len(conf_files)):
print(str(f+1) + " -> " + conf_files[f+1])
response = 0
while response == 0 or response > len(conf_files):
try:
response = int(raw_input("Enter the NUMBER of the uninstaller config file ").strip())
except:
response = int(input("Enter the NUMBER of the uninstaller config file ").strip())
conf_file = conf_files[response]
else:
print("!!! NO VALID W34_INSTALLER CONFIG FILE. UNINSTALL ABORTED!!!")
sys.exit(1)
print("Uninstaller Config file " + conf_file + " was chosen.")
with open(conf_file) as infile:
d = eval(re.sub(".*\"##.*\n",'', infile.read()).replace("\n", "").replace("\t", ""))
copy_list = list(d["copy_paths"].split(","))
path_list = {copy_list[i+1]:copy_list[i] for i in range(0, len(copy_list), 2)}
for f in DIRS_TO_DELETE:
parts = f.split(':')
path = os.path.join(path_list[parts[0]], parts[1])
try: shutil.rmtree(path)
except: print("Directory Not Found " + path)
for f in FILES_TO_DELETE:
parts = f.split(':')
path = os.path.join(path_list[parts[0]], parts[1])
try: os.remove(path)
except: print("File Not Found " + path)
weewx_config_file = d["weewx_config_file"]
distutils.file_util.copy_file(weewx_config_file, weewx_config_file + "." + str(int(time.time())))
print('Updating weewx config')
config_data = ConfigObj(weewx_config_file, encoding='utf8', list_values=False,write_empty_values=True)
for k in KEYS_TO_DELETE:
try:
if ":" in k:
parts = k.split(":")
del config_data.get(parts[0])[parts[1]]
else:
del config_data[k]
config_data.write()
except Exception as e:
print('Key not found:' + k)
for k in VALUES_TO_DELETE:
try:
parts = k.split(":")
section = config_data
for i in range(len(parts)-2):
section = section.get(parts[i])
data = section.get(parts[-2])
if parts[-1] in data:
section[parts[-2]] = data.replace(", " + parts[-1], "")
config_data.write()
else:
print('Value not found:' + k)
except Exception as e:
print (e)
print('Done! Will Need To Restart Weewx For Changes To Be Active')
except Exception as e:
traceback.print_exc()
print (e)
if __name__ == '__main__':
w34_uninstaller(sys.argv[1] if len(sys.argv) > 1 else None)