-
Notifications
You must be signed in to change notification settings - Fork 46
/
NsisScriptGenerate.py
90 lines (73 loc) · 3.19 KB
/
NsisScriptGenerate.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
import os
import sys
g_extract_file_cmd_total = 0
g_create_dir_cmd_total = 0
g_cur_script_index = 0
g_insert_nsis_script_list = []
g_root_dir = ""
def nsis_cmd_num_statistics(dir):
global g_extract_file_cmd_total
global g_create_dir_cmd_total
if not os.path.isdir(dir):
return
files = os.listdir(dir)
for fi in files:
fi_d = os.path.join(dir, fi)
if os.path.isdir(fi_d):
g_create_dir_cmd_total += 1
nsis_cmd_num_statistics(fi_d)
else:
g_extract_file_cmd_total += 1
def generate_nsis_script(dir):
global g_cur_script_index
if not os.path.isdir(dir):
return
for fi in os.listdir(dir):
full_path = os.path.join(dir, fi)
if os.path.isdir(full_path):
sub_dir = full_path[len(g_root_dir):]
g_cur_script_index += 1
g_insert_nsis_script_list.append(' CreateDirectory "$INSTDIR{0}"'.format(sub_dir))
g_insert_nsis_script_list.append(' {0}::SetInstallStepDescription "Create Directory: {1}" {2}'
.format('${UI_PLUGIN_NAME}', '$INSTDIR' + sub_dir,
g_cur_script_index * 100 / (g_extract_file_cmd_total + g_create_dir_cmd_total)))
generate_nsis_script(full_path)
else:
g_cur_script_index += 1
g_insert_nsis_script_list.append(' SetOutPath "$INSTDIR{0}"'.format(dir[len(g_root_dir):]))
g_insert_nsis_script_list.append(' File "{0}"'.format( full_path))
g_insert_nsis_script_list.append(' {0}::SetInstallStepDescription "Extract File: $INSTDIR{1}" {2}'
.format('${UI_PLUGIN_NAME}', full_path[len(g_root_dir):], g_cur_script_index * 100 / (g_extract_file_cmd_total + g_create_dir_cmd_total)))
def do_main(nsis_script_template_path):
global g_root_dir
if g_root_dir[len(g_root_dir) - 1] == '\\':
g_root_dir = g_root_dir[:len(g_root_dir) - 1]
nsis_cmd_num_statistics(g_root_dir)
print("file total: " + str(g_extract_file_cmd_total) + ", dir total: " + str(g_create_dir_cmd_total))
generate_nsis_script(g_root_dir)
g_insert_nsis_script_list.append(' ${UI_PLUGIN_NAME}::SetInstallStepDescription "Finished" 100')
f = open(nsis_script_template_path, "r")
all_nsis_script_lines = []
cur_line_index = -1
insert_line_index = -1
for s in f.readlines():
all_nsis_script_lines.append(s)
cur_line_index += 1
if s.find('Function ___ExtractFiles') != -1:
insert_line_index = cur_line_index + 1
f.close()
if insert_line_index == -1:
return
for s in g_insert_nsis_script_list:
all_nsis_script_lines.insert(insert_line_index, s)
insert_line_index += 1
nsis_script_path = nsis_script_template_path[:len(nsis_script_template_path) - len('-template.nsi')]
nsis_script_path += '.nsi'
f = open(nsis_script_path, "w")
all_script = '\n'.join(all_nsis_script_lines)
f.write(all_script)
f.close()
if __name__ == '__main__':
nsis_script_template_path = sys.argv[1]
g_root_dir = sys.argv[2]
do_main(nsis_script_template_path)