-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathw34_installer.py
133 lines (130 loc) · 6.35 KB
/
w34_installer.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
from zipfile import ZipFile
from configobj import ConfigObj
import distutils.dir_util
import sys
import time
import grp
import pwd
import os
def change_permissions_recursive(path_list, paths, paths_values):
uid = pwd.getpwnam("www-data").pw_uid
gid = grp.getgrnam("www-data").gr_gid
for p in path_list:
os.chmod(p, 0o755)
os.chown(p, uid, gid)
for root, dirs, files in os.walk(p, topdown=False):
for directory in [os.path.join(root,d) for d in dirs]:
os.chmod(directory, 0o755)
os.chown(directory, uid, gid)
if "json_day" in directory:
os.chmod(directory, 0o777)
for filename in [os.path.join(root, f) for f in files]:
os.chmod(filename, (0o777 if "wee_reports_w34" in filename else 0o755))
os.chown(filename, uid, gid)
if "plots_config.js" in filename:
with open(filename, 'r') as infile:
config_lines = infile.readlines()
count = 0
for j in range(len(config_lines)):
for i in range(len(paths)):
if paths[i] in config_lines[j]:
parts = config_lines[j].split("//")
config_lines[j] = paths[i] + "'" + paths_values[i] + "'" + " //" + parts[1]
count +=1
if count >= len(paths):
break
with open(filename, 'w') as outfile:
for i in config_lines:
outfile.write(i)
def find_replace(d, k, v, do_overwite, append = False):
found = k in d
if found:
if do_overwrite:
if append:
if not v in d[k]:
d[k].append(v)
else:
d[k] = eval(v)
else:
print("Config entry " + k + " already exists and over_write is set to False")
else:
for value in d.values():
if isinstance(value, dict):
found = find_replace(value, k, v, do_overwrite, append)
if found:
break
return found
conf_files = {}
try:
file_count = 1
files = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith(".conf")]
for f in files:
conf_files[file_count] = f
print(str(file_count) + " -> " + f)
file_count += 1
response = 0
while response == 0 or response > len(conf_files):
try:
response = int(raw_input("Enter the NUMBER of the installer config file ").strip())
except:
response = int(input("Enter the NUMBER of the installer config file ").strip())
conf_file = conf_files[response]
print("Installer Config file " + conf_file + " was chosen.")
with open(conf_file) as infile:
d = eval(infile.read().replace("\n", "").replace("\t", ""))
copy_list = list(d["copy_paths"].split(","))
do_overwrite = True if d["over_write"] == "True" else False
extract_path = d["extract_to_path"]
if extract_path == None or len(extract_path) == 0:
extract_path = "temp"
zip_file = d["zip_file"]
if len(zip_file) > 0:
with ZipFile(zip_file, 'r') as zip_file:
if not os.path.exists(extract_path):
os.makedirs(extract_path)
else:
if not do_overwrite:
print ("Extract path exists and overwrite set to false aborting install")
sys.exit(1)
print('Extracting all the files to ' + extract_path)
zip_file.extractall(extract_path)
print('Files extracted')
try:
for i in xrange(0, len(copy_list), 2):
distutils.dir_util.copy_tree(os.path.join(extract_path, copy_list[i+1].strip()), copy_list[i].strip(), update = do_overwrite)
except:
for i in range(0, len(copy_list), 2):
distutils.dir_util.copy_tree(os.path.join(extract_path, copy_list[i+1].strip()), copy_list[i].strip(), update = do_overwrite)
locations = {copy_list[i+1]:copy_list[i] for i in range(0, len(copy_list), 2)}
paths = ["var pathweewx = ", "var pathpws = ", "var pathweewxbin = "]
paths_values = ["/" + locations["www"].split("/var/www/html/")[1].split("/")[0] + "/", locations["www"].split("/var/www/html")[1], locations["user"].split("/user/")[0]]
change_permissions_recursive([locations["www"]], paths, paths_values)
if d["delete_extracted_files"] == "True":
if extract_path != os.getcwd():
distutils.dir_util.remove_tree(extract_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')
for i in d:
if i.startswith("config_entries"):
if "append" in i:
parts = d[i].split("=", 1)
if not find_replace(config_data, parts[0].strip(), parts[1], do_overwrite, True):
print("Append to key " + parts[0] + " skipped because key was not found")
else:
parts = d[i].split("=", 1)
zpath = parts[0]
parts = parts[1].split(":", 1)
key = parts[0].replace("'", "").strip()
if not find_replace(config_data, key, parts[1], do_overwrite):
if zpath == None or len(zpath) == 0:
config_data[key] = eval(parts[1])
config_data.comments[key] = ['#\n################################################################################\n']
else:
config_data[zpath][key] = eval(parts[1])
config_data[zpath].comments[key] = ['#\n ############################################################################\n']
config_data.write()
print('Done!')
except Exception as e:
print (e)