summaryrefslogtreecommitdiffstats
path: root/Tools/wx/packaging/build-mac-installer.py
blob: d0ef9458f5d02559a904ba9ebed862b243208090 (plain)
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
#!/usr/bin/env python

# Copyright (C) 2009 Kevin Ollivier  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
#
# Script for building Mac .pkg installer

import commands
import datetime
import distutils.sysconfig
import glob
import optparse
import os
import shutil
import string
import sys
import tempfile

script_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.abspath(os.path.join(script_dir, "..", "build")))

from build_utils import *

import wx

wxwk_root = os.path.abspath(os.path.join(script_dir, "..", "..", ".."))
wxwebkit_dir = os.path.abspath(os.path.join(wxwk_root, "WebKitBuild", get_config(wxwk_root) + git_branch_name()))

wx_version = wx.__version__[:5]
py_version = sys.version[:3]

date = str(datetime.date.today())

platform = "osx"
    
pkgname = "wxWebKit-%s-wx%s-py%s-%s" % (platform, wx_version[:3], py_version, date)

tempdir = "/tmp/%s" % (pkgname)

if os.path.exists(tempdir):
    shutil.rmtree(tempdir)
    os.makedirs(tempdir)

installroot = os.path.join(tempdir, "install-root")
installapps = os.path.join(tempdir, "install-apps")

sp_root = distutils.sysconfig.get_python_lib()
wx_root = sp_root
if sys.platform.startswith("darwin"):
    wx_root = "/usr/local/lib/wxPython-unicode-%s" % wx.__version__
    sp_root = "%s/lib/python%s/site-packages" % (wx_root, py_version)
sitepackages = "%s/wx-%s-mac-unicode/wx" % (sp_root, wx_version[:3])
prefix = wxroot + "/lib"

def mac_update_dependencies(dylib, prefix):
    """
    Copies any non-system dependencies into the bundle, and
    updates the install name path to the new path in the bundle.
    """
    global wx_root
    system_prefixes = ["/usr/lib", "/System/Library", wx_root]

    output = commands.getoutput("otool -L %s" % dylib).strip()
    for line in output.split("\n"):
        filename = line.split("(")[0].strip()
        if os.path.exists(filename):
            print "checking dll %s" % filename
            copy = True
            for sys_prefix in system_prefixes:
                if filename.startswith(sys_prefix):
                    copy = False
            
            if copy:
                copydir = os.path.dirname(dylib)
                
                filedir, basename = os.path.split(filename)
                dest_filename = os.path.join(prefix, basename)
                copyname = os.path.join(copydir, basename)
                if not os.path.exists(copyname):
                    shutil.copy(filename, copydir)
                    os.system("install_name_tool -id %s %s" % (dest_filename, copyname))
                
                os.system("install_name_tool -change %s %s %s" % (filename, dest_filename, dylib))

def exitIfError(cmd):
    print cmd
    retval = os.system(cmd)
    if retval != 0:
        if os.path.exists(tempdir):
            shutil.rmtree(tempdir)
        sys.exit(1)

wxroot = installroot + prefix
wxpythonroot = installroot + sitepackages

try:
    if not os.path.exists(wxroot):
        os.makedirs(wxroot)
    
    if not os.path.exists(wxpythonroot):
        os.makedirs(wxpythonroot)
    
    for wildcard in ["*.py", "*.so"]:
        files = glob.glob(os.path.join(wxwebkit_dir, wildcard))
        for afile in files:
            shutil.copy(afile, wxpythonroot)
    
    for wildcard in ["*.dylib"]:
        files = glob.glob(os.path.join(wxwebkit_dir, wildcard))
        for afile in files:
            shutil.copy(afile, wxroot)
    
    if sys.platform.startswith("darwin"):
        dylib_path = os.path.join(wxpythonroot, "libwxwebkit.dylib")
        os.system("install_name_tool -id %s %s" % (os.path.join(prefix, "libwxwebkit.dylib"), dylib_path))
        mac_update_dependencies(dylib_path, prefix)
        mac_update_dependencies(os.path.join(wxpythonroot, "_webview.so"), prefix)
        
        demodir = installroot + "/Applications/wxWebKit/Demos"
        if not os.path.exists(demodir):
            os.makedirs(demodir)
            
        shutil.copy(os.path.join(wxwk_root, "WebKit", "wx", "bindings", "python", "samples", "simple.py"), demodir)
        
        if os.path.exists(pkgname + ".pkg"):
            shutil.rmtree(pkgname + ".pkg")
    
        pkg_args = ['--title ' + pkgname,
                    '--out %s.pkg' % pkgname,
                    '--version ' + date.strip(),
                    '--id org.wxwebkit.wxwebkit',
                    '--domain system',
                    '--root-volume-only',
                    '--root ' + installroot,
                    '--resources %s/mac/resources' % script_dir,
                    '--verbose'
                    ]
    
        packagemaker = "/Developer/Applications/Utilities/PackageMaker.app/Contents/MacOS/PackageMaker"
        exitIfError(packagemaker + " %s" % (string.join(pkg_args, " ")))
finally:
    if os.path.exists(tempdir):
        shutil.rmtree(tempdir)