This repository was archived by the owner on Aug 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
320 lines (277 loc) · 11.3 KB
/
setup.py
File metadata and controls
320 lines (277 loc) · 11.3 KB
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
VERSION = "0.0.0"
import glob
import itertools
import os
import shutil
import StringIO
import subprocess
import sys
import pkgutil
from setuptools import setup, find_packages
import pkg_resources
import py2app.util as py2app_util
from py2app.util import strip_files
from py2app.build_app import py2app
# Patch py2app extension loaders to work with gevent
py2app_util.LOADER = """
def __load():
imp = __import__("imp")
os = __import__("os")
sys = __import__("sys")
ext = %r
for path in sys.path:
if not path.endswith('lib-dynload'):
continue
ext_path = os.path.join(path, ext)
if os.path.exists(ext_path):
mod = imp.load_dynamic(__name__, ext_path)
break
else:
raise ImportError(repr(ext) + " not found")
__load()
del __load
"""
setuptools_sitefix = """
import site
site.USER_BASE = None
site.USER_SITE = None
import os
os.environ['PATH'] = '%s:%s' % (
os.path.join(os.environ['RESOURCEPATH'], 'bin'),
os.environ['PATH'],
)
"""
def system(command, cwd=os.getcwd()):
p = subprocess.Popen(
command,
cwd = cwd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
stdout, stderr = p.communicate()
return stdout
class YaybuAppBuild(py2app):
user_options = py2app.user_options + [
("build-number=", "b", "build number"),
("signing-identity=", "s", "identity to sign app with"),
("channel=", "c", "update channel to use"),
]
def setup_distribution(self):
#self.distribution.app = ['Application.py']
#self.distribution.packages = [
# 'yaybu.core.main',
# ]
pass
def initialize_options(self):
self.signing_identity = None
self.build_number = 0
self.channel = "nightlies"
py2app.initialize_options(self)
def finalize_options(self):
py2app.finalize_options(self)
self.setup_distribution()
self.setup_plist()
def recipe_distutils(self, py2app, mf):
if not mf.findNode('distutils.command.install'):
return None
return {"prescripts": [StringIO.StringIO(setuptools_sitefix)]}
def recipe_egg_info(self, py2app, mf):
loader_files = []
for d in pkg_resources.working_set:
node = mf.findNode(d.project_name)
if not node:
node = mf.findNode(d.project_name.lower())
if node:
print " -> %s.egg-info" % d.project_name
with open("/tmp/%s.egg-info" % d.project_name, "w") as fp:
fp.write("Metadata-Version: 1.0\n")
fp.write("Name: %s\n" % d.project_name)
fp.write("Version: %s\n" % d.version)
loader_files.append('/tmp/%s.egg-info'% d.project_name)
self.egg_info_files = loader_files
return None
def recipe_yaybu(self, py2app, mf):
m = mf.findNode('yaybu')
if not m:
return None
mf.import_hook("yaybu.provisioner.resources", m, ['*'])
mf.import_hook("yaybu.provisioner.providers", m, ['*'])
return {}
def collect_recipedict(self):
dict = py2app.collect_recipedict(self)
dict.update({
"egg_info": self.recipe_egg_info,
"yaybu": self.recipe_yaybu,
"distutils": self.recipe_distutils,
})
return dict
def strip_files(self, files):
# This method is originally defined in py2app, but we have to bodge it to not strip Sparkle.framework :-/
unstripped = 0
stripfiles = []
for fn in files:
if "Sparkle.framework" in fn and "relaunch" in fn:
print "not stripping '%s'" % fn
continue
unstripped += os.stat(fn).st_size
stripfiles.append(fn)
print 'stripping %s' % os.path.basename(fn)
strip_files(stripfiles, dry_run=self.dry_run, verbose=self.verbose)
stripped = 0
for fn in stripfiles:
stripped += os.stat(fn).st_size
print 'stripping saved %d bytes (%d / %d)', unstripped - stripped, stripped, unstripped
def update_binary_wrappers(self):
bindir = os.path.realpath(os.path.join(self.resdir, "../MacOS"))
for b in os.listdir(bindir):
if b == "python":
continue
system(["gcc", "main.c", "-o", os.path.join(bindir, b)])
def sort_out_egg_metadata(self):
print "Generating fake egg metadata..."
site_packages = os.path.join(self.resdir, "lib", "python2.7", "site-packages")
if not os.path.exists(site_packages):
os.makedirs(site_packages)
for egg_info in self.egg_info_files:
self.copy_file(
egg_info,
os.path.join(site_packages, os.path.basename(egg_info)),
)
def bundle_cacert_pem(self):
print "Bundling cacert.pem..."
cacerts = pkgutil.get_data("requests", "cacert.pem")
if not cacerts:
raise SystemExit("Unable to find cacerts.pem in requests module")
with open(os.path.join(self.resdir, "cacert.pem"), "w") as fp:
fp.write(cacerts)
def fix_pricing_json(self):
print "Bundling pricing.json..."
self.copy_file(
os.path.join(pkg_resources.get_distribution("apache-libcloud").location, "libcloud", "data", "pricing.json"),
os.path.join(self.resdir, "pricing.json"),
)
def fix_bin_permissions(self):
os.chmod(os.path.join(self.resdir, "../Frameworks/Sparkle.framework/Versions/A/Resources/relaunch"), 0755)
def fix_sparkle(self):
print "Fixing sparkle install name"
path = os.path.join(self.resdir, "..", "Frameworks/Sparkle.framework/Versions/A/Sparkle")
# old_install_name = "@executable_path/../Frameworks/Sparkle.framework/Versions/A/Sparkle"
new_install_name = "@loader_path/../Frameworks/Sparkle.framework/Versions/A/Sparkle"
# system(['install_name_tool', '-change', old_install_name, new_install_name, path])
system(['install_name_tool', '-id', new_install_name, path])
def sign_path(self, path):
print "Signing '%s'" % path
bundle_root = os.path.abspath(os.path.join(self.resdir, "..", ".."))
print system([
'codesign',
'--force', '--verify', '--verbose',
'--sign', self.signing_identity,
# '--entitlements', 'path/to/entitlements',
os.path.abspath(os.path.join(bundle_root, path)),
])
def sign(self):
if not self.signing_identity:
return
self.sign_path('Contents/Frameworks/Sparkle.framework/Versions/A')
self.sign_path('Contents/Frameworks/Python.framework/Versions/2.7')
self.sign_path('Contents/MacOS/python')
self.sign_path('Contents/MacOS/Yaybu')
self.sign_path('Contents/MacOS/YaybuShell')
self.sign_path('.')
def build_dmg(self):
print "Building DMG staging..."
name = self.distribution.get_name()
image_staging = os.path.join(self.dist_dir, "dmg-staging")
image_path = os.path.join(self.dist_dir, "%s.dmg" % name)
image_raw = os.path.join(self.dist_dir, "raw.%s.dmg" % name)
image_tmp = os.path.join(self.dist_dir, "tmp.%s.dmg" % name)
if os.path.exists(image_path):
os.unlink(image_path)
if os.path.exists(image_staging):
shutil.rmtree(image_staging)
os.mkdir(image_staging)
os.mkdir(os.path.join(image_staging, ".background"))
shutil.copyfile("background.png", os.path.join(image_staging, ".background", "background.png"))
shutil.copyfile("DS_Store", os.path.join(image_staging, ".DS_Store"))
system(["SetFile", "-a", "V", os.path.join(image_staging, ".DS_Store")])
os.symlink("/Applications", os.path.join(image_staging, "Applications"))
shutil.copytree(
os.path.join(self.dist_dir, "%s.app" % name),
os.path.join(image_staging, "%s.app" % name),
symlinks=True,
)
# Set up volume icon as per http://endrift.com/page/dmg-files-volume-icons-cli
shutil.copyfile("Resources/Yaybu.icns", os.path.join(image_staging, ".VolumeIcon.icns"))
system(["SetFile", "-c", "icnC", os.path.join(image_staging, ".VolumeIcon.icns")])
print "Building DMG image..."
system(["hdiutil", "create", "-srcfolder", image_staging, "-volname", name, "-format", "UDRW", image_tmp])
system(["hdiutil", "attach", image_tmp, "-mountpoint", image_raw])
system(["SetFile", "-a", "C", image_raw])
system(["hdiutil", "detach", image_raw])
system(["hdiutil", "convert", image_tmp, "-format", "UDBZ", "-o", image_path])
system(["hdiutil", "internet-enable", "-yes", image_path])
os.unlink(image_tmp)
def build_zip(self):
print "Building ZIP image (for Sparkle updates)"
p = os.path.join(self.dist_dir, "Yaybu.zip")
if os.path.exists(p):
os.unlink(p)
system(["zip", "-ry9", "Yaybu.zip", "Yaybu.app"], cwd=self.dist_dir)
signature = system(["sh", "-c", "cat Yaybu.zip | openssl dgst -sha1 -binary | openssl dgst -dss1 -sign ~/dsa_priv.pem | openssl enc -base64"], cwd=self.dist_dir)
signature = signature.strip()
print "Sparkle signature =", signature
with open(p + ".sig", "w") as fp:
fp.write(signature)
def setup_plist(self):
version = pkg_resources.get_distribution('Yaybu').version
self.plist = {
"CFBundleVersion": self.build_number,
"CFBundleShortVersionString": "%s (%s)" % (version, self.build_number),
"CFBundleIdentifier" : "com.yaybu.Yaybu",
"CFBundleDocumentTypes": [{
"LSItemContentTypes": ["public.data"],
"LSHandlerRank": "Owner",
}],
"SUFeedURL": "http://yaybu.com/%(channel)s/osx/appcast.xml" % dict(channel=self.channel),
"SUPublicDSAKeyFile": "dsa_pub.pem",
}
def run(self):
self.iconfile = "Resources/Yaybu.icns"
py2app.run(self)
def run_normal(self):
py2app.run_normal(self)
self.update_binary_wrappers()
self.sort_out_egg_metadata()
self.bundle_cacert_pem()
self.fix_pricing_json()
self.fix_bin_permissions()
self.fix_sparkle()
self.sign()
self.build_dmg()
self.build_zip()
print "Yaybu.app = ", system(["du", "-sh", os.path.join(self.dist_dir, "Yaybu.app")]).strip().split()[0]
print "Yaybu.dmg = ", system(["du", "-sh", os.path.join(self.dist_dir, "Yaybu.dmg")]).strip().split()[0]
print "Yaybu.zip = ", system(["du", "-sh", os.path.join(self.dist_dir, "Yaybu.zip")]).strip().split()[0]
setup(
name = "Yaybu",
version = VERSION,
packages = find_packages(),
author = "John Carr",
author_email = "john.carr@unrouted.co.uk",
description = "Yaybu service orchestration + deployment",
license = "Apache",
keywords = "python",
app=["Application.py"],
data_files = [
"Resources/Yaybu.icns",
"Resources/dsa_pub.pem",
],
cmdclass = {
'py2app': YaybuAppBuild,
},
options=dict(py2app=dict(
frameworks=["Sparkle.framework"],
extra_scripts=['YaybuShell.py'],
no_chdir=True,
)),
)