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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
# setup-mac.py - Build system for Ubuntu One Client package
#
# Copyright 2012 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, 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 this program. If not, see <http://www.gnu.org/licenses/>.
"""
Setup.py script for Ubuntu One Mac Client
Usage:
% python setup.py prepare # builds and arranges subpackages
use --from-trunk to grab sources from launchpad. otherwise, builds
from buildout parts dir.
use --only_prepare='sso, controlpanel, client' (or 'sso, client',
etc) to limit which of those three to stage. This is only useful
for development
% python setup.py py2app # builds .app
"""
import copy
import glob
import os
import shutil
import subprocess
import sys
import conf
from distutils.cmd import Command
from distutils.core import setup
from plistlib import Plist
try:
import py2app
except ImportError:
print "This setup requires py2app, check your path"
print "or see http://bitbucket.com/ronaldoussoren/py2app/"
sys.exit()
LOG_LEVEL = "DEBUG"
LOG_FILE_SIZE = "1000000"
INSTALL_DIR = os.path.abspath("installed")
# bzr env assumes homebrew install:
# This is necessary so we can run setup-mac with the buildout python,
# which breaks the paths needed for bzr.
BZR_PATH = "/usr/local/Cellar/bazaar/2.5.0/libexec/:/usr/local/bin:/usr/bin"
BZR_ENV = os.environ.copy()
BZR_ENV.update(dict(PYTHONPATH="/usr/local/Cellar/bazaar/2.5.0/libexec/",
PATH=BZR_PATH))
def branch_and_merge(urls):
"""Branch and merge all requested branches."""
project_name = urls[0].split(":")[1]
print "Getting sources for:", project_name
folder_name = os.path.join('sources', project_name)
current_folder = os.getcwd()
if os.path.isdir(folder_name): # Folder exists
shutil.rmtree(folder_name)
subprocess.check_call(["bzr", "branch", urls[0], folder_name],
env=BZR_ENV)
os.chdir(folder_name)
for url in urls[1:]:
print "Merging:", url
subprocess.check_call(["bzr", "merge", url], env=BZR_ENV)
subprocess.check_call(["bzr", "commit", "-m", '"merged %s"' % url],
env=BZR_ENV)
os.chdir(current_folder)
# pylint is complaining about parent classes having too many methods
# pylint: disable=R0904
class DoNotPrepareAnything(Command):
"""No-op, lets us call setup multiple times without re-preparing."""
user_options = user_options = [("from-trunk", None, "noop."),
("only-prepare=", None, "noop."),
("source-dir=", None, "noop.")]
def initialize_options(self):
"""No-op."""
self.only_prepare = None
self.from_trunk = None
self.source_dir = None
def finalize_options(self):
"""No-op."""
pass
def run(self):
"""No-op."""
pass
# pylint: enable=R0904
# pylint is complaining about parent classes having too many methods
# pylint: disable=R0904
class PrepareSources(Command):
"""Manipulate the sources to look as if they were installed."""
user_options = [("from-trunk", None,
"""Download sources from launchpad. default is to
use buildout instead."""),
("only-prepare=", None,
"""Only do expensive stuff for the packages on
this list of one or more of 'sso',
'controlpanel', or 'client'.
(NOTE: still downloads everything if from-trunk
is set)"""),
("source-dir=", None,
"""Set the directory to get the sources from.
The default depends on from-trunk:
* if from-trunk is not specified,
the default is buildout sources dir.
* if from-trunk is specified,
the default source dir is './sources'""")]
def initialize_options(self):
"""Init options."""
self.from_trunk = None
self.only_prepare = None
self.source_dir = None
def finalize_options(self):
"""Finalize options."""
if self.source_dir is None:
if self.from_trunk is not None:
self.source_dir = 'sources'
else:
self.source_dir = os.path.join(os.path.dirname(__file__),
"../..")
if self.only_prepare is None:
# default is to do everything:
self.only_prepare = "sso, controlpanel, client"
def run(self):
"""Copy, build and munge things to prepare for packaging."""
print "Starting Prepare step:"
for folder in ["bin", INSTALL_DIR, "data"]:
if not os.path.isdir(folder):
os.mkdir(folder)
if self.from_trunk is not None:
if not os.path.isdir(self.source_dir):
os.mkdir(self.source_dir)
branch_and_merge(conf.U1_CLIENT_BRANCHES)
branch_and_merge(conf.U1_CONTROL_PANEL_BRANCHES)
branch_and_merge(conf.UBUNTU_SSO_BRANCHES)
branch_and_merge(conf.U1_STORAGE_PROTOCOL_BRANCHES)
print "will look for sources in ", self.source_dir
print "and install into ", INSTALL_DIR
# Copy main executables. In some cases, add .py so py2app
# will recognize the file as python and not crash.
shutil.copyfile(os.path.join(self.source_dir,
"ubuntuone-client", "bin",
"ubuntuone-syncdaemon"),
os.path.join("bin", "ubuntuone-syncdaemon.py"))
shutil.copyfile(os.path.join(self.source_dir, "ubuntuone-client",
"bin", "u1sdtool"),
os.path.join("bin", "u1sdtool"))
dest = os.path.join("bin", "ubuntuone-proxy-tunnel.py")
shutil.copyfile(os.path.join(self.source_dir, "ubuntuone-client",
"bin", "ubuntuone-proxy-tunnel"),
dest)
dest = os.path.join("bin", "ubuntu-sso-login.py")
shutil.copyfile(os.path.join(self.source_dir, "ubuntu-sso-client",
"bin", "ubuntu-sso-login"), dest)
os.chmod(dest, 0755)
dest = os.path.join("bin", "ubuntu-sso-login-qt.py")
shutil.copyfile(os.path.join(self.source_dir, "ubuntu-sso-client",
"bin", "ubuntu-sso-login-qt"), dest)
shutil.copyfile(os.path.join(self.source_dir, "ubuntu-sso-client",
"bin", "ubuntu-sso-proxy-creds-qt"),
os.path.join("bin", "ubuntu-sso-proxy-creds-qt.py"))
shutil.copyfile(os.path.join(self.source_dir, "ubuntu-sso-client",
"bin", "ubuntu-sso-ssl-certificate-qt"),
os.path.join("bin",
"ubuntu-sso-ssl-certificate-qt.py"))
shutil.copyfile(os.path.join(self.source_dir,
"ubuntuone-control-panel", "bin",
"ubuntuone-control-panel-qt"),
os.path.join("bin", "ubuntuone-control-panel-qt.py"))
# Remove "installed" copy
try:
shutil.rmtree(os.path.join(INSTALL_DIR, "lib"))
except OSError, e:
print "WARNING: OSError %r removing %r" \
% (e, os.path.join(INSTALL_DIR, "lib"))
start_dir = os.getcwd()
# Build SSO UI files, copy packages.
dest_dir = os.path.join(INSTALL_DIR, "lib", "site-packages")
dest_sso = os.path.join(dest_dir, "ubuntu_sso")
os.chdir(os.path.join(self.source_dir, "ubuntu-sso-client"))
if "sso" in self.only_prepare:
print "building and installing ubuntu-sso-client"
print "from directory ", os.getcwd()
log_file_name = os.path.join(INSTALL_DIR, "sso-build.log")
with open(log_file_name, 'w') as logfile:
cmd = "python setup.py build"
retval = subprocess.call(cmd,
shell=True,
stderr=subprocess.STDOUT,
stdout=logfile
)
if retval != 0:
print "error running %r in %r. look in %r for details" %\
(cmd, os.getcwd(), logfile.name)
# install UI files from sso. ignore the ubuntu_sso module
# this installs:
log_file_name = os.path.join(INSTALL_DIR, "sso-install.log")
with open(log_file_name, 'w') as logfile:
cmd = "python setup.py install --prefix=%s" % INSTALL_DIR,
retval = subprocess.call(cmd,
shell=True,
stderr=subprocess.STDOUT,
stdout=logfile
)
if retval != 0:
print "error running %r in %r. look in %r for details" %\
(cmd, os.getcwd(), logfile.name)
# copy ubuntu_sso module separately to
# install/lib/site-packages/ubuntu_sso
try:
shutil.copytree("ubuntu_sso", dest_sso)
except Exception, e:
print e
print "tried to copy", os.path.abspath('ubuntu_sso'),
print "to ", dest_sso
sso_revno = subprocess.check_output(["bzr", "revno", "."],
env=BZR_ENV).strip()
os.chdir(start_dir)
# install u1 client package to install/lib/site-packages/ubuntuone:
os.chdir(os.path.join(self.source_dir, "ubuntuone-client"))
if "client" in self.only_prepare:
print "copying ubuntuone-client package"
shutil.copy(os.path.join("windows", "clientdefs.py"),
os.path.join("ubuntuone", "clientdefs.py"))
dest_client = os.path.join(dest_dir, "ubuntuone")
shutil.copytree("ubuntuone", dest_client)
# workaround for py2app issue #49:
# (it can't handle non-py files in a py module directory)
os.remove(os.path.join(dest_client,
"syncdaemon",
"u1fsfsm.ods"))
u1client_revno = subprocess.check_output(["bzr", "revno", "."],
env=BZR_ENV).strip()
os.chdir(start_dir)
# install controlpanel files
os.chdir(os.path.join(self.source_dir, "ubuntuone-control-panel"))
if "controlpanel" in self.only_prepare:
print "building ubuntuone-controlpanel"
log_file_name = os.path.join(INSTALL_DIR, "u1cp-build.log")
with open(log_file_name, 'w') as logfile:
cmd = "python setup.py build"
retval = subprocess.call(cmd,
shell=True,
stderr=subprocess.STDOUT,
stdout=logfile
)
if retval != 0:
print "error running %r in %r. look in %r for details" %\
(cmd, os.getcwd(), logfile.name)
# Copying by hand because the install is borked
dest_cp = os.path.join(dest_dir, "ubuntuone", "controlpanel")
shutil.copytree(os.path.join("ubuntuone", "controlpanel"),
dest_cp)
u1cp_revno = subprocess.check_output(["bzr", "revno", "."],
env=BZR_ENV).strip()
os.chdir(start_dir)
# Create revno file
with open(os.path.join("data", "revnos.txt"), "w+") as revnos:
revnos.write("ubuntu-sso-client: %s"
"ubuntuone-client: %s"
"ubuntuone-control-panel: %s" %
(sso_revno, u1client_revno, u1cp_revno))
# Copy storage-protocol's pem files
shutil.copyfile(os.path.join(self.source_dir,
"ubuntuone-storage-protocol", "data",
"UbuntuOne-Go_Daddy_CA.pem"),
os.path.join("data", "UbuntuOne-Go_Daddy_CA.pem"))
shutil.copyfile(os.path.join(self.source_dir,
"ubuntuone-storage-protocol", "data",
"UbuntuOne-Go_Daddy_Class_2_CA.pem"),
os.path.join("data", "UbuntuOne-Go_Daddy_Class_2_CA.pem"))
shutil.copyfile(os.path.join(self.source_dir,
"ubuntuone-storage-protocol", "data",
"ValiCert_Class_2_VA.pem"),
os.path.join("data", "ValiCert_Class_2_VA.pem"))
# Copy syncdaemon config data
shutil.copyfile(os.path.join(self.source_dir,
"ubuntuone-client", "data",
"syncdaemon.conf"),
os.path.join("data", "syncdaemon.conf"))
logging_path = os.path.join(self.source_dir,
"ubuntuone-client", "data",
"logging.conf.in")
with open(logging_path, "rb") as logconf:
data = logconf.read()
data = data.replace("@LOG_LEVEL@", LOG_LEVEL)
data = data.replace("@LOG_FILE_SIZE@", LOG_FILE_SIZE)
with open(os.path.join("data", "logging.conf"), "wb") as logconf:
logconf.write(data)
# clear path_importer_cache to make sure that a following
# py2app step can see the newly-copied
# INSTALL_PATH/lib/site-packages/ubuntu_sso module without
# clearing, py2app will fail on a clean run when both prepare
# and py2app are specified on the same command:
sys.path_importer_cache.clear()
print "Prepare done"
# pylint: enable=R0904
if __name__ == '__main__':
# this path is from homebrew:
paths = glob.glob("/usr/local/Cellar/qt/*/plugins")
if len(paths) != 1:
print "Warning: expected just one path to Homebrew Qt plugins"
print "but found ", paths
qt_plugin_path = paths[0]
qt_network_plugins = glob.glob(qt_plugin_path + "/bearer/*.dylib")
qt_imageformat_plugins = glob.glob(qt_plugin_path +
"/imageformats/*.dylib")
dylib_paths = qt_imageformat_plugins + qt_network_plugins
master_plist = Plist.fromFile('data/macapp_template.plist')
master_options = {"includes": ['google.protobuf.descriptor',
'sip',
'twisted.web.resource',
'twisted.web.client',
'ubuntu_sso.qt',
'ubuntu_sso.qt.ui',
'oauth'],
"excludes": ["fsm", "PyQt4.uic"],
"frameworks": dylib_paths,
"resources": ['data/qt.conf'],
"strip": True,
}
# make sure we can see the prepared libraries:
sys.path.insert(0, os.path.join(INSTALL_DIR, "lib", "site-packages"))
def do_setup(name, is_background, id, exename,
mainscript,
verbose=False,
doprepare=False):
"""Customize plist & options and run setup.
Returns the path of the generated .app bundle.
"""
print "calling setup for:", name
plist = copy.deepcopy(master_plist)
plist.update(LSUIElement=is_background,
CFBundleIdentifier=id,
CFBundleExecutable=exename,
CFBundleName=name)
options = copy.deepcopy(master_options)
options.update({"plist": plist,
"bdist_base": os.path.join("build", exename),
"dist_dir": os.path.join("dist", exename)})
if doprepare:
prepare_class = PrepareSources
else:
prepare_class = DoNotPrepareAnything
setup(cmdclass=dict(prepare=prepare_class),
app=[mainscript],
options=dict(py2app=options),
verbose=verbose)
return os.path.join("dist", exename, name + ".app")
sso_login_qt_app_path = do_setup("Ubuntu Single Sign-On",
True,
"com.ubuntu.sso.login-qt",
"ubuntu-sso-login-qt",
os.path.join("bin",
"ubuntu-sso-login-qt.py"),
doprepare=True)
sso_login_app_path = do_setup("Ubuntu SSO Helper",
True,
"com.ubuntu.sso.login",
"ubuntu-sso-login",
os.path.join("bin", "ubuntu-sso-login.py"))
proxy_tunnel_app_path = do_setup("UbuntuOne Proxy Tunnel",
True,
"com.ubuntu.one.proxy-tunnel",
"ubuntuone-proxy-tunnel",
os.path.join("bin",
"ubuntuone-proxy-tunnel.py"))
control_panel_script = os.path.join("bin", "ubuntuone-control-panel-qt.py")
control_panel_app_path = do_setup("UbuntuOne",
True,
"com.ubuntu.one.controlpanel",
"ubuntuone-control-panel",
control_panel_script)
control_panel_resources_path = os.path.join(control_panel_app_path,
"Contents", "Resources")
# only do tweaks if you've done setup.py py2app
if 'py2app' not in sys.argv:
print "skipping post-setup tweaks."
print "run setup.py py2app to build apps and finalize packaging"
print "note that --source-dir only applies to 'prepare'."
sys.exit()
print "Starting post-setup tweaks"
# move plugins around
frameworks_paths = [os.path.join(p, "Contents", "Frameworks") for p in
sso_login_qt_app_path,
sso_login_app_path,
control_panel_app_path]
plugin_names = [os.path.basename(f) for f in dylib_paths]
for f_path in frameworks_paths:
bearers_dir = os.path.join(f_path, "plugins", "bearer")
imagefmts_dir = os.path.join(f_path, "plugins", "imageformats")
try:
os.makedirs(imagefmts_dir)
except Exception as e:
print "ignoring exception in makedirs(%r): %r" % (imagefmts_dir,
e)
try:
os.makedirs(bearers_dir)
except Exception as e:
print "ignoring exception in makedirs(%r): %r" % (bearers_dir, e)
try:
for plugin_name in plugin_names:
if "bearer" in plugin_name:
print "moving ", os.path.join(f_path, plugin_name), "to"
print " ", os.path.join(bearers_dir, plugin_name)
os.rename(os.path.join(f_path, plugin_name),
os.path.join(bearers_dir, plugin_name))
else:
os.rename(os.path.join(f_path, plugin_name),
os.path.join(imagefmts_dir, plugin_name))
print "moving ", os.path.join(f_path, plugin_name), "to"
print " ", os.path.join(imagefmts_dir, plugin_name)
except Exception as e:
print "ERROR moving:", e
print "copying helper apps into main app"
def copy_helper(helper_path):
"""Copy sub-apps into main app resources folder."""
helper_app_name = os.path.basename(helper_path)
dest = os.path.join(control_panel_resources_path,
helper_app_name)
if os.path.exists(dest):
print "removing", dest
shutil.rmtree(dest)
shutil.copytree(helper_path, dest)
copy_helper(sso_login_qt_app_path)
copy_helper(sso_login_app_path)
copy_helper(proxy_tunnel_app_path)
print "DONE. see dist/ for the .app."
|