-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuildPackage.py
executable file
·211 lines (178 loc) · 6.84 KB
/
buildPackage.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
#!/usr/bin/env python3
""" Script exports files which are required to
share the addon.
"""
import optparse
import os.path
import os
import glob
import errno
import shutil
# See parse_options for help
settings = {
"dest_folder": "/dev/shm",
"copy_templates": False,
"pack_textures": False,
"zip_addon": False, # Not implemented
"force": False,
"ignored_patterns": [".*", ".git",
"screenshots",
"media",
"720p",
"themes",
"tmp",
"xml",
"build.bat",
"buildPackage.py",
"TexturePacker*",
"__pycache__", "*.pyc",
],
# This files are required to rebuild the skin.
"template_files": ["templates",
"parseTemplates.py",
"buildPackage.py"
],
}
if not hasattr(globals()["__builtins__"], "raw_input"):
""" Python2 raw_input is called input in Python3. """
raw_input = input
def parse_options():
p = optparse.OptionParser()
p.add_option('--dest', '-d', default=settings["dest_folder"],
help="Destination folder for package")
p.add_option('--force', '-f', action="store_true",
help="Skip confirm prompt.")
p.add_option('--pack', '-p', action="store_true",
help="Call TexturePacker to create texture bundles.")
p.add_option('--no-pack', '-n', action="store_true",
help="Ignore texture bundles and copy unpacked images.")
p.add_option('--with-templates', '-t', action="store_true",
help="Copy template folder and buildScripts.")
options, arguments = p.parse_args()
settings["dest_folder"] = options.dest
settings["copy_templates"] = bool(options.with_templates)
settings["pack_textures"] = bool(options.pack)
settings["ignore_packed"] = bool(options.no_pack)
settings["force"] = bool(options.force)
def makedirs(path, mode=511, exist_ok=True):
""" No exist_ok arg in Python2. """
try:
os.makedirs(path, mode)
except OSError as e:
if e.errno != errno.EEXIST or not exist_ok:
raise e
def copy(src, dest, ignore_patterns=[]):
try:
shutil.copytree(
src, dest,
ignore=shutil.ignore_patterns(
*ignore_patterns))
except OSError as e:
# If the error was caused because the source wasn't a directory
if e.errno == errno.ENOTDIR:
shutil.copy(src, dest)
else:
print(' Directory not copied. Error: %s' % e)
def call_theme_packer(to_pack_folder):
if to_pack_folder == "media":
pack_path = "media/{}".format("Textures.xbt")
arg = "-dupecheck"
else:
pack_path = "media/{}{}".format(
os.path.basename(to_pack_folder), ".xbt")
arg = ""
if os.path.exists("./TexturePacker"):
bin_name = "./TexturePacker"
else:
bin_name = "TexturePacker"
ret = os.system("which {0}".format(bin_name))
if ret != 0:
print("Error: Can not find TexturePacker. Look at "
"http://kodi.wiki/view/TexturePacker "
"for more information.\n\n"
"For Linux users: You could use "
"'./TexturePackerBuild.sh' to build the binary without cloning "
"the whole xbmc repository."
)
else:
os.system("{0} {1} -input {2} -output {3}".format(
bin_name, arg, to_pack_folder, pack_path))
def copy_addon(source_folder, dest_folder):
if "^"+source_folder in "^"+dest_folder:
print(""" Bad input arguments?!
Destination folder will be cleared, but contains
the source folder. Please check out the path settings..
""")
return
elif os.path.exists(dest_folder):
# shutil.copytree require nonexistance of destination.
shutil.rmtree(dest_folder)
print(" - Create addon structure.")
to_ignore = settings["ignored_patterns"] + settings["template_files"]
copy(source_folder, dest_folder, to_ignore)
if settings["pack_textures"]:
print(" - Pack default theme")
call_theme_packer("media")
theme_folders = list(glob.glob(
os.path.join(source_folder, "themes", "*")))
for theme in theme_folders:
if os.path.isdir(theme):
print(" - Pack %s" % (theme))
call_theme_packer(theme)
copy_textures(source_folder, dest_folder)
if settings["copy_templates"]:
copy_templates(source_folder, dest_folder)
print(" - Build finished.")
def copy_textures(source_folder, dest_folder):
""" Copy texture pack files if available and
copy unpacked files otherwise.
"""
default_texture_file = list(glob.glob(
os.path.join(source_folder, "media", "Textures.xbt")))
texture_files = list(glob.glob(
os.path.join(source_folder, "media", "*.xbt")))
texture_files = [x for x in texture_files if x not in default_texture_file]
if len(default_texture_file) > 0:
f = default_texture_file[0]
print(" - Copy default theme %s." % (f,))
makedirs(os.path.join(dest_folder, "media"), exist_ok=True)
copy(f, os.path.join(dest_folder, "media", os.path.basename(f)))
else:
copy(os.path.join(source_folder, "media"),
os.path.join(dest_folder, "media"))
if len(texture_files) > 0:
for f in texture_files:
print(" - Copy %s " % (f))
copy(f, os.path.join(dest_folder, "media",
os.path.basename(f)))
else:
if os.path.exists(os.path.join(source_folder, "themes")):
copy(os.path.join(source_folder, "themes"),
os.path.join(dest_folder, "themes"))
def copy_templates(source_folder, dest_folder):
print(" - Copy templates and build files")
for f_or_d in settings["template_files"]:
copy(os.path.join(source_folder, f_or_d),
os.path.join(dest_folder, f_or_d),
settings["ignored_patterns"])
def main():
parse_options()
this_folder_name = os.path.basename(
os.path.abspath(os.path.curdir))
dest_folder_name = os.path.join(
settings["dest_folder"],
this_folder_name)
if settings["force"]:
confirm = True
else:
confirm_str = raw_input(" Copy\t'%s'\n to\t%s.\n Continue? (Y/n) " % (
this_folder_name, dest_folder_name))
confirm = confirm_str.lower() in ["y", "yes", ""]
if confirm:
print(" - Start build.")
copy_addon(os.path.curdir, dest_folder_name)
else:
print(" Build abort by user.")
# ==========================================================
if __name__ == '__main__':
main()