forked from rhult/ige-mac-bundler
-
Notifications
You must be signed in to change notification settings - Fork 15
/
project.py
401 lines (315 loc) · 13.2 KB
/
project.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import errno
import sys
import re
import os
import xml.dom.minidom
from xml.dom.minidom import Node
from plistlib import Plist
import utils
# Base class for anything that can be copied into a bundle with a
# source and dest.
class Path:
def __init__(self, source, dest=None):
if source and len(source) == 0:
source = None
if dest and len(dest) == 0:
dest = None
if source and os.path.isabs(source):
source = os.path.normpath(source)
if dest and os.path.isabs(dest):
dest = os.path.normpath(dest)
self.source = source
self.dest = dest
def from_node(cls, node, validate=True):
source = utils.node_get_string(node)
dest = node.getAttribute("dest")
if len(dest) == 0:
dest = None
if validate:
Path.validate(source, dest)
return Path(source, dest)
from_node = classmethod(from_node)
def validate(cls, source, dest):
if source and len(source) == 0:
source = None
if dest and len(dest) == 0:
dest = None
if not source or len(source) == 0:
raise Exception("The source path cannot be empty")
if source.startswith("${bundle}"):
raise Exception("The source path cannot use a ${bundle} macro")
if dest and dest.startswith("${prefix"):
raise Exception("The destination path cannot use a ${prefix} macro")
if not os.path.isabs(source):
if not (source.startswith("${project}") or source.startswith("${env:") or \
source.startswith("${pkg:") or source.startswith("${prefix}") or \
source.startswith("${prefix:")):
raise Exception("The source path must be absolute or use one of the "
"predefined macros ${project}, ${prefix}, ${prefix:*}, "
"${env:*}, or ${pkg:*:*}")
if not source.startswith("${prefix") and os.path.isabs(source):
if not dest:
raise Exception("If the source doesn't use a ${prefix} or ${prefix:*} "
"macro, the destination path must be set " + dest)
if not dest and not source.startswith("${prefix"):
raise Exception("If the destination path is empty, the source must use "
"a ${prefix} or ${prefix:*} macro")
if dest and len(dest) > 0 and not dest.startswith("${bundle}"):
raise Exception("The destination path must start with ${bundle}")
return True
validate = classmethod(validate)
# Used for anything that has a name and value.
class Variable:
def __init__(self, node):
self.name = node.getAttribute("name")
self.value = utils.node_get_string(node)
class Environment:
def __init__(self, node):
self.runtime_variables = []
self.scripts = []
variables = utils.node_get_elements_by_tag_name(node, "runtime-variable")
for child in variables:
self.runtime_variables.append(Variable(child))
scripts = utils.node_get_elements_by_tag_name(node, "script")
for child in scripts:
script = Path(utils.node_get_string(child), "${bundle}/Resources/Scripts")
self.scripts.append(script)
class Meta:
def __init__(self, node):
self.prefixes = {}
prefixes = utils.node_get_elements_by_tag_name(node, "prefix")
for child in prefixes:
name = child.getAttribute("name")
if len(name) == 0:
name = "default"
value = utils.evaluate_environment_variables(utils.node_get_string(child))
self.prefixes[name] = value
child = utils.node_get_element_by_tag_name(node, "image")
if child:
pass # FIXME: implement
child = utils.node_get_element_by_tag_name(node, "run-install-name-tool")
if child:
self.run_install_name_tool = True
else:
self.run_install_name_tool = False
child = utils.node_get_element_by_tag_name(node, "destination")
self.overwrite = utils.node_get_property_boolean(child, "overwrite", False)
self.dest = utils.node_get_string(child, "${project}")
class Framework(Path):
def __init__(self, source):
Path.__init__(self, source, self.get_dest_path_from_source(source))
def from_node(cls, node):
framework = Path.from_node(node, validate=False)
framework.dest = Framework.get_dest_path_from_source(framework.source)
return framework
from_node = classmethod(from_node)
def get_dest_path_from_source(cls, source):
(head, tail) = os.path.split(source)
return "${bundle}/Contents/Frameworks/" + tail
get_dest_path_from_source = classmethod(get_dest_path_from_source)
class Binary(Path):
def __init__(self, source, dest):
Path.__init__(self, source, dest)
def from_node(cls, node):
binary = Path.from_node(node)
if not binary.source:
raise "The tag 'binary' must have a 'source' property"
if binary.dest:
raise "The tag 'binary' is not allowed to have a 'dest' property"
return binary
from_node = classmethod(from_node)
class Translation(Path):
def __init__(self, name, sourcepath, destpath):
Path.__init__(self, sourcepath, destpath)
self.name = name
def from_node(cls, node):
source = utils.node_get_string(node)
dest = node.getAttribute("dest")
if len(dest) == 0:
dest = None
name = node.getAttribute("name")
if len(name) == 0:
raise "The tag 'translations' must have a 'name' property"
return Translation(name, source, dest)
from_node = classmethod(from_node)
class Data(Path):
pass
class IconTheme:
ICONS_NONE, ICONS_ALL, ICONS_AUTO = range(3)
def __init__(self, name, icons=ICONS_AUTO):
self.name = name
self.source = "${prefix}/share/icons/" + name
self.icons = icons
def from_node(cls, node):
name = utils.node_get_string(node)
if not name:
raise Exception("Icon theme must have a 'name' property")
string = node.getAttribute("icons")
if string == "all":
icons = IconTheme.ICONS_ALL
elif string == "none":
icons = IconTheme.ICONS_NONE
elif string == "auto" or len(string) == 0:
icons = IconTheme.ICONS_AUTO
return IconTheme(name, icons)
from_node = classmethod(from_node)
class Project:
def __init__(self, project_path=None):
if not os.path.isabs(project_path):
project_path = os.path.join(os.getcwd(), project_path)
self.project_path = project_path
self.root = None
if project_path and os.path.exists(project_path):
try:
doc = xml.dom.minidom.parse(project_path)
# Get the first app-bundle tag and ignore any others.
self.root = utils.node_get_element_by_tag_name(doc, "app-bundle")
except:
print "Could not load project %s:" % (project_path)
raise
# The directory the project file is in (as opposed to
# project_path which is the path including the filename).
self.project_dir, tail = os.path.split(project_path)
plist_path = self.get_plist_path()
try:
plist = Plist.fromFile(plist_path)
except EnvironmentError, e:
if e.errno == errno.ENOENT:
print "Info.plist file not found: " + plist_path
sys.exit(1)
else:
raise
self.name = plist.CFBundleExecutable
"""
Replace ${env:?}, ${prefix}, ${prefix:?}, ${project},
${pkg:?:?}, ${bundle}, and ${name} variables.
"""
def evaluate_path(self, path, include_bundle=True):
p = re.compile("^\${prefix}")
path = p.sub(self.get_prefix(), path)
p = re.compile("^\${prefix:(.*?)}")
m = p.match(path)
if m:
path = p.sub(self.get_prefix(m.group(1)), path)
p = re.compile("^\${project}")
path = p.sub(self.project_dir, path)
try:
p = re.compile("\${name}")
path = p.sub(self.name, path)
except AttributeError:
pass # can be used before name path is set
if include_bundle:
try:
p = re.compile("^\${bundle}")
path = p.sub(self.get_bundle_path(), path)
except AttributeError:
pass # can be used before bundle path is set
path = utils.evaluate_environment_variables(path)
path = utils.evaluate_pkgconfig_variables(path)
return os.path.normpath(path)
def get_name(self):
return self.name
def get_prefix(self, name="default"):
meta = self.get_meta()
return meta.prefixes[name]
def get_project_path(self):
return self.project_path
def get_project_dir(self):
return self.project_dir
def get_bundle_path(self, *args):
dest = self.get_meta().dest
bundle_path = os.path.join(dest, "." + self.get_name() + ".app")
bundle_path = self.evaluate_path(bundle_path, False)
return os.path.join(bundle_path, *args)
def get_plist_path(self):
plist = utils.node_get_element_by_tag_name(self.root, "plist")
if not plist:
raise Exception("The 'plist' tag is required")
return self.evaluate_path(utils.node_get_string(plist))
def get_launcher_script(self):
node = utils.node_get_element_by_tag_name(self.root, "launcher-script")
if node:
path = Path.from_node(node, False)
path.dest = "${bundle}/Contents/MacOS/${name}"
else:
# Use the default launcher.
launcher = os.path.join(os.path.dirname(__file__), "launcher.sh")
path = Path(launcher, "${bundle}/Contents/MacOS/${name}")
return path
def get_icon_themes(self):
themes = []
nodes = utils.node_get_elements_by_tag_name(self.root, "icon-theme")
for node in nodes:
themes.append(IconTheme.from_node(node))
# The hicolor theme is mandatory.
if not filter(lambda l: l.name == "hicolor", themes):
themes.append(IconTheme("hicolor"))
return themes
def get_meta(self):
node = utils.node_get_element_by_tag_name(self.root, "meta")
return Meta(node)
def get_environment(self):
node = utils.node_get_element_by_tag_name(self.root, "environment")
return Environment(node)
def get_frameworks(self):
frameworks = []
nodes = utils.node_get_elements_by_tag_name(self.root, "framework")
for node in nodes:
frameworks.append(Framework.from_node(node))
return frameworks
def get_translations(self):
translations = []
nodes = utils.node_get_elements_by_tag_name(self.root, "translations")
for node in nodes:
translations.append(Translation.from_node(node))
return translations
def get_main_binary(self):
node = utils.node_get_element_by_tag_name(self.root, "main-binary")
if not node:
raise Exception("The file has no <main-binary> tag")
binary = Binary.from_node(node)
launcher = self.get_launcher_script()
if launcher:
suffix = "-bin"
else:
suffix = ""
binary.dest = "${bundle}/Contents/MacOS/${name}" + suffix
return binary
def get_binaries(self):
binaries = []
nodes = utils.node_get_elements_by_tag_name(self.root, "binary")
for node in nodes:
binaries.append(Binary.from_node(node))
return binaries
def get_data(self):
data = []
nodes = utils.node_get_elements_by_tag_name(self.root, "data")
for node in nodes:
data.append(Data.from_node(node))
return data
if __name__ == '__main__':
project = Project(os.path.join(os.getcwd(), 'giggle.bundle'))
print "General:"
print " Project path: %s" % (project.get_project_path())
print " Plist path: %s" % (project.get_plist_path())
print " App name: %s" % (project.name)
print " Destination: %s" % (project.get_meta().dest)
print " Overwrite: %s" % (str(project.get_meta().overwrite))
environment = project.get_environment()
print "Environment:"
for variable in environment.runtime_variables:
print " %s=%s" % (variable.name, variable.value)
for script in environment.scripts:
print " %s => %s" % (script.source, script.dest)
print "Frameworks:"
for framework in project.get_frameworks():
print " ", framework
print "Main binary:"
binary = project.get_main_binary()
print " %s => %s" % (binary.source, binary.dest)
print "Launcher:"
launcher_script = project.get_launcher_script()
print " %s => %s" % (launcher_script.source, launcher_script.dest)
print "Binaries:"
for binary in project.get_binaries():
print " %s => %s" % (binary.source, binary.dest)