This repository has been archived by the owner on Feb 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
tgz2dmg.sh
executable file
·176 lines (158 loc) · 4.83 KB
/
tgz2dmg.sh
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
#!/bin/bash
#
# Copyright 2011 Google. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# #
# Translate a tgz into an installable pkg inside a dmg.
set -i
set -e
if [[ $(uname) != "Darwin" ]]; then
echo $0 must run on OS X
exit 1
fi
TGZ="$1"
OUT="$2"
ORIGPWD="$PWD"
ID="com.google.code.simian"
VERSION="1"
PKGONLY=""
PKGBUILD=$(which pkgbuild)
if [[ "$#" -lt 2 ]]; then
echo usage: $0 tgz_input_file dmg\|pkg_output_file [options...]
echo
echo options:
echo -vep binary add binary to directory which is prepended onto
echo PATH during postflight virtualenv install.
echo -pyver version create python version hint e.g. \"2.5\"
echo -pkgonly do not create a dmg, just create a pkg
echo -version version set package version to version
echo -id id set package id, default $ID
echo -s script_file add script to package
echo -r resource_file add resource file to package resources
echo -R resource_dir add a file OR dir into package resources
echo NOTE: IF YOU USE GLOBS PUT THE PATTERN IN QUOTES\!
echo -c src dst copy a file into the installation tree of package
echo e.g. -c /tmp/foo.py Library/Foo/Bar/foo.py
echo
exit 1
fi
shift ; shift
if [[ -z "$PKGBUILD" ]]; then
echo "cannot find executable Apple pkgbuild tool."
exit 1
fi
TMPDIR=$(mktemp -d tgz2dmgXXXXXX)
echo "TMPDIR is ${TMPDIR}"
mkdir -p "${TMPDIR}/contents"
mkdir -p "${TMPDIR}/pkg"
mkdir -p "${TMPDIR}/resources"
mkdir -p "${TMPDIR}/resources/vep"
mkdir -p "${TMPDIR}/scripts"
trap "rm -rf \"${TMPDIR}\"" EXIT
if [[ "$TGZ" != "" ]]; then
tar -zpxf "$TGZ" -C "${TMPDIR}/contents"
fi
# parse argv
next=""
while [[ "$#" -gt 0 ]]; do
if [[ "$next" = "" ]]; then
if [[ "$1" = "-s" ]]; then
next="script"
elif [[ "$1" = "-r" ]]; then
next="rsrc"
elif [[ "$1" = "-c" ]]; then
next="copy"
elif [[ "$1" = "-R" ]]; then
next="rsrcdir"
elif [[ "$1" = "-id" ]]; then
next="id"
elif [[ "$1" = "-version" ]]; then
next="version"
elif [[ "$1" = "-pkgonly" ]]; then
PKGONLY="1"
next=""
elif [[ "$1" = "-pyver" ]]; then
next="pyver"
elif [[ "$1" = "-vep" ]]; then
next="vep"
else
echo Unknown argument "$1"
fi
else
if [[ "$next" = "script" ]]; then
if [[ $PKGBUILD == "" ]]; then
cp "$1" "${TMPDIR}/scripts"
else
cp postinstall "${TMPDIR}/scripts"
cp roots.pem "${TMPDIR}/scripts"
fi
elif [[ "$next" = "rsrc" ]]; then
cp "$1" "${TMPDIR}/resources"
elif [[ "$next" = "rsrcdir" ]]; then
# try to helpfully warn the user if they supplied the arguments
# as -R *.foo (which the shell has already expanded) versus
# hiding the glob inside quotes as -R '*.foo'.
defensive_next="$2"
if [[ "$#" -gt 1 && "${defensive_next:0:1}" != "-" ]]; then
echo "warning: did you mean -R '*.pattern' (quote glob patterns)"
fi
r="$1"
declare -a rsrcdirs
rsrcdirs=($1)
for dirent in "${rsrcdirs[@]}"; do
[[ -f "${dirent}" ]] && cp "${dirent}" "${TMPDIR}/resources"
[[ -d "${dirent}" ]] && cp -R "${dirent}" "${TMPDIR}/resources"
done
elif [[ "$next" = "id" ]]; then
ID="$1"
elif [[ "$next" = "version" ]]; then
VERSION="$1"
elif [[ "$next" = "copy" ]]; then
src="$1"
shift
dst="$1"
cp "$src" "${TMPDIR}/contents/$dst"
elif [[ "$next" = "pyver" ]]; then
echo "$1" > "${TMPDIR}/resources/python_version"
elif [[ "$next" = "vep" ]]; then
cp "$1" "${TMPDIR}/resources/vep"
else
echo Unknown argument "$1"
fi
next=""
fi
shift
done
cd "${TMPDIR}/contents"
rm -f Distribution
# At this stage we can edit the pkg contents
# and write a preflight to handle other python deps.
cd "$ORIGPWD"
if [[ -z "$PKGONLY" ]]; then
pkgout="${TMPDIR}/pkg/simian.pkg"
else
pkgout="$OUT"
fi
echo "Using pkgbuild"
cp -R "${TMPDIR}/resources" "${TMPDIR}/scripts/Resources"
${PKGBUILD} --root "${TMPDIR}/contents" \
--identifier "$ID" \
--scripts "${TMPDIR}/scripts" \
--version "${VERSION}" \
"${pkgout}"
if [[ -z "$PKGONLY" ]]; then
hdiutil create -fs HFS+ -srcfolder "${TMPDIR}/pkg" -layout NONE -volname Simian "$OUT"
fi
rm -rf "${TMPDIR}"
echo output at "$OUT"