-
Notifications
You must be signed in to change notification settings - Fork 42
/
build.py
146 lines (105 loc) · 3.83 KB
/
build.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
'''
Copyright 2011 Mikel Azkolain
This file is part of Spotimc.
Spotimc is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Spotimc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Spotimc. If not, see <http://www.gnu.org/licenses/>.
'''
import os
import fnmatch
import xml.etree.ElementTree as ET
import zipfile
#Get the working dir of the script
work_dir = os.getcwd()
include_files = [
'resources/*',
'addon.xml',
'changelog.txt',
'default.py',
'spotimc.py',
'icon.png',
'LICENSE.txt',
'README.md',
]
exclude_files = [
'*.pyc',
'*.pyo',
'*/Thumbs.db',
'resources/libs/spotimcgui/appkey.py-template',
'resources/libs/pyspotify-ctypes/tmp',
'resources/libs/pyspotify-ctypes/dlls',
'resources/libs/pyspotify-ctypes/libs',
'resources/libs/pyspotify-ctypes-proxy/tmp',
'resources/libs/pyspotify-ctypes-proxy/dlls',
'resources/libs/pyspotify-ctypes-proxy/libs',
]
def get_addon_info():
path = os.path.join(work_dir, 'addon.xml')
root = ET.parse(path).getroot()
return root.attrib['id'], root.attrib['version']
def is_included(path):
for item in include_files:
#Try fnmatching agains the include rule
if fnmatch.fnmatch(path, item):
return True
#Also test if it gets included by a contained file
elif path.startswith(item):
return True
#Or if the path is part of a pattern
elif item.startswith(path):
return True
return False
def is_excluded(path):
#Exclude hidden files and folders
if os.path.basename(path).startswith('.'):
return True
#Iterate over the exclude patterns
else:
for item in exclude_files:
#Try fnmatching agains the exclude entry
if fnmatch.fnmatch(path, item):
return True
return False
def generate_file_list(path):
file_list = []
for item in os.listdir(path):
cur_path = os.path.join(path, item)
cur_rel_path = os.path.relpath(cur_path, work_dir)
if is_included(cur_rel_path) and not is_excluded(cur_rel_path):
file_list.append(cur_rel_path)
if os.path.isdir(cur_path):
file_list.extend(generate_file_list(cur_path))
return file_list
def create_build_dir():
build_dir = os.path.join(work_dir, 'build')
if not os.path.isdir(build_dir):
os.mkdir(build_dir)
return build_dir
def generate_zip(build_dir, addon_id, addon_version, file_list):
#for item in file_list:
# print item
zip_name = '{0}-{1}.zip'.format(addon_id, addon_version)
zip_path = os.path.join(build_dir, zip_name)
zip_obj = zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED)
for item in file_list:
abs_path = os.path.join(work_dir, item)
if not os.path.isdir(abs_path):
arc_path = os.path.join(addon_id, item)
zip_obj.write(abs_path, arc_path)
zip_obj.close()
return zip_path
def main():
build_dir = create_build_dir()
addon_id, addon_version = get_addon_info()
file_list = generate_file_list(work_dir)
out_file = generate_zip(build_dir, addon_id, addon_version, file_list)
print('generated zip: {0}'.format(os.path.relpath(out_file, work_dir)))
if __name__ == '__main__':
main()