1#!/usr/bin/env python
2
3# Copyright (C) 2009 Kevin Ollivier  All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25#
26# Script for building Mac .pkg installer
27
28import commands
29import datetime
30import distutils.sysconfig
31import glob
32import optparse
33import os
34import shutil
35import string
36import sys
37import tempfile
38
39script_dir = os.path.abspath(os.path.dirname(__file__))
40sys.path.append(os.path.abspath(os.path.join(script_dir, "..", "build")))
41
42from build_utils import *
43
44import wx
45
46wxwk_root = os.path.abspath(os.path.join(script_dir, "..", "..", ".."))
47wxwebkit_dir = os.path.abspath(os.path.join(wxwk_root, "WebKitBuild", get_config(wxwk_root) + git_branch_name()))
48
49wx_version = wx.__version__[:5]
50py_version = sys.version[:3]
51
52date = str(datetime.date.today())
53
54platform = "osx"
55
56pkgname = "wxWebKit-%s-wx%s-py%s-%s" % (platform, wx_version[:3], py_version, date)
57
58tempdir = "/tmp/%s" % (pkgname)
59
60if os.path.exists(tempdir):
61    shutil.rmtree(tempdir)
62    os.makedirs(tempdir)
63
64installroot = os.path.join(tempdir, "install-root")
65installapps = os.path.join(tempdir, "install-apps")
66
67sp_root = distutils.sysconfig.get_python_lib()
68wx_root = sp_root
69if sys.platform.startswith("darwin"):
70    wx_root = "/usr/local/lib/wxPython-unicode-%s" % wx.__version__
71    sp_root = "%s/lib/python%s/site-packages" % (wx_root, py_version)
72sitepackages = "%s/wx-%s-mac-unicode/wx" % (sp_root, wx_version[:3])
73prefix = wx_root + "/lib"
74
75def mac_update_dependencies(dylib, prefix):
76    """
77    Copies any non-system dependencies into the bundle, and
78    updates the install name path to the new path in the bundle.
79    """
80    global wx_root
81    system_prefixes = ["/usr/lib", "/System/Library", wx_root]
82
83    output = commands.getoutput("otool -L %s" % dylib).strip()
84    for line in output.split("\n"):
85        filename = line.split("(")[0].strip()
86        if os.path.exists(filename):
87            print "checking dll %s" % filename
88            copy = True
89            for sys_prefix in system_prefixes:
90                if filename.startswith(sys_prefix):
91                    copy = False
92
93            if copy:
94                copydir = os.path.dirname(dylib)
95
96                filedir, basename = os.path.split(filename)
97                dest_filename = os.path.join(prefix, basename)
98                copyname = os.path.join(copydir, basename)
99                if not os.path.exists(copyname):
100                    shutil.copy(filename, copydir)
101                    os.system("install_name_tool -id %s %s" % (dest_filename, copyname))
102
103                os.system("install_name_tool -change %s %s %s" % (filename, dest_filename, dylib))
104
105def exitIfError(cmd):
106    print cmd
107    retval = os.system(cmd)
108    if retval != 0:
109        if os.path.exists(tempdir):
110            shutil.rmtree(tempdir)
111        sys.exit(1)
112
113wxroot = installroot + prefix
114wxpythonroot = installroot + sitepackages
115
116try:
117    if not os.path.exists(wxroot):
118        os.makedirs(wxroot)
119
120    if not os.path.exists(wxpythonroot):
121        os.makedirs(wxpythonroot)
122
123    for wildcard in ["*.py", "*.so"]:
124        files = glob.glob(os.path.join(wxwebkit_dir, wildcard))
125        for afile in files:
126            shutil.copy(afile, wxpythonroot)
127
128    for wildcard in ["*.dylib"]:
129        files = glob.glob(os.path.join(wxwebkit_dir, wildcard))
130        for afile in files:
131            shutil.copy(afile, wxroot)
132
133    if sys.platform.startswith("darwin"):
134        dylib_path = os.path.join(wxroot, "libwxwebkit.dylib")
135        os.system("install_name_tool -id %s %s" % (os.path.join(prefix, "libwxwebkit.dylib"), dylib_path))
136        mac_update_dependencies(dylib_path, prefix)
137        mac_update_dependencies(os.path.join(wxpythonroot, "_webview.so"), prefix)
138
139        demodir = installroot + "/Applications/wxWebKit/Demos"
140        if not os.path.exists(demodir):
141            os.makedirs(demodir)
142
143        shutil.copy(os.path.join(wxwk_root, "Source", "WebKit", "wx", "bindings", "python", "samples", "simple.py"), demodir)
144
145        if os.path.exists(pkgname + ".pkg"):
146            shutil.rmtree(pkgname + ".pkg")
147
148        pkg_args = ['--title ' + pkgname,
149                    '--out %s.pkg' % pkgname,
150                    '--version ' + date.strip(),
151                    '--id org.wxwebkit.wxwebkit',
152                    '--domain system',
153                    '--root-volume-only',
154                    '--root ' + installroot,
155                    '--verbose'
156                    ]
157
158        packagemaker = "/Developer/Applications/Utilities/PackageMaker.app/Contents/MacOS/PackageMaker"
159        exitIfError(packagemaker + " %s" % (string.join(pkg_args, " ")))
160finally:
161    if os.path.exists(tempdir):
162        shutil.rmtree(tempdir)
163