-
Notifications
You must be signed in to change notification settings - Fork 4
/
prepare_diamond.py
181 lines (155 loc) · 6.48 KB
/
prepare_diamond.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
#!/usr/bin/python3
import os
import shutil
from pathlib import Path
import fileinput
def remove(path):
""" param <path> could either be relative or absolute. """
if os.path.isfile(path) or os.path.islink(path):
os.remove(path) # remove the file
elif os.path.isdir(path):
shutil.rmtree(path) # remove dir and all contains
else:
raise ValueError("file {} is not a file or dir.".format(path))
def search_replace(old_addonID, addon_ID, update_files):
for fi in update_files:
filename = Path(fi)
print(str(old_addonID)+'= REPLACE OLD ADDONID - '+ fi + ', ' + str(addon_ID) + ' = NEW ADDONID -- DIAMONDINFO_MOD')
with fileinput.FileInput(filename, inplace=True, backup='.bak') as file:
for line in file:
if 'settings.xml' in fi:
if not '(Diamond)' in str(line):
print(line.replace(old_addonID, str(addon_ID)), end='')
else:
print(line.replace(old_addonID, str(addon_ID)), end='')
def prepare_diamond():
diamond_dest = '/home/osmc/repository.thenewdiamond/script.diamondinfo/'
extended_dest = '/home/osmc/repository.thenewdiamond/script.extendedinfo/'
if os.path.exists(diamond_dest):
remove(diamond_dest)
if os.path.exists(extended_dest):
remove(extended_dest)
diamond_source = '/home/osmc/.kodi/addons/script.extendedinfo/'
remove_pycache = []
for i in os.walk(diamond_source):
for j in i[1]:
if j == '__pycache__':
pycache_path = i[0] + '/' + j
remove_pycache.append(pycache_path)
for i in remove_pycache:
print('remove', i)
remove(i)
repo_source = '/home/osmc/repository.thenewdiamond/'
remove_pycache = []
for i in os.walk(diamond_source):
for j in i[1]:
if j == '__pycache__':
pycache_path = i[0] + '/' + j
remove_pycache.append(pycache_path)
for i in remove_pycache:
print('remove', i)
remove(i)
shutil.copytree(diamond_source, diamond_dest)
os.remove(diamond_dest + 'addon.xml')
# Using readlines()
file1 = open(diamond_dest + 'addon.xml.DIAMONDINFO', 'r')
Lines = file1.readlines()
"""
output_lines = ''
for line in Lines:
if '<addon id="' in str(line):
x = 0
for i in line.split('"'):
if x == 0:
new_line = i
if x > 0:
if line.split('"')[x-1] == ' version=':
version_number = round(float(i) + 0.01,2)
print('addon.xml.EXTENDEDINFO', version_number)
new_line += str(version_number)
else:
new_line += i
new_line += '"'
x = x + 1
output_lines += new_line
else:
output_lines += line
"""
output_lines = ''
for line in Lines:
if '<addon id="' in str(line):
print("%.2f" % (round(float(line.split('version="')[1].split('" provider')[0])+0.01,2)))
new_line = line.replace(line.split('version="')[1].split('" provider')[0], "%.2f" % round(float(line.split('version="')[1].split('" provider')[0])+0.01,2))
else:
new_line = line
output_lines = output_lines + str(new_line)
outF = open(diamond_dest + 'addon.xml', "w")
outF.write(output_lines)
outF.close()
shutil.copyfile(diamond_dest + 'addon.xml', diamond_source + 'addon.xml.DIAMONDINFO')
shutil.copytree(diamond_source, extended_dest)
os.remove(extended_dest + 'addon.xml')
# Using readlines()
file1 = open(extended_dest + 'addon.xml.EXTENDEDINFO', 'r')
Lines = file1.readlines()
"""
output_lines = ''
for line in Lines:
if '<addon id="' in str(line):
x = 0
for i in line.split('"'):
if x == 0:
new_line = i
if x > 0:
if line.split('"')[x-1] == ' version=':
version_number = round(float(i) + 0.01,2)
print('addon.xml.EXTENDEDINFO', version_number)
new_line += str(version_number)
else:
new_line += i
new_line += '"'
x = x + 1
output_lines += new_line
else:
output_lines += line
"""
output_lines = ''
for line in Lines:
if '<addon id="' in str(line):
print("%.2f" % (round(float(line.split('version="')[1].split('" provider')[0])+0.01,2)))
new_line = line.replace(line.split('version="')[1].split('" provider')[0], "%.2f" % round(float(line.split('version="')[1].split('" provider')[0])+0.01,2))
else:
new_line = line
output_lines = output_lines + str(new_line)
outF = open(extended_dest + 'addon.xml', "w")
outF.write(output_lines)
outF.close()
shutil.copyfile(extended_dest + 'addon.xml', diamond_source + 'addon.xml.EXTENDEDINFO')
os.remove(diamond_dest + 'addon.xml.DIAMONDINFO')
os.remove(diamond_dest + 'addon.xml.EXTENDEDINFO')
os.remove(extended_dest + 'addon.xml.DIAMONDINFO')
os.remove(extended_dest + 'addon.xml.EXTENDEDINFO')
update_files = []
update_files.append(extended_dest + 'resources/settings.xml')
update_files.append(extended_dest + 'README.md')
update_files.append(extended_dest + 'readme.txt')
update_files.append(extended_dest + 'direct.diamond_player.json')
update_files.append(extended_dest + 'direct.diamond_player_bluray.json')
update_files.append(extended_dest + 'direct.diamond_player_bluray2.json')
old_addonID = 'script.diamondinfo'
addon_ID = 'script.extendedinfo'
search_replace(old_addonID, addon_ID, update_files)
for i in update_files:
os.remove(i + '.bak')
update_files = []
update_files.append(diamond_dest + 'resources/settings.xml')
update_files.append(diamond_dest + 'README.md')
update_files.append(diamond_dest + 'readme.txt')
update_files.append(diamond_dest + 'direct.diamond_player.json')
update_files.append(diamond_dest + 'direct.diamond_player_bluray.json')
update_files.append(diamond_dest + 'direct.diamond_player_bluray2.json')
addon_ID = 'script.diamondinfo'
old_addonID = 'script.extendedinfo'
search_replace(old_addonID, addon_ID, update_files)
for i in update_files:
os.remove(i + '.bak')