~jon-hill/spud/mac_port

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
#!/bin/bash
#
# Package script for Diamond.
#
# Thanks: http://stackoverflow.com/questions/1596945/building-osx-app-bundle

# Also fix $INSTALLDIR/MacOS/diamond in case this number changes
PYVER=2.7
APP=Diamond.app
INSTALLDIR=$APP/Contents
LIBDIR=$INSTALLDIR/lib
APPDIR=/Applications/$INSTALLDIR
LOCALDIR=/opt/gtk

# set up our virtual python environment
virtualenv --python=python$PYVER --no-site-packages $INSTALLDIR

# install diamond in it
$INSTALLDIR/bin/python setup.py install  --prefix=.

# install dxdiff
cd ../dxdiff;
../diamond/$INSTALLDIR/bin/python setup.py install

cd ../diamond;

# This has placed a bin/diamond file which just launches our bin/diamond
# Unfortunately, it's in the wrong place, so move it 
mkdir $INSTALLDIR/MacOS

# Sort out the MacResources
cp MacOS_Resources/PkgInfo $INSTALLDIR/
cp MacOS_Resources/Info.plist $INSTALLDIR/
cp MacOS_Resources/pango_rc $INSTALLDIR/
mkdir $INSTALLDIR/Resources
cp MacOS_Resources/diamond.icns $INSTALLDIR/Resources/
cp MacOS_Resources/diamond $INSTALLDIR/MacOS

# Now we have to play silly buggers with some bits of the diamond file
# as the Mac app packages adds a command line argument, which we want to ignore
sed -i -e 's/sys.argv\[1:\]/sys.argv\[2:\]/' $INSTALLDIR/lib/python2.7/site-packages/diamond-1.0-py2.7.egg/EGG-INFO/scripts/diamond

# Now we have to feed the app some schemas or it's all for nothing
# Set up the schema folders
mkdir -p $INSTALLDIR/share/schemata

# Let's get the latest fluidity release schema
# NOTE: UPDATE URL AFTER A RELEASE
if [ ! -d fluidity ]; then
	bzr branch lp:fluidity/4.1 fluidity
else
	cd fluidity
	bzr up
	cd ../
fi
# Make the schemata description
# The path of the RNG is relative to diamond.egg/EGG_INFO directory
cat > $INSTALLDIR/share/schemata/flml << EOF
Fluidity Markup Language
../../../../../share/schemata/fluidity/fluidity_options.rng
EOF
rm -rf $INSTALLDIR/share/schemata/fluidity
mkdir $INSTALLDIR/share/schemata/fluidity
cp fluidity/schemas/*.rng $INSTALLDIR/share/schemata/fluidity/
cp ../schema/*.rng $INSTALLDIR/share/schemata/fluidity/
# clean up
#rm -rf fluidity

# Do the above for any other schema we want to distribute
# Don't forget the spud-base!



# Let's get lxml installed
$INSTALLDIR/bin/easy_install --allow-hosts=lxml.de,*.python.org lxml

# Temp. solution - Just manually copy stuff we know we need
SITEPACKAGES=$LIBDIR/python$PYVER/site-packages

mkdir -p $SITEPACKAGES

# This locates pygtk.pyc. We want the source file
pygtk=`python -c "import pygtk; print pygtk.__file__[:-1]"`
oldsite=`dirname $pygtk`
gobject=`python -c "import gobject; print gobject.__file__[:-1]"`
glib=`python -c "import glib; print glib.__file__[:-1]"`

# Copy PyGtk and related libraries
cp $pygtk $SITEPACKAGES
cp -r `dirname $gobject` $SITEPACKAGES
cp -r `dirname $glib` $SITEPACKAGES
cp -r $oldsite/cairo $SITEPACKAGES
cp -r $oldsite/gtk-2.0 $SITEPACKAGES
cp $oldsite/pygtk.pth $SITEPACKAGES


# Modules, config, etc.
for dir in lib etc/pango etc/gtk-2.0 share/themes; do
  mkdir -p $INSTALLDIR/$dir
  cp -r $LOCALDIR/$dir/* $INSTALLDIR/$dir
done

# Resources, are processed on startup
for dir in etc/gtk-2.0 etc/pango lib/gdk-pixbuf-2.0/2.10.0; do
  mkdir -p $INSTALLDIR/Resources/$dir
  cp $LOCALDIR/$dir/* $INSTALLDIR/Resources/$dir
done

# Somehow files are writen with mode 444
find $INSTALLDIR -type f -exec chmod u+w {} \;

function log() {
  echo $* >&2
}

function resolve_deps() {
  local lib=$1
  local dep
  otool -L $lib | grep -e "^.$LOCALDIR/" |\
      while read dep _; do
    echo $dep
  done
}

function fix_paths() {
  local lib=$1
  log Fixing $lib
  for dep in `resolve_deps $lib`; do
    log Fixing `basename $lib`
    log "|  $dep"
    install_name_tool -change $dep @executable_path/../lib/`basename $dep` $lib
  done
}

binlibs=`find $INSTALLDIR -type f -name '*.so'`

for lib in $binlibs; do
  log Resolving $lib
  resolve_deps $lib
  fix_paths $lib
done | sort -u | while read lib; do
  log Copying $lib
  cp $lib $LIBDIR
  chmod u+w $LIBDIR/`basename $lib`
  fix_paths $LIBDIR/`basename $lib`
done

for lib in $dylibs; do
  log Resolving $lib
  resolve_deps $lib
  fix_paths $lib
done | sort -u | while read lib; do
  log Copying $lib
  cp $lib $LIBDIR
  chmod u+w $LIBDIR/`basename $lib`
  fix_paths $LIBDIR/`basename $lib`
done


# Fix config files
sed -i -e 's#/opt/gtk/#'$APPDIR'/#' $INSTALLDIR/etc/pango/pango.modules 
sed -i -e 's#/opt/gtk/#'$APPDIR'/#' $INSTALLDIR/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache
sed -i -e 's#/opt/gtk/#'$APPDIR'/#' $INSTALLDIR/Resources/etc/pango/pango.modules 
sed -i -e 's#/opt/gtk/#'$APPDIR'/#' $INSTALLDIR/Resources/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache



# Package!
VERSION=0.01
# we now need to fiddle with the Python run path on the diamond script
# COMMENT THESE OUT IF YOU WANT TO TEST YOUR APP WITHOUT INSTALLING
# EDIT AS REQUIRED
sed -i -e 's|/Users/amcg/Software/spud/mac_port/diamond|/Applications|' $INSTALLDIR/lib/python2.7/site-packages/diamond-1.0-py2.7.egg/EGG-INFO/scripts/diamond
sed -i -e 's|/Users/amcg/Software/spud/mac_port/diamond|/Applications|' $INSTALLDIR/MacOS/diamond

zip -rq Diamond-$VERSION-osx.zip $APP
hdiutil create -srcfolder $APP Diamond-$VERSION.dmg


# To test, temporarily move your /opt/gtk folder
# Move the Diamond.app folder to /Applications and go
# open -a Console 
# helps to debug