forked from minj/foxtrick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-update.py
executable file
·295 lines (228 loc) · 9.34 KB
/
module-update.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python
# Update module listing in manifest files
#
# Author: Anastasios Ventouris <[email protected]>
# LA-MJ <[email protected]>
import re
import argparse
import os
import sys
import codecs
tag = '<!-- %s -->'
targets = [
{
"file": "manifest.json",
"prefix": "\t\t\t\"content/",
"suffix": "\",\n"
},
{
"file": "Info.plist",
"prefix": "\t\t\t\t<string>content/",
"suffix": "</string>\n"
},
{
"file": "content/preferences.html",
"prefix": "\t<script type=\"application/x-javascript\" src=\"./",
"suffix": "\"></script>\n"
},
{
"file": "content/background.html",
"prefix": "\t<script type=\"application/x-javascript\" src=\"./",
"suffix": "\"></script>\n"
}
]
def build(args=dict(sourcefile="modules", excludefile=None, dirfile=".")):
sourcefile = args['sourcefile']
excludefile = args['excludefile']
dirfile = args['dirfile']
if dirfile.endswith('/'):
path = dirfile
else:
path = dirfile+"/"
# get module file list from file *modules*
with codecs.open(sourcefile, mode='r', encoding='utf-8') as source:
modules = source.read().splitlines()
# if exclude file in fuction, read the file
if excludefile and os.path.isfile(excludefile):
with codecs.open(excludefile, mode='r', encoding='utf-8') as ignore:
ignorelist = ignore.read().splitlines()
for mod in ignorelist:
# remove mod from modules
if mod in modules:
modules.remove(mod)
# delete files from ignorelist in the build
pathfile = path + 'content/' + mod
# replace the backslash with a slash
# pathfile = pathfile.replace("/",'\\')
if os.path.isfile(pathfile):
os.remove(pathfile)
# iterate through targets
for tar in targets:
# check if file exists
pathfile = path + tar['file']
if not os.path.isfile(pathfile):
continue
# open the file and copy the content to a list variable
with codecs.open(pathfile, mode='r', encoding='utf-8') as target:
# keep \n
lines = target.read().splitlines(True)
# find the index of start and end tags
start_tag = tag % 'categorized modules'
end_tag = tag % 'end categorized modules'
start = -1
end = -1
for step in lines:
if start_tag in step:
start = lines.index(step)
elif end_tag in step:
end = lines.index(step)
if start == -1 or end == -1:
print('No \'%s\' in %s. Skipping.' % (start_tag, pathfile))
continue
# create the text with the modules list
modlines = []
for mod in modules:
line = tar['prefix'] + mod + tar['suffix']
modlines.append(line)
# replace the content of the file
lines[start+1:end] = modlines
# write the new file
with codecs.open(pathfile, mode='w', encoding='utf-8') as f_out:
f_out.writelines(lines)
print('%s updated.' % pathfile)
def add(args):
filename = args['filename']
light = args['light']
module = normalize_path(filename)
add_module('modules', module)
if light:
add_module('included-modules-light', module)
build()
def rm(args):
filename = args['filename']
module = normalize_path(filename)
rm_module('modules', module)
rm_module('included-modules-light', module)
build()
def normalize_path(path):
# convert '/path/to/foxtrick/content/category/module.js' to 'category/module.js
r = re.compile('^(.*?/)?content/')
path = r.sub('', path)
return path
def rm_module(sourcefile, module):
# get module file list from file *sourcefile*
with codecs.open(sourcefile, mode='r', encoding='utf-8') as source:
# take \n
modules = source.read().splitlines(True)
module += '\n'
if module in modules:
modules.remove(module)
modules.sort()
# write the new file
with codecs.open(sourcefile, mode='w', encoding='utf-8') as f_out:
f_out.writelines(modules)
def add_module(sourcefile, module):
# get module file list from file *sourcefile*
with codecs.open(sourcefile, mode='r', encoding='utf-8') as source:
# take \n
modules = source.read().splitlines(True)
module += '\n'
if module not in modules:
modules.append(module)
modules.sort()
# write the new file
with codecs.open(sourcefile, mode='w', encoding='utf-8') as f_out:
f_out.writelines(modules)
def add_util(args):
# util types that should not be sorted (due to dependencies)
NO_SORT_TYPES = ['ext-lib']
filename = args['filename']
util_type = args['type']
util = normalize_path(filename)
# iterate through targets
for tar in targets:
# check if file exists
pathfile = tar['file']
if not os.path.isfile(pathfile):
continue
# open the file and copy the content to a list variable
with codecs.open(pathfile, mode='r', encoding='utf-8') as fh:
# keep \n
lines = fh.read().splitlines(True)
# find the index of start and end tags
start_tag = tag % util_type
end_tag = tag % ('end ' + util_type)
start = -1
end = -1
for step in lines:
if start_tag in step:
start = lines.index(step)
elif end_tag in step:
end = lines.index(step)
if start == -1 or end == -1:
print('No \'%s\' in %s. Skipping.' % (start_tag, pathfile))
continue
# create a copy of utils
utils = lines[start+1:end]
# create the new line and add it
utils.append(tar['prefix'] + util + tar['suffix'])
# sort and replace
if util_type not in NO_SORT_TYPES:
utils.sort()
lines[start+1:end] = utils
# write the new file
with codecs.open(pathfile, mode='w', encoding='utf-8') as f_out:
f_out.writelines(lines)
print('%s updated.' % pathfile)
def rm_util(args):
filename = args['filename']
util = normalize_path(filename)
# iterate through targets
for tar in targets:
# check if file exists
pathfile = tar['file']
if not os.path.isfile(pathfile):
continue
# open the file and copy the content to a list variable
with codecs.open(pathfile, mode='w', encoding='utf-8') as fh:
# keep \n
lines = fh.read().splitlines(True)
search = tar['prefix'] + util + tar['suffix']
try:
pos = lines.index(search)
except ValueError:
print('No \'%s\' in %s. Skipping.' % (util, pathfile))
continue
# remove
lines[pos:pos+1] = []
# write the new file
with codecs.open(pathfile, mode='w', encoding='utf-8') as f_out:
f_out.writelines(lines)
print('%s updated.' % pathfile)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Update module listings in manifest files. Author: Anastasios Ventouris, LA-MJ')
subparsers = parser.add_subparsers(help='Available commands:')
parser_build = subparsers.add_parser('build', help='Update manifest files (default action). Optionally removes excluded modules.')
parser_build.add_argument('-s', '--sources-file', dest='sourcefile', help='The name of the sources file. Default value = modules', required=False, default="modules")
parser_build.add_argument('-e', '--excludes-file', dest='excludefile', help='The name of the file with sources to ignore when building. Default value = None', default=None, required=False)
parser_build.add_argument('-d', '--build-dir', dest='dirfile', help='The path to the new working directory. Default value = CWD', default=".", required=False)
parser_build.set_defaults(func=build)
parser_add = subparsers.add_parser('add', help='Link a module. The file will be loaded in Foxtrick.')
parser_add.add_argument('-l', '--light-module', dest='light', help='Also include this module in Foxtrick Light.', required=False, action='store_true')
parser_add.add_argument('filename', help='The module file you want to link.')
parser_add.set_defaults(func=add)
parser_rm = subparsers.add_parser('rm', help='Unlink a module. The file will no longer be loaded in Foxtrick.')
parser_rm.add_argument('filename', help='The module file you want to unlink.')
parser_rm.set_defaults(func=rm)
parser_add_util = subparsers.add_parser('add-util', help='Link an util.')
parser_add_util.add_argument('-t', '--type', dest='type', help='Type of util to add: util (default), ext-lib, page-util, api-util, core.', required=False, action='store', default='util')
parser_add_util.add_argument('filename', help='The util file you want to link.')
parser_add_util.set_defaults(func=add_util)
parser_rm_util = subparsers.add_parser('rm-util', help='Unlink an util')
parser_rm_util.add_argument('filename', help='The util file you want to unlink.')
parser_rm_util.set_defaults(func=rm_util)
if (len(sys.argv) < 2):
args = parser.parse_args(['build'])
else:
args = parser.parse_args()
args.func(vars(args))