tools/support/dmg-license.py
changeset 9343 67552b2e20ca
equal deleted inserted replaced
9341:4dfebad83d51 9343:67552b2e20ca
       
     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.
       
     6 
       
     7 Copyright (C) 2011 Jared Hobbs
       
     8 
       
     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:
       
    15 
       
    16 The above copyright notice and this permission notice shall be included in
       
    17 all copies or substantial portions of the Software.
       
    18 
       
    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
       
    31 
       
    32 
       
    33 class Path(str):
       
    34     def __enter__(self):
       
    35         return self
       
    36 
       
    37     def __exit__(self, type, value, traceback):
       
    38         os.unlink(self)
       
    39 
       
    40 
       
    41 def mktemp(dir=None, suffix=''):
       
    42     (fd, filename) = tempfile.mkstemp(dir=dir, suffix=suffix)
       
    43     os.close(fd)
       
    44     return Path(filename)
       
    45 
       
    46 
       
    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 'LPic' (5000) {
       
    52     $"0002 0011 0003 0001 0000 0000 0002 0000"
       
    53     $"0000 000E 0006 0001 0005 0007 0000 0007"
       
    54     $"0008 0000 0047 0009 0000 0034 000A 0001"
       
    55     $"0035 000B 0001 0020 000C 0000 0011 000D"
       
    56     $"0000 005B 0004 0000 0033 000F 0001 000C"
       
    57     $"0010 0000 000B 000E 0000"
       
    58 };\n\n""")
       
    59             with open(license, 'r') as l:
       
    60                 f.write('data \'TEXT\' (5002, "English") {\n')
       
    61                 for line in l:
       
    62                     if len(line) < 1000:
       
    63                         f.write('    "' + line.strip().replace('"', '\\"') +
       
    64                                 '\\n"\n')
       
    65                     else:
       
    66                         for liner in line.split('.'):
       
    67                             f.write('    "' +
       
    68                                     liner.strip().replace('"', '\\"') +
       
    69                                     '. \\n"\n')
       
    70                 f.write('};\n\n')
       
    71             f.write("""resource 'STR#' (5002, "English") {
       
    72     {
       
    73         "English",
       
    74         "Agree",
       
    75         "Disagree",
       
    76         "Print",
       
    77         "Save...",
       
    78         "IMPORTANT - By clicking on the \\"Agree\\" button, you agree "
       
    79         "to be bound by the terms of the License Agreement.",
       
    80         "Software License Agreement",
       
    81         "This text cannot be saved. This disk may be full or locked, or the "
       
    82         "file may be locked.",
       
    83         "Unable to print. Make sure you have selected a printer."
       
    84     }
       
    85 };""")
       
    86         os.system('/usr/bin/hdiutil unflatten -quiet "%s"' % dmgFile)
       
    87         os.system('%s "%s/"*.r %s -a -o "%s"' %
       
    88                   (options.rez, options.flat_carbon, tmpFile, dmgFile))
       
    89 
       
    90         os.system('/usr/bin/hdiutil flatten -quiet "%s"' % dmgFile)
       
    91         if options.compression is not None:
       
    92             os.system('cp %s %s.temp.dmg' % (dmgFile, dmgFile))
       
    93             os.remove(dmgFile)
       
    94             if options.compression == "bz2":
       
    95                 os.system('hdiutil convert %s.temp.dmg -format UDBZ -o %s' %
       
    96                           (dmgFile, dmgFile))
       
    97             elif options.compression == "gz":
       
    98                 os.system('hdiutil convert %s.temp.dmg -format ' % dmgFile +
       
    99                           'UDZO -imagekey zlib-devel=9 -o %s' % dmgFile)
       
   100             os.remove('%s.temp.dmg' % dmgFile)
       
   101     print "Successfully added license to '%s'" % dmgFile
       
   102 
       
   103 if __name__ == '__main__':
       
   104     parser = optparse.OptionParser()
       
   105     parser.set_usage("""%prog <dmgFile> <licenseFile> [OPTIONS]
       
   106   This program adds a software license agreement to a DMG file.
       
   107   It requires Xcode and a plain ascii text <licenseFile>.
       
   108 
       
   109   See --help for more details.""")
       
   110     parser.add_option(
       
   111         '--rez',
       
   112         '-r',
       
   113         action='store',
       
   114         default='/Applications/Xcode.app/Contents/Developer/Tools/Rez',
       
   115         help='The path to the Rez tool. Defaults to %default'
       
   116     )
       
   117     parser.add_option(
       
   118         '--flat-carbon',
       
   119         '-f',
       
   120         action='store',
       
   121         default='/Applications/Xcode.app/Contents/Developer/Platforms'
       
   122                 '/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk'
       
   123                 '/Developer/Headers/FlatCarbon',
       
   124         help='The path to the FlatCarbon headers. Defaults to %default'
       
   125     )
       
   126     parser.add_option(
       
   127         '--compression',
       
   128         '-c',
       
   129         action='store',
       
   130         choices=['bz2', 'gz'],
       
   131         default=None,
       
   132         help='Optionally compress dmg using specified compression type. '
       
   133              'Choices are bz2 and gz.'
       
   134     )
       
   135     options, args = parser.parse_args()
       
   136     cond = len(args) != 2 or not os.path.exists(options.rez) \
       
   137         or not os.path.exists(options.flat_carbon)
       
   138     if cond:
       
   139         parser.print_usage()
       
   140         sys.exit(1)
       
   141     main(options, args)