Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / package / osx / create-dmg / support / dmg-license.py
blob9003a7c5e74fb72d070c1570584e3ff6e8b5096d
1 #! /usr/bin/env python
2 """
3 This script adds a license file to a DMG. Requires Xcode and a plain ascii text
4 license file.
5 Obviously only runs on a Mac.
7 Copyright (C) 2011-2013 Jared Hobbs
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 """
27 import os
28 import sys
29 import tempfile
30 import optparse
33 class Path(str):
34 def __enter__(self):
35 return self
37 def __exit__(self, type, value, traceback):
38 os.unlink(self)
41 def mktemp(dir=None, suffix=''):
42 (fd, filename) = tempfile.mkstemp(dir=dir, suffix=suffix)
43 os.close(fd)
44 return Path(filename)
47 def main(options, args):
48 dmgFile, license = args
49 with mktemp('.') as tmpFile:
50 with open(tmpFile, 'w') as f:
51 f.write("""data 'TMPL' (128, "LPic") {
52 $"1344 6566 6175 6C74 204C 616E 6775 6167"
53 $"6520 4944 4457 5244 0543 6F75 6E74 4F43"
54 $"4E54 042A 2A2A 2A4C 5354 430B 7379 7320"
55 $"6C61 6E67 2049 4444 5752 441E 6C6F 6361"
56 $"6C20 7265 7320 4944 2028 6F66 6673 6574"
57 $"2066 726F 6D20 3530 3030 4457 5244 1032"
58 $"2D62 7974 6520 6C61 6E67 7561 6765 3F44"
59 $"5752 4404 2A2A 2A2A 4C53 5445"
62 data 'LPic' (5000) {
63 $"0000 0002 0000 0000 0000 0000 0004 0000"
66 data 'STR#' (5000, "English buttons") {
67 $"0006 0D45 6E67 6C69 7368 2074 6573 7431"
68 $"0541 6772 6565 0844 6973 6167 7265 6505"
69 $"5072 696E 7407 5361 7665 2E2E 2E7A 4966"
70 $"2079 6F75 2061 6772 6565 2077 6974 6820"
71 $"7468 6520 7465 726D 7320 6F66 2074 6869"
72 $"7320 6C69 6365 6E73 652C 2063 6C69 636B"
73 $"2022 4167 7265 6522 2074 6F20 6163 6365"
74 $"7373 2074 6865 2073 6F66 7477 6172 652E"
75 $"2020 4966 2079 6F75 2064 6F20 6E6F 7420"
76 $"6167 7265 652C 2070 7265 7373 2022 4469"
77 $"7361 6772 6565 2E22"
80 data 'STR#' (5002, "English") {
81 $"0006 0745 6E67 6C69 7368 0541 6772 6565"
82 $"0844 6973 6167 7265 6505 5072 696E 7407"
83 $"5361 7665 2E2E 2E7B 4966 2079 6F75 2061"
84 $"6772 6565 2077 6974 6820 7468 6520 7465"
85 $"726D 7320 6F66 2074 6869 7320 6C69 6365"
86 $"6E73 652C 2070 7265 7373 2022 4167 7265"
87 $"6522 2074 6F20 696E 7374 616C 6C20 7468"
88 $"6520 736F 6674 7761 7265 2E20 2049 6620"
89 $"796F 7520 646F 206E 6F74 2061 6772 6565"
90 $"2C20 7072 6573 7320 2244 6973 6167 7265"
91 $"6522 2E"
92 };\n\n""")
93 with open(license, 'r') as l:
94 kind = 'RTF ' if license.lower().endswith('.rtf') else 'TEXT'
95 f.write('data \'%s\' (5000, "English") {\n' % kind)
96 def escape(s):
97 return s.strip().replace('\\', '\\\\').replace('"', '\\"')
99 for line in l:
100 if len(line) < 1000:
101 f.write(' "' + escape(line) + '\\n"\n')
102 else:
103 for liner in line.split('.'):
104 f.write(' "' + escape(liner) + '. \\n"\n')
105 f.write('};\n\n')
106 f.write("""data 'styl' (5000, "English") {
107 $"0003 0000 0000 000C 0009 0014 0000 0000"
108 $"0000 0000 0000 0000 0027 000C 0009 0014"
109 $"0100 0000 0000 0000 0000 0000 002A 000C"
110 $"0009 0014 0000 0000 0000 0000 0000"
111 };\n""")
112 os.system('hdiutil unflatten -quiet "%s"' % dmgFile)
113 ret = os.system('%s -a %s -o "%s"' %
114 (options.rez, tmpFile, dmgFile))
115 os.system('hdiutil flatten -quiet "%s"' % dmgFile)
116 if options.compression is not None:
117 os.system('cp %s %s.temp.dmg' % (dmgFile, dmgFile))
118 os.remove(dmgFile)
119 if options.compression == "bz2":
120 os.system('hdiutil convert %s.temp.dmg -format UDBZ -o %s' %
121 (dmgFile, dmgFile))
122 elif options.compression == "gz":
123 os.system('hdiutil convert %s.temp.dmg -format ' % dmgFile +
124 'UDZO -imagekey zlib-devel=9 -o %s' % dmgFile)
125 os.remove('%s.temp.dmg' % dmgFile)
126 if ret == 0:
127 print "Successfully added license to '%s'" % dmgFile
128 else:
129 print "Failed to add license to '%s'" % dmgFile
131 if __name__ == '__main__':
132 parser = optparse.OptionParser()
133 parser.set_usage("""%prog <dmgFile> <licenseFile> [OPTIONS]
134 This program adds a software license agreement to a DMG file.
135 It requires Xcode and either a plain ascii text <licenseFile>
136 or a <licenseFile.rtf> with the RTF contents.
138 See --help for more details.""")
139 parser.add_option(
140 '--rez',
141 '-r',
142 action='store',
143 default='/Applications/Xcode.app/Contents/Developer/Tools/Rez',
144 help='The path to the Rez tool. Defaults to %default'
146 parser.add_option(
147 '--compression',
148 '-c',
149 action='store',
150 choices=['bz2', 'gz'],
151 default=None,
152 help='Optionally compress dmg using specified compression type. '
153 'Choices are bz2 and gz.'
155 options, args = parser.parse_args()
156 cond = len(args) != 2
157 if not os.path.exists(options.rez):
158 print 'Failed to find Rez at "%s"!\n' % options.rez
159 cond = True
160 if cond:
161 parser.print_usage()
162 sys.exit(1)
163 main(options, args)